From c5b22fbc324b8465199a7db472b1e57feb362da6 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Thu, 9 May 2024 19:00:41 +0800 Subject: [PATCH] core: make txpool handle reorg due to setHead (#19308) --- core/tx_pool.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/tx_pool.go b/core/tx_pool.go index b0e43fd468b0..ac3074ce3476 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -415,6 +415,22 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) { rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64()) add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64()) ) + if rem == nil { + // This can happen if a setHead is performed, where we simply discard the old + // head from the chain. + // If that is the case, we don't have the lost transactions any more, and + // there's nothing to add + if newNum < oldNum { + // If the reorg ended up on a lower number, it's indicative of setHead being the cause + log.Debug("Skipping transaction reset caused by setHead", + "old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum) + } else { + // If we reorged to a same or higher number, then it's not a case of setHead + log.Warn("Transaction pool reset with missing oldhead", + "old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum) + } + return + } for rem.NumberU64() > add.NumberU64() { discarded = append(discarded, rem.Transactions()...) if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {