Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(mempool): refactor MempoolState to have Partial and Full states #507

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 use generic type, for the compiler.
_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
Loading