Skip to content

Commit

Permalink
test(tx-pool): add unit test for remove_sender_count (paradigmxyz#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Oct 21, 2024
1 parent 1f4ca32 commit 4d3b35d
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions crates/transaction-pool/src/pool/parked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub struct ParkedPool<T: ParkedOrd> {
best: BTreeSet<ParkedPoolTransaction<T>>,
/// Keeps track of last submission id for each sender.
///
/// This are sorted in Reverse order, so the last (highest) submission id is first, and the
/// lowest(oldest) is the last.
/// This are sorted in reverse order, so the last (highest) submission id is first, and the
/// lowest (oldest) is the last.
last_sender_submission: BTreeSet<SubmissionSenderId>,
/// Keeps track of the number of transactions in the pool by the sender and the last submission
/// id.
Expand Down Expand Up @@ -856,4 +856,64 @@ mod tests {
assert_eq!(submission_info2.sender_id, sender2);
assert_eq!(submission_info2.submission_id, 2);
}

#[test]
fn test_remove_sender_count() {
// Initialize a mock transaction factory
let mut f = MockTransactionFactory::default();
// Create an empty transaction pool
let mut pool = ParkedPool::<BasefeeOrd<_>>::default();
// Generate two validated transactions and add them to the pool
let tx1 = f.validated_arc(MockTransaction::eip1559().inc_price());
let tx2 = f.validated_arc(MockTransaction::eip1559().inc_price());
pool.add_transaction(tx1);
pool.add_transaction(tx2);

// Define two different sender IDs and their corresponding submission IDs
let sender1: SenderId = 11.into();
let sender2: SenderId = 22.into();

// Add the sender counts to the pool
pool.add_sender_count(sender1, 1);

// We add sender 2 multiple times to test the removal of sender counts
pool.add_sender_count(sender2, 2);
pool.add_sender_count(sender2, 3);

// Before removing the sender count we should have 4 sender transaction counts
assert_eq!(pool.sender_transaction_count.len(), 4);
assert!(pool.sender_transaction_count.contains_key(&sender1));

// We should have 1 sender transaction count for sender 1 before removing the sender count
assert_eq!(pool.sender_transaction_count.get(&sender1).unwrap().count, 1);

// Remove the sender count for sender 1
pool.remove_sender_count(sender1);

// After removing the sender count we should have 3 sender transaction counts remaining
assert_eq!(pool.sender_transaction_count.len(), 3);
assert!(!pool.sender_transaction_count.contains_key(&sender1));

// Check the sender transaction count for sender 2 before removing the sender count
assert_eq!(
*pool.sender_transaction_count.get(&sender2).unwrap(),
SenderTransactionCount { count: 2, last_submission_id: 3 }
);

// Remove the sender count for sender 2
pool.remove_sender_count(sender2);

// After removing the sender count for sender 2, we still have 3 sender transaction counts
// remaining.
//
// This is because we added sender 2 multiple times and we only removed the last submission.
assert_eq!(pool.sender_transaction_count.len(), 3);
assert!(pool.sender_transaction_count.contains_key(&sender2));

// Sender transaction count for sender 2 should be updated correctly
assert_eq!(
*pool.sender_transaction_count.get(&sender2).unwrap(),
SenderTransactionCount { count: 1, last_submission_id: 3 }
);
}
}

0 comments on commit 4d3b35d

Please sign in to comment.