From d03859d49e8297aed73958d9ec0a60aebf97d694 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Wed, 25 Dec 2024 11:02:44 +0800 Subject: [PATCH] core: update the fast block on chain import at the end of the fast sync phase (#23576) --- core/blockchain.go | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index be6281b6ca..9fd7582960 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -749,11 +749,10 @@ func (bc *BlockChain) writeHeadBlock(block *types.Block, writeBlock bool) { blockHash := block.Hash() blockNumberU64 := block.NumberU64() - // If the block is on a side chain or an unknown one, force other heads onto it too - updateHeads := rawdb.ReadCanonicalHash(bc.db, blockNumberU64) != blockHash - // Add the block to the canonical chain number scheme and mark as the head batch := bc.db.NewBatch() + rawdb.WriteHeadHeaderHash(batch, blockHash) + rawdb.WriteHeadFastBlockHash(batch, blockHash) rawdb.WriteCanonicalHash(batch, blockHash, blockNumberU64) rawdb.WriteTxLookupEntriesByBlock(batch, block) rawdb.WriteHeadBlockHash(batch, blockHash) @@ -761,23 +760,17 @@ func (bc *BlockChain) writeHeadBlock(block *types.Block, writeBlock bool) { rawdb.WriteBlock(batch, block) } - // If the block is better than our head or is on a different chain, force update heads - if updateHeads { - rawdb.WriteHeadHeaderHash(batch, blockHash) - rawdb.WriteHeadFastBlockHash(batch, blockHash) - } - // Flush the whole batch into the disk, exit the node if failed if err := batch.Write(); err != nil { log.Crit("Failed to update chain indexes and markers", "err", err) } // Update all in-memory chain markers in the last step - if updateHeads { - bc.hc.SetCurrentHeader(block.Header()) - bc.currentFastBlock.Store(block) - headFastBlockGauge.Update(int64(blockNumberU64)) - } + bc.hc.SetCurrentHeader(block.Header()) + + bc.currentFastBlock.Store(block) + headFastBlockGauge.Update(int64(blockNumberU64)) + bc.currentBlock.Store(block) headBlockGauge.Update(int64(block.NumberU64()))