Skip to content

Commit

Permalink
add test for BestTransactionFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Aug 23, 2024
1 parent cf425e7 commit 572836e
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions crates/transaction-pool/src/pool/best.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl<T: TransactionOrdering> crate::traits::BestTransactions for BestTransaction
}
}

impl<T: TransactionOrdering> crate::traits::BestTransactionsFilter for BestTransactionsWithFees<T> {}

impl<T: TransactionOrdering> Iterator for BestTransactionsWithFees<T> {
type Item = Arc<ValidPoolTransaction<T::Transaction>>;

Expand Down Expand Up @@ -162,6 +164,8 @@ impl<T: TransactionOrdering> crate::traits::BestTransactions for BestTransaction
}
}

impl<T: TransactionOrdering> crate::traits::BestTransactionsFilter for BestTransactions<T> {}

impl<T: TransactionOrdering> Iterator for BestTransactions<T> {
type Item = Arc<ValidPoolTransaction<T::Transaction>>;

Expand Down Expand Up @@ -596,4 +600,34 @@ mod tests {
assert_eq!(best.independent.len(), 2);
assert!(!best.independent.contains(&pending_tx2));
}

#[test]
fn test_best_transactions_filter_trait_object() {
// Initialize a new PendingPool with default MockOrdering and MockTransactionFactory
let mut pool = PendingPool::new(MockOrdering::default());
let mut f = MockTransactionFactory::default();

// Add 5 transactions with increasing nonces to the pool
let num_tx = 5;
let tx = MockTransaction::eip1559();
for nonce in 0..num_tx {
let tx = tx.clone().rng_hash().with_nonce(nonce);
let valid_tx = f.validated(tx);
pool.add_transaction(Arc::new(valid_tx), 0);
}

// Create a trait object of BestTransactions iterator from the pool
let best: Box<dyn crate::traits::BestTransactions<Item = _>> = Box::new(pool.best());

// Create a filter that only returns transactions with even nonces
let filter =
BestTransactionFilter::new(best, |tx: &Arc<ValidPoolTransaction<MockTransaction>>| {
tx.nonce() % 2 == 0
});

// Verify that the filter only returns transactions with even nonces
for tx in filter {
assert_eq!(tx.nonce() % 2, 0);
}
}
}

0 comments on commit 572836e

Please sign in to comment.