Skip to content

Commit

Permalink
feat(mempool): in add_tx, delete pool transactions with lower nonces …
Browse files Browse the repository at this point in the history
…than account nonce (#448)
  • Loading branch information
ayeletstarkware authored Aug 15, 2024
1 parent 9956744 commit 18b4df2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ impl Mempool {
let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } =
input;

// Remove transactions with lower nonce than the account nonce.
self.tx_pool.remove_up_to_nonce(sender_address, nonce);

self.tx_pool.insert(tx)?;

// Maybe close nonce gap.
Expand Down
23 changes: 23 additions & 0 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,29 @@ fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) {
expected_mempool_content.assert_eq_mempool_content(&mempool);
}

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

let queue_txs = [TransactionReference::new(&tx_nonce_0_account_nonce_0.tx)];
let pool_txs = [tx_nonce_0_account_nonce_0.tx];
let mut mempool: Mempool = MempoolContent::new(pool_txs, queue_txs).into();

// Test.
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_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);
}

#[rstest]
fn test_tip_priority_over_tx_hash(mut mempool: Mempool) {
// Setup.
Expand Down

0 comments on commit 18b4df2

Please sign in to comment.