Skip to content

Commit

Permalink
feat: add multi-nonce support (#429)
Browse files Browse the repository at this point in the history
commit-id:c822f343

Co-authored-by: Gilad Chase <[email protected]>
  • Loading branch information
giladchase and Gilad Chase authored Jul 11, 2024
1 parent 7c08d83 commit 9ea9c98
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 7 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
}
34 changes: 31 additions & 3 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ fn add_tx(mempool: &mut Mempool, input: &MempoolInput) {
/// Creates a valid input for mempool's `add_tx` with optional default values.
/// Usage:
/// 1. add_tx_input!(tip: 1, tx_hash: 2, sender_address: 3_u8, tx_nonce: 4, account_nonce: 3)
/// 2. add_tx_input!(tip: 1, tx_hash: 2, sender_address: 3_u8)
/// 3. add_tx_input!(tip: 1, tx_hash: 2)
/// 2. add_tx_input!(tx_hash: 2, sender_address: 3_u8, tx_nonce: 4, account_nonce: 3)
/// 3. add_tx_input!(tip: 1, tx_hash: 2, sender_address: 3_u8)
/// 4. add_tx_input!(tip: 1, tx_hash: 2)
macro_rules! add_tx_input {
// Pattern for all four arguments with keyword arguments.
(tip: $tip:expr, tx_hash: $tx_hash:expr, sender_address: $sender_address:expr,
Expand All @@ -61,6 +62,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,7 +83,7 @@ fn mempool() -> Mempool {

// TODO(Ayelet): replace with MempoolState checker.
#[track_caller]
fn _assert_eq_mempool_state(
fn assert_eq_mempool_state(
mempool: &Mempool,
expected_pool: &[ThinTransaction],
expected_queue: &[ThinTransaction],
Expand Down Expand Up @@ -152,6 +157,29 @@ fn test_add_tx(mut mempool: Mempool) {
assert_eq_mempool_queue(&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];

assert_eq_mempool_state(&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 9ea9c98

Please sign in to comment.