Skip to content

Commit

Permalink
feat: add multi-nonce support
Browse files Browse the repository at this point in the history
commit-id:c822f343
  • Loading branch information
Gilad Chase committed Jul 10, 2024
1 parent a5823b1 commit d7004cb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
14 changes: 10 additions & 4 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::{Tip, TransactionHash};
use starknet_mempool_types::mempool_types::{
AccountState, MempoolInput, MempoolResult, ThinTransaction,
Account, AccountState, MempoolInput, MempoolResult, ThinTransaction,
};

use crate::transaction_pool::TransactionPool;
Expand Down Expand Up @@ -76,12 +76,14 @@ impl Mempool {
}

fn insert_tx(&mut self, input: MempoolInput) -> MempoolResult<()> {
let tx = input.tx;
let MempoolInput { tx, account } = input;
let tx_reference = TransactionReference::new(&tx);

self.tx_pool.insert(tx)?;
// FIXME: Check nonce before adding!
self.tx_queue.insert(tx_reference);

if is_eligible_for_sequencing(tx_reference, account) {
self.tx_queue.insert(tx_reference);
}

Ok(())
}
Expand Down Expand Up @@ -114,3 +116,7 @@ impl TransactionReference {
}
}
}

fn is_eligible_for_sequencing(tx_reference: TransactionReference, account: Account) -> bool {
tx_reference.nonce == account.state.nonce
}
33 changes: 31 additions & 2 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ macro_rules! add_tx_input {
};
MempoolInput { tx, account }
}};
// Pattern for four arguments.
(tx_hash: $tx_hash:expr, sender_address: $sender_address:expr, tx_nonce: $tx_nonce:expr, account_nonce: $account_nonce:expr) => {
add_tx_input!(tip: 0, tx_hash: $tx_hash, sender_address: $sender_address, tx_nonce: $tx_nonce, account_nonce: $account_nonce)
};
// Pattern for three arguments.
(tip: $tip:expr, tx_hash: $tx_hash:expr, sender_address: $sender_address:expr) => {
add_tx_input!(tip: $tip, tx_hash: $tx_hash, sender_address: $sender_address, tx_nonce: 0_u8, account_nonce: 0_u8)
Expand All @@ -78,12 +82,12 @@ fn mempool() -> Mempool {

// TODO(Ayelet): replace with MempoolState checker.
#[track_caller]
fn _verify_mempool_state_eq(
fn verify_mempool_state_eq(
mempool: &Mempool,
expected_txs: &[ThinTransaction],
expected_queue: &[ThinTransaction],
) {
check_mempool_queue_eq(mempool, expected_queue);
verify_mempool_queue_eq(mempool, expected_queue);

let expected_txs: HashMap<_, _> =
expected_txs.iter().cloned().map(|tx| (tx.tx_hash, tx)).collect();
Expand Down Expand Up @@ -153,6 +157,31 @@ fn test_add_tx(mut mempool: Mempool) {
verify_mempool_queue_eq(&mempool, expected_queue)
}

#[rstest]
fn test_add_tx_multi_nonce_success(mut mempool: Mempool) {
let input_address_0_nonce_0 =
add_tx_input!(tx_hash: 1, sender_address: "0x0", tx_nonce: 0_u8, account_nonce: 0_u8);

let input_address_1 =
add_tx_input!(tx_hash: 2, sender_address: "0x1", tx_nonce: 0_u8,account_nonce: 0_u8);

let input_address_0_nonce_1 =
add_tx_input!(tx_hash: 3, sender_address: "0x0", tx_nonce: 1_u8, account_nonce: 0_u8);

add_tx(&mut mempool, &input_address_0_nonce_0);
add_tx(&mut mempool, &input_address_1);
add_tx(&mut mempool, &input_address_0_nonce_1);

let expected_pool_all_txs = &[
input_address_0_nonce_0.tx.clone(),
input_address_1.tx.clone(),
input_address_0_nonce_1.tx,
];
let expected_queue_only_zero_nonce_txs = &[input_address_1.tx, input_address_0_nonce_0.tx];

verify_mempool_state_eq(&mempool, expected_pool_all_txs, expected_queue_only_zero_nonce_txs);
}

#[test]
fn test_new_with_duplicate_tx() {
let input = add_tx_input!(tip: 0, tx_hash: 1);
Expand Down

0 comments on commit d7004cb

Please sign in to comment.