Skip to content

Commit 36f2ae5

Browse files
core: reset tx lookup cache if necessary (ethereum#28865) (ethereum#1157)
This pull request resets the txlookup cache if chain reorg happens, preventing them from remaining reachable. It addresses failures in the hive tests. Co-authored-by: rjl493456442 <[email protected]>
1 parent 08a2608 commit 36f2ae5

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

core/blockchain.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2682,6 +2682,12 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
26822682

26832683
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))
26842684
}
2685+
// Reset the tx lookup cache in case to clear stale txlookups.
2686+
// This is done before writing any new chain data to avoid the
2687+
// weird scenario that canonical chain is changed while the
2688+
// stale lookups are still cached.
2689+
bc.txLookupCache.Purge()
2690+
26852691
// Insert the new chain(except the head block(reverse order)),
26862692
// taking care of the proper incremental order.
26872693
for i := len(newChain) - 1; i >= 1; i-- {
@@ -2696,11 +2702,13 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
26962702

26972703
// Delete useless indexes right now which includes the non-canonical
26982704
// transaction indexes, canonical chain indexes which above the head.
2699-
indexesBatch := bc.db.NewBatch()
2700-
for _, tx := range types.HashDifference(deletedTxs, addedTxs) {
2705+
var (
2706+
indexesBatch = bc.db.NewBatch()
2707+
diffs = types.HashDifference(deletedTxs, addedTxs)
2708+
)
2709+
for _, tx := range diffs {
27012710
rawdb.DeleteTxLookupEntry(indexesBatch, tx)
27022711
}
2703-
27042712
// Delete all hash markers that are not part of the new canonical chain.
27052713
// Because the reorg function does not handle new chain head, all hash
27062714
// markers greater than or equal to new chain head should be deleted.

0 commit comments

Comments
 (0)