From 85383c4fa4652e2a76e9ab38680dab8fe9cf8405 Mon Sep 17 00:00:00 2001 From: Yair Bakalchuk Date: Tue, 17 Dec 2024 13:16:32 +0200 Subject: [PATCH] feat(starknet_integration_tests): pass test scenarios from the test body --- .../starknet_integration_tests/src/utils.rs | 9 +++- .../tests/end_to_end_flow_test.rs | 44 ++++++++++++++++--- .../tests/mempool_p2p_flow_test.rs | 28 ++++++++---- 3 files changed, 65 insertions(+), 16 deletions(-) diff --git a/crates/starknet_integration_tests/src/utils.rs b/crates/starknet_integration_tests/src/utils.rs index a95d33b1a25..00f41d9fecb 100644 --- a/crates/starknet_integration_tests/src/utils.rs +++ b/crates/starknet_integration_tests/src/utils.rs @@ -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 { const ACCOUNT_ID_0: AccountId = 0; @@ -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, send_rpc_tx_fn: &'a mut dyn FnMut(RpcTransaction) -> Fut, + test_tx_hashes_fn: impl Fn(&[TransactionHash]) -> Vec, ) -> Vec where Fut: Future + '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 { // 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. diff --git a/crates/starknet_integration_tests/tests/end_to_end_flow_test.rs b/crates/starknet_integration_tests/tests/end_to_end_flow_test.rs index 14e66aa71d9..aae67d0a26e 100644 --- a/crates/starknet_integration_tests/tests/end_to_end_flow_test.rs +++ b/crates/starknet_integration_tests/tests/end_to_end_flow_test.rs @@ -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; @@ -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( @@ -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() diff --git a/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs b/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs index ff088822d9a..0aea3e91ed0 100644 --- a/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs +++ b/crates/starknet_integration_tests/tests/mempool_p2p_flow_test.rs @@ -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; @@ -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() { @@ -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