Skip to content

Commit

Permalink
test(mempool): add function to assert equality and implement Eq,Parti…
Browse files Browse the repository at this point in the history
…alEq for related structs
  • Loading branch information
ayeletstarkware committed Jul 11, 2024
1 parent 1a21314 commit ba6056d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
15 changes: 11 additions & 4 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impl MempoolState {
let tx_queue: TransactionQueue = queue_txs.into_iter().collect();
MempoolState { tx_pool, tx_queue }
}

fn assert_eq_mempool_state(&self, mempool: &Mempool) {
assert_eq!(self.tx_pool, mempool.tx_pool);
assert_eq!(self.tx_queue, mempool.tx_queue);
}
}

impl From<MempoolState> for Mempool {
Expand Down Expand Up @@ -153,15 +158,17 @@ fn test_get_txs(#[case] requested_txs: usize) {
add_tx_input[2].tx.clone(), // tip 10
];

// This ensures we do not exceed the number of transactions available in the mempool.
// Ensure we do not exceed the number of transactions available in the mempool.
let max_requested_txs = requested_txs.min(add_tx_input.len());

// checks that the returned transactions are the ones with the highest priority.
// Check that the returned transactions are the ones with the highest priority.
let (expected_queue, remaining_txs) = sorted_txs.split_at(max_requested_txs);
assert_eq!(txs, expected_queue);

// checks that the transactions that were not returned are still in the mempool.
assert_eq_mempool_queue(&mempool, remaining_txs);
// Check that the transactions that were not returned are still in the mempool.
let remaining_ref_txs = remaining_txs.iter().map(TransactionReference::new);
let mempool_state = MempoolState::new(remaining_txs.to_vec(), remaining_ref_txs);
mempool_state.assert_eq_mempool_state(&mempool);
}

#[rstest]
Expand Down
4 changes: 2 additions & 2 deletions crates/mempool/src/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type HashToTransaction = HashMap<TransactionHash, ThinTransaction>;
/// Invariant: both data structures are consistent regarding the existence of transactions:
/// A transaction appears in one if and only if it appears in the other.
/// No duplicate transactions appear in the pool.
#[derive(Debug, Default)]
#[derive(Debug, Default, Eq, PartialEq)]
pub struct TransactionPool {
// Holds the complete transaction objects; it should be the sole entity that does so.
tx_pool: HashToTransaction,
Expand Down Expand Up @@ -79,7 +79,7 @@ impl TransactionPool {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Eq, PartialEq)]
struct AccountTransactionIndex(HashMap<ContractAddress, BTreeMap<Nonce, TransactionReference>>);

impl AccountTransactionIndex {
Expand Down
9 changes: 4 additions & 5 deletions crates/mempool/src/transaction_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::transaction::TransactionHash;

use crate::mempool::TransactionReference;
// Assumption: for the MVP only one transaction from the same contract class can be in the mempool
// at a time. When this changes, saving the transactions themselves on the queu might no longer be
// appropriate, because we'll also need to stores transactions without indexing them. For example,
// transactions with future nonces will need to be stored, and potentially indexed on block commits.
#[derive(Debug, Default)]

// Note: PartialEq and Eq for TransactionQueue are derived, ensuring that
// equality checks take into account the sorted nature of the BTreeSet.
#[derive(Debug, Default, Eq, PartialEq)]
pub struct TransactionQueue {
// Priority queue of transactions with associated priority.
queue: BTreeSet<QueuedTransaction>,
Expand Down

0 comments on commit ba6056d

Please sign in to comment.