Skip to content

Commit 20a79a8

Browse files
committed
core: cache block hash and number
1 parent 5c6a9fa commit 20a79a8

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

core/blockchain.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -678,12 +678,14 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
678678
//
679679
// Note, this function assumes that the `mu` mutex is held!
680680
func (bc *BlockChain) insert(block *types.Block, writeBlock bool) {
681+
blockHash := block.Hash()
682+
blockNumberU64 := block.NumberU64()
681683
// If the block is on a side chain or an unknown one, force other heads onto it too
682-
updateHeads := GetCanonicalHash(bc.db, block.NumberU64()) != block.Hash()
684+
updateHeads := GetCanonicalHash(bc.db, blockNumberU64) != blockHash
683685

684686
// Add the block to the canonical chain number scheme and mark as the head
685-
rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64())
686-
rawdb.WriteHeadBlockHash(bc.db, block.Hash())
687+
rawdb.WriteCanonicalHash(bc.db, blockHash, blockNumberU64)
688+
rawdb.WriteHeadBlockHash(bc.db, blockHash)
687689
if writeBlock {
688690
rawdb.WriteBlock(bc.db, block)
689691
}
@@ -693,15 +695,15 @@ func (bc *BlockChain) insert(block *types.Block, writeBlock bool) {
693695
if bc.chainConfig.XDPoS != nil && !bc.chainConfig.IsTIPSigning(block.Number()) {
694696
engine, ok := bc.Engine().(*XDPoS.XDPoS)
695697
if ok {
696-
engine.CacheNoneTIPSigningTxs(block.Header(), block.Transactions(), bc.GetReceiptsByHash(block.Hash()))
698+
engine.CacheNoneTIPSigningTxs(block.Header(), block.Transactions(), bc.GetReceiptsByHash(blockHash))
697699
}
698700
}
699701

700702
// If the block is better than our head or is on a different chain, force update heads
701703
if updateHeads {
702704
bc.hc.SetCurrentHeader(block.Header())
703705

704-
if err := WriteHeadFastBlockHash(bc.db, block.Hash()); err != nil {
706+
if err := WriteHeadFastBlockHash(bc.db, blockHash); err != nil {
705707
log.Crit("Failed to insert head fast block hash", "err", err)
706708
}
707709
bc.currentFastBlock.Store(block)

0 commit comments

Comments
 (0)