From ff90079f9186a1e4e4f755a363e51b92f0b0e9ad Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Wed, 16 Oct 2024 11:30:15 +0200 Subject: [PATCH] chore: use highest known nonce --- crates/transaction-pool/src/pool/txpool.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index c470faf3a1bf..c89aca830f0a 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -110,13 +110,21 @@ impl TxPool { /// Returns the transaction with the highest nonce that is executable given the on chain nonce. /// + /// If the pool already tracks a higher nonce for the given sender, then this nonce is used + /// instead. + /// /// Note: The next pending pooled transaction must have the on chain nonce. pub(crate) fn get_highest_consecutive_transaction_by_sender( &self, - on_chain: TransactionId, + mut on_chain: TransactionId, ) -> Option>> { let mut last_consecutive_tx = None; + // ensure this operates on the most recent + if let Some(current) = self.sender_info.get(&on_chain.sender) { + on_chain.nonce = on_chain.nonce.max(current.state_nonce); + } + let mut next_expected_nonce = on_chain.nonce; for (id, tx) in self.all().descendant_txs_inclusive(&on_chain) { if next_expected_nonce != id.nonce { @@ -2784,7 +2792,7 @@ mod tests { // Create transactions with nonces 0, 1, 2, 4, 5. let sender = Address::random(); - let txs: Vec<_> = vec![0, 1, 2, 4, 5]; + let txs: Vec<_> = vec![0, 1, 2, 4, 5, 8, 9]; for nonce in txs { let mut mock_tx = MockTransaction::eip1559(); mock_tx.set_sender(sender); @@ -2804,6 +2812,13 @@ mod tests { let next_tx = pool.get_highest_consecutive_transaction_by_sender(sender_id.into_id(5)); assert_eq!(next_tx.map(|tx| tx.nonce()), Some(5), "Expected nonce 5 for on-chain nonce 5"); + + // update the tracked nonce + let mut info = SenderInfo::default(); + info.update(8, U256::ZERO); + pool.sender_info.insert(sender_id, info); + let next_tx = pool.get_highest_consecutive_transaction_by_sender(sender_id.into_id(5)); + assert_eq!(next_tx.map(|tx| tx.nonce()), Some(9), "Expected nonce 9 for on-chain nonce 8"); } #[test]