From 5f0a5437e6a821b4aff15d39fa1fc986cc7cf992 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 15 Feb 2024 16:22:59 +0100 Subject: [PATCH] refactor onBlockStart params, new skip method --- core/blockchain.go | 34 ++++++++++++++++++++++++++-------- eth/tracers/live/noop.go | 4 +++- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 3e40ab50ea..c4b18da48d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -185,6 +185,15 @@ func DefaultCacheConfigWithScheme(scheme string) *CacheConfig { 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. // Please make a copy of the referenced types if you intend to retain them. type BlockchainLogger interface { @@ -193,12 +202,12 @@ type BlockchainLogger interface { OnBlockchainInit(chainConfig *params.ChainConfig) // OnBlockStart is called before executing `block`. // `td` is the total difficulty prior to `block`. - // `skip` indicates processing of this previously known block - // 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) + OnBlockStart(event BlockEvent) 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) } @@ -1770,8 +1779,12 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) } stats.processed++ if bc.logger != nil { - bc.logger.OnBlockStart(block, bc.GetTd(block.ParentHash(), block.NumberU64()-1), bc.CurrentFinalBlock(), bc.CurrentSafeBlock(), true) - bc.logger.OnBlockEnd(nil) + bc.logger.OnSkippedBlock(BlockEvent{ + 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 @@ -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) { if bc.logger != nil { 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() { bc.logger.OnBlockEnd(blockEndErr) }() diff --git a/eth/tracers/live/noop.go b/eth/tracers/live/noop.go index c565feced0..fe835e1d81 100644 --- a/eth/tracers/live/noop.go +++ b/eth/tracers/live/noop.go @@ -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) 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) OnSkippedBlock(ev core.BlockEvent) {} + func (t *noop) OnBlockchainInit(chainConfig *params.ChainConfig) { }