Skip to content

Commit

Permalink
test(starknet_mempool): add case for tx_from_address_exists
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jan 8, 2025
1 parent e126e22 commit 0e50d4e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 9 additions & 0 deletions crates/starknet_mempool/src/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ impl MempoolState {
assert!(committed_nonce <= next_nonce, "NOT SUPPORTED YET {address:?} {next_nonce:?}.")
}
}

#[cfg(test)]
pub fn create_for_testing(
committed: AddressToNonce,
staged: AddressToNonce,
tentative: AddressToNonce,
) -> Self {
Self { committed, staged, tentative }
}
}

#[derive(Debug, Default)]
Expand Down
26 changes: 24 additions & 2 deletions crates/starknet_mempool/src/mempool_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use papyrus_test_utils::{get_rng, GetTestInstance};
use pretty_assertions::assert_eq;
use rstest::{fixture, rstest};
use starknet_api::block::GasPrice;
use starknet_api::core::{ContractAddress, Nonce};
use starknet_api::executable_transaction::AccountTransaction;
use starknet_api::rpc_transaction::{
RpcDeployAccountTransaction,
Expand All @@ -17,6 +18,7 @@ use starknet_mempool_p2p_types::communication::MockMempoolP2pPropagatorClient;
use starknet_mempool_types::communication::AddTransactionArgsWrapper;
use starknet_mempool_types::errors::MempoolError;
use starknet_mempool_types::mempool_types::AddTransactionArgs;
use starknet_types_core::felt::Felt;

use crate::communication::MempoolCommunicationWrapper;
use crate::mempool::{Mempool, MempoolConfig, MempoolState, TransactionReference};
Expand Down Expand Up @@ -93,7 +95,6 @@ impl MempoolContentBuilder {
self
}

#[allow(dead_code)]
fn with_state(mut self, state: MempoolState) -> Self {
self.state = Some(state);
self
Expand Down Expand Up @@ -894,10 +895,31 @@ fn test_rejected_tx_deleted_from_mempool(mut mempool: Mempool) {
expected_mempool_content.assert_eq(&mempool);
}

// TODO(Arni): Add positive flow.
#[rstest]
fn tx_from_address_exists_negative_flow() {
let mempool = MempoolContentBuilder::new().build_into_mempool();

assert!(!mempool.tx_from_address_exists(contract_address!(100_u32)));
}

#[rstest]
#[case::tentative(|address| MempoolState::create_for_testing(
Default::default(), Default::default(), [(address, Nonce(Felt::ZERO))].into_iter().collect()
))]
#[case::staged(|address| MempoolState::create_for_testing(
Default::default(), [(address, Nonce(Felt::ZERO))].into_iter().collect(), Default::default()
))]
#[case::committed(|address| MempoolState::create_for_testing(
[(address, Nonce(Felt::ZERO))].into_iter().collect(), Default::default(), Default::default()
))]
fn tx_from_address_exists_positive_flow(
#[case] state_creator: impl FnOnce(ContractAddress) -> MempoolState,
) {
let deployer_address = contract_address!(100_u32);
let state = state_creator(deployer_address);

let mempool_content_builder = MempoolContentBuilder::new().with_state(state);
let mempool = mempool_content_builder.build_into_mempool();

assert!(mempool.tx_from_address_exists(deployer_address));
}

0 comments on commit 0e50d4e

Please sign in to comment.