Skip to content

Commit

Permalink
feat: add tx_pool checker for mempool test (#428)
Browse files Browse the repository at this point in the history
* feat: improve mempool_test debug print

Previous assertion wasn't informative enough on fail.

commit-id:4b2c49c7

* feat: add tx_pool checker for mempool test

- Requires adding accessors to the private tx_pools; these should not be
used outside of our own tests.

- Didn't add a standalone tx_pool checker, can't think of a reason to
  test the pool without the queue (but the reverse is sensible
  somewhat).

- New tester will soon be used to test multi-nonce

commit-id:ba871431

---------

Co-authored-by: Gilad Chase <[email protected]>
  • Loading branch information
giladchase and Gilad Chase authored Jul 10, 2024
1 parent a4de205 commit 0fc2090
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 5 additions & 0 deletions crates/mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ impl Mempool {

Ok(())
}

#[cfg(test)]
pub(crate) fn _tx_pool(&self) -> &TransactionPool {
&self.tx_pool
}
}

/// Provides a lightweight representation of a transaction for mempool usage (e.g., excluding
Expand Down
16 changes: 16 additions & 0 deletions crates/mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use assert_matches::assert_matches;
use itertools::{enumerate, zip_eq};
use pretty_assertions::assert_eq;
Expand Down Expand Up @@ -74,6 +76,20 @@ fn mempool() -> Mempool {
Mempool::empty()
}

// TODO(Ayelet): replace with MempoolState checker.
#[track_caller]
fn _assert_eq_mempool_state(
mempool: &Mempool,
expected_pool: &[ThinTransaction],
expected_queue: &[ThinTransaction],
) {
assert_eq_mempool_queue(mempool, expected_queue);

let expected_pool: HashMap<_, _> =
expected_pool.iter().cloned().map(|tx| (tx.tx_hash, tx)).collect();
assert_eq!(mempool._tx_pool()._tx_pool(), &expected_pool);
}

// Asserts that the transactions in the mempool are in ascending order as per the expected
// transactions.
#[track_caller]
Expand Down
9 changes: 8 additions & 1 deletion crates/mempool/src/transaction_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ use starknet_mempool_types::mempool_types::{MempoolResult, ThinTransaction};

use crate::mempool::TransactionReference;

type HashToTransaction = HashMap<TransactionHash, ThinTransaction>;

/// Contains all transactions currently held in the mempool.
/// Invariant: both data structures are consistent regarding the existence of transactions:
/// A transaction appears in one if and only if it appears in the other.
/// No duplicate transactions appear in the pool.
#[derive(Debug, Default)]
pub struct TransactionPool {
// Holds the complete transaction objects; it should be the sole entity that does so.
tx_pool: HashMap<TransactionHash, ThinTransaction>,
tx_pool: HashToTransaction,
// Transactions organized by account address, sorted by ascending nonce values.
txs_by_account: AccountTransactionIndex,
}
Expand Down Expand Up @@ -70,6 +72,11 @@ impl TransactionPool {
) -> Option<&TransactionReference> {
self.txs_by_account.get(address, nonce)
}

#[cfg(test)]
pub(crate) fn _tx_pool(&self) -> &HashToTransaction {
&self.tx_pool
}
}

#[derive(Debug, Default)]
Expand Down

0 comments on commit 0fc2090

Please sign in to comment.