use defer for OnBlockEnd

This commit is contained in:
Sina Mahmoodi 2023-08-31 18:46:45 +02:00
parent 0be6e22eaf
commit abd880712b

View file

@ -1774,18 +1774,20 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
// Process block using the parent state as reference point // Process block using the parent state as reference point
pstart := time.Now() pstart := time.Now()
// The traced section of block import.
err, stop := func() (blockEndErr error, _ bool) {
if bc.logger != nil { if bc.logger != nil {
td := bc.GetTd(block.ParentHash(), block.NumberU64()-1) td := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
bc.logger.OnBlockStart(block, td, bc.CurrentFinalBlock(), bc.CurrentSafeBlock()) bc.logger.OnBlockStart(block, td, bc.CurrentFinalBlock(), bc.CurrentSafeBlock())
defer func() {
bc.logger.OnBlockEnd(blockEndErr)
}()
} }
receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig) receipts, logs, usedGas, err := bc.processor.Process(block, statedb, bc.vmConfig)
if err != nil { if err != nil {
bc.reportBlock(block, receipts, err) bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true) followupInterrupt.Store(true)
if bc.logger != nil { return err, true
bc.logger.OnBlockEnd(err)
}
return it.index, err
} }
ptime := time.Since(pstart) ptime := time.Since(pstart)
@ -1793,10 +1795,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil { if err := bc.validator.ValidateState(block, statedb, receipts, usedGas); err != nil {
bc.reportBlock(block, receipts, err) bc.reportBlock(block, receipts, err)
followupInterrupt.Store(true) followupInterrupt.Store(true)
if bc.logger != nil { return err, true
bc.logger.OnBlockEnd(err)
}
return it.index, err
} }
vtime := time.Since(vstart) vtime := time.Since(vstart)
proctime := time.Since(start) // processing + validation proctime := time.Since(start) // processing + validation
@ -1830,10 +1829,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
} }
followupInterrupt.Store(true) followupInterrupt.Store(true)
if err != nil { if err != nil {
if bc.logger != nil { return err, true
bc.logger.OnBlockEnd(err)
}
return it.index, err
} }
// Update the metrics touched during block commit // Update the metrics touched during block commit
accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them accountCommitTimer.Update(statedb.AccountCommits) // Account commits are complete, we can mark them
@ -1851,16 +1847,12 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
dirty, _ := bc.triedb.Size() dirty, _ := bc.triedb.Size()
stats.report(chain, it.index, dirty, setHead) stats.report(chain, it.index, dirty, setHead)
if bc.logger != nil {
bc.logger.OnBlockEnd(nil)
}
if !setHead { if !setHead {
// After merge we expect few side chains. Simply count // After merge we expect few side chains. Simply count
// all blocks the CL gives us for GC processing time // all blocks the CL gives us for GC processing time
bc.gcproc += proctime bc.gcproc += proctime
return it.index, nil // Direct block insertion of a single block return nil, true // Direct block insertion of a single block
} }
switch status { switch status {
case CanonStatTy: case CanonStatTy:
@ -1888,6 +1880,11 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
"txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()), "txs", len(block.Transactions()), "gas", block.GasUsed(), "uncles", len(block.Uncles()),
"root", block.Root()) "root", block.Root())
} }
return nil, false
}()
if err != nil || stop {
return it.index, err
}
} }
// Any blocks remaining here? The only ones we care about are the future ones // Any blocks remaining here? The only ones we care about are the future ones