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 fc5813a commit f5a081b
Showing 1 changed file with 58 additions and 17 deletions.
75 changes: 58 additions & 17 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,75 @@ use crate::mempool::{Mempool, MempoolInput, TransactionReference};
use crate::transaction_pool::TransactionPool;
use crate::transaction_queue::TransactionQueue;

/// Represents the internal state of the mempool.
/// MempoolState represents the internal state of the mempool.
/// Enables customized (and potentially inconsistent) creation for unit testing.
struct MempoolState {
tx_pool: TransactionPool,
tx_queue: TransactionQueue,
/// The `PhantomData` marks the state type (`FullState` or `PartialState`) without storing data of
/// that type, enabling type-safe operations based on the state.
struct MempoolState<T> {
tx_pool: Option<TransactionPool>,
tx_queue: Option<TransactionQueue>,
_phantom: std::marker::PhantomData<T>,
}

impl MempoolState {
fn new<PoolTxs, QueueTxs>(pool_txs: PoolTxs, queue_txs: QueueTxs) -> Self
struct FullState;
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
PoolTxs: IntoIterator<Item = ThinTransaction>,
QueueTxs: IntoIterator<Item = TransactionReference>,
P: IntoIterator<Item = ThinTransaction>,
{
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_pool: Some(pool_txs.into_iter().collect()), ..Default::default() }
}

fn _with_queue<Q>(queue_txs: Q) -> Self
where
Q: IntoIterator<Item = TransactionReference>,
{
Self { tx_queue: Some(queue_txs.into_iter().collect()), ..Default::default() }
}
}

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<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() }
}
}

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 Default for MempoolState<PartialState> {
fn default() -> Self {
Self { tx_pool: None, tx_queue: None, _phantom: std::marker::PhantomData }
}
}

Expand Down

0 comments on commit f5a081b

Please sign in to comment.