Skip to content

Commit

Permalink
refactor(blockifier): refactor to_discounted_l1_gas
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <dori@starkware.co>
  • Loading branch information
dorimedini-starkware committed Oct 5, 2024
1 parent f39c078 commit e16d88b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
15 changes: 13 additions & 2 deletions crates/blockifier/src/fee/fee_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ use crate::fee::receipt::TransactionReceipt;
use crate::fee::resources::GasVector;
use crate::state::state_api::StateReader;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::{FeeType, TransactionExecutionResult, TransactionInfo};
use crate::transaction::objects::{
FeeType,
HasRelatedFeeType,
TransactionExecutionResult,
TransactionInfo,
};

#[derive(Clone, Copy, Debug, Error)]
pub enum FeeCheckError {
Expand Down Expand Up @@ -178,7 +183,13 @@ impl FeeCheckReport {
gas_vector: &GasVector,
tx_context: &TransactionContext,
) -> FeeCheckResult<()> {
let total_discounted_gas_used = gas_vector.to_discounted_l1_gas(tx_context);
let total_discounted_gas_used = gas_vector.to_discounted_l1_gas(
tx_context
.block_context
.block_info
.gas_prices
.get_gas_prices_by_fee_type(&tx_context.tx_info.fee_type()),
);
if total_discounted_gas_used > GasAmount(l1_bounds.max_amount) {
return Err(FeeCheckError::MaxGasAmountExceeded {
resource: L1Gas,
Expand Down
10 changes: 8 additions & 2 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::fee::resources::{
};
use crate::state::cached_state::StateChangesCount;
use crate::test_utils::{DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE};
use crate::transaction::objects::FeeType;
use crate::transaction::objects::{FeeType, HasRelatedFeeType};
use crate::transaction::test_utils::account_invoke_tx;
use crate::utils::u64_from_usize;
use crate::versioned_constants::{ResourceCost, VersionedConstants};
Expand Down Expand Up @@ -234,7 +234,13 @@ fn test_discounted_gas_from_gas_vector_computation() {
BlockContext::create_for_testing().to_tx_context(&account_invoke_tx(invoke_tx_args! {}));
let gas_usage =
GasVector { l1_gas: GasAmount(100), l1_data_gas: GasAmount(2), ..Default::default() };
let actual_result = gas_usage.to_discounted_l1_gas(&tx_context);
let actual_result = gas_usage.to_discounted_l1_gas(
&tx_context
.block_context
.block_info
.gas_prices
.get_gas_prices_by_fee_type(&tx_context.tx_info.fee_type()),
);

let result_div_ceil = gas_usage.l1_gas
+ (gas_usage.l1_data_gas.nonzero_checked_mul(DEFAULT_ETH_L1_DATA_GAS_PRICE).unwrap())
Expand Down
17 changes: 6 additions & 11 deletions crates/blockifier/src/fee/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use starknet_api::core::ContractAddress;
use starknet_api::execution_resources::GasAmount;
use starknet_api::transaction::Fee;

use crate::context::TransactionContext;
use crate::blockifier::block::GasPricesForFeeType;
use crate::execution::call_info::{EventSummary, ExecutionSummary};
use crate::fee::eth_gas_constants;
use crate::fee::fee_utils::get_vm_resources_cost;
Expand All @@ -18,7 +18,6 @@ use crate::fee::gas_usage::{
};
use crate::state::cached_state::{StateChanges, StateChangesCount};
use crate::transaction::errors::TransactionFeeError;
use crate::transaction::objects::HasRelatedFeeType;
use crate::utils::u64_from_usize;
use crate::versioned_constants::{
resource_cost_to_u128_ratio,
Expand Down Expand Up @@ -375,21 +374,17 @@ impl GasVector {
/// 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 dicount
/// function does nothing.
pub fn to_discounted_l1_gas(&self, tx_context: &TransactionContext) -> GasAmount {
let gas_prices = &tx_context.block_context.block_info.gas_prices;
let fee_type = tx_context.tx_info.fee_type();
let gas_price = gas_prices.get_l1_gas_price_by_fee_type(&fee_type);
let data_gas_price = gas_prices.get_l1_data_gas_price_by_fee_type(&fee_type);
pub fn to_discounted_l1_gas(&self, gas_prices: &GasPricesForFeeType) -> GasAmount {
self.l1_gas
+ (self.l1_data_gas.nonzero_saturating_mul(data_gas_price))
.checked_div_ceil(gas_price)
+ (self.l1_data_gas.nonzero_saturating_mul(gas_prices.l1_data_gas_price))
.checked_div_ceil(gas_prices.l1_gas_price)
.unwrap_or_else(|| {
log::warn!(
"Discounted L1 gas cost overflowed: division of L1 data gas cost ({:?} * \
{:?}) by regular L1 gas price ({:?}) resulted in overflow.",
self.l1_data_gas,
data_gas_price,
gas_price
gas_prices.l1_data_gas_price,
gas_prices.l1_gas_price
);
GasAmount::MAX
})
Expand Down
8 changes: 7 additions & 1 deletion crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,13 @@ impl AccountTransaction {
ValidResourceBounds::L1Gas(l1_gas_resource_bounds) => vec![(
L1Gas,
l1_gas_resource_bounds,
minimal_gas_amount_vector.to_discounted_l1_gas(tx_context),
minimal_gas_amount_vector.to_discounted_l1_gas(
&tx_context
.block_context
.block_info
.gas_prices
.get_gas_prices_by_fee_type(&tx_context.tx_info.fee_type()),
),
block_info.gas_prices.get_l1_gas_price_by_fee_type(fee_type),
)],
ValidResourceBounds::AllResources(AllResourceBounds {
Expand Down

0 comments on commit e16d88b

Please sign in to comment.