Skip to content

Commit

Permalink
test(mempool): refactor add-tx util
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadNassar1 committed Jul 29, 2024
1 parent c60f856 commit afd7823
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ impl FromIterator<TransactionReference> for TransactionQueue {
}

#[track_caller]
fn add_tx(mempool: &mut Mempool, input: &MempoolInput) {
fn add_tx_expect_success(mempool: &mut Mempool, input: &MempoolInput) {
assert_eq!(mempool.add_tx(input.clone()), Ok(()));
}

#[track_caller]
fn _add_tx_expect_error(mempool: &mut Mempool, input: &MempoolInput, expected_err: MempoolError) {
assert_eq!(mempool.add_tx(input.clone()), Err(expected_err));
}

/// 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)
Expand Down Expand Up @@ -304,7 +309,7 @@ fn test_add_tx(mut mempool: Mempool) {

// Test.
for input in &add_tx_inputs {
add_tx(&mut mempool, input);
add_tx_expect_success(&mut mempool, input);
}

// TODO(Ayelet): Consider share this code.
Expand All @@ -330,9 +335,9 @@ fn test_add_tx_multi_nonce_success(mut mempool: Mempool) {
add_tx_input!(tx_hash: 3, sender_address: "0x0", tx_nonce: 1_u8, account_nonce: 0_u8);

// Test.
add_tx(&mut mempool, &input_address_0_nonce_0);
add_tx(&mut mempool, &input_address_1_nonce_0);
add_tx(&mut mempool, &input_address_0_nonce_1);
add_tx_expect_success(&mut mempool, &input_address_0_nonce_0);
add_tx_expect_success(&mut mempool, &input_address_1_nonce_0);
add_tx_expect_success(&mut mempool, &input_address_0_nonce_1);

// Assert: only the eligible transactions appear in the queue.
let expected_queue_txs =
Expand Down Expand Up @@ -361,7 +366,7 @@ fn test_add_tx_with_duplicate_tx(mut mempool: Mempool) {
let duplicate_input = input.clone();

// Test.
add_tx(&mut mempool, &input);
add_tx_expect_success(&mut mempool, &input);
assert_matches!(
mempool.add_tx(duplicate_input),
Err(MempoolError::DuplicateTransaction { .. })
Expand All @@ -382,8 +387,8 @@ fn test_add_tx_with_identical_tip_succeeds(mut mempool: Mempool) {
let input2 = add_tx_input!(tip: 1, tx_hash: 1, sender_address: "0x1");

// Test.
add_tx(&mut mempool, &input1);
add_tx(&mut mempool, &input2);
add_tx_expect_success(&mut mempool, &input1);
add_tx_expect_success(&mut mempool, &input2);

// Assert: both transactions are in the mempool.
let expected_queue_txs =
Expand All @@ -406,8 +411,8 @@ fn test_tip_priority_over_tx_hash(mut mempool: Mempool) {
let input_small_tip_big_hash = add_tx_input!(tip: 1, tx_hash: Felt::TWO, sender_address: "0x1");

// Test.
add_tx(&mut mempool, &input_big_tip_small_hash);
add_tx(&mut mempool, &input_small_tip_big_hash);
add_tx_expect_success(&mut mempool, &input_big_tip_small_hash);
add_tx_expect_success(&mut mempool, &input_small_tip_big_hash);

// Assert: ensure that the transaction with the higher tip is prioritized higher.
let expected_queue_txs =
Expand All @@ -426,12 +431,12 @@ fn test_add_tx_account_state_fills_hole(mut mempool: Mempool) {
// Test and assert.

// First, with gap.
add_tx(&mut mempool, &tx_input_nonce_1);
add_tx_expect_success(&mut mempool, &tx_input_nonce_1);
let expected_mempool_state = MempoolState::with_queue([]);
expected_mempool_state.assert_eq_queue_state(&mempool);

// Then, fill it.
add_tx(&mut mempool, &tx_input_nonce_2);
add_tx_expect_success(&mut mempool, &tx_input_nonce_2);
let expected_queue_txs = [&tx_input_nonce_1.tx].map(TransactionReference::new);
let expected_mempool_state = MempoolState::with_queue(expected_queue_txs);
expected_mempool_state.assert_eq_queue_state(&mempool);
Expand Down Expand Up @@ -468,8 +473,8 @@ fn test_add_tx_sequential_nonces(mut mempool: Mempool) {
let input_nonce_1 = add_tx_input!(tx_hash: 1, tx_nonce: 1_u8, account_nonce: 0_u8);

// Test.
add_tx(&mut mempool, &input_nonce_0);
add_tx(&mut mempool, &input_nonce_1);
add_tx_expect_success(&mut mempool, &input_nonce_0);
add_tx_expect_success(&mut mempool, &input_nonce_1);

// Assert: only eligible transaction appears in the queue.
let expected_queue_txs = [TransactionReference::new(&input_nonce_0.tx)];
Expand Down Expand Up @@ -505,7 +510,7 @@ fn test_add_tx_filling_hole(mut mempool: Mempool) {
let input_nonce_1 = add_tx_input!(tx_hash: 2, tx_nonce: 1_u8, account_nonce: 0_u8);

// Test: add the second transaction first, which creates a hole in the sequence.
add_tx(&mut mempool, &input_nonce_1);
add_tx_expect_success(&mut mempool, &input_nonce_1);

// Assert: the second transaction is in the pool and not in the queue.
let expected_queue_txs = [];
Expand All @@ -514,7 +519,7 @@ fn test_add_tx_filling_hole(mut mempool: Mempool) {
expected_mempool_state.assert_eq_mempool_state(&mempool);

// Test: add the first transaction, which fills the hole.
add_tx(&mut mempool, &input_nonce_0);
add_tx_expect_success(&mut mempool, &input_nonce_0);

// Assert: only the eligible transaction appears in the queue.
let expected_queue_txs = [TransactionReference::new(&input_nonce_0.tx)];
Expand All @@ -534,15 +539,15 @@ fn test_flow_filling_holes(mut mempool: Mempool) {
add_tx_input!(tx_hash: 3, sender_address: "0x1", tx_nonce: 0_u8, account_nonce: 0_u8);

// Test.
add_tx(&mut mempool, &input_address_0_nonce_1);
add_tx(&mut mempool, &input_address_1_nonce_0);
add_tx_expect_success(&mut mempool, &input_address_0_nonce_1);
add_tx_expect_success(&mut mempool, &input_address_1_nonce_0);
let txs = mempool.get_txs(2).unwrap();

// Assert: only the eligible transaction is returned.
assert_eq!(txs, &[input_address_1_nonce_0.tx]);

// Test.
add_tx(&mut mempool, &input_address_0_nonce_0);
add_tx_expect_success(&mut mempool, &input_address_0_nonce_0);
let txs = mempool.get_txs(2).unwrap();

// Assert: all remaining transactions are returned.
Expand Down

0 comments on commit afd7823

Please sign in to comment.