Skip to content

Commit 8ebe992

Browse files
committed
Move rollback of nonce range reservation to eoaEnqueueTxs()
1 parent c21529e commit 8ebe992

2 files changed

Lines changed: 56 additions & 35 deletions

File tree

services/requester/batch_tx_pool.go

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -490,11 +490,7 @@ func (t *BatchTxPool) processPooledTransactions(ctx context.Context) {
490490
// In case of any submission errors, add the transactions back
491491
// to the pool as a retry mechanism. This is an important part
492492
// to avoid gaps, which would require users to resubmit.
493-
// Rollback the nonce range reservation from before — but only
494-
// if a concurrent Add() has not already advanced past it via
495-
// the fast path, otherwise we'd erase legitimate activity.
496-
eoaQueue, ok := t.eoaEnqueueTxs(address, batch.txs)
497-
if !ok {
493+
if !t.eoaEnqueueTxs(address, batch) {
498494
t.logger.Warn().Err(err).Msgf(
499495
"max number of retries reached for EOA: %s, batch count: %d, nonce: %d, tx hash: %s",
500496
address.Hex(),
@@ -504,10 +500,6 @@ func (t *BatchTxPool) processPooledTransactions(ctx context.Context) {
504500
)
505501
t.collector.TransactionsDropped(len(batch.txs))
506502
}
507-
if eoaQueue.lastSubmittedNonce == batch.txs[len(batch.txs)-1].nonce {
508-
eoaQueue.lastSubmittedNonce = batch.lastSubmittedNonce
509-
eoaQueue.lastSubmittedAt = batch.lastSubmittedAt
510-
}
511503
} else {
512504
// Merge the ack with any concurrent Add() fast-path that
513505
// advanced the queue while we were off-lock: never regress
@@ -622,29 +614,41 @@ func (t *BatchTxPool) eoaQueueEntry(address gethCommon.Address) *txQueue {
622614
}
623615

624616
// eoaEnqueueTxs re-adds the given transactions to the corresponding txQueue
625-
// for the given address (used as a rollback path on submission failure), and
626-
// returns the txQueue. One will be created if it doesn't yet exist. A same-
627-
// nonce entry already in the queue is preserved: a concurrent Add() may have
628-
// dropped a fresher payload there while we were off-lock, and last-write-wins
629-
// for the client means the fresh payload must win over the failed batch.
617+
// for the given address (used as a rollback path on submission failure).
618+
// One will be created if it doesn't yet exist. A same-nonce entry already
619+
// in the queue is preserved: a concurrent Add() may have dropped a fresher
620+
// payload there while we were off-lock, and last-write-wins for the client
621+
// means the fresh payload must win over the failed batch.
630622
// Up to `maxSubmissionRetries` are allowed and a boolean value is returned to
631623
// denote enqueue success.
632624
func (t *BatchTxPool) eoaEnqueueTxs(
633625
address gethCommon.Address,
634-
txs []pooledEvmTx,
635-
) (*txQueue, bool) {
626+
batch batchSubmission,
627+
) bool {
636628
queue := t.eoaQueueEntry(address)
629+
// Rollback the nonce range reservation from before — but only
630+
// if a concurrent Add() has not already advanced past it via
631+
// the fast path, otherwise we'd erase legitimate activity.
632+
if queue.lastSubmittedNonce == batch.txs[len(batch.txs)-1].nonce {
633+
queue.lastSubmittedNonce = batch.lastSubmittedNonce
634+
queue.lastSubmittedAt = batch.lastSubmittedAt
635+
}
636+
637637
if queue.retries >= maxSubmissionRetries {
638-
return queue, false
638+
return false
639639
}
640-
for _, tx := range txs {
640+
641+
// re-add the transactions from the batch, in the per-EOA pool
642+
for _, tx := range batch.txs {
641643
if _, exists := queue.txs[tx.nonce]; exists {
642644
continue
643645
}
644646
queue.txs[tx.nonce] = tx
645647
}
648+
649+
// increment the counter of submission retries
646650
queue.retries += 1
647-
return queue, true
651+
return true
648652
}
649653

650654
// logSubmission records the fate of a submitted batch so a transaction is

services/requester/batch_tx_pool_test.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,12 @@ func Test_BatchTxPool_EnqueuePreservesFresh(t *testing.T) {
237237

238238
stale := makePooledTx(3)
239239
stale.txHash = gethCommon.BytesToHash([]byte("stale"))
240-
pool.eoaEnqueueTxs(addr, []pooledEvmTx{stale})
240+
pool.eoaEnqueueTxs(
241+
addr,
242+
batchSubmission{
243+
txs: []pooledEvmTx{stale},
244+
},
245+
)
241246

242247
assert.Equal(t, fresh.txHash, q.txs[3].txHash,
243248
"eoaEnqueueTxs must not overwrite a fresh same-nonce entry")
@@ -252,11 +257,15 @@ func Test_BatchTxPool_EnqueueFillsMissingNonces(t *testing.T) {
252257
}
253258
addr := gethCommon.HexToAddress("0xabc")
254259

255-
pool.eoaEnqueueTxs(addr, []pooledEvmTx{
256-
makePooledTx(1),
257-
makePooledTx(2),
258-
makePooledTx(3),
259-
})
260+
pool.eoaEnqueueTxs(addr,
261+
batchSubmission{
262+
txs: []pooledEvmTx{
263+
makePooledTx(1),
264+
makePooledTx(2),
265+
makePooledTx(3),
266+
},
267+
},
268+
)
260269

261270
q := pool.eoaQueueEntry(addr)
262271
assert.Len(t, q.txs, 3)
@@ -322,11 +331,15 @@ func Test_BatchTxPool_SubmissionFailureNonceReservationRollback(t *testing.T) {
322331
require.NoError(t, err)
323332
addr := crypto.PubkeyToAddress(key.PublicKey)
324333

325-
pool.eoaEnqueueTxs(addr, []pooledEvmTx{
326-
makePooledTx(2),
327-
makePooledTx(3),
328-
makePooledTx(4),
329-
})
334+
pool.eoaEnqueueTxs(addr,
335+
batchSubmission{
336+
txs: []pooledEvmTx{
337+
makePooledTx(2),
338+
makePooledTx(3),
339+
makePooledTx(4),
340+
},
341+
},
342+
)
330343

331344
q := pool.eoaQueueEntry(addr)
332345
assert.Len(t, q.txs, 3)
@@ -400,11 +413,15 @@ func Test_BatchTxPool_SubmissionSuccessNonRegressionMerge(t *testing.T) {
400413
require.NoError(t, err)
401414
addr := crypto.PubkeyToAddress(key.PublicKey)
402415

403-
pool.eoaEnqueueTxs(addr, []pooledEvmTx{
404-
makePooledTx(2),
405-
makePooledTx(3),
406-
makePooledTx(4),
407-
})
416+
pool.eoaEnqueueTxs(addr,
417+
batchSubmission{
418+
txs: []pooledEvmTx{
419+
makePooledTx(2),
420+
makePooledTx(3),
421+
makePooledTx(4),
422+
},
423+
},
424+
)
408425

409426
q := pool.eoaQueueEntry(addr)
410427
assert.Len(t, q.txs, 3)

0 commit comments

Comments
 (0)