core: cache block hash and number

This commit is contained in:
JukLee0ira 2024-08-07 11:44:28 +08:00 committed by Daniel Liu
parent 074095b1d5
commit 544190f584

View file

@ -678,12 +678,14 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
// //
// Note, this function assumes that the `mu` mutex is held! // Note, this function assumes that the `mu` mutex is held!
func (bc *BlockChain) insert(block *types.Block, writeBlock bool) { func (bc *BlockChain) insert(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 // If the block is on a side chain or an unknown one, force other heads onto it too
updateHeads := GetCanonicalHash(bc.db, block.NumberU64()) != block.Hash() updateHeads := GetCanonicalHash(bc.db, blockNumberU64) != blockHash
// Add the block to the canonical chain number scheme and mark as the head // Add the block to the canonical chain number scheme and mark as the head
rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64()) rawdb.WriteCanonicalHash(bc.db, blockHash, blockNumberU64)
rawdb.WriteHeadBlockHash(bc.db, block.Hash()) rawdb.WriteHeadBlockHash(bc.db, blockHash)
if writeBlock { if writeBlock {
rawdb.WriteBlock(bc.db, block) rawdb.WriteBlock(bc.db, block)
} }
@ -693,7 +695,7 @@ func (bc *BlockChain) insert(block *types.Block, writeBlock bool) {
if bc.chainConfig.XDPoS != nil && !bc.chainConfig.IsTIPSigning(block.Number()) { if bc.chainConfig.XDPoS != nil && !bc.chainConfig.IsTIPSigning(block.Number()) {
engine, ok := bc.Engine().(*XDPoS.XDPoS) engine, ok := bc.Engine().(*XDPoS.XDPoS)
if ok { if ok {
engine.CacheNoneTIPSigningTxs(block.Header(), block.Transactions(), bc.GetReceiptsByHash(block.Hash())) engine.CacheNoneTIPSigningTxs(block.Header(), block.Transactions(), bc.GetReceiptsByHash(blockHash))
} }
} }
@ -701,7 +703,7 @@ func (bc *BlockChain) insert(block *types.Block, writeBlock bool) {
if updateHeads { if updateHeads {
bc.hc.SetCurrentHeader(block.Header()) bc.hc.SetCurrentHeader(block.Header())
if err := WriteHeadFastBlockHash(bc.db, block.Hash()); err != nil { if err := WriteHeadFastBlockHash(bc.db, blockHash); err != nil {
log.Crit("Failed to insert head fast block hash", "err", err) log.Crit("Failed to insert head fast block hash", "err", err)
} }
bc.currentFastBlock.Store(block) bc.currentFastBlock.Store(block)