@@ -2188,6 +2188,12 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
21882188 // rewind the canonical chain to a lower point.
21892189 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 ))
21902190 }
2191+ // Reset the tx lookup cache in case to clear stale txlookups.
2192+ // This is done before writing any new chain data to avoid the
2193+ // weird scenario that canonical chain is changed while the
2194+ // stale lookups are still cached.
2195+ bc .txLookupCache .Purge ()
2196+
21912197 // Insert the new chain(except the head block(reverse order)),
21922198 // taking care of the proper incremental order.
21932199 for i := len (newChain ) - 1 ; i >= 1 ; i -- {
@@ -2202,11 +2208,13 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
22022208
22032209 // Delete useless indexes right now which includes the non-canonical
22042210 // transaction indexes, canonical chain indexes which above the head.
2205- indexesBatch := bc .db .NewBatch ()
2206- for _ , tx := range types .HashDifference (deletedTxs , addedTxs ) {
2211+ var (
2212+ indexesBatch = bc .db .NewBatch ()
2213+ diffs = types .HashDifference (deletedTxs , addedTxs )
2214+ )
2215+ for _ , tx := range diffs {
22072216 rawdb .DeleteTxLookupEntry (indexesBatch , tx )
22082217 }
2209-
22102218 // Delete all hash markers that are not part of the new canonical chain.
22112219 // Because the reorg function does not handle new chain head, all hash
22122220 // markers greater than or equal to new chain head should be deleted.
0 commit comments