Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use highest known nonce #11784

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,21 @@ impl<T: TransactionOrdering> TxPool<T> {

/// 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<Arc<ValidPoolTransaction<T::Transaction>>> {
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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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]
Expand Down
Loading