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(tx-pool): add unit tests for BestTransactions add_new_transactions #9355

Merged
merged 2 commits into from
Jul 8, 2024
Merged
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
116 changes: 116 additions & 0 deletions crates/transaction-pool/src/pool/best.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ mod tests {
use crate::{
pool::pending::PendingPool,
test_utils::{MockOrdering, MockTransaction, MockTransactionFactory},
Priority,
};
use reth_primitives::U256;

#[test]
fn test_best_iter() {
Expand Down Expand Up @@ -478,4 +480,118 @@ mod tests {
// No more transactions should be returned
assert!(best.next().is_none());
}

#[test]
fn test_best_add_transaction_with_next_nonce() {
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 BestTransactions iterator from the pool
let mut best = pool.best();

// Use a broadcast channel for transaction updates
let (tx_sender, tx_receiver) =
tokio::sync::broadcast::channel::<PendingTransaction<MockOrdering>>(1000);
best.new_transaction_receiver = Some(tx_receiver);

// Create a new transaction with nonce 5 and validate it
let new_tx = MockTransaction::eip1559().rng_hash().with_nonce(5);
let valid_new_tx = f.validated(new_tx);

// Send the new transaction through the broadcast channel
let pending_tx = PendingTransaction {
submission_id: 10,
transaction: Arc::new(valid_new_tx.clone()),
priority: Priority::Value(U256::from(1000)),
};
tx_sender.send(pending_tx.clone()).unwrap();

// Add new transactions to the iterator
best.add_new_transactions();

// Verify that the new transaction has been added to the 'all' map
assert_eq!(best.all.len(), 6);
assert!(best.all.contains_key(valid_new_tx.id()));

// Verify that the new transaction has been added to the 'independent' set
assert_eq!(best.independent.len(), 2);
assert!(best.independent.contains(&pending_tx));
}

#[test]
fn test_best_add_transaction_with_existing_id() {
// 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 BestTransactions iterator from the pool
let mut best = pool.best();

// Use a broadcast channel for transaction updates
let (tx_sender, tx_receiver) =
tokio::sync::broadcast::channel::<PendingTransaction<MockOrdering>>(1000);
best.new_transaction_receiver = Some(tx_receiver);

// Create a new transaction with nonce 5 and validate it
let new_tx = MockTransaction::eip1559().rng_hash().with_nonce(5);
let valid_new_tx = f.validated(new_tx);

// Send the new transaction through the broadcast channel
let pending_tx = PendingTransaction {
submission_id: 10,
transaction: Arc::new(valid_new_tx.clone()),
priority: Priority::Value(U256::from(1000)),
};
tx_sender.send(pending_tx.clone()).unwrap();

// Add new transactions to the iterator
best.add_new_transactions();

// Verify that the new transaction has been added to the 'all' map
assert_eq!(best.all.len(), 6);
assert!(best.all.contains_key(valid_new_tx.id()));

// Verify that the new transaction has been added to the 'independent' set
assert_eq!(best.independent.len(), 2);
assert!(best.independent.contains(&pending_tx));

// Attempt to add the same transaction again
let new_tx = MockTransaction::eip1559().rng_hash().with_nonce(5);
let valid_new_tx = f.validated(new_tx);

// Send the duplicate transaction through the broadcast channel
let pending_tx = PendingTransaction {
submission_id: 10,
transaction: Arc::new(valid_new_tx.clone()),
priority: Priority::Value(U256::from(1000)),
};
tx_sender.send(pending_tx.clone()).unwrap();

// Add new transactions to the iterator
best.add_new_transactions();

// Verify that the duplicate transaction has been added to 'all' but not to 'independent'
assert_eq!(best.all.len(), 7);
assert!(best.all.contains_key(valid_new_tx.id()));
assert_eq!(best.independent.len(), 2);
assert!(!best.independent.contains(&pending_tx));
}
}
Loading