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 more unit tests for parked pool #12591

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Changes from all commits
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
142 changes: 142 additions & 0 deletions crates/transaction-pool/src/pool/parked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -916,4 +916,146 @@ mod tests {
SenderTransactionCount { count: 1, last_submission_id: 3 }
);
}

#[test]
fn test_pool_size() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Create a transaction with a specific size and add it to the pool
let tx = f.validated_arc(MockTransaction::eip1559().set_size(1024).clone());
pool.add_transaction(tx);

// Assert that the reported size of the pool is correct
assert_eq!(pool.size(), 1024);
}

#[test]
fn test_pool_len() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Initially, the pool should have zero transactions
assert_eq!(pool.len(), 0);

// Add a transaction to the pool and check the length
let tx = f.validated_arc(MockTransaction::eip1559());
pool.add_transaction(tx);
assert_eq!(pool.len(), 1);
}

#[test]
fn test_pool_contains() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Create a transaction and get its ID
let tx = f.validated_arc(MockTransaction::eip1559());
let tx_id = *tx.id();

// Before adding, the transaction should not be in the pool
assert!(!pool.contains(&tx_id));

// After adding, the transaction should be present in the pool
pool.add_transaction(tx);
assert!(pool.contains(&tx_id));
}

#[test]
fn test_get_transaction() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Add a transaction to the pool and get its ID
let tx = f.validated_arc(MockTransaction::eip1559());
let tx_id = *tx.id();
pool.add_transaction(tx.clone());

// Retrieve the transaction using `get()` and assert it matches the added transaction
let retrieved = pool.get(&tx_id).expect("Transaction should exist in the pool");
assert_eq!(retrieved.transaction.id(), tx.id());
}

#[test]
fn test_all_transactions() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Add two transactions to the pool
let tx1 = f.validated_arc(MockTransaction::eip1559());
let tx2 = f.validated_arc(MockTransaction::eip1559());
pool.add_transaction(tx1.clone());
pool.add_transaction(tx2.clone());

// Collect all transaction IDs from the pool
let all_txs: Vec<_> = pool.all().map(|tx| *tx.id()).collect();
assert_eq!(all_txs.len(), 2);

// Check that the IDs of both transactions are present
assert!(all_txs.contains(tx1.id()));
assert!(all_txs.contains(tx2.id()));
}

#[test]
fn test_truncate_pool_edge_case() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Add two transactions to the pool
let tx1 = f.validated_arc(MockTransaction::eip1559());
let tx2 = f.validated_arc(MockTransaction::eip1559());
pool.add_transaction(tx1);
pool.add_transaction(tx2);

// Set a limit that matches the current number of transactions
let limit = SubPoolLimit { max_txs: 2, max_size: usize::MAX };
let removed = pool.truncate_pool(limit);

// No transactions should be removed
assert!(removed.is_empty());

// Set a stricter limit that requires truncating one transaction
let limit = SubPoolLimit { max_txs: 1, max_size: usize::MAX };
let removed = pool.truncate_pool(limit);

// One transaction should be removed, and the pool should have one left
assert_eq!(removed.len(), 1);
assert_eq!(pool.len(), 1);
}

#[test]
fn test_satisfy_base_fee_transactions() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Add two transactions with different max fees
let tx1 = f.validated_arc(MockTransaction::eip1559().set_max_fee(100).clone());
let tx2 = f.validated_arc(MockTransaction::eip1559().set_max_fee(200).clone());
pool.add_transaction(tx1);
pool.add_transaction(tx2.clone());

// Check that only the second transaction satisfies the base fee requirement
let satisfied = pool.satisfy_base_fee_transactions(150);
assert_eq!(satisfied.len(), 1);
assert_eq!(satisfied[0].id(), tx2.id())
}

#[test]
fn test_remove_transaction() {
let mut f = MockTransactionFactory::default();
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();

// Add a transaction to the pool and get its ID
let tx = f.validated_arc(MockTransaction::eip1559());
let tx_id = *tx.id();
pool.add_transaction(tx);

// Ensure the transaction is in the pool before removal
assert!(pool.contains(&tx_id));

// Remove the transaction and check that it is no longer in the pool
let removed = pool.remove_transaction(&tx_id);
assert!(removed.is_some());
assert!(!pool.contains(&tx_id));
}
}
Loading