From 5c6a9fa153a886a245ac234d8eb9de84df86996b Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Wed, 7 Aug 2024 11:39:57 +0800 Subject: [PATCH 1/2] core: fix writeBlock --- core/blockchain.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/core/blockchain.go b/core/blockchain.go index e6aedc143c39..f3cf58bdcc0b 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -684,7 +684,9 @@ func (bc *BlockChain) insert(block *types.Block, writeBlock bool) { // Add the block to the canonical chain number scheme and mark as the head rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64()) rawdb.WriteHeadBlockHash(bc.db, block.Hash()) - rawdb.WriteBlock(bc.db, block) + if writeBlock { + rawdb.WriteBlock(bc.db, block) + } bc.currentBlock.Store(block) // save cache BlockSigners From 20a79a8d9ac1c04c41fa796760e78a8f1390e3b0 Mon Sep 17 00:00:00 2001 From: JukLee0ira Date: Wed, 7 Aug 2024 11:44:28 +0800 Subject: [PATCH 2/2] core: cache block hash and number --- core/blockchain.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index f3cf58bdcc0b..b0bb87d87d0f 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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! 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 - 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 - rawdb.WriteCanonicalHash(bc.db, block.Hash(), block.NumberU64()) - rawdb.WriteHeadBlockHash(bc.db, block.Hash()) + rawdb.WriteCanonicalHash(bc.db, blockHash, blockNumberU64) + rawdb.WriteHeadBlockHash(bc.db, blockHash) if writeBlock { 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()) { engine, ok := bc.Engine().(*XDPoS.XDPoS) 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 { 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) } bc.currentFastBlock.Store(block)