Skip to content

Commit

Permalink
fix(mempool): in add_tx, delete queue transaction with lower nonce th…
Browse files Browse the repository at this point in the history
…at account nonce (#452)
  • Loading branch information
ayeletstarkware authored and guy-starkware committed Aug 20, 2024
1 parent 26ad7f7 commit f83d33c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
6 changes: 5 additions & 1 deletion crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,11 @@ impl Mempool {
let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } =
input;

// Remove transactions with lower nonce than the account nonce.
// Note: != is actually equivalent to > here, as lower nonces are rejected in validation.
if self.tx_queue.get_nonce(sender_address).is_some_and(|queued_nonce| queued_nonce != nonce)
{
self.tx_queue.remove(sender_address);
}
self.tx_pool.remove_up_to_nonce(sender_address, nonce);

self.tx_pool.insert(tx)?;
Expand Down
29 changes: 25 additions & 4 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,27 @@ fn test_add_tx_lower_than_queued_nonce() {
expected_mempool_content.assert_eq_queue_content(&mempool);
}

#[rstest]
fn test_add_tx_updates_queue_with_higher_account_nonce() {
// Setup.
let input =
add_tx_input!(tx_hash: 1, sender_address: "0x0", tx_nonce: 0_u8, account_nonce: 0_u8);
let higher_account_nonce_input =
add_tx_input!(tx_hash: 2, sender_address: "0x0", tx_nonce: 1_u8, account_nonce: 1_u8);

let queue_txs = [TransactionReference::new_from_thin_tx(&input.tx)];
let mut mempool: Mempool = MempoolContent::with_queue(queue_txs).into();

// Test.
add_tx(&mut mempool, &higher_account_nonce_input);

// Assert: the higher account nonce transaction is in the queue.
let expected_queue_txs =
[TransactionReference::new_from_thin_tx(&higher_account_nonce_input.tx)];
let expected_mempool_content = MempoolContent::with_queue(expected_queue_txs);
expected_mempool_content.assert_eq_queue_content(&mempool);
}

#[rstest]
fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) {
// Setup.
Expand Down Expand Up @@ -478,11 +499,11 @@ fn test_add_tx_delete_tx_with_lower_nonce_than_account_nonce() {
add_tx(&mut mempool, &tx_nonce_1_account_nonce_1);

// Assert the transaction with the lower nonce is removed.
// TODO(Ayelet): Assert the queue after modifying add_tx to delete lower nonce transactions
// in the queue.
let expected_queue_txs =
[TransactionReference::new_from_thin_tx(&tx_nonce_1_account_nonce_1.tx)];
let expected_pool_txs = [tx_nonce_1_account_nonce_1.tx];
let expected_mempool_content = MempoolContent::with_pool(expected_pool_txs);
expected_mempool_content.assert_eq_pool_content(&mempool);
let expected_mempool_content = MempoolContent::new(expected_pool_txs, expected_queue_txs);
expected_mempool_content.assert_eq_mempool_content(&mempool);
}

#[rstest]
Expand Down

0 comments on commit f83d33c

Please sign in to comment.