Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(starknet_api,blockifier): to_discounted_gas_amount #2874

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(|| {
Expand Down
79 changes: 41 additions & 38 deletions crates/starknet_api/src/execution_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading