refactor onBlockStart params, new skip method

This commit is contained in:
Sina Mahmoodi 2024-02-15 16:22:59 +01:00
parent 3ba6b92dfb
commit 5f0a5437e6
2 changed files with 29 additions and 9 deletions

View File

@ -185,6 +185,15 @@ func DefaultCacheConfigWithScheme(scheme string) *CacheConfig {
return &config return &config
} }
// BlockEvent is emitted upon tracing an incoming block.
// It contains the block as well as consensus related information.
type BlockEvent struct {
Block *types.Block
TD *big.Int
Finalized *types.Header
Safe *types.Header
}
// BlockchainLogger is used to collect traces during chain processing. // BlockchainLogger is used to collect traces during chain processing.
// Please make a copy of the referenced types if you intend to retain them. // Please make a copy of the referenced types if you intend to retain them.
type BlockchainLogger interface { type BlockchainLogger interface {
@ -193,12 +202,12 @@ type BlockchainLogger interface {
OnBlockchainInit(chainConfig *params.ChainConfig) OnBlockchainInit(chainConfig *params.ChainConfig)
// OnBlockStart is called before executing `block`. // OnBlockStart is called before executing `block`.
// `td` is the total difficulty prior to `block`. // `td` is the total difficulty prior to `block`.
// `skip` indicates processing of this previously known block OnBlockStart(event BlockEvent)
// will be skipped. OnBlockStart and OnBlockEnd will be emitted to
// convey how chain is progressing. E.g. known blocks will be skipped
// when node is started after a crash.
OnBlockStart(block *types.Block, td *big.Int, finalized *types.Header, safe *types.Header, skip bool)
OnBlockEnd(err error) OnBlockEnd(err error)
// OnSkippedBlock indicates a block was skipped during processing
// due to it being known previously. This can happen e.g. when recovering
// from a crash.
OnSkippedBlock(event BlockEvent)
OnGenesisBlock(genesis *types.Block, alloc GenesisAlloc) OnGenesisBlock(genesis *types.Block, alloc GenesisAlloc)
} }
@ -1770,8 +1779,12 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
} }
stats.processed++ stats.processed++
if bc.logger != nil { if bc.logger != nil {
bc.logger.OnBlockStart(block, bc.GetTd(block.ParentHash(), block.NumberU64()-1), bc.CurrentFinalBlock(), bc.CurrentSafeBlock(), true) bc.logger.OnSkippedBlock(BlockEvent{
bc.logger.OnBlockEnd(nil) Block: block,
TD: bc.GetTd(block.ParentHash(), block.NumberU64()-1),
Finalized: bc.CurrentFinalBlock(),
Safe: bc.CurrentSafeBlock(),
})
} }
// We can assume that logs are empty here, since the only way for consecutive // We can assume that logs are empty here, since the only way for consecutive
@ -1899,7 +1912,12 @@ type blockProcessingResult struct {
func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, start time.Time, setHead bool) (_ *blockProcessingResult, blockEndErr error) { func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, start time.Time, setHead bool) (_ *blockProcessingResult, blockEndErr error) {
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(), false) bc.logger.OnBlockStart(BlockEvent{
Block: block,
TD: td,
Finalized: bc.CurrentFinalBlock(),
Safe: bc.CurrentSafeBlock(),
})
defer func() { defer func() {
bc.logger.OnBlockEnd(blockEndErr) bc.logger.OnBlockEnd(blockEndErr)
}() }()

View File

@ -61,12 +61,14 @@ func (t *noop) CaptureTxStart(env *vm.EVM, tx *types.Transaction, from common.Ad
func (t *noop) CaptureTxEnd(receipt *types.Receipt, err error) { func (t *noop) CaptureTxEnd(receipt *types.Receipt, err error) {
} }
func (t *noop) OnBlockStart(b *types.Block, td *big.Int, finalized, safe *types.Header, skip bool) { func (t *noop) OnBlockStart(ev core.BlockEvent) {
} }
func (t *noop) OnBlockEnd(err error) { func (t *noop) OnBlockEnd(err error) {
} }
func (t *noop) OnSkippedBlock(ev core.BlockEvent) {}
func (t *noop) OnBlockchainInit(chainConfig *params.ChainConfig) { func (t *noop) OnBlockchainInit(chainConfig *params.ChainConfig) {
} }