Skip to content

Commit

Permalink
feat(starknet_integration_tests): pass test scenarios from the test body
Browse files Browse the repository at this point in the history
  • Loading branch information
yair-starkware committed Dec 19, 2024
1 parent 24503a1 commit 85383c4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
9 changes: 7 additions & 2 deletions crates/starknet_integration_tests/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn create_integration_test_tx_generator() -> MultiAccountTransactionGenerato
tx_generator
}

fn create_txs_for_integration_test(
pub fn create_txs_for_integration_test(
tx_generator: &mut MultiAccountTransactionGenerator,
) -> Vec<RpcTransaction> {
const ACCOUNT_ID_0: AccountId = 0;
Expand Down Expand Up @@ -198,14 +198,19 @@ where
/// list of transaction hashes, in the order they are expected to be in the mempool.
pub async fn run_integration_test_scenario<'a, Fut>(
tx_generator: &mut MultiAccountTransactionGenerator,
create_rpc_txs_fn: impl Fn(&mut MultiAccountTransactionGenerator) -> Vec<RpcTransaction>,
send_rpc_tx_fn: &'a mut dyn FnMut(RpcTransaction) -> Fut,
test_tx_hashes_fn: impl Fn(&[TransactionHash]) -> Vec<TransactionHash>,
) -> Vec<TransactionHash>
where
Fut: Future<Output = TransactionHash> + 'a,
{
let rpc_txs = create_txs_for_integration_test(tx_generator);
let rpc_txs = create_rpc_txs_fn(tx_generator);
let tx_hashes = send_rpc_txs(rpc_txs, send_rpc_tx_fn).await;
test_tx_hashes_fn(&tx_hashes)
}

pub fn test_tx_hashes_for_integration_test(tx_hashes: &[TransactionHash]) -> Vec<TransactionHash> {
// Return the transaction hashes in the order they should be given by the mempool:
// Transactions from the same account are ordered by nonce; otherwise, higher tips are given
// priority.
Expand Down
44 changes: 38 additions & 6 deletions crates/starknet_integration_tests/tests/end_to_end_flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use starknet_api::transaction::TransactionHash;
use starknet_integration_tests::flow_test_setup::{FlowTestSetup, SequencerSetup};
use starknet_integration_tests::utils::{
create_integration_test_tx_generator,
create_txs_for_integration_test,
run_integration_test_scenario,
test_tx_hashes_for_integration_test,
};
use starknet_sequencer_infra::trace_util::configure_tracing;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -49,6 +51,7 @@ async fn end_to_end_flow(mut tx_generator: MultiAccountTransactionGenerator) {
);

let next_height = INITIAL_HEIGHT.unchecked_next();
let n_heights = next_height.iter_up_to(LAST_HEIGHT.unchecked_next()).count();
let heights_to_build = next_height.iter_up_to(LAST_HEIGHT.unchecked_next());
let expected_content_ids = [
Felt::from_hex_unchecked(
Expand All @@ -66,15 +69,44 @@ async fn end_to_end_flow(mut tx_generator: MultiAccountTransactionGenerator) {
// We start at height 1, so we need to skip the proposer of the initial height.
expected_proposer_iter.next().unwrap();

let create_rpc_txs_scenarios =
[create_txs_for_integration_test, create_txs_for_integration_test];

let test_tx_hashes_scenarios =
[test_tx_hashes_for_integration_test, test_tx_hashes_for_integration_test];

assert_eq!(
n_heights,
expected_content_ids.len(),
"Expected the same number of heights and content ids"
);
assert_eq!(
n_heights,
create_rpc_txs_scenarios.len(),
"Expected the same number of heights and scenarios"
);
assert_eq!(
n_heights,
test_tx_hashes_scenarios.len(),
"Expected the same number of heights and scenarios"
);

// Build multiple heights to ensure heights are committed.
for (height, expected_content_id) in itertools::zip_eq(heights_to_build, expected_content_ids) {
for (height, expected_content_id, create_rpc_txs_fn, test_tx_hashes_fn) in itertools::izip!(
heights_to_build,
expected_content_ids,
create_rpc_txs_scenarios.iter(),
test_tx_hashes_scenarios.iter(),
) {
debug!("Starting height {}.", height);
// Create and send transactions.
let expected_batched_tx_hashes =
run_integration_test_scenario(&mut tx_generator, &mut |tx| {
sequencer_to_add_txs.assert_add_tx_success(tx)
})
.await;
let expected_batched_tx_hashes = run_integration_test_scenario(
&mut tx_generator,
create_rpc_txs_fn,
&mut |tx| sequencer_to_add_txs.assert_add_tx_success(tx),
test_tx_hashes_fn,
)
.await;
let expected_validator_id = expected_proposer_iter
.next()
.unwrap()
Expand Down
28 changes: 20 additions & 8 deletions crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ use starknet_integration_tests::utils::{
create_gateway_config,
create_http_server_config,
create_integration_test_tx_generator,
create_txs_for_integration_test,
run_integration_test_scenario,
test_rpc_state_reader_config,
test_tx_hashes_for_integration_test,
};
use starknet_mempool_p2p::config::MempoolP2pConfig;
use starknet_mempool_p2p::MEMPOOL_TOPIC;
Expand Down Expand Up @@ -128,10 +130,15 @@ async fn test_mempool_sends_tx_to_other_peer(mut tx_generator: MultiAccountTrans
let mut expected_txs = HashSet::new();

// Create and send transactions.
let _tx_hashes = run_integration_test_scenario(&mut tx_generator, &mut |tx: RpcTransaction| {
expected_txs.insert(tx.clone()); // push the sent tx to the expected_txs list
add_tx_http_client.assert_add_tx_success(tx)
})
let _tx_hashes = run_integration_test_scenario(
&mut tx_generator,
create_txs_for_integration_test,
&mut |tx: RpcTransaction| {
expected_txs.insert(tx.clone()); // push the sent tx to the expected_txs list
add_tx_http_client.assert_add_tx_success(tx)
},
test_tx_hashes_for_integration_test,
)
.await;

while !expected_txs.is_empty() {
Expand Down Expand Up @@ -161,10 +168,15 @@ async fn test_mempool_receives_tx_from_other_peer(

let mut expected_txs = HashSet::new();

let _tx_hashes = run_integration_test_scenario(&mut tx_generator, &mut |tx: RpcTransaction| {
expected_txs.insert(tx.clone());
ready(TransactionHash::default()) // using the default value because we don't use the hash anyways.
})
let _tx_hashes = run_integration_test_scenario(
&mut tx_generator,
create_txs_for_integration_test,
&mut |tx: RpcTransaction| {
expected_txs.insert(tx.clone());
ready(TransactionHash::default()) // using the default value because we don't use the hash anyways.
},
test_tx_hashes_for_integration_test,
)
.await;
for tx in &expected_txs {
broadcast_channels
Expand Down

0 comments on commit 85383c4

Please sign in to comment.