Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
AnkushinDaniil committed Feb 20, 2025
1 parent b7f6ac0 commit 840f791
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions vm/rust/src/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ pub fn is_l2_gas_accounting_enabled(
return Ok(false);
}


let min_sierra_version = &block_context
.versioned_constants()
.min_sierra_version_for_sierra_gas;
Expand Down Expand Up @@ -112,13 +111,13 @@ where

// Simulate transaction execution with maximum possible gas to get actual gas usage.
set_l2_gas_limit(transaction, GasAmount::MAX);
// TODO: Consider getting the upper bound from the balance and not changing the execution flags
// TODO: Consider getting the upper bound from the balance and not changing the execution flags
match transaction {
Transaction::Account(account_transaction) => {
account_transaction.execution_flags.charge_fee = false;
account_transaction.execution_flags.validate = false;
}
_ =>{},
_ => {}
};
let (simulation_result, _) = match simulate_execution(transaction, state, block_context) {
Ok(info) => info,
Expand All @@ -142,54 +141,53 @@ where
let l2_gas_adjusted = GasAmount(gas_used.saturating_add(gas_used / 10));
set_l2_gas_limit(transaction, l2_gas_adjusted);

let (l2_gas_limit, _, tx_state) =
match simulate_execution(transaction, state, block_context) {
Ok((tx_info, tx_state)) => {
// If 110% of the actual transaction gas fee is enough, we use that
// as the estimate and skip the binary search.
(l2_gas_adjusted, tx_info, tx_state)
}
Err(SimulationError::OutOfGas(_)) => {
let mut lower_bound = GasAmount(gas_used);
let mut upper_bound = GasAmount::MAX;
let mut current_l2_gas_limit = calculate_midpoint(lower_bound, upper_bound);

// Run a binary search to find the minimal gas limit that still allows the
// transaction to execute without running out of L2 gas.
let (tx_info, tx_state) = loop {
set_l2_gas_limit(transaction, current_l2_gas_limit);
let (l2_gas_limit, _, tx_state) = match simulate_execution(transaction, state, block_context) {
Ok((tx_info, tx_state)) => {
// If 110% of the actual transaction gas fee is enough, we use that
// as the estimate and skip the binary search.
(l2_gas_adjusted, tx_info, tx_state)
}
Err(SimulationError::OutOfGas(_)) => {
let mut lower_bound = GasAmount(gas_used);
let mut upper_bound = GasAmount::MAX;
let mut current_l2_gas_limit = calculate_midpoint(lower_bound, upper_bound);

// Special case where the search would get stuck if `current_l2_gas_limit ==
// lower_bound` but the required amount is equal to the upper bound.
let bounds_diff = upper_bound
.checked_sub(lower_bound)
.expect("Upper bound >= lower bound");
if bounds_diff == GasAmount(1) && current_l2_gas_limit == lower_bound {
lower_bound = upper_bound;
current_l2_gas_limit = upper_bound;
}
// Run a binary search to find the minimal gas limit that still allows the
// transaction to execute without running out of L2 gas.
let (tx_info, tx_state) = loop {
set_l2_gas_limit(transaction, current_l2_gas_limit);

match simulate_execution(transaction, state, block_context) {
Ok((tx_info, tx_state)) => {
if is_search_complete(lower_bound, upper_bound, L2_GAS_SEARCH_MARGIN) {
break (tx_info, tx_state);
}
// Special case where the search would get stuck if `current_l2_gas_limit ==
// lower_bound` but the required amount is equal to the upper bound.
let bounds_diff = upper_bound
.checked_sub(lower_bound)
.expect("Upper bound >= lower bound");
if bounds_diff == GasAmount(1) && current_l2_gas_limit == lower_bound {
lower_bound = upper_bound;
current_l2_gas_limit = upper_bound;
}

upper_bound = current_l2_gas_limit;
current_l2_gas_limit = calculate_midpoint(lower_bound, upper_bound);
}
Err(SimulationError::OutOfGas(_)) => {
lower_bound = current_l2_gas_limit;
current_l2_gas_limit = calculate_midpoint(lower_bound, upper_bound);
match simulate_execution(transaction, state, block_context) {
Ok((tx_info, tx_state)) => {
if is_search_complete(lower_bound, upper_bound, L2_GAS_SEARCH_MARGIN) {
break (tx_info, tx_state);
}
Err(SimulationError::ExecutionError(error)) => return Err(error),

upper_bound = current_l2_gas_limit;
current_l2_gas_limit = calculate_midpoint(lower_bound, upper_bound);
}
Err(SimulationError::OutOfGas(_)) => {
lower_bound = current_l2_gas_limit;
current_l2_gas_limit = calculate_midpoint(lower_bound, upper_bound);
}
};
Err(SimulationError::ExecutionError(error)) => return Err(error),
}
};

(current_l2_gas_limit, tx_info, tx_state)
}
Err(SimulationError::ExecutionError(error)) => return Err(error),
};
(current_l2_gas_limit, tx_info, tx_state)
}
Err(SimulationError::ExecutionError(error)) => return Err(error),
};
tx_state.abort();

// If the computed gas limit exceeds the initial limit, revert the transaction.
Expand Down

0 comments on commit 840f791

Please sign in to comment.