From 9e9a71d9a7cfb79ce7947ec5e923dd2e552fc7cb Mon Sep 17 00:00:00 2001 From: Aviv Greenburg Date: Sun, 1 Dec 2024 15:05:42 +0200 Subject: [PATCH] chore(blockifier): ues account_transaction::ExecutionFlags instead of transaction::ExecutionFlags --- .../src/blockifier/transaction_executor.rs | 6 +- .../blockifier/transaction_executor_test.rs | 26 ++-- .../blockifier/src/concurrency/test_utils.rs | 2 +- .../src/concurrency/worker_logic.rs | 10 +- .../src/execution/stack_trace_test.rs | 6 + .../src/transaction/account_transaction.rs | 12 +- .../transaction/account_transactions_test.rs | 19 ++- .../src/transaction/execution_flavors_test.rs | 134 ++++++++++++------ .../blockifier/src/transaction/test_utils.rs | 24 ++-- .../src/transaction/transaction_execution.rs | 8 +- .../src/transaction/transactions.rs | 8 +- .../src/transaction/transactions_test.rs | 13 +- .../state_reader/reexecution_state_reader.rs | 8 +- .../native_blockifier/src/py_transaction.rs | 23 ++- crates/papyrus_execution/src/lib.rs | 18 ++- crates/starknet_batcher/src/block_builder.rs | 9 +- .../src/stateful_transaction_validator.rs | 10 +- 17 files changed, 226 insertions(+), 110 deletions(-) diff --git a/crates/blockifier/src/blockifier/transaction_executor.rs b/crates/blockifier/src/blockifier/transaction_executor.rs index 4ad8bfb285..f693daf8ff 100644 --- a/crates/blockifier/src/blockifier/transaction_executor.rs +++ b/crates/blockifier/src/blockifier/transaction_executor.rs @@ -17,7 +17,7 @@ use crate::state::cached_state::{CachedState, CommitmentStateDiff, Transactional use crate::state::errors::StateError; use crate::state::state_api::{StateReader, StateResult}; use crate::transaction::errors::TransactionExecutionError; -use crate::transaction::objects::{TransactionExecutionInfo, TransactionInfoCreator}; +use crate::transaction::objects::TransactionExecutionInfo; use crate::transaction::transaction_execution::Transaction; use crate::transaction::transactions::{ExecutableTransaction, ExecutionFlags}; @@ -100,11 +100,9 @@ impl TransactionExecutor { let mut transactional_state = TransactionalState::create_transactional( self.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR), ); - let tx_charge_fee = tx.create_tx_info().enforce_fee(); // Executing a single transaction cannot be done in a concurrent mode. - let execution_flags = - ExecutionFlags { charge_fee: tx_charge_fee, validate: true, concurrency_mode: false }; + let execution_flags = ExecutionFlags { concurrency_mode: false }; let tx_execution_result = tx.execute_raw(&mut transactional_state, &self.block_context, execution_flags); match tx_execution_result { diff --git a/crates/blockifier/src/blockifier/transaction_executor_test.rs b/crates/blockifier/src/blockifier/transaction_executor_test.rs index 50e198fa91..c01dc739b5 100644 --- a/crates/blockifier/src/blockifier/transaction_executor_test.rs +++ b/crates/blockifier/src/blockifier/transaction_executor_test.rs @@ -21,6 +21,7 @@ use crate::test_utils::contracts::FeatureContract; use crate::test_utils::declare::declare_tx; use crate::test_utils::deploy_account::deploy_account_tx; use crate::test_utils::initial_test_state::test_state; +use crate::test_utils::invoke::invoke_tx; use crate::test_utils::l1_handler::l1handler_tx; use crate::test_utils::{ create_calldata, @@ -32,7 +33,6 @@ use crate::test_utils::{ use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use crate::transaction::errors::TransactionExecutionError; use crate::transaction::test_utils::{ - account_invoke_tx, block_context, calculate_class_info_for_testing, create_test_init_data, @@ -41,6 +41,7 @@ use crate::transaction::test_utils::{ TestInitData, }; use crate::transaction::transaction_execution::Transaction; +use crate::transaction::transactions::enforce_fee; fn tx_executor_test_body( state: CachedState, block_context: BlockContext, @@ -131,7 +132,9 @@ fn test_declare( }, calculate_class_info_for_testing(declared_contract.get_class()), ); - let execution_flags = ExecutionFlags::default(); + let only_query = false; + let charge_fee = enforce_fee(&declare_tx, only_query); + let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() }; let tx = AccountTransaction { tx: declare_tx, execution_flags }.into(); tx_executor_test_body(state, block_context, tx, expected_bouncer_weights); } @@ -145,7 +148,7 @@ fn test_deploy_account( let account_contract = FeatureContract::AccountWithoutValidations(cairo_version); let state = test_state(&block_context.chain_info, BALANCE, &[(account_contract, 0)]); - let deploy_account_tx = deploy_account_tx( + let deploy_tx = deploy_account_tx( deploy_account_tx_args! { class_hash: account_contract.get_class_hash(), resource_bounds: l1_resource_bounds(0_u8.into(), DEFAULT_STRK_L1_GAS_PRICE.into()), @@ -153,13 +156,17 @@ fn test_deploy_account( }, &mut NonceManager::default(), ); - let tx = AccountTransaction { tx: deploy_account_tx, execution_flags: ExecutionFlags::default() }.into(); + let only_query = false; + let charge_fee = enforce_fee(&deploy_tx, only_query); + let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() }; + let tx = AccountTransaction { tx: deploy_tx, execution_flags }.into(); let expected_bouncer_weights = BouncerWeights { state_diff_size: 3, message_segment_length: 0, n_events: 0, ..BouncerWeights::empty() - }; + } + .into(); tx_executor_test_body(state, block_context, tx, expected_bouncer_weights); } @@ -219,12 +226,15 @@ fn test_invoke( let calldata = create_calldata(test_contract.get_instance_address(0), entry_point_name, &entry_point_args); - let tx = account_invoke_tx(invoke_tx_args! { + let invoke_tx = invoke_tx(invoke_tx_args! { sender_address: account_contract.get_instance_address(0), calldata, version, - }) - .into(); + }); + let only_query = false; + let charge_fee = enforce_fee(&invoke_tx, only_query); + let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() }; + let tx = AccountTransaction { tx: invoke_tx, execution_flags }.into(); tx_executor_test_body(state, block_context, tx, expected_bouncer_weights); } diff --git a/crates/blockifier/src/concurrency/test_utils.rs b/crates/blockifier/src/concurrency/test_utils.rs index 96e8d20261..a14e96de6f 100644 --- a/crates/blockifier/src/concurrency/test_utils.rs +++ b/crates/blockifier/src/concurrency/test_utils.rs @@ -76,7 +76,7 @@ pub fn create_fee_transfer_call_info( ) -> CallInfo { let block_context = BlockContext::create_for_account_testing(); let mut transactional_state = TransactionalState::create_transactional(state); - let execution_flags = ExecutionFlags { charge_fee: true, validate: true, concurrency_mode }; + let execution_flags = ExecutionFlags { concurrency_mode }; let execution_info = account_tx.execute_raw(&mut transactional_state, &block_context, execution_flags).unwrap(); diff --git a/crates/blockifier/src/concurrency/worker_logic.rs b/crates/blockifier/src/concurrency/worker_logic.rs index af2ec35c36..dee56aa6aa 100644 --- a/crates/blockifier/src/concurrency/worker_logic.rs +++ b/crates/blockifier/src/concurrency/worker_logic.rs @@ -17,11 +17,7 @@ use crate::concurrency::TxIndex; use crate::context::BlockContext; use crate::state::cached_state::{ContractClassMapping, StateMaps, TransactionalState}; use crate::state::state_api::{StateReader, UpdatableState}; -use crate::transaction::objects::{ - TransactionExecutionInfo, - TransactionExecutionResult, - TransactionInfoCreator, -}; +use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult}; use crate::transaction::transaction_execution::Transaction; use crate::transaction::transactions::{ExecutableTransaction, ExecutionFlags}; @@ -127,11 +123,9 @@ impl<'a, S: StateReader> WorkerExecutor<'a, S> { fn execute_tx(&self, tx_index: TxIndex) { let mut tx_versioned_state = self.state.pin_version(tx_index); let tx = &self.chunk[tx_index]; - let tx_charge_fee = tx.create_tx_info().enforce_fee(); let mut transactional_state = TransactionalState::create_transactional(&mut tx_versioned_state); - let execution_flags = - ExecutionFlags { charge_fee: tx_charge_fee, validate: true, concurrency_mode: true }; + let execution_flags = ExecutionFlags { concurrency_mode: true }; let execution_result = tx.execute_raw(&mut transactional_state, self.block_context, execution_flags); diff --git a/crates/blockifier/src/execution/stack_trace_test.rs b/crates/blockifier/src/execution/stack_trace_test.rs index 80e77478a6..16d2cdb5d9 100644 --- a/crates/blockifier/src/execution/stack_trace_test.rs +++ b/crates/blockifier/src/execution/stack_trace_test.rs @@ -45,6 +45,7 @@ use crate::execution::syscalls::hint_processor::ENTRYPOINT_FAILED_ERROR; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::{fund_account, test_state}; use crate::test_utils::{create_calldata, CairoVersion, BALANCE}; +use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use crate::transaction::test_utils::{ account_invoke_tx, block_context, @@ -608,6 +609,11 @@ fn test_validate_trace( } } + // TODO(AvivG): Change this fixup to not create account_tx twice w wrong charge_fee. + let execution_flags = + ExecutionFlags { charge_fee: account_tx.enforce_fee(), ..ExecutionFlags::default() }; + let account_tx = AccountTransaction { tx: account_tx.tx, execution_flags }; + let contract_address = *sender_address.0.key(); let expected_error = match cairo_version { diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index a0151dbdc3..fabeac7e7a 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -721,7 +721,7 @@ impl ExecutableTransaction for AccountTransaction { &self, state: &mut TransactionalState<'_, U>, block_context: &BlockContext, - execution_flags: TransactionExecutionFlags, + execution_flags_: TransactionExecutionFlags, ) -> TransactionExecutionResult { let tx_context = Arc::new(block_context.to_tx_context(self)); self.verify_tx_version(tx_context.tx_info.version())?; @@ -731,7 +731,7 @@ impl ExecutableTransaction for AccountTransaction { self.perform_pre_validation_stage( state, &tx_context, - execution_flags.charge_fee, + self.execution_flags.charge_fee, strict_nonce_check, )?; @@ -752,15 +752,15 @@ impl ExecutableTransaction for AccountTransaction { state, &mut remaining_gas, tx_context.clone(), - execution_flags.validate, - execution_flags.charge_fee, + self.execution_flags.validate, + self.execution_flags.charge_fee, )?; let fee_transfer_call_info = Self::handle_fee( state, tx_context, final_fee, - execution_flags.charge_fee, - execution_flags.concurrency_mode, + self.execution_flags.charge_fee, + execution_flags_.concurrency_mode, )?; let tx_execution_info = TransactionExecutionInfo { diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 292c3b20ca..3a946a3d33 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -66,6 +66,7 @@ use crate::test_utils::contracts::FeatureContract; use crate::test_utils::declare::declare_tx; use crate::test_utils::deploy_account::deploy_account_tx; use crate::test_utils::initial_test_state::{fund_account, test_state}; +use crate::test_utils::invoke::invoke_tx; use crate::test_utils::syscall::build_recurse_calldata; use crate::test_utils::{ create_calldata, @@ -1371,8 +1372,7 @@ fn test_count_actual_storage_changes( nonce: nonce_manager.next(account_address), }; let account_tx = account_invoke_tx(invoke_args.clone()); - let execution_flags = - ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: false }; + let execution_flags = ExecutionFlags { concurrency_mode: false }; let execution_info = account_tx.execute_raw(&mut state, &block_context, execution_flags).unwrap(); @@ -1542,8 +1542,7 @@ fn test_concurrency_execute_fee_transfer( // Case 1: The transaction did not read form/ write to the sequenser balance before executing // fee transfer. let mut transactional_state = TransactionalState::create_transactional(state); - let execution_flags = - ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: true }; + let execution_flags = ExecutionFlags { concurrency_mode: true }; let result = account_tx.execute_raw(&mut transactional_state, &block_context, execution_flags).unwrap(); assert!(!result.is_reverted()); @@ -1638,8 +1637,7 @@ fn test_concurrent_fee_transfer_when_sender_is_sequencer( let fee_token_address = block_context.chain_info.fee_token_address(fee_type); let mut transactional_state = TransactionalState::create_transactional(state); - let execution_flags = - ExecutionFlags { charge_fee: true, validate: true, concurrency_mode: true }; + let execution_flags = ExecutionFlags { concurrency_mode: true }; let result = account_tx.execute_raw(&mut transactional_state, &block_context, execution_flags).unwrap(); assert!(!result.is_reverted()); @@ -1766,12 +1764,13 @@ fn test_revert_in_execute( // Skip validate phase, as we want to test the revert in the execute phase. let validate = false; - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { resource_bounds: default_all_resource_bounds, ..tx_args - }) - .execute(state, &block_context, true, validate) - .unwrap(); + }); + let execution_flags = AccountExecutionFlags { validate, ..AccountExecutionFlags::default() }; + let account_tx = AccountTransaction { tx, execution_flags }; + let tx_execution_info = account_tx.execute(state, &block_context, true, validate).unwrap(); assert!(tx_execution_info.is_reverted()); assert!( diff --git a/crates/blockifier/src/transaction/execution_flavors_test.rs b/crates/blockifier/src/transaction/execution_flavors_test.rs index fbae899ebf..2c2c6409ab 100644 --- a/crates/blockifier/src/transaction/execution_flavors_test.rs +++ b/crates/blockifier/src/transaction/execution_flavors_test.rs @@ -26,6 +26,7 @@ use crate::state::state_api::StateReader; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::dict_state_reader::DictStateReader; use crate::test_utils::initial_test_state::test_state; +use crate::test_utils::invoke::invoke_tx; use crate::test_utils::{ create_calldata, create_trivial_calldata, @@ -37,6 +38,7 @@ use crate::test_utils::{ DEFAULT_STRK_L1_GAS_PRICE, MAX_FEE, }; +use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use crate::transaction::errors::{ TransactionExecutionError, TransactionFeeError, @@ -222,9 +224,10 @@ fn test_invalid_nonce_pre_validate( // First scenario: invalid nonce. Regardless of flags, should fail. let invalid_nonce = nonce!(7_u8); let account_nonce = state.get_nonce_at(account_address).unwrap(); - let result = - account_invoke_tx(invoke_tx_args! {nonce: invalid_nonce, ..pre_validation_base_args}) - .execute(&mut state, &block_context, charge_fee, validate); + let tx = invoke_tx(invoke_tx_args! {nonce: invalid_nonce, ..pre_validation_base_args}); + let execution_flags = ExecutionFlags { only_query, charge_fee, validate }; + let account_tx = AccountTransaction { tx, execution_flags }; + let result = account_tx.execute(&mut state, &block_context, charge_fee, validate); assert_matches!( result.unwrap_err(), TransactionExecutionError::TransactionPreValidationError( @@ -265,6 +268,7 @@ fn test_simulate_validate_pre_validate_with_charge_fee( max_fee: Fee(10), resource_bounds: l1_resource_bounds(10_u8.into(), 10_u8.into()), nonce: nonce_manager.next(account_address), + ..pre_validation_base_args.clone() }) .execute(&mut state, &block_context, charge_fee, validate) @@ -294,16 +298,21 @@ fn test_simulate_validate_pre_validate_with_charge_fee( // Second scenario: resource bounds greater than balance. let gas_price = block_context.block_info.gas_prices.l1_gas_price(&fee_type); let balance_over_gas_price = BALANCE.checked_div(gas_price).unwrap(); - let result = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { max_fee: Fee(BALANCE.0 + 1), resource_bounds: l1_resource_bounds( (balance_over_gas_price.0 + 10).into(), gas_price.into() ), nonce: nonce_manager.next(account_address), + ..pre_validation_base_args.clone() - }) - .execute(&mut state, &block_context, charge_fee, validate); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let result = account_tx.execute(&mut state, &block_context, charge_fee, validate); nonce_manager.rollback(account_address); if is_deprecated { @@ -329,13 +338,17 @@ fn test_simulate_validate_pre_validate_with_charge_fee( // Third scenario: L1 gas price bound lower than the price on the block. if !is_deprecated { - let err = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { resource_bounds: l1_resource_bounds(DEFAULT_L1_GAS_AMOUNT, (gas_price.get().0 - 1).into()), nonce: nonce_manager.next(account_address), + ..pre_validation_base_args - }) - .execute(&mut state, &block_context, charge_fee, validate) - .unwrap_err(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let err = account_tx.execute(&mut state, &block_context, charge_fee, validate).unwrap_err(); nonce_manager.rollback(account_address); assert_matches!( @@ -368,12 +381,16 @@ fn test_simulate_validate_pre_validate_not_charge_fee( get_pre_validate_test_args(cairo_version, version, only_query); let account_address = pre_validation_base_args.sender_address; - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { nonce: nonce_manager.next(account_address), ..pre_validation_base_args.clone() - }) - .execute(&mut state, &block_context, charge_fee, false) - .unwrap(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate: false }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &block_context, charge_fee, false).unwrap(); let base_gas = calculate_actual_gas(&tx_execution_info, &block_context, false); assert!( base_gas @@ -387,14 +404,19 @@ fn test_simulate_validate_pre_validate_not_charge_fee( let (actual_gas_used, actual_fee) = gas_and_fee(base_gas, validate, &fee_type); macro_rules! execute_and_check_gas_and_fee { ($max_fee:expr, $resource_bounds:expr) => {{ - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { max_fee: $max_fee, resource_bounds: $resource_bounds, nonce: nonce_manager.next(account_address), + ..pre_validation_base_args.clone() - }) - .execute(&mut state, &block_context, charge_fee, validate) - .unwrap(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &block_context, charge_fee, validate).unwrap(); check_gas_and_fee( &block_context, &tx_execution_info, @@ -447,7 +469,7 @@ fn execute_fail_validation( } = create_flavors_test_state(&block_context.chain_info, cairo_version); // Validation scenario: fallible validation. - account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { max_fee, resource_bounds: max_resource_bounds, signature: TransactionSignature(vec![ @@ -458,9 +480,12 @@ fn execute_fail_validation( calldata: create_calldata(faulty_account_address, "foo", &[]), version, nonce: nonce_manager.next(faulty_account_address), - only_query, - }) - .execute(&mut falliable_state, &block_context, charge_fee, validate) + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + account_tx.execute(&mut falliable_state, &block_context, charge_fee, validate) } /// Test simulate / charge_fee flag combinations in (fallible) validation stage. @@ -576,13 +601,18 @@ fn test_simulate_validate_charge_fee_mid_execution( }; // First scenario: logic error. Should result in revert; actual fee should be shown. - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { calldata: recurse_calldata(test_contract_address, true, 3), nonce: nonce_manager.next(account_address), + only_query, ..execution_base_args.clone() - }) - .execute(&mut state, &block_context, charge_fee, validate) - .unwrap(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &block_context, charge_fee, validate).unwrap(); let base_gas = calculate_actual_gas(&tx_execution_info, &block_context, validate); let (revert_gas_used, revert_fee) = gas_and_fee(base_gas, validate, &fee_type); assert!( @@ -622,15 +652,21 @@ fn test_simulate_validate_charge_fee_mid_execution( validate, &fee_type, ); - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { max_fee: fee_bound, resource_bounds: l1_resource_bounds(gas_bound, gas_price.into()), calldata: recurse_calldata(test_contract_address, false, 1000), nonce: nonce_manager.next(account_address), + only_query, + ..execution_base_args.clone() - }) - .execute(&mut state, &block_context, charge_fee, validate) - .unwrap(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &block_context, charge_fee, validate).unwrap(); assert_eq!(tx_execution_info.is_reverted(), charge_fee); if charge_fee { assert!( @@ -675,15 +711,20 @@ fn test_simulate_validate_charge_fee_mid_execution( GasVector::from_l1_gas(block_limit_gas), &fee_type, ); - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + + let tx = invoke_tx(invoke_tx_args! { max_fee: huge_fee, resource_bounds: l1_resource_bounds(huge_gas_limit, gas_price.into()), calldata: recurse_calldata(test_contract_address, false, 10000), nonce: nonce_manager.next(account_address), ..execution_base_args - }) - .execute(&mut state, &low_step_block_context, charge_fee, validate) - .unwrap(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &low_step_block_context, charge_fee, validate).unwrap(); assert!( tx_execution_info.revert_error.clone().unwrap().to_string().contains("no remaining steps") ); @@ -759,7 +800,7 @@ fn test_simulate_validate_charge_fee_post_execution( validate, &fee_type, ); - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { max_fee: just_not_enough_fee_bound, resource_bounds: l1_resource_bounds(just_not_enough_gas_bound, gas_price.into()), calldata: recurse_calldata(test_contract_address, false, 1000), @@ -767,9 +808,13 @@ fn test_simulate_validate_charge_fee_post_execution( sender_address: account_address, version, only_query, - }) - .execute(&mut state, &block_context, charge_fee, validate) - .unwrap(); + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &block_context, charge_fee, validate).unwrap(); assert_eq!(tx_execution_info.is_reverted(), charge_fee); if charge_fee { let expected_error_prefix = @@ -819,7 +864,7 @@ fn test_simulate_validate_charge_fee_post_execution( felt!(0_u8), ], ); - let tx_execution_info = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { max_fee: actual_fee, resource_bounds: l1_resource_bounds(success_actual_gas, gas_price.into()), calldata: transfer_calldata, @@ -827,9 +872,14 @@ fn test_simulate_validate_charge_fee_post_execution( sender_address: account_address, version, only_query, - }) - .execute(&mut state, &block_context, charge_fee, validate) - .unwrap(); + + }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { only_query, charge_fee, validate }, + }; + let tx_execution_info = + account_tx.execute(&mut state, &block_context, charge_fee, validate).unwrap(); assert_eq!(tx_execution_info.is_reverted(), charge_fee); if charge_fee { diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index ea6b45abeb..e3ec59391e 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -21,7 +21,6 @@ use starknet_api::{calldata, declare_tx_args, deploy_account_tx_args, felt, invo use starknet_types_core::felt::Felt; use strum::IntoEnumIterator; -use super::account_transaction::ExecutionFlags; use crate::context::{BlockContext, ChainInfo}; use crate::state::cached_state::CachedState; use crate::state::state_api::State; @@ -43,10 +42,10 @@ use crate::test_utils::{ DEFAULT_STRK_L2_GAS_PRICE, MAX_FEE, }; -use crate::transaction::account_transaction::AccountTransaction; +use crate::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult}; use crate::transaction::transaction_types::TransactionType; -use crate::transaction::transactions::ExecutableTransaction; +use crate::transaction::transactions::{enforce_fee, ExecutableTransaction}; // Corresponding constants to the ones in faulty_account. pub const VALID: u64 = 0; @@ -294,6 +293,7 @@ pub fn create_account_tx_for_validate_test( calldata: execute_calldata, version: tx_version, nonce: nonce_manager.next(sender_address), + }); AccountTransaction { tx, execution_flags } } @@ -312,10 +312,14 @@ pub fn run_invoke_tx( block_context: &BlockContext, invoke_args: InvokeTxArgs, ) -> TransactionExecutionResult { - let tx = account_invoke_tx(invoke_args); - let charge_fee = tx.enforce_fee(); + let only_query = invoke_args.only_query; + let tx = invoke_tx(invoke_args); + let charge_fee = enforce_fee(&tx, only_query); + let execution_flags = ExecutionFlags { charge_fee, only_query, ..ExecutionFlags::default() }; + let account_tx = AccountTransaction { tx, execution_flags }; + // let charge_fee = tx.enforce_fee(); - tx.execute(state, block_context, charge_fee, true) + account_tx.execute(state, block_context, charge_fee, true) } /// Creates a `ResourceBoundsMapping` with the given `max_amount` and `max_price` for L1 gas limits. @@ -385,9 +389,13 @@ pub fn emit_n_events_tx( felt!(0_u32), // data length. ]; let calldata = create_calldata(contract_address, "test_emit_events", &entry_point_args); - account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { sender_address: account_contract, calldata, nonce - }) + }); + let only_query = false; + let charge_fee = enforce_fee(&tx, only_query); + let execution_flags = ExecutionFlags { only_query, charge_fee, ..ExecutionFlags::default() }; + AccountTransaction { tx, execution_flags } } diff --git a/crates/blockifier/src/transaction/transaction_execution.rs b/crates/blockifier/src/transaction/transaction_execution.rs index 103db62607..1351abb18b 100644 --- a/crates/blockifier/src/transaction/transaction_execution.rs +++ b/crates/blockifier/src/transaction/transaction_execution.rs @@ -84,6 +84,8 @@ impl Transaction { paid_fee_on_l1: Option, deployed_contract_address: Option, only_query: bool, + charge_fee: bool, + validate: bool, ) -> TransactionExecutionResult { let executable_tx = match tx { StarknetApiTransaction::L1Handler(l1_handler) => { @@ -127,11 +129,7 @@ impl Transaction { }; Ok(AccountTransaction { tx: executable_tx, - execution_flags: AccountExecutionFlags { - only_query, - charge_fee: false, - validate: false, - }, + execution_flags: AccountExecutionFlags { only_query, charge_fee, validate }, } .into()) } diff --git a/crates/blockifier/src/transaction/transactions.rs b/crates/blockifier/src/transaction/transactions.rs index effa40574f..1214845b1b 100644 --- a/crates/blockifier/src/transaction/transactions.rs +++ b/crates/blockifier/src/transaction/transactions.rs @@ -53,8 +53,6 @@ mod test; #[derive(Clone, Copy, Debug)] pub struct ExecutionFlags { - pub charge_fee: bool, - pub validate: bool, pub concurrency_mode: bool, } @@ -65,12 +63,12 @@ pub trait ExecutableTransaction: Sized { &self, state: &mut U, block_context: &BlockContext, - charge_fee: bool, - validate: bool, + _charge_fee: bool, + _validate: bool, ) -> TransactionExecutionResult { log::debug!("Executing Transaction..."); let mut transactional_state = TransactionalState::create_transactional(state); - let execution_flags = ExecutionFlags { charge_fee, validate, concurrency_mode: false }; + let execution_flags = ExecutionFlags { concurrency_mode: false }; let execution_result = self.execute_raw(&mut transactional_state, block_context, execution_flags); diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 297706e98e..1bcb9e4d55 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -1971,11 +1971,16 @@ fn test_validate_accounts_tx( sender_address, class_hash, validate_constructor, + validate: true, + charge_fee: false, ..Default::default() }; // Negative flows. + // We test `__validate__`, and don't care about the cahrge fee flow. + let charge_fee = false; + // Logic failure. let account_tx = create_account_tx_for_validate_test_nonce_0(FaultyAccountTxCreatorArgs { scenario: INVALID, @@ -1983,8 +1988,6 @@ fn test_validate_accounts_tx( additional_data: None, ..default_args }); - // We test `__validate__`, and don't care about the cahrge fee flow. - let charge_fee = false; let error = account_tx.execute(state, block_context, charge_fee, true).unwrap_err(); check_tx_execution_error_for_invalid_scenario!(cairo_version, error, validate_constructor,); @@ -2150,11 +2153,15 @@ fn test_valid_flag( &[(account_contract, 1), (test_contract, 1)], ); - let account_tx = account_invoke_tx(invoke_tx_args! { + let tx = invoke_tx(invoke_tx_args! { sender_address: account_contract.get_instance_address(0), calldata: create_trivial_calldata(test_contract.get_instance_address(0)), resource_bounds: default_all_resource_bounds, }); + let account_tx = AccountTransaction { + tx, + execution_flags: ExecutionFlags { validate: false, ..ExecutionFlags::default() }, + }; let actual_execution_info = account_tx.execute(state, block_context, true, false).unwrap(); diff --git a/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs b/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs index 3e3c3f030d..5272993583 100644 --- a/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs +++ b/crates/blockifier_reexecution/src/state_reader/reexecution_state_reader.rs @@ -45,7 +45,9 @@ pub trait ReexecutionStateReader { .into_iter() .map(|(tx, tx_hash)| match tx { Transaction::Invoke(_) | Transaction::DeployAccount(_) => { - Ok(BlockifierTransaction::from_api(tx, tx_hash, None, None, None, false)?) + Ok(BlockifierTransaction::from_api( + tx, tx_hash, None, None, None, false, true, true, + )?) } Transaction::Declare(ref declare_tx) => { let class_info = self @@ -58,6 +60,8 @@ pub trait ReexecutionStateReader { None, None, false, + true, + true, )?) } Transaction::L1Handler(_) => Ok(BlockifierTransaction::from_api( @@ -67,6 +71,8 @@ pub trait ReexecutionStateReader { Some(MAX_FEE), None, false, + true, + true, )?), Transaction::Deploy(_) => { diff --git a/crates/native_blockifier/src/py_transaction.rs b/crates/native_blockifier/src/py_transaction.rs index 6e72697b7d..b050e9d029 100644 --- a/crates/native_blockifier/src/py_transaction.rs +++ b/crates/native_blockifier/src/py_transaction.rs @@ -3,6 +3,7 @@ use std::collections::BTreeMap; use blockifier::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use blockifier::transaction::transaction_execution::Transaction; use blockifier::transaction::transaction_types::TransactionType; +use blockifier::transaction::transactions::enforce_fee; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; use starknet_api::block::GasPrice; @@ -133,21 +134,37 @@ pub fn py_tx( let tx_type = get_py_tx_type(tx)?; let tx_type: TransactionType = tx_type.parse().map_err(NativeBlockifierInputError::ParseError)?; + let only_query = false; Ok(match tx_type { TransactionType::Declare => { let non_optional_py_class_info: PyClassInfo = optional_py_class_info .expect("A class info must be passed in a Declare transaction."); let tx = ExecutableTransaction::Declare(py_declare(tx, non_optional_py_class_info)?); - AccountTransaction { tx, execution_flags: ExecutionFlags::default() }.into() + let execution_flags = ExecutionFlags { + only_query, + charge_fee: enforce_fee(&tx, only_query), + ..ExecutionFlags::default() + }; + AccountTransaction { tx, execution_flags }.into() } TransactionType::DeployAccount => { let tx = ExecutableTransaction::DeployAccount(py_deploy_account(tx)?); - AccountTransaction { tx, execution_flags: ExecutionFlags::default() }.into() + let execution_flags = ExecutionFlags { + only_query, + charge_fee: enforce_fee(&tx, only_query), + ..ExecutionFlags::default() + }; + AccountTransaction { tx, execution_flags }.into() } TransactionType::InvokeFunction => { let tx = ExecutableTransaction::Invoke(py_invoke_function(tx)?); - AccountTransaction { tx, execution_flags: ExecutionFlags::default() }.into() + let execution_flags = ExecutionFlags { + only_query, + charge_fee: enforce_fee(&tx, only_query), + ..ExecutionFlags::default() + }; + AccountTransaction { tx, execution_flags }.into() } TransactionType::L1Handler => py_l1_handler(tx)?.into(), }) diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index 369222aec5..4b405eda7a 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -700,7 +700,7 @@ fn execute_transactions( ) => Some(*class_hash), _ => None, }; - let blockifier_tx = to_blockifier_tx(tx, tx_hash, transaction_index)?; + let blockifier_tx = to_blockifier_tx(tx, tx_hash, transaction_index, charge_fee, validate)?; // TODO(Yoni): use the TransactionExecutor instead. let tx_execution_info_result = blockifier_tx.execute(&mut transactional_state, &block_context, charge_fee, validate); @@ -762,6 +762,8 @@ fn to_blockifier_tx( tx: ExecutableTransactionInput, tx_hash: TransactionHash, transaction_index: usize, + charge_fee: bool, + validate: bool, ) -> ExecutionResult { // TODO(yair): support only_query version bit (enable in the RPC v0.6 and use the correct // value). @@ -774,6 +776,8 @@ fn to_blockifier_tx( None, None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } @@ -786,6 +790,8 @@ fn to_blockifier_tx( None, None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } @@ -813,6 +819,8 @@ fn to_blockifier_tx( None, None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } @@ -838,6 +846,8 @@ fn to_blockifier_tx( None, None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } @@ -862,6 +872,8 @@ fn to_blockifier_tx( None, None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } @@ -886,6 +898,8 @@ fn to_blockifier_tx( None, None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } @@ -897,6 +911,8 @@ fn to_blockifier_tx( Some(paid_fee), None, only_query, + charge_fee, + validate, ) .map_err(|err| ExecutionError::from((transaction_index, err))) } diff --git a/crates/starknet_batcher/src/block_builder.rs b/crates/starknet_batcher/src/block_builder.rs index a44eaf7719..085235f832 100644 --- a/crates/starknet_batcher/src/block_builder.rs +++ b/crates/starknet_batcher/src/block_builder.rs @@ -18,6 +18,7 @@ use blockifier::state::global_cache::GlobalContractCache; use blockifier::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; use blockifier::transaction::objects::TransactionExecutionInfo; use blockifier::transaction::transaction_execution::Transaction as BlockifierTransaction; +use blockifier::transaction::transactions::enforce_fee; use blockifier::versioned_constants::{VersionedConstants, VersionedConstantsOverrides}; use indexmap::IndexMap; #[cfg(test)] @@ -162,10 +163,16 @@ impl BlockBuilderTrait for BlockBuilder { for tx in &next_tx_chunk { let executable_tx = match tx { Transaction::Account(account_tx) => { + let only_query = false; + let charge_fee = enforce_fee(&account_tx, only_query); BlockifierTransaction::Account(AccountTransaction { // TODO(yair): Avoid this clone. tx: account_tx.clone(), - execution_flags: ExecutionFlags::default(), + execution_flags: ExecutionFlags { + only_query, + charge_fee, + validate: true, + }, }) } Transaction::L1Handler(l1_handler_tx) => { diff --git a/crates/starknet_gateway/src/stateful_transaction_validator.rs b/crates/starknet_gateway/src/stateful_transaction_validator.rs index 6e0549e50d..d09080c94e 100644 --- a/crates/starknet_gateway/src/stateful_transaction_validator.rs +++ b/crates/starknet_gateway/src/stateful_transaction_validator.rs @@ -6,6 +6,7 @@ use blockifier::bouncer::BouncerConfig; use blockifier::context::{BlockContext, ChainInfo}; use blockifier::state::cached_state::CachedState; use blockifier::transaction::account_transaction::{AccountTransaction, ExecutionFlags}; +use blockifier::transaction::transactions::enforce_fee; use blockifier::versioned_constants::VersionedConstants; #[cfg(test)] use mockall::automock; @@ -75,10 +76,11 @@ impl StatefulTransactionValidator { mut validator: V, ) -> StatefulTransactionValidatorResult<()> { let skip_validate = skip_stateful_validations(executable_tx, account_nonce); - let account_tx = AccountTransaction { - tx: executable_tx.clone(), - execution_flags: ExecutionFlags::default(), - }; + let only_query = false; + let charge_fee = enforce_fee(&executable_tx, only_query); + let execution_flags = ExecutionFlags { only_query, charge_fee, validate: true }; + + let account_tx = AccountTransaction { tx: executable_tx.clone(), execution_flags }; validator .validate(account_tx, skip_validate) .map_err(|err| GatewaySpecError::ValidationFailure { data: err.to_string() })?;