diff --git a/crates/blockifier/src/fee/fee_checks.rs b/crates/blockifier/src/fee/fee_checks.rs index 2657fffed8..6ec555b8ee 100644 --- a/crates/blockifier/src/fee/fee_checks.rs +++ b/crates/blockifier/src/fee/fee_checks.rs @@ -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, diff --git a/crates/blockifier/src/fee/fee_utils.rs b/crates/blockifier/src/fee/fee_utils.rs index 097ccf9c56..92de4adb63 100644 --- a/crates/blockifier/src/fee/fee_utils.rs +++ b/crates/blockifier/src/fee/fee_utils.rs @@ -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), ), } } diff --git a/crates/blockifier/src/fee/resources.rs b/crates/blockifier/src/fee/resources.rs index b155ca330c..edc1d9249e 100644 --- a/crates/blockifier/src/fee/resources.rs +++ b/crates/blockifier/src/fee/resources.rs @@ -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:?} \ diff --git a/crates/blockifier/src/test_utils.rs b/crates/blockifier/src/test_utils.rs index 94ac064489..ddc79c9d53 100644 --- a/crates/blockifier/src/test_utils.rs +++ b/crates/blockifier/src/test_utils.rs @@ -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), ), } } diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index 85f61f483a..7be354b494 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -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. diff --git a/crates/starknet_api/src/execution_resources.rs b/crates/starknet_api/src/execution_resources.rs index c9831b34aa..e3c14d1dbb 100644 --- a/crates/starknet_api/src/execution_resources.rs +++ b/crates/starknet_api/src/execution_resources.rs @@ -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())