From 1f8d613bedd6ae205f348aeac13deb8800ac5b95 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Sun, 22 Dec 2024 16:48:16 +0200 Subject: [PATCH] refactor(starknet_api,blockifier): to_discounted_gas_amount Signed-off-by: Dori Medini --- crates/blockifier/src/fee/fee_utils.rs | 9 ++- .../starknet_api/src/execution_resources.rs | 79 ++++++++++--------- 2 files changed, 48 insertions(+), 40 deletions(-) diff --git a/crates/blockifier/src/fee/fee_utils.rs b/crates/blockifier/src/fee/fee_utils.rs index 6935426d45..0272206a2d 100644 --- a/crates/blockifier/src/fee/fee_utils.rs +++ b/crates/blockifier/src/fee/fee_utils.rs @@ -6,7 +6,7 @@ use num_bigint::BigUint; use starknet_api::abi::abi_utils::get_fee_token_var_address; use starknet_api::block::{BlockInfo, FeeType, GasPriceVector}; use starknet_api::core::ContractAddress; -use starknet_api::execution_resources::{GasAmount, GasVector}; +use starknet_api::execution_resources::{to_discounted_l1_gas, GasAmount, GasVector}; use starknet_api::state::StorageKey; use starknet_api::transaction::fields::ValidResourceBounds::{AllResources, L1Gas}; use starknet_api::transaction::fields::{Fee, GasVectorComputationMode, Resource}; @@ -42,7 +42,12 @@ impl GasVectorToL1GasForFee for GasVector { versioned_constants: &VersionedConstants, ) -> GasAmount { // Discounted gas converts data gas to L1 gas. Add L2 gas using conversion ratio. - let discounted_l1_gas = self.to_discounted_l1_gas(gas_prices); + let discounted_l1_gas = to_discounted_l1_gas( + gas_prices.l1_gas_price, + gas_prices.l1_data_gas_price.into(), + self.l1_gas, + self.l1_data_gas, + ); discounted_l1_gas .checked_add(versioned_constants.sierra_gas_to_l1_gas_amount_round_up(self.l2_gas)) .unwrap_or_else(|| { diff --git a/crates/starknet_api/src/execution_resources.rs b/crates/starknet_api/src/execution_resources.rs index 9c3629796d..21a573f274 100644 --- a/crates/starknet_api/src/execution_resources.rs +++ b/crates/starknet_api/src/execution_resources.rs @@ -162,46 +162,49 @@ impl GasVector { } sum } +} - /// Compute l1_gas estimation from gas_vector using the following formula: - /// One byte of data costs either 1 data gas (in blob mode) or 16 gas (in calldata - /// mode). For gas price GP and data gas price DGP, the discount for using blobs - /// would be DGP / (16 * GP). - /// X non-data-related gas consumption and Y bytes of data, in non-blob mode, would - /// cost (X + 16*Y) units of gas. Applying the discount ratio to the data-related - /// summand, we get total_gas = (X + Y * DGP / GP). - /// If this function is called with kzg_flag==false, then l1_data_gas==0, and this discount - /// function does nothing. - /// Panics on overflow. - /// Does not take L2 gas into account - more context is required to convert L2 gas units to L1 - /// gas units (context defined outside of the current crate). - pub fn to_discounted_l1_gas(&self, gas_prices: &GasPriceVector) -> GasAmount { - let l1_data_gas_fee = self - .l1_data_gas - .checked_mul(gas_prices.l1_data_gas_price.into()) - .unwrap_or_else(|| { - panic!( - "Discounted L1 gas cost overflowed: multiplication of L1 data gas ({}) by L1 \ - data gas price ({}) resulted in overflow.", - self.l1_data_gas, gas_prices.l1_data_gas_price - ); - }); - let l1_data_gas_in_l1_gas_units = - l1_data_gas_fee.checked_div_ceil(gas_prices.l1_gas_price).unwrap_or_else(|| { - panic!( - "Discounted L1 gas cost overflowed: division of L1 data fee ({}) by regular \ - L1 gas price ({}) resulted in overflow.", - l1_data_gas_fee, gas_prices.l1_gas_price - ); - }); - self.l1_gas.checked_add(l1_data_gas_in_l1_gas_units).unwrap_or_else(|| { +/// Computes the total L1 gas amount estimation from the different given L1 gas arguments using the +/// following formula: +/// One byte of data costs either 1 data gas (in blob mode) or 16 gas (in calldata +/// mode). For gas price GP and data gas price DGP, the discount for using blobs +/// would be DGP / (16 * GP). +/// X non-data-related gas consumption and Y bytes of data, in non-blob mode, would +/// cost (X + 16*Y) units of gas. Applying the discount ratio to the data-related +/// summand, we get total_gas = (X + Y * DGP / GP). +/// If this function is called with kzg_flag==false, then l1_data_gas==0, and this discount +/// function does nothing. +/// Panics on overflow. +/// Does not take L2 gas into account - more context is required to convert L2 gas units to L1 gas +/// units (context defined outside of the current crate). +pub fn to_discounted_l1_gas( + l1_gas_price: NonzeroGasPrice, + l1_data_gas_price: GasPrice, + l1_gas: GasAmount, + l1_data_gas: GasAmount, +) -> GasAmount { + let l1_data_gas_fee = l1_data_gas.checked_mul(l1_data_gas_price).unwrap_or_else(|| { + panic!( + "Discounted L1 gas cost overflowed: multiplication of L1 data gas ({}) by L1 data gas \ + price ({}) resulted in overflow.", + l1_data_gas, l1_data_gas_price + ); + }); + let l1_data_gas_in_l1_gas_units = + l1_data_gas_fee.checked_div_ceil(l1_gas_price).unwrap_or_else(|| { panic!( - "Overflow while computing discounted L1 gas: L1 gas ({}) + L1 data gas in L1 gas \ - units ({}) resulted in overflow.", - self.l1_gas, l1_data_gas_in_l1_gas_units - ) - }) - } + "Discounted L1 gas cost overflowed: division of L1 data fee ({}) by regular L1 \ + gas price ({}) resulted in overflow.", + l1_data_gas_fee, l1_gas_price + ); + }); + l1_gas.checked_add(l1_data_gas_in_l1_gas_units).unwrap_or_else(|| { + panic!( + "Overflow while computing discounted L1 gas: L1 gas ({}) + L1 data gas in L1 gas \ + units ({}) resulted in overflow.", + l1_gas, l1_data_gas_in_l1_gas_units + ) + }) } /// The execution resources used by a transaction.