Skip to content

Commit

Permalink
test(mempool): refactor MempoolState to have Partial and Full states
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware committed Jul 24, 2024
1 parent 93de0bd commit 4050a0a
Showing 1 changed file with 62 additions and 16 deletions.
78 changes: 62 additions & 16 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,78 @@ use crate::transaction_queue::TransactionQueue;

/// Represents the internal state of the mempool.
/// Enables customized (and potentially inconsistent) creation for unit testing.
struct MempoolState {
tx_pool: TransactionPool,
tx_queue: TransactionQueue,
#[derive(Debug)]
struct MempoolState<T> {
tx_pool: Option<TransactionPool>,
tx_queue: Option<TransactionQueue>,
// PhantomData<T> artificially associates the struct with type T.
_phantom: std::marker::PhantomData<T>,
}

impl MempoolState {
fn new<PoolTxs, QueueTxs>(pool_txs: PoolTxs, queue_txs: QueueTxs) -> Self
#[derive(Debug)]
struct FullState;
#[allow(dead_code)]
#[derive(Debug)]
struct PartialState;

impl MempoolState<FullState> {
fn new<P, Q>(pool_txs: P, queue_txs: Q) -> Self
where
P: IntoIterator<Item = ThinTransaction>,
// TODO(Ayelet): Consider using `&ThinTransaction` instead of `TransactionReference`.
Q: IntoIterator<Item = TransactionReference>,
{
Self {
tx_pool: Some(pool_txs.into_iter().collect()),
tx_queue: Some(queue_txs.into_iter().collect()),
_phantom: std::marker::PhantomData,
}
}
}

impl MempoolState<PartialState> {
fn _with_pool<P>(pool_txs: P) -> Self
where
P: IntoIterator<Item = ThinTransaction>,
{
Self {
tx_pool: Some(pool_txs.into_iter().collect()),
tx_queue: None,
_phantom: std::marker::PhantomData,
}
}

fn _with_queue<Q>(queue_txs: Q) -> Self
where
PoolTxs: IntoIterator<Item = ThinTransaction>,
QueueTxs: IntoIterator<Item = TransactionReference>,
Q: IntoIterator<Item = TransactionReference>,
{
let tx_pool: TransactionPool = pool_txs.into_iter().collect();
let tx_queue: TransactionQueue = queue_txs.into_iter().collect();
MempoolState { tx_pool, tx_queue }
Self {
tx_queue: Some(queue_txs.into_iter().collect()),
tx_pool: None,
_phantom: std::marker::PhantomData,
}
}
}

impl<T> MempoolState<T> {
fn assert_eq_mempool_state(&self, mempool: &Mempool) {
assert_eq!(self.tx_pool, mempool.tx_pool);
assert_eq!(self.tx_queue, mempool.tx_queue);
self.assert_eq_pool_state(mempool);
self.assert_eq_queue_state(mempool);
}

fn assert_eq_pool_state(&self, mempool: &Mempool) {
assert_eq!(self.tx_pool.as_ref().unwrap(), &mempool.tx_pool);
}

fn assert_eq_queue_state(&self, mempool: &Mempool) {
assert_eq!(self.tx_queue.as_ref().unwrap(), &mempool.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<T> From<MempoolState<T>> for Mempool {
fn from(mempool_state: MempoolState<T>) -> Mempool {
let MempoolState { tx_pool, tx_queue, _phantom: _ } = mempool_state;
Mempool { tx_pool: tx_pool.unwrap_or_default(), tx_queue: tx_queue.unwrap_or_default() }
}
}

Expand Down

0 comments on commit 4050a0a

Please sign in to comment.