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: add assert_invariants #4623

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions crates/transaction-pool/src/pool/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ impl<T: PoolTransaction> BlobTransactions<T> {
pub(crate) fn contains(&self, id: &TransactionId) -> bool {
self.by_id.contains_key(id)
}

/// Asserts that the bijection between `by_id` and `all` is valid.
#[cfg(any(test, feature = "test-utils"))]
pub(crate) fn assert_invariants(&self) {
assert_eq!(self.by_id.len(), self.all.len(), "by_id.len() != all.len()");
}
}

impl<T: PoolTransaction> Default for BlobTransactions<T> {
Expand Down
6 changes: 6 additions & 0 deletions crates/transaction-pool/src/pool/parked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ impl<T: ParkedOrd> ParkedPool<T> {
pub(crate) fn contains(&self, id: &TransactionId) -> bool {
self.by_id.contains_key(id)
}

/// Asserts that the bijection between `by_id` and `best` is valid.
#[cfg(any(test, feature = "test-utils"))]
pub(crate) fn assert_invariants(&self) {
assert_eq!(self.by_id.len(), self.best.len(), "by_id.len() != best.len()");
}
}

impl<T: PoolTransaction> ParkedPool<BasefeeOrd<T>> {
Expand Down
10 changes: 10 additions & 0 deletions crates/transaction-pool/src/pool/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,16 @@ impl<T: TransactionOrdering> PendingPool<T> {
pub(crate) fn contains(&self, id: &TransactionId) -> bool {
self.by_id.contains_key(id)
}

/// Asserts that the bijection between `by_id` and `all` is valid.
#[cfg(any(test, feature = "test-utils"))]
pub(crate) fn assert_invariants(&self) {
assert_eq!(self.by_id.len(), self.all.len(), "by_id.len() != all.len()");
assert!(
self.independent_transactions.len() <= self.all.len(),
"independent.len() > all.len()"
);
}
}

/// A transaction that is ready to be included in a block.
Expand Down
32 changes: 32 additions & 0 deletions crates/transaction-pool/src/pool/txpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,25 @@ impl<T: TransactionOrdering> TxPool<T> {
pub(crate) fn is_empty(&self) -> bool {
self.all_transactions.is_empty()
}

/// Asserts all invariants of the pool's:
///
/// - All maps are bijections (`by_id`, `by_hash`)`
/// - Total size is equal to the sum of all sub-pools
///
/// # Panics
/// if any invariant is violated
#[cfg(any(test, feature = "test-utils"))]
pub fn assert_invariants(&self) {
let size = self.size();
let actual = size.basefee + size.pending + size.queued;
assert_eq!(size.total, actual, "total size must be equal to the sum of all sub-pools, basefee:{}, pending:{}, queued:{}", size.basefee, size.pending, size.queued);
self.all_transactions.assert_invariants();
self.pending_pool.assert_invariants();
self.basefee_pool.assert_invariants();
self.queued_pool.assert_invariants();
self.blob_transactions.assert_invariants();
}
}

#[cfg(any(test, feature = "test-utils"))]
Expand All @@ -703,6 +722,13 @@ impl TxPool<crate::test_utils::MockOrdering> {
}
}

#[cfg(test)]
impl<T: TransactionOrdering> Drop for TxPool<T> {
fn drop(&mut self) {
self.assert_invariants();
}
}

// Additional test impls
#[cfg(any(test, feature = "test-utils"))]
#[allow(missing_docs)]
Expand Down Expand Up @@ -1462,6 +1488,12 @@ impl<T: PoolTransaction> AllTransactions<T> {
pub(crate) fn is_empty(&self) -> bool {
self.txs.is_empty()
}

/// Asserts that the bijection between `by_hash` and `txs` is valid.
#[cfg(any(test, feature = "test-utils"))]
pub(crate) fn assert_invariants(&self) {
assert_eq!(self.by_hash.len(), self.txs.len(), "by_hash.len() != txs.len()");
}
}

#[cfg(test)]
Expand Down
Loading