Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Nov 28, 2024
1 parent d33ffac commit 406bacf
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
11 changes: 11 additions & 0 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
10 changes: 6 additions & 4 deletions crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<TransactionContext>,
mode: ExecutionMode,
Expand Down Expand Up @@ -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| {
Expand Down
44 changes: 36 additions & 8 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::cmp::min;
use std::sync::Arc;

use starknet_api::abi::abi_utils::selector_from_name;
Expand Down Expand Up @@ -589,7 +590,8 @@ impl AccountTransaction {
&self,
state: &mut TransactionalState<'_, S>,
tx_context: Arc<TransactionContext>,
remaining_gas: &mut u64,
remaining_validation_gas: &mut u64,
additional_execution_gas: u64,
validate: bool,
charge_fee: bool,
) -> TransactionExecutionResult<ValidateExecuteCallInfo> {
Expand All @@ -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,
)?;
Expand All @@ -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.
Expand Down Expand Up @@ -727,16 +734,37 @@ impl AccountTransaction {
fn run_or_revert<S: StateReader>(
&self,
state: &mut TransactionalState<'_, S>,
remaining_gas: &mut u64,
tx_context: Arc<TransactionContext>,
validate: bool,
charge_fee: bool,
) -> TransactionExecutionResult<ValidateExecuteCallInfo> {
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,
)
}
}

Expand All @@ -760,7 +788,8 @@ impl<U: UpdatableState> ExecutableTransaction<U> 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,
Expand All @@ -774,7 +803,6 @@ impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
},
} = self.run_or_revert(
state,
&mut remaining_gas,
tx_context.clone(),
execution_flags.validate,
execution_flags.charge_fee,
Expand Down

0 comments on commit 406bacf

Please sign in to comment.