Skip to content

Commit

Permalink
core: moved governance initialization check after block validation fails
Browse files Browse the repository at this point in the history
  • Loading branch information
sadoci committed Jan 22, 2022
1 parent c66e0f9 commit e51e813
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,22 +1834,16 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
if parent == nil {
parent = bc.GetHeader(block.ParentHash(), block.NumberU64()-1)
}

// metadium: reward calculation uses governance contract, meaning
// the previous block is required. For fast sync, we need to wait for
// governance is initialized and try again.
retryCount := 2
retry:
statedb, err := state.New(parent.Root, bc.stateCache, bc.snaps)
if err != nil {
return it.index, err
}

if !metaminer.IsPoW() {
// make sure the previous block exists in order to calculate rewards distribution
for {
if _, _, err := metaminer.CalculateRewards(
block.Number(), big.NewInt(0), big.NewInt(100000000), nil); err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
}

// Enable prefetching to pull in trie node paths while processing transactions
statedb.StartPrefetcher("chain")
activeState = statedb
Expand Down Expand Up @@ -1895,6 +1889,17 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
// Validate the state using the default validator
substart = time.Now()
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas, fees); err != nil {
if retryCount--; !metaminer.IsPoW() && retryCount > 0 {
// make sure the previous block exists in order to calculate rewards distribution
for try := 100; try > 0; try-- {
if _, _, err := metaminer.CalculateRewards(block.Number(), big.NewInt(0), big.NewInt(100000000), nil); err == nil {
break
}
time.Sleep(100 * time.Millisecond)
}
goto retry
}

bc.reportBlock(block, receipts, err)
atomic.StoreUint32(&followupInterrupt, 1)
return it.index, err
Expand Down

0 comments on commit e51e813

Please sign in to comment.