Skip to content

Commit b8b4fb0

Browse files
authored
Merge pull request #19308 from holiman/fix_reset_txpool
core: make txpool handle reorg due to setHead
2 parents f034022 + 650ad19 commit b8b4fb0

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

core/tx_pool.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,26 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {
387387
} else {
388388
// Reorg seems shallow enough to pull in all transactions into memory
389389
var discarded, included types.Transactions
390-
391390
var (
392391
rem = pool.chain.GetBlock(oldHead.Hash(), oldHead.Number.Uint64())
393392
add = pool.chain.GetBlock(newHead.Hash(), newHead.Number.Uint64())
394393
)
394+
if rem == nil {
395+
// This can happen if a setHead is performed, where we simply discard the old
396+
// head from the chain.
397+
// If that is the case, we don't have the lost transactions any more, and
398+
// there's nothing to add
399+
if newNum < oldNum {
400+
// If the reorg ended up on a lower number, it's indicative of setHead being the cause
401+
log.Debug("Skipping transaction reset caused by setHead",
402+
"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
403+
} else {
404+
// If we reorged to a same or higher number, then it's not a case of setHead
405+
log.Warn("Transaction pool reset with missing oldhead",
406+
"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
407+
}
408+
return
409+
}
395410
for rem.NumberU64() > add.NumberU64() {
396411
discarded = append(discarded, rem.Transactions()...)
397412
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {

0 commit comments

Comments
 (0)