Skip to content

Commit

Permalink
core: reset tx lookup cache if necessary (#28865)
Browse files Browse the repository at this point in the history
This pull request resets the txlookup cache if chain reorg happens,
preventing them from remaining reachable. It addresses failures in
the hive tests.
  • Loading branch information
rjl493456442 authored and ucwong committed Jan 30, 2024
1 parent 6c45a3f commit 940200e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2088,6 +2088,12 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
} else {
log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "newnum", newBlock.Number(), "newhash", newBlock.Hash())
}
// 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-- {
Expand All @@ -2102,11 +2108,16 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *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)
}
// Delete any canonical number assignments above the new head
// 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.
number := commonBlock.NumberU64()
if len(newChain) > 1 {
number = newChain[1].NumberU64()
Expand Down

0 comments on commit 940200e

Please sign in to comment.