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
  • Loading branch information
MohammadNassar1 committed Jul 24, 2024
1 parent fc5813a commit 10015be
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,26 @@ 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;

// Invalid input.
// TODO(Mohammad): use `should_insert` method.
if tx.nonce < nonce {
return Err(MempoolError::DuplicateTransaction { tx_hash: tx.tx_hash });
}

self.tx_pool.insert(tx)?;

if is_eligible_for_sequencing(tx_reference, account) {
self.tx_queue.insert(tx_reference);
// If the queue is empty, check if a transaction can be inserted into the transaction queue:
// 1. If the input transaction can be added to the queue, this can happen when the input
// transaction's nonce equals the next nonce.
// 2. If the input state fills a gap in the transaction queue, insert it into the queue.
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 @@ -139,7 +152,3 @@ impl TransactionReference {
}
}
}

fn is_eligible_for_sequencing(tx_reference: TransactionReference, account: Account) -> bool {
tx_reference.nonce == account.state.nonce
}

0 comments on commit 10015be

Please sign in to comment.