From 61ba5887a1d2ce3ddbacb2a6f69c9d36d13df55e Mon Sep 17 00:00:00 2001 From: Aner Ben Efraim Date: Wed, 14 Aug 2024 08:27:25 +0300 Subject: [PATCH] fix(blockifier): change l2 gas price type to nonzero u128 --- crates/blockifier/src/blockifier/block.rs | 33 ++++++++++++++------ crates/blockifier/src/versioned_constants.rs | 8 +++++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/crates/blockifier/src/blockifier/block.rs b/crates/blockifier/src/blockifier/block.rs index 060e17666e..0ac14eecb6 100644 --- a/crates/blockifier/src/blockifier/block.rs +++ b/crates/blockifier/src/blockifier/block.rs @@ -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 { @@ -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 { @@ -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, @@ -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, diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index a975ab0e98..d6bb930e33 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -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 = + 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;