diff --git a/crates/apollo_batcher/src/batcher_test.rs b/crates/apollo_batcher/src/batcher_test.rs index 7d4468b1d42..0a8921fff59 100644 --- a/crates/apollo_batcher/src/batcher_test.rs +++ b/crates/apollo_batcher/src/batcher_test.rs @@ -88,8 +88,8 @@ const PROPOSAL_ID: ProposalId = ProposalId(0); const BUILD_BLOCK_FAIL_ON_ERROR: BlockBuilderError = BlockBuilderError::FailOnError(FailOnErrorCause::BlockFull); -fn proposal_commitment() -> ProposalCommitment { - BlockExecutionArtifacts::create_for_testing().commitment() +async fn proposal_commitment() -> ProposalCommitment { + BlockExecutionArtifacts::create_for_testing().await.commitment() } fn propose_block_input(proposal_id: ProposalId) -> ProposeBlockInput { @@ -420,12 +420,12 @@ async fn ignore_l1_handler_provider_not_ready(#[case] proposer: bool) { mock_create_builder_for_propose_block( &mut deps.block_builder_factory, vec![], - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); } else { mock_create_builder_for_validate_block( &mut deps.block_builder_factory, - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); } deps.l1_provider_client.expect_start_block().returning(|_, _| { @@ -459,7 +459,7 @@ async fn consecutive_heights_success() { mock_create_builder_for_propose_block( &mut block_builder_factory, vec![], - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); } @@ -497,7 +497,7 @@ async fn validate_block_full_flow() { let recorder = PrometheusBuilder::new().build_recorder(); let _recorder_guard = metrics::set_default_local_recorder(&recorder); let mut batcher = create_batcher_with_active_validate_block(Ok( - BlockExecutionArtifacts::create_for_testing(), + BlockExecutionArtifacts::create_for_testing().await, )) .await; let metrics = recorder.handle().render(); @@ -518,7 +518,9 @@ async fn validate_block_full_flow() { }; assert_eq!( batcher.send_proposal_content(finish_proposal).await.unwrap(), - SendProposalContentResponse { response: ProposalStatus::Finished(proposal_commitment()) } + SendProposalContentResponse { + response: ProposalStatus::Finished(proposal_commitment().await) + } ); let metrics = recorder.handle().render(); assert_proposal_metrics(&metrics, 1, 1, 0, 0); @@ -582,7 +584,7 @@ async fn send_proposal_content_after_finish_or_abort( #[case] content: SendProposalContent, ) { let mut batcher = create_batcher_with_active_validate_block(Ok( - BlockExecutionArtifacts::create_for_testing(), + BlockExecutionArtifacts::create_for_testing().await, )) .await; @@ -636,7 +638,7 @@ async fn propose_block_full_flow() { mock_create_builder_for_propose_block( &mut block_builder_factory, expected_streamed_txs.clone(), - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); let mut l1_provider_client = MockL1ProviderClient::new(); @@ -674,8 +676,9 @@ async fn propose_block_full_flow() { commitment, GetProposalContentResponse { content: GetProposalContent::Finished { - id: proposal_commitment(), + id: proposal_commitment().await, final_n_executed_txs: BlockExecutionArtifacts::create_for_testing() + .await .final_n_executed_txs } } @@ -703,7 +706,7 @@ async fn multiple_proposals_with_l1_every_n_proposals() { mock_create_builder_for_propose_block( &mut block_builder_factory, expected_streamed_txs.clone(), - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); } @@ -796,11 +799,11 @@ async fn consecutive_proposal_generation_success() { mock_create_builder_for_propose_block( &mut block_builder_factory, vec![], - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); mock_create_builder_for_validate_block( &mut block_builder_factory, - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); } let mut l1_provider_client = MockL1ProviderClient::new(); @@ -842,7 +845,7 @@ async fn concurrent_proposals_generation_fail() { for _ in 0..2 { mock_create_builder_for_validate_block( &mut block_builder_factory, - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); } let mut batcher = start_batcher_with_active_validate(block_builder_factory).await; @@ -874,7 +877,7 @@ async fn proposal_startup_failure_allows_new_proposals() { let mut block_builder_factory = MockBlockBuilderFactoryTrait::new(); mock_create_builder_for_validate_block( &mut block_builder_factory, - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); let mut l1_provider_client = MockL1ProviderClient::new(); l1_provider_client.expect_start_block().returning(|_, _| Ok(())); @@ -1069,7 +1072,7 @@ async fn decision_reached() { let recorder = PrometheusBuilder::new().build_recorder(); let _recorder_guard = metrics::set_default_local_recorder(&recorder); let mut mock_dependencies = MockDependencies::default(); - let expected_artifacts = BlockExecutionArtifacts::create_for_testing(); + let expected_artifacts = BlockExecutionArtifacts::create_for_testing().await; mock_dependencies .mempool_client @@ -1110,7 +1113,7 @@ async fn decision_reached() { mock_create_builder_for_propose_block( &mut mock_dependencies.block_builder_factory, vec![], - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); let decision_reached_response = @@ -1169,7 +1172,7 @@ async fn test_execution_info_order_is_kept() { mock_dependencies.l1_provider_client.expect_commit_block().returning(|_, _, _| Ok(())); mock_dependencies.storage_writer.expect_commit_proposal().returning(|_, _, _| Ok(())); - let block_builder_result = BlockExecutionArtifacts::create_for_testing(); + let block_builder_result = BlockExecutionArtifacts::create_for_testing().await; // Check that the execution_infos were initiated properly for this test. let execution_infos = block_builder_result .execution_data @@ -1259,7 +1262,7 @@ async fn decision_reached_return_success_when_l1_commit_block_fails( mock_create_builder_for_propose_block( &mut mock_dependencies.block_builder_factory, vec![], - Ok(BlockExecutionArtifacts::create_for_testing()), + Ok(BlockExecutionArtifacts::create_for_testing().await), ); let result = batcher_propose_and_commit_block(mock_dependencies).await; diff --git a/crates/apollo_batcher/src/block_builder.rs b/crates/apollo_batcher/src/block_builder.rs index 48d878f2958..c01e379fed0 100644 --- a/crates/apollo_batcher/src/block_builder.rs +++ b/crates/apollo_batcher/src/block_builder.rs @@ -43,7 +43,6 @@ use starknet_api::block_hash::block_hash_calculator::{ PartialBlockHashComponents, TransactionHashingData, }; -use starknet_api::block_hash::state_diff_hash::calculate_state_diff_hash; use starknet_api::consensus_transaction::InternalConsensusTransaction; use starknet_api::core::{ContractAddress, Nonce, SequencerContractAddress}; use starknet_api::data_availability::L1DataAvailabilityMode; @@ -124,10 +123,59 @@ pub struct BlockExecutionArtifacts { // The number of transactions executed by the proposer out of the transactions that were sent. // This value includes rejected transactions. pub final_n_executed_txs: usize, - pub block_info: BlockInfo, + partial_block_hash_components: PartialBlockHashComponents, } impl BlockExecutionArtifacts { + pub async fn new( + BlockExecutionSummary { + state_diff: commitment_state_diff, + compressed_state_diff, + bouncer_weights, + casm_hash_computation_data_sierra_gas, + casm_hash_computation_data_proving_gas, + compiled_class_hashes_for_migration, + block_info, + }: BlockExecutionSummary, + execution_data: BlockTransactionExecutionData, + final_n_executed_txs: usize, + ) -> Self { + let l1_da_mode = L1DataAvailabilityMode::from_use_kzg_da(block_info.use_kzg_da); + let starknet_version = block_info.starknet_version; + let transactions_data = + prepare_txs_hashing_data(&execution_data.execution_infos_and_signatures); + let header_commitments = calculate_block_commitments( + &transactions_data, + commitment_state_diff_as_thin_state_diff(&commitment_state_diff), + l1_da_mode, + &starknet_version, + ) + .await; + let partial_block_hash_components = PartialBlockHashComponents { + header_commitments, + block_number: block_info.block_number, + l1_gas_price: block_info.gas_prices.l1_gas_price_per_token(), + l1_data_gas_price: block_info.gas_prices.l1_data_gas_price_per_token(), + l2_gas_price: block_info.gas_prices.l2_gas_price_per_token(), + sequencer: SequencerContractAddress(block_info.sequencer_address), + timestamp: block_info.block_timestamp, + starknet_version, + }; + let l2_gas_used = execution_data.l2_gas_used(); + Self { + execution_data, + commitment_state_diff, + compressed_state_diff, + bouncer_weights, + l2_gas_used, + casm_hash_computation_data_sierra_gas, + casm_hash_computation_data_proving_gas, + compiled_class_hashes_for_migration, + final_n_executed_txs, + partial_block_hash_components, + } + } + pub fn address_to_nonce(&self) -> HashMap { HashMap::from_iter( self.commitment_state_diff @@ -142,49 +190,21 @@ impl BlockExecutionArtifacts { } pub fn thin_state_diff(&self) -> ThinStateDiff { - // TODO(Ayelet): Remove the clones. - let commitment_state_diff = self.commitment_state_diff.clone(); - ThinStateDiff { - deployed_contracts: commitment_state_diff.address_to_class_hash, - storage_diffs: commitment_state_diff.storage_updates, - class_hash_to_compiled_class_hash: commitment_state_diff - .class_hash_to_compiled_class_hash, - nonces: commitment_state_diff.address_to_nonce, - // TODO(AlonH): Remove this when the structure of storage diffs changes. - deprecated_declared_classes: Vec::new(), - } + commitment_state_diff_as_thin_state_diff(&self.commitment_state_diff) } pub fn commitment(&self) -> ProposalCommitment { ProposalCommitment { - state_diff_commitment: calculate_state_diff_hash(&self.thin_state_diff()), + state_diff_commitment: self + .partial_block_hash_components + .header_commitments + .state_diff_commitment, } } - // TODO(Nimrod): Consider caching this method. /// Returns the [PartialBlockHashComponents] based on the execution artifacts. pub async fn partial_block_hash_components(&self) -> PartialBlockHashComponents { - let l1_da_mode = L1DataAvailabilityMode::from_use_kzg_da(self.block_info.use_kzg_da); - let starknet_version = self.block_info.starknet_version; - let transactions_data = - prepare_txs_hashing_data(&self.execution_data.execution_infos_and_signatures); - let header_commitments = calculate_block_commitments( - &transactions_data, - self.thin_state_diff(), - l1_da_mode, - &starknet_version, - ) - .await; - PartialBlockHashComponents { - header_commitments, - block_number: self.block_info.block_number, - l1_gas_price: self.block_info.gas_prices.l1_gas_price_per_token(), - l1_data_gas_price: self.block_info.gas_prices.l1_data_gas_price_per_token(), - l2_gas_price: self.block_info.gas_prices.l2_gas_price_per_token(), - sequencer: SequencerContractAddress(self.block_info.sequencer_address), - timestamp: self.block_info.block_timestamp, - starknet_version, - } + self.partial_block_hash_components.clone() } } @@ -204,6 +224,22 @@ fn prepare_txs_hashing_data( .collect() } +fn commitment_state_diff_as_thin_state_diff( + commitment_state_diff: &CommitmentStateDiff, +) -> ThinStateDiff { + // TODO(Ayelet): Remove the clones. + ThinStateDiff { + deployed_contracts: commitment_state_diff.address_to_class_hash.clone(), + storage_diffs: commitment_state_diff.storage_updates.clone(), + class_hash_to_compiled_class_hash: commitment_state_diff + .class_hash_to_compiled_class_hash + .clone(), + nonces: commitment_state_diff.address_to_nonce.clone(), + // TODO(AlonH): Remove this when the structure of storage diffs changes. + deprecated_declared_classes: Vec::new(), + } +} + /// The BlockBuilderTrait is responsible for building a new block from transactions provided by the /// tx_provider. The block building will stop at time deadline. /// The transactions that were added to the block will be streamed to the output_content_sender. @@ -406,16 +442,6 @@ impl BlockBuilder { .await .expect("Failed to spawn blocking executor task.")?; - let BlockExecutionSummary { - state_diff, - compressed_state_diff, - bouncer_weights, - casm_hash_computation_data_sierra_gas, - casm_hash_computation_data_proving_gas, - compiled_class_hashes_for_migration, - block_info, - } = block_summary; - let mut execution_data = std::mem::take(&mut self.execution_data); if let Some(final_n_executed_txs) = final_n_executed_txs { // Remove the transactions that were executed, but eventually not included in the block. @@ -425,19 +451,8 @@ impl BlockBuilder { self.block_txs[final_n_executed_txs..].iter().map(|tx| tx.tx_hash()).collect(); execution_data.remove_last_txs(&remove_tx_hashes); } - let l2_gas_used = execution_data.l2_gas_used(); - Ok(BlockExecutionArtifacts { - execution_data, - commitment_state_diff: state_diff, - compressed_state_diff, - bouncer_weights, - l2_gas_used, - casm_hash_computation_data_sierra_gas, - casm_hash_computation_data_proving_gas, - final_n_executed_txs: final_n_executed_txs_nonopt, - compiled_class_hashes_for_migration, - block_info, - }) + Ok(BlockExecutionArtifacts::new(block_summary, execution_data, final_n_executed_txs_nonopt) + .await) } /// Returns the number of transactions that are currently being executed by the executor. diff --git a/crates/apollo_batcher/src/block_builder_test.rs b/crates/apollo_batcher/src/block_builder_test.rs index 07cead83d06..6631e312f6f 100644 --- a/crates/apollo_batcher/src/block_builder_test.rs +++ b/crates/apollo_batcher/src/block_builder_test.rs @@ -79,7 +79,7 @@ fn output_channel() tokio::sync::mpsc::unbounded_channel() } -fn block_execution_artifacts( +async fn block_execution_artifacts( execution_infos_and_signatures: IndexMap< TransactionHash, (TransactionExecutionInfo, Option), @@ -88,24 +88,21 @@ fn block_execution_artifacts( consumed_l1_handler_tx_hashes: IndexSet, final_n_executed_txs: usize, ) -> BlockExecutionArtifacts { - let l2_gas_used = GasAmount(execution_infos_and_signatures.len().try_into().unwrap()); - BlockExecutionArtifacts { - execution_data: BlockTransactionExecutionData { - execution_infos_and_signatures, - rejected_tx_hashes, - consumed_l1_handler_tx_hashes, - }, - commitment_state_diff: Default::default(), + let block_summary = BlockExecutionSummary { + state_diff: Default::default(), compressed_state_diff: Default::default(), bouncer_weights: BouncerWeights { l1_gas: 100, ..BouncerWeights::empty() }, - // Each mock transaction uses 1 L2 gas so the total amount should be the number of txs. - l2_gas_used, casm_hash_computation_data_sierra_gas: CasmHashComputationData::default(), casm_hash_computation_data_proving_gas: CasmHashComputationData::default(), compiled_class_hashes_for_migration: vec![], - final_n_executed_txs, block_info: BlockInfo::create_for_testing(), - } + }; + let execution_data = BlockTransactionExecutionData { + execution_infos_and_signatures, + rejected_tx_hashes, + consumed_l1_handler_tx_hashes, + }; + BlockExecutionArtifacts::new(block_summary, execution_data, final_n_executed_txs).await } // Filling the execution_info with some non-default values to make sure the block_builder uses them. @@ -123,11 +120,11 @@ fn execution_info() -> TransactionExecutionInfo { } } -fn one_chunk_test_expectations() -> TestExpectations { +async fn one_chunk_test_expectations() -> TestExpectations { let input_txs = test_txs(0..3); let block_size = input_txs.len(); let (mock_transaction_executor, expected_block_artifacts) = - one_chunk_mock_executor(&input_txs, block_size, false); + one_chunk_mock_executor(&input_txs, block_size, false).await; let mock_tx_provider = mock_tx_provider_limitless_calls(vec![input_txs.clone()]); @@ -199,7 +196,7 @@ impl ExpectationHelper { } } -fn one_chunk_mock_executor( +async fn one_chunk_mock_executor( input_txs: &[InternalConsensusTransaction], block_size: usize, is_validator: bool, @@ -218,7 +215,7 @@ fn one_chunk_mock_executor( helper.deadline_expectations(); let expected_block_artifacts = - set_close_block_expectations(&mut helper.mock_transaction_executor, block_size); + set_close_block_expectations(&mut helper.mock_transaction_executor, block_size).await; (helper.mock_transaction_executor, expected_block_artifacts) } @@ -254,13 +251,13 @@ fn two_chunks_mock_executor( (first_chunk, second_chunk, helper.mock_transaction_executor) } -fn two_chunks_test_expectations() -> TestExpectations { +async fn two_chunks_test_expectations() -> TestExpectations { let (first_chunk, second_chunk, mut mock_transaction_executor) = two_chunks_mock_executor(false); let block_size = first_chunk.len() + second_chunk.len(); let expected_block_artifacts = - set_close_block_expectations(&mut mock_transaction_executor, block_size); + set_close_block_expectations(&mut mock_transaction_executor, block_size).await; let mock_tx_provider = mock_tx_provider_limitless_calls(vec![first_chunk.clone(), second_chunk.clone()]); @@ -276,13 +273,13 @@ fn two_chunks_test_expectations() -> TestExpectations { } } -fn empty_block_test_expectations() -> TestExpectations { +async fn empty_block_test_expectations() -> TestExpectations { let mut helper = ExpectationHelper::new(); helper.deadline_expectations(); helper.mock_transaction_executor.expect_add_txs_to_block().times(0); let expected_block_artifacts = - set_close_block_expectations(&mut helper.mock_transaction_executor, 0); + set_close_block_expectations(&mut helper.mock_transaction_executor, 0).await; let mock_tx_provider = mock_tx_provider_limitless_calls(vec![]); @@ -297,7 +294,7 @@ fn empty_block_test_expectations() -> TestExpectations { } } -fn block_full_test_expectations(before_is_done: bool) -> TestExpectations { +async fn block_full_test_expectations(before_is_done: bool) -> TestExpectations { let input_txs = test_txs(0..3); let mut helper = ExpectationHelper::new(); @@ -310,7 +307,8 @@ fn block_full_test_expectations(before_is_done: bool) -> TestExpectations { helper.expect_successful_get_new_results(if before_is_done { 0 } else { 1 }); let mut mock_transaction_executor = helper.mock_transaction_executor; - let expected_block_artifacts = set_close_block_expectations(&mut mock_transaction_executor, 1); + let expected_block_artifacts = + set_close_block_expectations(&mut mock_transaction_executor, 1).await; let mock_tx_provider = mock_tx_provider_limited_calls(vec![input_txs.clone()]); @@ -353,7 +351,7 @@ fn mock_partial_transaction_execution( helper.mock_transaction_executor } -fn test_expectations_partial_transaction_execution() -> TestExpectations { +async fn test_expectations_partial_transaction_execution() -> TestExpectations { let n_completed_txs = 1; let input_txs = test_txs(0..N_CONCURRENT_TXS + n_completed_txs); let first_chunk = input_txs[0..N_CONCURRENT_TXS].to_vec(); @@ -364,7 +362,7 @@ fn test_expectations_partial_transaction_execution() -> TestExpectations { mock_partial_transaction_execution(&first_chunk, &second_chunk, n_completed_txs, false); let expected_block_artifacts = - set_close_block_expectations(&mut mock_transaction_executor, n_completed_txs); + set_close_block_expectations(&mut mock_transaction_executor, n_completed_txs).await; let mock_tx_provider = mock_tx_provider_limited_calls(vec![first_chunk, second_chunk]); @@ -379,7 +377,7 @@ fn test_expectations_partial_transaction_execution() -> TestExpectations { } } -fn transaction_failed_test_expectations() -> TestExpectations { +async fn transaction_failed_test_expectations() -> TestExpectations { let n_txs = 6; let input_invoke_txs = test_txs(0..3); let input_l1_handler_txs = test_l1_handler_txs(3..n_txs); @@ -425,7 +423,8 @@ fn transaction_failed_test_expectations() -> TestExpectations { failed_tx_hashes, consumed_l1_handler_tx_hashes, n_txs, - ); + ) + .await; let expected_block_artifacts_copy = expected_block_artifacts.clone(); helper.mock_transaction_executor.expect_close_block().times(1).return_once(move |_| { Ok(BlockExecutionSummary { @@ -438,7 +437,7 @@ fn transaction_failed_test_expectations() -> TestExpectations { .casm_hash_computation_data_proving_gas, compiled_class_hashes_for_migration: expected_block_artifacts_copy .compiled_class_hashes_for_migration, - block_info: expected_block_artifacts_copy.block_info, + block_info: BlockInfo::create_for_testing(), }) }); @@ -456,7 +455,7 @@ fn transaction_failed_test_expectations() -> TestExpectations { } } -fn idle_execution_timeout_test_expectations() -> TestExpectations { +async fn idle_execution_timeout_test_expectations() -> TestExpectations { let input_txs = test_txs(0..2); let input_txs_clone = input_txs.clone(); @@ -472,7 +471,7 @@ fn idle_execution_timeout_test_expectations() -> TestExpectations { helper.mock_transaction_executor.expect_is_done().returning(|| false); let expected_block_artifacts = - set_close_block_expectations(&mut helper.mock_transaction_executor, input_txs.len()); + set_close_block_expectations(&mut helper.mock_transaction_executor, input_txs.len()).await; // Mock provider that returns initial transactions, then empty chunks. let mut mock_tx_provider = MockTransactionProvider::new(); @@ -502,7 +501,7 @@ fn idle_execution_timeout_test_expectations() -> TestExpectations { // Fill the executor outputs with some non-default values to make sure the block_builder uses // them. -fn block_builder_expected_output( +async fn block_builder_expected_output( execution_info_len: usize, final_n_executed_txs: usize, ) -> BlockExecutionArtifacts { @@ -516,13 +515,14 @@ fn block_builder_expected_output( Default::default(), final_n_executed_txs, ) + .await } -fn set_close_block_expectations( +async fn set_close_block_expectations( mock_transaction_executor: &mut MockTransactionExecutorTrait, block_size: usize, ) -> BlockExecutionArtifacts { - let output_block_artifacts = block_builder_expected_output(block_size, block_size); + let output_block_artifacts = block_builder_expected_output(block_size, block_size).await; let output_block_artifacts_copy = output_block_artifacts.clone(); mock_transaction_executor.expect_close_block().times(1).return_once(move |_| { Ok(BlockExecutionSummary { @@ -535,7 +535,7 @@ fn set_close_block_expectations( .casm_hash_computation_data_proving_gas, compiled_class_hashes_for_migration: output_block_artifacts .compiled_class_hashes_for_migration, - block_info: output_block_artifacts.block_info, + block_info: BlockInfo::create_for_testing(), }) }); output_block_artifacts_copy @@ -719,14 +719,14 @@ async fn run_build_block( } #[rstest] -#[case::one_chunk_block(one_chunk_test_expectations())] -#[case::two_chunks_block(two_chunks_test_expectations())] -#[case::empty_block(empty_block_test_expectations())] -#[case::block_full_before_is_done(block_full_test_expectations(true))] -#[case::block_full_after_is_done(block_full_test_expectations(false))] -#[case::deadline_reached_after_first_chunk(test_expectations_partial_transaction_execution())] -#[case::transaction_failed(transaction_failed_test_expectations())] -#[case::idle_execution_timeout(idle_execution_timeout_test_expectations())] +#[case::one_chunk_block(one_chunk_test_expectations().await)] +#[case::two_chunks_block(two_chunks_test_expectations().await)] +#[case::empty_block(empty_block_test_expectations().await)] +#[case::block_full_before_is_done(block_full_test_expectations(true).await)] +#[case::block_full_after_is_done(block_full_test_expectations(false).await)] +#[case::deadline_reached_after_first_chunk(test_expectations_partial_transaction_execution().await)] +#[case::transaction_failed(transaction_failed_test_expectations().await)] +#[case::idle_execution_timeout(idle_execution_timeout_test_expectations().await)] #[tokio::test] async fn test_build_block(#[case] test_expectations: TestExpectations) { let recorder = PrometheusBuilder::new().build_recorder(); @@ -776,7 +776,7 @@ async fn test_build_block(#[case] test_expectations: TestExpectations) { async fn test_validate_block() { let input_txs = test_txs(0..3); let (mock_transaction_executor, expected_block_artifacts) = - one_chunk_mock_executor(&input_txs, input_txs.len(), true); + one_chunk_mock_executor(&input_txs, input_txs.len(), true).await; let mock_tx_provider = mock_tx_provider_stream_done(input_txs); let (_abort_sender, abort_receiver) = tokio::sync::oneshot::channel(); @@ -804,7 +804,7 @@ async fn test_validate_block_excluded_txs() { let final_n_executed_txs = n_executed_txs - 1; let expected_block_artifacts = - set_close_block_expectations(&mut mock_transaction_executor, final_n_executed_txs); + set_close_block_expectations(&mut mock_transaction_executor, final_n_executed_txs).await; let mut mock_tx_provider = mock_tx_provider_limited_calls_ex( vec![first_chunk, second_chunk], @@ -985,7 +985,8 @@ async fn test_build_block_abort_immediately() { async fn test_l2_gas_used() { let n_txs = 3; let input_txs = test_txs(0..n_txs); - let (mock_transaction_executor, _) = one_chunk_mock_executor(&input_txs, input_txs.len(), true); + let (mock_transaction_executor, _) = + one_chunk_mock_executor(&input_txs, input_txs.len(), true).await; let mock_tx_provider = mock_tx_provider_stream_done(input_txs); let (_abort_sender, abort_receiver) = tokio::sync::oneshot::channel(); @@ -1014,7 +1015,7 @@ async fn test_execution_info_order() { two_chunks_mock_executor(false); let input_txs = first_chunk.iter().chain(second_chunk.iter()).collect::>(); - set_close_block_expectations(&mut mock_transaction_executor, input_txs.len()); + set_close_block_expectations(&mut mock_transaction_executor, input_txs.len()).await; let mock_tx_provider = mock_tx_provider_limitless_calls(vec![first_chunk.clone(), second_chunk.clone()]); @@ -1117,7 +1118,8 @@ async fn partial_chunk_execution_proposer() { Default::default(), Default::default(), executed_txs.len(), - ); + ) + .await; let expected_block_artifacts_copy = expected_block_artifacts.clone(); helper.mock_transaction_executor.expect_close_block().times(1).return_once(move |_| { @@ -1131,7 +1133,7 @@ async fn partial_chunk_execution_proposer() { .casm_hash_computation_data_proving_gas, compiled_class_hashes_for_migration: expected_block_artifacts .compiled_class_hashes_for_migration, - block_info: expected_block_artifacts.block_info, + block_info: BlockInfo::create_for_testing(), }) }); @@ -1170,7 +1172,7 @@ async fn partial_chunk_execution_validator(#[case] successful: bool) { let expected_block_artifacts = if successful { helper.mock_transaction_executor.expect_abort_block().times(0); - Some(set_close_block_expectations(&mut helper.mock_transaction_executor, 2)) + Some(set_close_block_expectations(&mut helper.mock_transaction_executor, 2).await) } else { // Validator continues the loop even after the scheduler is done. helper.mock_transaction_executor.expect_get_new_results().times(1..).returning(Vec::new); diff --git a/crates/apollo_batcher/src/test_utils.rs b/crates/apollo_batcher/src/test_utils.rs index 38222bf4ea8..6c7907ba187 100644 --- a/crates/apollo_batcher/src/test_utils.rs +++ b/crates/apollo_batcher/src/test_utils.rs @@ -1,6 +1,7 @@ use std::ops::Range; use async_trait::async_trait; +use blockifier::blockifier::transaction_executor::BlockExecutionSummary; use blockifier::bouncer::{BouncerWeights, CasmHashComputationData}; use blockifier::fee::receipt::TransactionReceipt; use blockifier::state::cached_state::CommitmentStateDiff; @@ -8,7 +9,6 @@ use blockifier::transaction::objects::TransactionExecutionInfo; use indexmap::IndexMap; use starknet_api::block::BlockInfo; use starknet_api::consensus_transaction::InternalConsensusTransaction; -use starknet_api::execution_resources::GasAmount; use starknet_api::test_utils::invoke::{internal_invoke_tx, InvokeTxArgs}; use starknet_api::test_utils::l1_handler::{executable_l1_handler_tx, L1HandlerTxArgs}; use starknet_api::transaction::fields::{Fee, TransactionSignature}; @@ -136,15 +136,15 @@ pub fn verify_indexed_execution_infos( } impl BlockExecutionArtifacts { - pub fn create_for_testing() -> Self { + pub async fn create_for_testing() -> Self { // Use a non-empty commitment_state_diff to get a valuable test verification of the result. - Self { - execution_data: BlockTransactionExecutionData { - execution_infos_and_signatures: indexed_execution_infos_and_signatures(), - rejected_tx_hashes: test_txs(10..15).iter().map(|tx| tx.tx_hash()).collect(), - consumed_l1_handler_tx_hashes: Default::default(), - }, - commitment_state_diff: CommitmentStateDiff { + let execution_data = BlockTransactionExecutionData { + execution_infos_and_signatures: indexed_execution_infos_and_signatures(), + rejected_tx_hashes: test_txs(10..15).iter().map(|tx| tx.tx_hash()).collect(), + consumed_l1_handler_tx_hashes: Default::default(), + }; + let block_execution_summary = BlockExecutionSummary { + state_diff: CommitmentStateDiff { address_to_class_hash: IndexMap::from_iter([( contract_address!("0x7"), class_hash!("0x11111111"), @@ -155,12 +155,11 @@ impl BlockExecutionArtifacts { }, compressed_state_diff: Default::default(), bouncer_weights: BouncerWeights::empty(), - l2_gas_used: GasAmount::default(), casm_hash_computation_data_sierra_gas: CasmHashComputationData::empty(), casm_hash_computation_data_proving_gas: CasmHashComputationData::empty(), compiled_class_hashes_for_migration: vec![], - final_n_executed_txs: DUMMY_FINAL_N_EXECUTED_TXS, block_info: BlockInfo::create_for_testing(), - } + }; + Self::new(block_execution_summary, execution_data, DUMMY_FINAL_N_EXECUTED_TXS).await } }