From 7b97c59f2f290f5f4bf20ff999040a4b0c08fdf2 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Wed, 24 Jan 2024 15:58:48 +0800 Subject: [PATCH 1/3] core: reset tx lookup cache if necessary --- core/blockchain.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 93c40591c6b..8ead80c2533 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2202,11 +2202,17 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error { // Delete useless indexes right now which includes the non-canonical // transaction indexes, canonical chain indexes which above the head. - indexesBatch := bc.db.NewBatch() - for _, tx := range types.HashDifference(deletedTxs, addedTxs) { + var ( + indexesBatch = bc.db.NewBatch() + diffs = types.HashDifference(deletedTxs, addedTxs) + ) + for _, tx := range diffs { rawdb.DeleteTxLookupEntry(indexesBatch, tx) } - + // Reset the tx lookup cache in case some indexes are removed. + if len(diffs) > 0 { + bc.txLookupCache.Purge() + } // Delete all hash markers that are not part of the new canonical chain. // Because the reorg function does not handle new chain head, all hash // markers greater than or equal to new chain head should be deleted. From 064315d6b3cc327d300208f157e761ed2b767081 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 29 Jan 2024 14:34:53 +0800 Subject: [PATCH 2/3] core: reset txlookup cache if reorg happens --- core/blockchain.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 8ead80c2533..24f09983b1d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2209,10 +2209,9 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error { for _, tx := range diffs { rawdb.DeleteTxLookupEntry(indexesBatch, tx) } - // Reset the tx lookup cache in case some indexes are removed. - if len(diffs) > 0 { - bc.txLookupCache.Purge() - } + // Reset the tx lookup cache in case to clear stale txlookups. + bc.txLookupCache.Purge() + // Delete all hash markers that are not part of the new canonical chain. // Because the reorg function does not handle new chain head, all hash // markers greater than or equal to new chain head should be deleted. From 2173b6f8852efdce62b841dabcd6ab45bca2e573 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Mon, 29 Jan 2024 22:13:39 +0800 Subject: [PATCH 3/3] core: clear cache before writing canonical chain --- core/blockchain.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 24f09983b1d..b45ac8e643c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2188,6 +2188,12 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error { // rewind the canonical chain to a lower point. log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain)) } + // Reset the tx lookup cache in case to clear stale txlookups. + // This is done before writing any new chain data to avoid the + // weird scenario that canonical chain is changed while the + // stale lookups are still cached. + bc.txLookupCache.Purge() + // Insert the new chain(except the head block(reverse order)), // taking care of the proper incremental order. for i := len(newChain) - 1; i >= 1; i-- { @@ -2209,9 +2215,6 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error { for _, tx := range diffs { rawdb.DeleteTxLookupEntry(indexesBatch, tx) } - // Reset the tx lookup cache in case to clear stale txlookups. - bc.txLookupCache.Purge() - // Delete all hash markers that are not part of the new canonical chain. // Because the reorg function does not handle new chain head, all hash // markers greater than or equal to new chain head should be deleted.