Skip to content

Commit

Permalink
test(mempool): add mempool state struct and new function
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware committed Jul 10, 2024
1 parent 068f596 commit f38b0b7
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,31 @@ use crate::mempool::{Mempool, MempoolInput, TransactionReference};
use crate::transaction_pool::TransactionPool;
use crate::transaction_queue::TransactionQueue;

/// `MempoolState` is a utility struct for testing mempool scenarios. It allows the creation of
/// different mempool states, without following the regular mempool behavior.
struct MempoolState {
tx_pool: TransactionPool,
tx_queue: TransactionQueue,
}

impl MempoolState {
fn new<T>(pool_txs: T, queue_txs: T) -> Self
where
T: IntoIterator<Item = ThinTransaction>,
{
let tx_pool = pool_txs.into_iter().collect();
let tx_queue = queue_txs.into_iter().collect();
MempoolState { tx_pool, tx_queue }
}
}

impl From<MempoolState> for Mempool {
fn from(mempool_state: MempoolState) -> Mempool {
let MempoolState { tx_pool, tx_queue } = mempool_state;
Mempool { tx_pool, tx_queue }
}
}

impl FromIterator<ThinTransaction> for TransactionPool {
fn from_iter<T: IntoIterator<Item = ThinTransaction>>(txs: T) -> TransactionPool {
let mut pool = TransactionPool::default();
Expand Down Expand Up @@ -98,13 +123,13 @@ fn test_get_txs(#[case] requested_txs: usize) {
let input_tip_10_address_2 = add_tx_input!(tip: 10, tx_hash: 3, sender_address: "0x2");

let txs = [
input_tip_50_address_0.clone(),
input_tip_100_address_1.clone(),
input_tip_10_address_2.clone(),
input_tip_50_address_0.clone().tx,
input_tip_100_address_1.clone().tx,
input_tip_10_address_2.clone().tx,
];
let n_txs = txs.len();

let mut mempool = Mempool::new(txs).unwrap();
let mut mempool: Mempool = MempoolState::new(txs.clone(), txs).into();

let sorted_txs =
[input_tip_100_address_1.tx, input_tip_50_address_0.tx, input_tip_10_address_2.tx];
Expand Down

0 comments on commit f38b0b7

Please sign in to comment.