From ed0645743920d2a241a89559b8d46c1de9a27812 Mon Sep 17 00:00:00 2001 From: Aviv Greenburg Date: Thu, 5 Dec 2024 16:14:58 +0200 Subject: [PATCH] chore(blockifier): access self.execution_flags instead of passing as args --- .../src/blockifier/stateful_validator.rs | 2 - .../src/transaction/account_transaction.rs | 96 +++++++------------ .../src/transaction/transactions_test.rs | 6 +- 3 files changed, 40 insertions(+), 64 deletions(-) diff --git a/crates/blockifier/src/blockifier/stateful_validator.rs b/crates/blockifier/src/blockifier/stateful_validator.rs index 1d2a12ae3e5..df1caf9edb3 100644 --- a/crates/blockifier/src/blockifier/stateful_validator.rs +++ b/crates/blockifier/src/blockifier/stateful_validator.rs @@ -94,11 +94,9 @@ impl StatefulValidator { ) -> StatefulValidatorResult<()> { let strict_nonce_check = false; // Run pre-validation in charge fee mode to perform fee and balance related checks. - let charge_fee = tx.enforce_fee(); tx.perform_pre_validation_stage( self.tx_executor.block_state.as_mut().expect(BLOCK_STATE_ACCESS_ERR), tx_context, - charge_fee, strict_nonce_check, )?; diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 430b33eeb6b..18433b8134a 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -242,13 +242,12 @@ impl AccountTransaction { &self, state: &mut S, tx_context: &TransactionContext, - charge_fee: bool, strict_nonce_check: bool, ) -> TransactionPreValidationResult<()> { let tx_info = &tx_context.tx_info; Self::handle_nonce(state, tx_info, strict_nonce_check)?; - if charge_fee { + if self.execution_flags.charge_fee { self.check_fee_bounds(tx_context)?; verify_can_pay_committed_bounds(state, tx_context)?; @@ -374,10 +373,9 @@ impl AccountTransaction { state: &mut dyn State, tx_context: Arc, remaining_gas: &mut u64, - validate: bool, - limit_steps_by_resources: bool, ) -> TransactionExecutionResult> { - if validate { + let limit_steps_by_resources = self.execution_flags.charge_fee; + if self.execution_flags.validate { self.validate_tx(state, tx_context, remaining_gas, limit_steps_by_resources) } else { Ok(None) @@ -519,8 +517,6 @@ impl AccountTransaction { state: &mut TransactionalState<'_, S>, tx_context: Arc, remaining_gas: &mut u64, - validate: bool, - charge_fee: bool, ) -> TransactionExecutionResult { let validate_call_info: Option; let execute_call_info: Option; @@ -528,26 +524,20 @@ impl AccountTransaction { // Handle `DeployAccount` transactions separately, due to different order of things. // Also, the execution context required form the `DeployAccount` execute phase is // validation context. - let mut execution_context = - EntryPointExecutionContext::new_validate(tx_context.clone(), charge_fee); - execute_call_info = self.run_execute(state, &mut execution_context, remaining_gas)?; - validate_call_info = self.handle_validate_tx( - state, + let mut execution_context = EntryPointExecutionContext::new_validate( tx_context.clone(), - remaining_gas, - validate, - charge_fee, - )?; + self.execution_flags.charge_fee, + ); + execute_call_info = self.run_execute(state, &mut execution_context, remaining_gas)?; + validate_call_info = + self.handle_validate_tx(state, tx_context.clone(), remaining_gas)?; } else { - let mut execution_context = - EntryPointExecutionContext::new_invoke(tx_context.clone(), charge_fee); - validate_call_info = self.handle_validate_tx( - state, + let mut execution_context = EntryPointExecutionContext::new_invoke( tx_context.clone(), - remaining_gas, - validate, - charge_fee, - )?; + self.execution_flags.charge_fee, + ); + validate_call_info = + self.handle_validate_tx(state, tx_context.clone(), remaining_gas)?; execute_call_info = self.run_execute(state, &mut execution_context, remaining_gas)?; } @@ -562,8 +552,12 @@ impl AccountTransaction { 0, ); - let post_execution_report = - PostExecutionReport::new(state, &tx_context, &tx_receipt, charge_fee)?; + let post_execution_report = PostExecutionReport::new( + state, + &tx_context, + &tx_receipt, + self.execution_flags.charge_fee, + )?; match post_execution_report.error() { Some(error) => Err(error.into()), None => Ok(ValidateExecuteCallInfo::new_accepted( @@ -579,19 +573,14 @@ impl AccountTransaction { state: &mut TransactionalState<'_, S>, tx_context: Arc, remaining_gas: &mut u64, - validate: bool, - charge_fee: bool, ) -> TransactionExecutionResult { - let mut execution_context = - EntryPointExecutionContext::new_invoke(tx_context.clone(), charge_fee); - // Run the validation, and if execution later fails, only keep the validation diff. - let validate_call_info = self.handle_validate_tx( - state, + let mut execution_context = EntryPointExecutionContext::new_invoke( tx_context.clone(), - remaining_gas, - validate, - charge_fee, - )?; + self.execution_flags.charge_fee, + ); + // Run the validation, and if execution later fails, only keep the validation diff. + let validate_call_info = + self.handle_validate_tx(state, tx_context.clone(), remaining_gas)?; let n_allotted_execution_steps = execution_context.subtract_validation_and_overhead_steps( &validate_call_info, @@ -653,7 +642,7 @@ impl AccountTransaction { &mut execution_state, &tx_context, &tx_receipt, - charge_fee, + self.execution_flags.charge_fee, )?; match post_execution_report.error() { Some(post_execution_error) => { @@ -687,8 +676,12 @@ impl AccountTransaction { let revert_receipt = get_revert_receipt(); // Error during execution. Revert, even if the error is sequencer-related. execution_state.abort(); - let post_execution_report = - PostExecutionReport::new(state, &tx_context, &revert_receipt, charge_fee)?; + let post_execution_report = PostExecutionReport::new( + state, + &tx_context, + &revert_receipt, + self.execution_flags.charge_fee, + )?; Ok(ValidateExecuteCallInfo::new_reverted( validate_call_info, gen_tx_execution_error_trace(&execution_error).into(), @@ -726,14 +719,12 @@ impl AccountTransaction { state: &mut TransactionalState<'_, S>, remaining_gas: &mut u64, tx_context: Arc, - validate: bool, - charge_fee: bool, ) -> TransactionExecutionResult { if self.is_non_revertible(&tx_context.tx_info) { - return self.run_non_revertible(state, tx_context, remaining_gas, validate, charge_fee); + return self.run_non_revertible(state, tx_context, remaining_gas); } - self.run_revertible(state, tx_context, remaining_gas, validate, charge_fee) + self.run_revertible(state, tx_context, remaining_gas) } } @@ -749,13 +740,7 @@ impl ExecutableTransaction for AccountTransaction { // Nonce and fee check should be done before running user code. let strict_nonce_check = true; - self.perform_pre_validation_stage( - state, - &tx_context, - // TODO (AvivG): access execution_flags within func instead of passing them as args. - self.execution_flags.charge_fee, - strict_nonce_check, - )?; + self.perform_pre_validation_stage(state, &tx_context, strict_nonce_check)?; // Run validation and execution. let mut remaining_gas = tx_context.initial_sierra_gas(); @@ -770,14 +755,7 @@ impl ExecutableTransaction for AccountTransaction { resources: final_resources, gas: total_gas, }, - } = self.run_or_revert( - state, - &mut remaining_gas, - tx_context.clone(), - // TODO (AvivG): access execution_flags within func instead of passing them as args. - self.execution_flags.validate, - self.execution_flags.charge_fee, - )?; + } = self.run_or_revert(state, &mut remaining_gas, tx_context.clone())?; let fee_transfer_call_info = Self::handle_fee( state, tx_context, diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 80450355b94..6afdfaef889 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -1385,7 +1385,7 @@ fn test_invalid_nonce( ); let invalid_tx_context = block_context.to_tx_context(&invalid_tx); let pre_validation_err = invalid_tx - .perform_pre_validation_stage(&mut transactional_state, &invalid_tx_context, false, true) + .perform_pre_validation_stage(&mut transactional_state, &invalid_tx_context, true) .unwrap_err(); // Test error. @@ -1406,7 +1406,7 @@ fn test_invalid_nonce( let valid_tx_context = block_context.to_tx_context(&valid_tx); valid_tx - .perform_pre_validation_stage(&mut transactional_state, &valid_tx_context, false, false) + .perform_pre_validation_stage(&mut transactional_state, &valid_tx_context, false) .unwrap(); // Negative flow: account nonce = 1, incoming tx nonce = 0. @@ -1416,7 +1416,7 @@ fn test_invalid_nonce( ); let invalid_tx_context = block_context.to_tx_context(&invalid_tx); let pre_validation_err = invalid_tx - .perform_pre_validation_stage(&mut transactional_state, &invalid_tx_context, false, false) + .perform_pre_validation_stage(&mut transactional_state, &invalid_tx_context, false) .unwrap_err(); // Test error.