Skip to content

Commit

Permalink
chore(blockifier): convert Sierra gas to L1 gas if in L1 gas mode (#2451
Browse files Browse the repository at this point in the history
)
  • Loading branch information
Yoni-Starkware authored Dec 4, 2024
1 parent eaa6067 commit af955af
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/src/fee/fee_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ impl FeeCheckReport {
) -> FeeCheckResult<()> {
let total_discounted_gas_used =
gas_vector.to_discounted_l1_gas(tx_context.get_gas_prices());

if total_discounted_gas_used > l1_bounds.max_amount {
return Err(FeeCheckError::MaxGasAmountExceeded {
resource: L1Gas,
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn get_vm_resources_cost(
match computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(vm_l1_gas_usage),
GasVectorComputationMode::All => GasVector::from_l2_gas(
versioned_constants.convert_l1_to_l2_gas_amount_round_up(vm_l1_gas_usage),
versioned_constants.l1_gas_to_sierra_gas_amount_round_up(vm_l1_gas_usage),
),
}
}
Expand Down
13 changes: 10 additions & 3 deletions crates/blockifier/src/fee/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,21 @@ impl ComputationResources {
self.n_reverted_steps,
computation_mode,
);
let sierra_gas_cost = GasVector::from_l2_gas(

let total_sierra_gas =
self.sierra_gas.checked_add(self.reverted_sierra_gas).unwrap_or_else(|| {
panic!(
"Sierra gas overflowed: tried to add {} to {}",
self.sierra_gas, self.reverted_sierra_gas
)
}),
);
});
let sierra_gas_cost = match computation_mode {
GasVectorComputationMode::All => GasVector::from_l2_gas(total_sierra_gas),
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(
versioned_constants.sierra_gas_to_l1_gas_amount_round_up(total_sierra_gas),
),
};

vm_cost.checked_add(sierra_gas_cost).unwrap_or_else(|| {
panic!(
"Computation resources to gas vector overflowed: tried to add {sierra_gas_cost:?} \
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ pub fn gas_vector_from_vm_usage(
match computation_mode {
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(vm_usage_in_l1_gas),
GasVectorComputationMode::All => GasVector::from_l2_gas(
versioned_constants.convert_l1_to_l2_gas_amount_round_up(vm_usage_in_l1_gas),
versioned_constants.l1_gas_to_sierra_gas_amount_round_up(vm_usage_in_l1_gas),
),
}
}
Expand Down
26 changes: 14 additions & 12 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,29 +220,31 @@ impl VersionedConstants {
Ok(serde_json::from_reader(std::fs::File::open(path)?)?)
}

/// Converts from L1 gas price to L2 gas price with **upward rounding**.
/// Converts from L1 gas price to L2 gas price with **upward rounding**, based on the
/// conversion of a Cairo step from Sierra gas to L1 gas.
pub fn convert_l1_to_l2_gas_price_round_up(&self, l1_gas_price: GasPrice) -> GasPrice {
(*(resource_cost_to_u128_ratio(self.l1_to_l2_gas_price_ratio()) * l1_gas_price.0)
(*(resource_cost_to_u128_ratio(self.sierra_gas_in_l1_gas_amount()) * l1_gas_price.0)
.ceil()
.numer())
.into()
}

/// Converts from L1 gas amount to L2 gas amount with **upward rounding**.
pub fn convert_l1_to_l2_gas_amount_round_up(&self, l1_gas_amount: GasAmount) -> GasAmount {
/// Converts L1 gas amount to Sierra (L2) gas amount with **upward rounding**.
pub fn l1_gas_to_sierra_gas_amount_round_up(&self, l1_gas_amount: GasAmount) -> GasAmount {
// The amount ratio is the inverse of the price ratio.
(*(self.l1_to_l2_gas_price_ratio().inv() * l1_gas_amount.0).ceil().numer()).into()
(*(self.sierra_gas_in_l1_gas_amount().inv() * l1_gas_amount.0).ceil().numer()).into()
}

/// Returns the following ratio: L2_gas_price/L1_gas_price.
fn l1_to_l2_gas_price_ratio(&self) -> ResourceCost {
Ratio::new(1, self.os_constants.gas_costs.step_gas_cost)
* self.vm_resource_fee_cost().n_steps
/// Converts Sierra (L2) gas amount to L1 gas amount with **upward rounding**.
pub fn sierra_gas_to_l1_gas_amount_round_up(&self, l2_gas_amount: GasAmount) -> GasAmount {
(*(self.sierra_gas_in_l1_gas_amount() * l2_gas_amount.0).ceil().numer()).into()
}

#[cfg(any(feature = "testing", test))]
pub fn get_l1_to_l2_gas_price_ratio(&self) -> ResourceCost {
self.l1_to_l2_gas_price_ratio()
/// Returns the equivalent L1 gas amount of one unit of Sierra gas.
/// The conversion is based on the pricing of a single Cairo step.
fn sierra_gas_in_l1_gas_amount(&self) -> ResourceCost {
Ratio::new(1, self.os_constants.gas_costs.step_gas_cost)
* self.vm_resource_fee_cost().n_steps
}

/// Returns the default initial gas for VM mode transactions.
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_api/src/execution_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ impl GasVector {
/// function does nothing.
/// Panics on overflow.
pub fn to_discounted_l1_gas(&self, gas_prices: &GasPriceVector) -> GasAmount {
if self.l2_gas.0 > 0 {
// TODO(Yoni, 10/12/2024): convert L2 gas as well.
todo!();
}
let l1_data_gas_fee = self
.l1_data_gas
.checked_mul(gas_prices.l1_data_gas_price.into())
Expand Down

0 comments on commit af955af

Please sign in to comment.