Skip to content

Commit

Permalink
fix(blockifier): change l2 gas price type to nonzero u128
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Aug 15, 2024
1 parent e63fc33 commit 61ba588
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
33 changes: 23 additions & 10 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ use crate::abi::constants;
use crate::state::errors::StateError;
use crate::state::state_api::{State, StateResult};
use crate::transaction::objects::FeeType;
use crate::versioned_constants::ResourceCost;
use crate::versioned_constants::{VersionedConstants, VersionedConstantsOverrides};

#[cfg(test)]
#[path = "block_test.rs"]
pub mod block_test;
pub const L2_GAS_FOR_CAIRO_STEP: u128 = 100;
pub const CAIRO_STEPS_PER_L1_GAS: u128 = 400;
pub const L2_TO_L1_GAS_PRICE_RATIO: u128 = L2_GAS_FOR_CAIRO_STEP * CAIRO_STEPS_PER_L1_GAS;

#[derive(Clone, Debug)]
pub struct BlockInfo {
Expand All @@ -35,8 +32,8 @@ pub struct GasPrices {
strk_l1_gas_price: NonZeroU128, // In fri.
eth_l1_data_gas_price: NonZeroU128, // In wei.
strk_l1_data_gas_price: NonZeroU128, // In fri.
eth_l2_gas_price: ResourceCost, // In wei.
strk_l2_gas_price: ResourceCost, // In fri.
eth_l2_gas_price: NonZeroU128, // In wei.
strk_l2_gas_price: NonZeroU128, // In fri.
}

impl GasPrices {
Expand All @@ -46,9 +43,25 @@ impl GasPrices {
eth_l1_data_gas_price: NonZeroU128,
strk_l1_data_gas_price: NonZeroU128,
) -> Self {
let eth_l2_gas_price = ResourceCost::new(eth_l1_gas_price.into(), L2_TO_L1_GAS_PRICE_RATIO);
let strk_l2_gas_price =
ResourceCost::new(strk_l1_gas_price.into(), L2_TO_L1_GAS_PRICE_RATIO);
// TODO(Aner): get gas prices from python.
let eth_l2_gas_price = NonZeroU128::new(
VersionedConstants::get_versioned_constants(VersionedConstantsOverrides {
validate_max_n_steps: 0,
max_recursion_depth: 0,
versioned_constants_base_overrides: None,
})
.l1_to_l2_gas_price_conversion(eth_l1_gas_price.into()),
)
.expect("L1 to L2 price conversion error (Rust side).");
let strk_l2_gas_price = NonZeroU128::new(
VersionedConstants::get_versioned_constants(VersionedConstantsOverrides {
validate_max_n_steps: 0,
max_recursion_depth: 0,
versioned_constants_base_overrides: None,
})
.l1_to_l2_gas_price_conversion(strk_l1_gas_price.into()),
)
.expect("L1 to L2 price conversion error (Rust side).");
GasPrices {
eth_l1_gas_price,
strk_l1_gas_price,
Expand All @@ -73,7 +86,7 @@ impl GasPrices {
}
}

pub fn get_l2_gas_price_by_fee_type(&self, fee_type: &FeeType) -> ResourceCost {
pub fn get_l2_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonZeroU128 {
match fee_type {
FeeType::Strk => self.strk_l2_gas_price,
FeeType::Eth => self.eth_l2_gas_price,
Expand Down
8 changes: 8 additions & 0 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ impl VersionedConstants {
Self::get(StarknetVersion::Latest)
}

/// Converts from l1 gas cost to l2 gas cost with **upward rounding**
pub fn l1_to_l2_gas_price_conversion(&self, l1_gas_price: u128) -> u128 {
let l1_to_l2_gas_price_ratio: Ratio<u128> =
Ratio::new(1, u128::from(self.os_constants.gas_costs.step_gas_cost))
* self.vm_resource_fee_cost()["n_steps"];
*(l1_to_l2_gas_price_ratio * l1_gas_price).ceil().numer()
}

/// Returns the initial gas of any transaction to run with.
pub fn tx_initial_gas(&self) -> u64 {
let os_consts = &self.os_constants;
Expand Down

0 comments on commit 61ba588

Please sign in to comment.