diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index 9a866ad2d6..b2fc747b8d 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -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. diff --git a/crates/mempool/src/mempool_test.rs b/crates/mempool/src/mempool_test.rs index 43c27fa715..6343b02e12 100644 --- a/crates/mempool/src/mempool_test.rs +++ b/crates/mempool/src/mempool_test.rs @@ -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.