test: add assert_invariants (#4623)

This commit is contained in:
Matthias Seitz
2023-09-18 15:02:20 +02:00
committed by GitHub
parent 8baf2344d4
commit cbb74346bd
4 changed files with 54 additions and 0 deletions

View File

@ -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> {

View File

@ -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>> {

View File

@ -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.

View File

@ -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"))]
@ -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)]
@ -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)]