Skip to content

Commit

Permalink
fix(mempool): fix add-tx method by updating the queue according to in…
Browse files Browse the repository at this point in the history
…put state (#58)
  • Loading branch information
MohammadNassar1 authored Jul 25, 2024
1 parent 19c30db commit 8058c4f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
16 changes: 8 additions & 8 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,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(())
Expand Down Expand Up @@ -143,7 +147,3 @@ impl TransactionReference {
}
}
}

fn is_eligible_for_sequencing(tx_reference: TransactionReference, account: Account) -> bool {
tx_reference.nonce == account.state.nonce
}
19 changes: 19 additions & 0 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 8058c4f

Please sign in to comment.