Skip to content

Commit 76b29b2

Browse files
MariusVanDerWijdengzliudan
authored andcommitted
core: reset txpool state on sethead (ethereum#22247)
fixes an issue where local transactions that were included in the chain before a SetHead were rejected if resubmitted, since the txpool had not reset the state to the current (older) state.
1 parent 289304f commit 76b29b2

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

core/tx_pool.go

+30-29
Original file line numberDiff line numberDiff line change
@@ -1287,44 +1287,45 @@ func (pool *TxPool) reset(oldHead, newHead *types.Header) {
12871287
// head from the chain.
12881288
// If that is the case, we don't have the lost transactions any more, and
12891289
// there's nothing to add
1290-
if newNum < oldNum {
1291-
// If the reorg ended up on a lower number, it's indicative of setHead being the cause
1292-
log.Debug("Skipping transaction reset caused by setHead",
1293-
"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
1294-
} else {
1290+
if newNum >= oldNum {
12951291
// If we reorged to a same or higher number, then it's not a case of setHead
12961292
log.Warn("Transaction pool reset with missing oldhead",
12971293
"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
1298-
}
1299-
return
1300-
}
1301-
for rem.NumberU64() > add.NumberU64() {
1302-
discarded = append(discarded, rem.Transactions()...)
1303-
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
1304-
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
13051294
return
13061295
}
1307-
}
1308-
for add.NumberU64() > rem.NumberU64() {
1309-
included = append(included, add.Transactions()...)
1310-
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
1311-
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
1312-
return
1296+
// If the reorg ended up on a lower number, it's indicative of setHead being the cause
1297+
log.Debug("Skipping transaction reset caused by setHead",
1298+
"old", oldHead.Hash(), "oldnum", oldNum, "new", newHead.Hash(), "newnum", newNum)
1299+
// We still need to update the current state s.th. the lost transactions can be readded by the user
1300+
} else {
1301+
for rem.NumberU64() > add.NumberU64() {
1302+
discarded = append(discarded, rem.Transactions()...)
1303+
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
1304+
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
1305+
return
1306+
}
13131307
}
1314-
}
1315-
for rem.Hash() != add.Hash() {
1316-
discarded = append(discarded, rem.Transactions()...)
1317-
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
1318-
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
1319-
return
1308+
for add.NumberU64() > rem.NumberU64() {
1309+
included = append(included, add.Transactions()...)
1310+
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
1311+
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
1312+
return
1313+
}
13201314
}
1321-
included = append(included, add.Transactions()...)
1322-
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
1323-
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
1324-
return
1315+
for rem.Hash() != add.Hash() {
1316+
discarded = append(discarded, rem.Transactions()...)
1317+
if rem = pool.chain.GetBlock(rem.ParentHash(), rem.NumberU64()-1); rem == nil {
1318+
log.Error("Unrooted old chain seen by tx pool", "block", oldHead.Number, "hash", oldHead.Hash())
1319+
return
1320+
}
1321+
included = append(included, add.Transactions()...)
1322+
if add = pool.chain.GetBlock(add.ParentHash(), add.NumberU64()-1); add == nil {
1323+
log.Error("Unrooted new chain seen by tx pool", "block", newHead.Number, "hash", newHead.Hash())
1324+
return
1325+
}
13251326
}
1327+
reinject = types.TxDifference(discarded, included)
13261328
}
1327-
reinject = types.TxDifference(discarded, included)
13281329
}
13291330
}
13301331
// Initialize the internal state to the current head

0 commit comments

Comments
 (0)