diff --git a/crates/mempool/src/mempool.rs b/crates/mempool/src/mempool.rs index d6ee6382..c5178436 100644 --- a/crates/mempool/src/mempool.rs +++ b/crates/mempool/src/mempool.rs @@ -99,13 +99,17 @@ impl Mempool { } fn insert_tx(&mut self, input: MempoolInput) -> MempoolResult<()> { - let MempoolInput { tx, account } = input; - let tx_reference = TransactionReference::new(&tx); + let MempoolInput { tx, account: Account { sender_address, state: AccountState { nonce } } } = + input; self.tx_pool.insert(tx)?; - if is_eligible_for_sequencing(tx_reference, account) { - self.tx_queue.insert(tx_reference); + // Maybe close nonce gap. + if self.tx_queue.get_nonce(sender_address).is_none() { + if let Some(tx_reference) = self.tx_pool.get_by_address_and_nonce(sender_address, nonce) + { + self.tx_queue.insert(*tx_reference); + } } Ok(()) @@ -139,7 +143,3 @@ impl TransactionReference { } } } - -fn is_eligible_for_sequencing(tx_reference: TransactionReference, account: Account) -> bool { - tx_reference.nonce == account.state.nonce -} diff --git a/crates/mempool/src/mempool_test.rs b/crates/mempool/src/mempool_test.rs index 8f015548..ba70775d 100644 --- a/crates/mempool/src/mempool_test.rs +++ b/crates/mempool/src/mempool_test.rs @@ -309,6 +309,25 @@ fn test_tip_priority_over_tx_hash(mut mempool: Mempool) { assert_eq_mempool_queue(&mempool, &[input_big_tip_small_hash.tx, input_small_tip_big_hash.tx]) } +#[rstest] +fn test_add_tx_account_state_fills_hole(mut mempool: Mempool) { + // Setup. + let tx_input_nonce_1 = add_tx_input!(tx_hash: 1, tx_nonce: 1_u8, account_nonce: 0_u8); + // Input that increments the account state. + let tx_input_nonce_2 = add_tx_input!(tx_hash: 2, tx_nonce: 2_u8, account_nonce: 1_u8); + + // Test and assert. + + // First, with gap. + add_tx(&mut mempool, &tx_input_nonce_1); + // TODO(Mohammad): use Mempool partial state. + assert_eq_mempool_queue(&mempool, &[]); + + // Then, fill it. + add_tx(&mut mempool, &tx_input_nonce_2); + assert_eq_mempool_queue(&mempool, &[tx_input_nonce_1.tx]); +} + #[rstest] fn test_get_txs_with_holes_multiple_accounts() { // Setup.