From 406bacf7da09168481c02d3c4497e4497b1e5a07 Mon Sep 17 00:00:00 2001 From: Aner Ben Efraim Date: Thu, 28 Nov 2024 17:59:50 +0200 Subject: [PATCH] WIP --- crates/blockifier/src/context.rs | 11 +++++ .../blockifier/src/execution/entry_point.rs | 10 +++-- .../src/transaction/account_transaction.rs | 44 +++++++++++++++---- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/crates/blockifier/src/context.rs b/crates/blockifier/src/context.rs index bd69be574f..96d11afd76 100644 --- a/crates/blockifier/src/context.rs +++ b/crates/blockifier/src/context.rs @@ -56,6 +56,17 @@ impl TransactionContext { }) => l2_gas.max_amount.0, } } + + pub fn initial_sierra_gas_validation_execution_split(&self) -> (u64, u64) { + let user_gas_limit = self.initial_sierra_gas(); + match user_gas_limit > self.block_context.versioned_constants.validate_max_sierra_gas { + true => ( + self.block_context.versioned_constants.validate_max_sierra_gas, + user_gas_limit - self.block_context.versioned_constants.validate_max_sierra_gas, + ), + false => (user_gas_limit, 0), + } + } } #[derive(Clone, Debug)] diff --git a/crates/blockifier/src/execution/entry_point.rs b/crates/blockifier/src/execution/entry_point.rs index b977f57239..0c5cace8e8 100644 --- a/crates/blockifier/src/execution/entry_point.rs +++ b/crates/blockifier/src/execution/entry_point.rs @@ -109,7 +109,7 @@ pub struct CallEntryPoint { pub caller_address: ContractAddress, pub call_type: CallType, // We can assume that the initial gas is less than 2^64. - pub initial_gas: u64, + pub initial_gas: u64, // HERE } impl CallEntryPoint { @@ -225,6 +225,8 @@ pub struct EntryPointExecutionContext { } impl EntryPointExecutionContext { + // This is a global context for the execution of an entry point. + // HERE? Probably not... pub fn new( tx_context: Arc, mode: ExecutionMode, @@ -261,13 +263,13 @@ impl EntryPointExecutionContext { fn max_steps( tx_context: &TransactionContext, mode: &ExecutionMode, - limit_steps_by_resources: bool, + limit_steps_by_resources: bool, // ??? ) -> usize { let TransactionContext { block_context, tx_info } = tx_context; let BlockContext { block_info, versioned_constants, .. } = block_context; let block_upper_bound = match mode { - ExecutionMode::Validate => versioned_constants.validate_max_n_steps, - ExecutionMode::Execute => versioned_constants.invoke_tx_max_n_steps, + ExecutionMode::Validate => versioned_constants.validate_max_n_steps,//HERE - new fn where replaced with validate_max_sierra_gas + ExecutionMode::Execute => versioned_constants.invoke_tx_max_n_steps,//HERE - execute_max_sierra_gas } .try_into() .unwrap_or_else(|error| { diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index ca61f75576..d504f4f79b 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -1,3 +1,4 @@ +use std::cmp::min; use std::sync::Arc; use starknet_api::abi::abi_utils::selector_from_name; @@ -589,7 +590,8 @@ impl AccountTransaction { &self, state: &mut TransactionalState<'_, S>, tx_context: Arc, - remaining_gas: &mut u64, + remaining_validation_gas: &mut u64, + additional_execution_gas: u64, validate: bool, charge_fee: bool, ) -> TransactionExecutionResult { @@ -599,7 +601,7 @@ impl AccountTransaction { let validate_call_info = self.handle_validate_tx( state, tx_context.clone(), - remaining_gas, + remaining_validation_gas, validate, charge_fee, )?; @@ -618,8 +620,13 @@ impl AccountTransaction { // Both will be rolled back if the execution is reverted or committed upon success. let mut execution_state = TransactionalState::create_transactional(state); + // Adjust the remaining gas after validation and limit it by the global execution gas limit. + let mut remaining_gas = min( + *remaining_validation_gas + additional_execution_gas, + tx_context.block_context.versioned_constants.execute_max_sierra_gas, + ); let execution_result = - self.run_execute(&mut execution_state, &mut execution_context, remaining_gas); + self.run_execute(&mut execution_state, &mut execution_context, &mut remaining_gas); // Pre-compute cost in case of revert. // TODO(tzahi): add reverted_l2_gas to the receipt. @@ -727,16 +734,37 @@ impl AccountTransaction { fn run_or_revert( &self, 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); + // Limit the gas by global execution gas limit. + let mut remaining_gas = min( + tx_context.initial_sierra_gas(), + tx_context.block_context.versioned_constants.execute_max_sierra_gas, + ); + return self.run_non_revertible( + state, + tx_context, + &mut remaining_gas, + validate, + charge_fee, + ); } - self.run_revertible(state, tx_context, remaining_gas, validate, charge_fee) + // Limit the gas by global validation gas limit. The difference between validation and + // remaining user_gas / max_execution_sierra_gas needs to be readded after validation. + let (mut validation_gas, additional_execution_gas) = + tx_context.initial_sierra_gas_validation_execution_split(); + self.run_revertible( + state, + tx_context, + &mut validation_gas, + additional_execution_gas, + validate, + charge_fee, + ) } } @@ -760,7 +788,8 @@ impl ExecutableTransaction for AccountTransaction { )?; // Run validation and execution. - let mut remaining_gas = tx_context.initial_sierra_gas(); + // let mut remaining_gas = tx_context.initial_sierra_gas(); // ANER: HERE - need to add + // global gas limit let ValidateExecuteCallInfo { validate_call_info, execute_call_info, @@ -774,7 +803,6 @@ impl ExecutableTransaction for AccountTransaction { }, } = self.run_or_revert( state, - &mut remaining_gas, tx_context.clone(), execution_flags.validate, execution_flags.charge_fee,