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 18, 2024
1 parent 07c3c8c commit e3c7906
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 @@ -2,6 +2,7 @@ use std::collections::HashMap;

use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::{Tip, TransactionHash};
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::{
Account, AccountState, MempoolInput, MempoolResult, ThinTransaction,
};
Expand Down Expand Up @@ -96,13 +97,25 @@ 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.
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 @@ -136,7 +149,3 @@ impl TransactionReference {
}
}
}

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

0 comments on commit e3c7906

Please sign in to comment.