diff --git a/core/blockchain.go b/core/blockchain.go index 0c00a32fbd6f..ea900ac24921 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2071,7 +2071,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) firehoseContext.FinalizeBlock(block) ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) td := new(big.Int).Add(block.Difficulty(), ptd) - firehoseContext.EndBlock(block, bc.CurrentFinalBlock(), td) + finalBlock := getFinalBlockForFirehose(bc, block) + firehoseContext.EndBlock(block, finalBlock, td) } stats.processed++ @@ -2147,7 +2148,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) // Calculate the total difficulty of the block ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1) td := new(big.Int).Add(block.Difficulty(), ptd) - firehoseContext.EndBlock(block, bc.CurrentFinalBlock(), td) + finalBlock := getFinalBlockForFirehose(bc, block) + firehoseContext.EndBlock(block, finalBlock, td) } bc.cacheReceipts(block.Hash(), receipts, block) @@ -2200,7 +2202,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error) stats.usedGas += usedGas trieDiffNodes, trieBufNodes, trieImmutableBufNodes, _ := bc.triedb.Size() - stats.report(chain, it.index, trieDiffNodes, trieBufNodes, trieImmutableBufNodes, setHead) + stats.report(chain, it.index, trieDiffNodes, trieBufNodes, trieImmutableBufNodes, setHead, bc.CurrentFinalBlock()) if !setHead { // After merge we expect few side chains. Simply count @@ -3199,3 +3201,14 @@ func (bc *BlockChain) SetTrieFlushInterval(interval time.Duration) { func (bc *BlockChain) GetTrieFlushInterval() time.Duration { return time.Duration(bc.flushInterval.Load()) } + +// getFinalBlockForFirehose returns the current final block unless it is +// not canonical, then it returns nil +func getFinalBlockForFirehose(bc *BlockChain, block *types.Block) *types.Header { + if canon := bc.GetBlockByNumber(block.NumberU64()); canon != nil { + if canon.Hash() != block.Hash() { // block is not canonical, we ignore the final block indication + return nil + } + } + return bc.CurrentFinalBlock() +} diff --git a/core/blockchain_insert.go b/core/blockchain_insert.go index 520feaf277cf..c0b62e74ae69 100644 --- a/core/blockchain_insert.go +++ b/core/blockchain_insert.go @@ -39,7 +39,7 @@ const statsReportLimit = 8 * time.Second // report prints statistics if some number of blocks have been processed // or more than a few seconds have passed since the last message. -func (st *insertStats) report(chain []*types.Block, index int, trieDiffNodes, trieBufNodes, trieImmutableBufNodes common.StorageSize, setHead bool) { +func (st *insertStats) report(chain []*types.Block, index int, trieDiffNodes, trieBufNodes, trieImmutableBufNodes common.StorageSize, setHead bool, final *types.Header) { // Fetch the timings for the batch var ( now = mclock.Now() @@ -78,6 +78,9 @@ func (st *insertStats) report(chain []*types.Block, index int, trieDiffNodes, tr context = append(context, []interface{}{"ignored", st.ignored}...) } if setHead { + if final != nil { + context = append(context, []interface{}{"final_number", final.Number, "final_hash", final.Hash()}...) + } log.Info("Imported new chain segment", context...) } else { log.Info("Imported new potential chain segment", context...)