From f5c93b66d8003b786a10c8ce06f5d07fcb919140 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Sun, 24 Nov 2024 13:07:37 +0200 Subject: [PATCH] chore(blockifier): move GasPrices to snapi --- crates/blockifier/src/blockifier/block.rs | 120 ++++++++---------- crates/blockifier/src/fee/fee_test.rs | 8 +- .../blockifier/src/test_utils/struct_impls.rs | 4 +- crates/blockifier/src/transaction/objects.rs | 8 +- crates/native_blockifier/src/py_state_diff.rs | 4 +- crates/papyrus_execution/src/lib.rs | 4 +- crates/starknet_api/src/block.rs | 29 ++++- crates/starknet_api/src/transaction/fields.rs | 6 + crates/starknet_batcher/src/block_builder.rs | 4 +- crates/starknet_gateway/src/rpc_objects.rs | 4 +- 10 files changed, 99 insertions(+), 92 deletions(-) diff --git a/crates/blockifier/src/blockifier/block.rs b/crates/blockifier/src/blockifier/block.rs index 16f96cfe36..f90462fe3d 100644 --- a/crates/blockifier/src/blockifier/block.rs +++ b/crates/blockifier/src/blockifier/block.rs @@ -1,5 +1,6 @@ use log::warn; use serde::{Deserialize, Serialize}; +pub use starknet_api::block::GasPrices; use starknet_api::block::{ BlockHashAndNumber, BlockNumber, @@ -10,11 +11,11 @@ use starknet_api::block::{ }; use starknet_api::core::ContractAddress; use starknet_api::state::StorageKey; +use starknet_api::transaction::fields::FeeType; 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::VersionedConstants; #[cfg(test)] @@ -32,79 +33,58 @@ pub struct BlockInfo { pub use_kzg_da: bool, } -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct GasPrices { - pub eth_gas_prices: GasPriceVector, // In wei. - pub strk_gas_prices: GasPriceVector, // In fri. -} - -impl GasPrices { - /// Warns if the submitted gas prices do not match the expected gas prices. - fn validate_l2_gas_price(&self) { - // TODO(Aner): fix backwards compatibility. - let expected_eth_l2_gas_price = VersionedConstants::latest_constants() - .convert_l1_to_l2_gas_price_round_up(self.eth_gas_prices.l1_gas_price.into()); - if GasPrice::from(self.eth_gas_prices.l2_gas_price) != expected_eth_l2_gas_price { - // TODO!(Aner): change to panic! Requires fixing several tests. - warn!( - "eth_l2_gas_price {} does not match expected eth_l2_gas_price {}.", - self.eth_gas_prices.l2_gas_price, expected_eth_l2_gas_price - ) - } - let expected_strk_l2_gas_price = VersionedConstants::latest_constants() - .convert_l1_to_l2_gas_price_round_up(self.strk_gas_prices.l1_gas_price.into()); - if GasPrice::from(self.strk_gas_prices.l2_gas_price) != expected_strk_l2_gas_price { - // TODO!(Aner): change to panic! Requires fixing test_discounted_gas_overdraft - warn!( - "strk_l2_gas_price {} does not match expected strk_l2_gas_price {}.", - self.strk_gas_prices.l2_gas_price, expected_strk_l2_gas_price - ) - } - } - - pub fn validated_new( - eth_l1_gas_price: NonzeroGasPrice, - strk_l1_gas_price: NonzeroGasPrice, - eth_l1_data_gas_price: NonzeroGasPrice, - strk_l1_data_gas_price: NonzeroGasPrice, - eth_l2_gas_price: NonzeroGasPrice, - strk_l2_gas_price: NonzeroGasPrice, - ) -> Self { - let gas_prices = Self { - eth_gas_prices: GasPriceVector { - l1_gas_price: eth_l1_gas_price, - l1_data_gas_price: eth_l1_data_gas_price, - l2_gas_price: eth_l2_gas_price, - }, - strk_gas_prices: GasPriceVector { - l1_gas_price: strk_l1_gas_price, - l1_data_gas_price: strk_l1_data_gas_price, - l2_gas_price: strk_l2_gas_price, - }, - }; - gas_prices.validate_l2_gas_price(); - - gas_prices +/// Warns if the submitted gas prices do not match the expected gas prices. +fn validate_l2_gas_price(gas_prices: &GasPrices) { + // TODO(Aner): fix backwards compatibility. + let expected_eth_l2_gas_price = VersionedConstants::latest_constants() + .convert_l1_to_l2_gas_price_round_up( + gas_prices.get_gas_prices_by_fee_type(&FeeType::Eth).l1_gas_price.into(), + ); + let eth_l2_gas_price = gas_prices.get_gas_prices_by_fee_type(&FeeType::Eth).l2_gas_price; + if GasPrice::from(eth_l2_gas_price) != expected_eth_l2_gas_price { + // TODO!(Aner): change to panic! Requires fixing several tests. + warn!( + "eth_l2_gas_price {} does not match expected eth_l2_gas_price {}.", + eth_l2_gas_price, expected_eth_l2_gas_price + ) } - - pub fn get_l1_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice { - self.get_gas_prices_by_fee_type(fee_type).l1_gas_price - } - - pub fn get_l1_data_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice { - self.get_gas_prices_by_fee_type(fee_type).l1_data_gas_price + let expected_strk_l2_gas_price = VersionedConstants::latest_constants() + .convert_l1_to_l2_gas_price_round_up( + gas_prices.get_gas_prices_by_fee_type(&FeeType::Strk).l1_gas_price.into(), + ); + let strk_l2_gas_price = gas_prices.get_gas_prices_by_fee_type(&FeeType::Strk).l2_gas_price; + if GasPrice::from(strk_l2_gas_price) != expected_strk_l2_gas_price { + // TODO!(Aner): change to panic! Requires fixing test_discounted_gas_overdraft + warn!( + "strk_l2_gas_price {} does not match expected strk_l2_gas_price {}.", + strk_l2_gas_price, expected_strk_l2_gas_price + ) } +} - pub fn get_l2_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice { - self.get_gas_prices_by_fee_type(fee_type).l2_gas_price - } +pub fn gas_prices( + eth_l1_gas_price: NonzeroGasPrice, + strk_l1_gas_price: NonzeroGasPrice, + eth_l1_data_gas_price: NonzeroGasPrice, + strk_l1_data_gas_price: NonzeroGasPrice, + eth_l2_gas_price: NonzeroGasPrice, + strk_l2_gas_price: NonzeroGasPrice, +) -> GasPrices { + let gas_prices = GasPrices { + eth_gas_prices: GasPriceVector { + l1_gas_price: eth_l1_gas_price, + l1_data_gas_price: eth_l1_data_gas_price, + l2_gas_price: eth_l2_gas_price, + }, + strk_gas_prices: GasPriceVector { + l1_gas_price: strk_l1_gas_price, + l1_data_gas_price: strk_l1_data_gas_price, + l2_gas_price: strk_l2_gas_price, + }, + }; + validate_l2_gas_price(&gas_prices); - pub fn get_gas_prices_by_fee_type(&self, fee_type: &FeeType) -> &GasPriceVector { - match fee_type { - FeeType::Strk => &self.strk_gas_prices, - FeeType::Eth => &self.eth_gas_prices, - } - } + gas_prices } // Block pre-processing. diff --git a/crates/blockifier/src/fee/fee_test.rs b/crates/blockifier/src/fee/fee_test.rs index 2d090a7871..15956c9925 100644 --- a/crates/blockifier/src/fee/fee_test.rs +++ b/crates/blockifier/src/fee/fee_test.rs @@ -13,7 +13,7 @@ use starknet_api::transaction::fields::{ ValidResourceBounds, }; -use crate::blockifier::block::GasPrices; +use crate::blockifier::block::gas_prices; use crate::context::BlockContext; use crate::fee::fee_checks::{FeeCheckError, FeeCheckReportFields, PostExecutionReport}; use crate::fee::fee_utils::{get_fee_by_gas_vector, get_vm_resources_cost}; @@ -178,7 +178,7 @@ fn test_discounted_gas_overdraft( NonzeroGasPrice::try_from(data_gas_price).unwrap(), ); let mut block_context = BlockContext::create_for_account_testing(); - block_context.block_info.gas_prices = GasPrices::validated_new( + block_context.block_info.gas_prices = gas_prices( DEFAULT_ETH_L1_GAS_PRICE, gas_price, DEFAULT_ETH_L1_DATA_GAS_PRICE, @@ -313,7 +313,7 @@ fn test_get_fee_by_gas_vector_regression( #[case] expected_fee_strk: u128, ) { let mut block_info = BlockContext::create_for_account_testing().block_info; - block_info.gas_prices = GasPrices::validated_new( + block_info.gas_prices = gas_prices( 1_u8.try_into().unwrap(), 2_u8.try_into().unwrap(), 3_u8.try_into().unwrap(), @@ -347,7 +347,7 @@ fn test_get_fee_by_gas_vector_overflow( ) { let huge_gas_price = NonzeroGasPrice::try_from(2_u128 * u128::from(u64::MAX)).unwrap(); let mut block_info = BlockContext::create_for_account_testing().block_info; - block_info.gas_prices = GasPrices::validated_new( + block_info.gas_prices = gas_prices( huge_gas_price, huge_gas_price, huge_gas_price, diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index dd40419dc5..7b5fc0d85b 100644 --- a/crates/blockifier/src/test_utils/struct_impls.rs +++ b/crates/blockifier/src/test_utils/struct_impls.rs @@ -21,7 +21,7 @@ use super::{ TEST_ERC20_CONTRACT_ADDRESS2, TEST_SEQUENCER_ADDRESS, }; -use crate::blockifier::block::{BlockInfo, GasPrices}; +use crate::blockifier::block::{gas_prices, BlockInfo}; use crate::bouncer::{BouncerConfig, BouncerWeights, BuiltinCount}; use crate::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; use crate::execution::call_info::{CallExecution, CallInfo, Retdata}; @@ -152,7 +152,7 @@ impl BlockInfo { block_number: BlockNumber(CURRENT_BLOCK_NUMBER), block_timestamp: BlockTimestamp(CURRENT_BLOCK_TIMESTAMP), sequencer_address: contract_address!(TEST_SEQUENCER_ADDRESS), - gas_prices: GasPrices::validated_new( + gas_prices: gas_prices( DEFAULT_ETH_L1_GAS_PRICE, DEFAULT_STRK_L1_GAS_PRICE, DEFAULT_ETH_L1_DATA_GAS_PRICE, diff --git a/crates/blockifier/src/transaction/objects.rs b/crates/blockifier/src/transaction/objects.rs index 06e0e89835..66e58aa5b5 100644 --- a/crates/blockifier/src/transaction/objects.rs +++ b/crates/blockifier/src/transaction/objects.rs @@ -5,6 +5,7 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::execution_resources::GasVector; +pub use starknet_api::transaction::fields::FeeType; use starknet_api::transaction::fields::{ AccountDeploymentData, AllResourceBounds, @@ -22,7 +23,6 @@ use starknet_api::transaction::{ TransactionOptions, TransactionVersion, }; -use strum_macros::EnumIter; use crate::abi::constants as abi_constants; use crate::blockifier::block::BlockInfo; @@ -279,12 +279,6 @@ pub trait HasRelatedFeeType { } } -#[derive(Clone, Copy, Hash, EnumIter, Eq, PartialEq)] -pub enum FeeType { - Strk, - Eth, -} - pub trait TransactionInfoCreator { fn create_tx_info(&self) -> TransactionInfo; } diff --git a/crates/native_blockifier/src/py_state_diff.rs b/crates/native_blockifier/src/py_state_diff.rs index 6a78360363..74d5afc8b9 100644 --- a/crates/native_blockifier/src/py_state_diff.rs +++ b/crates/native_blockifier/src/py_state_diff.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::convert::TryFrom; -use blockifier::blockifier::block::{BlockInfo, GasPrices}; +use blockifier::blockifier::block::{gas_prices, BlockInfo}; use blockifier::state::cached_state::CommitmentStateDiff; use blockifier::test_utils::{ DEFAULT_ETH_L1_DATA_GAS_PRICE, @@ -182,7 +182,7 @@ impl TryFrom for BlockInfo { block_number: BlockNumber(block_info.block_number), block_timestamp: BlockTimestamp(block_info.block_timestamp), sequencer_address: ContractAddress::try_from(block_info.sequencer_address.0)?, - gas_prices: GasPrices::validated_new( + gas_prices: gas_prices( NonzeroGasPrice::try_from(block_info.l1_gas_price.price_in_wei).map_err(|_| { NativeBlockifierInputError::InvalidNativeBlockifierInputError( InvalidNativeBlockifierInputError::InvalidL1GasPriceWei( diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index d997f58f04..637df74f99 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -23,7 +23,7 @@ use std::cell::Cell; use std::collections::BTreeMap; use std::sync::{Arc, LazyLock}; -use blockifier::blockifier::block::{pre_process_block, BlockInfo, GasPrices}; +use blockifier::blockifier::block::{gas_prices, pre_process_block, BlockInfo}; use blockifier::bouncer::BouncerConfig; use blockifier::context::{BlockContext, ChainInfo, FeeTokenAddresses, TransactionContext}; use blockifier::execution::call_info::CallExecution; @@ -369,7 +369,7 @@ fn create_block_context( use_kzg_da, block_number, // TODO(yair): What to do about blocks pre 0.13.1 where the data gas price were 0? - gas_prices: GasPrices::validated_new( + gas_prices: gas_prices( NonzeroGasPrice::new(l1_gas_price.price_in_wei).unwrap_or(NonzeroGasPrice::MIN), NonzeroGasPrice::new(l1_gas_price.price_in_fri).unwrap_or(NonzeroGasPrice::MIN), NonzeroGasPrice::new(l1_data_gas_price.price_in_wei).unwrap_or(NonzeroGasPrice::MIN), diff --git a/crates/starknet_api/src/block.rs b/crates/starknet_api/src/block.rs index 32dde80eac..46d0b42c80 100644 --- a/crates/starknet_api/src/block.rs +++ b/crates/starknet_api/src/block.rs @@ -21,7 +21,7 @@ use crate::data_availability::L1DataAvailabilityMode; use crate::execution_resources::GasAmount; use crate::hash::StarkHash; use crate::serde_utils::{BytesAsHex, PrefixedBytesAsHex}; -use crate::transaction::fields::Fee; +use crate::transaction::fields::{Fee, FeeType}; use crate::transaction::{Transaction, TransactionHash, TransactionOutput}; use crate::StarknetApiError; @@ -444,6 +444,33 @@ pub struct GasPriceVector { pub l2_gas_price: NonzeroGasPrice, } +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct GasPrices { + pub eth_gas_prices: GasPriceVector, // In wei. + pub strk_gas_prices: GasPriceVector, // In fri. +} + +impl GasPrices { + pub fn get_l1_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice { + self.get_gas_prices_by_fee_type(fee_type).l1_gas_price + } + + pub fn get_l1_data_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice { + self.get_gas_prices_by_fee_type(fee_type).l1_data_gas_price + } + + pub fn get_l2_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice { + self.get_gas_prices_by_fee_type(fee_type).l2_gas_price + } + + pub fn get_gas_prices_by_fee_type(&self, fee_type: &FeeType) -> &GasPriceVector { + match fee_type { + FeeType::Strk => &self.strk_gas_prices, + FeeType::Eth => &self.eth_gas_prices, + } + } +} + /// The timestamp of a [Block](`crate::block::Block`). #[derive( Debug, Default, Copy, Clone, Eq, PartialEq, Hash, Deserialize, Serialize, PartialOrd, Ord, diff --git a/crates/starknet_api/src/transaction/fields.rs b/crates/starknet_api/src/transaction/fields.rs index 5e1df8e112..5caa3d80d9 100644 --- a/crates/starknet_api/src/transaction/fields.rs +++ b/crates/starknet_api/src/transaction/fields.rs @@ -464,3 +464,9 @@ impl AccountDeploymentData { self.0.is_empty() } } + +#[derive(Clone, Copy, Hash, EnumIter, Eq, PartialEq)] +pub enum FeeType { + Strk, + Eth, +} diff --git a/crates/starknet_batcher/src/block_builder.rs b/crates/starknet_batcher/src/block_builder.rs index ad0ead7cc2..e7867e2cb4 100644 --- a/crates/starknet_batcher/src/block_builder.rs +++ b/crates/starknet_batcher/src/block_builder.rs @@ -1,7 +1,7 @@ use std::collections::BTreeMap; use async_trait::async_trait; -use blockifier::blockifier::block::{BlockInfo, GasPrices}; +use blockifier::blockifier::block::{gas_prices, BlockInfo}; use blockifier::blockifier::config::TransactionExecutorConfig; use blockifier::blockifier::transaction_executor::{ TransactionExecutor, @@ -312,7 +312,7 @@ impl BlockBuilderFactory { // TODO (yael 7/10/2024): add logic to compute gas prices gas_prices: { let tmp_val = NonzeroGasPrice::MIN; - GasPrices::validated_new(tmp_val, tmp_val, tmp_val, tmp_val, tmp_val, tmp_val) + gas_prices(tmp_val, tmp_val, tmp_val, tmp_val, tmp_val, tmp_val) }, use_kzg_da: block_builder_config.use_kzg_da, }; diff --git a/crates/starknet_gateway/src/rpc_objects.rs b/crates/starknet_gateway/src/rpc_objects.rs index 7b539ab1b6..3baaaf27eb 100644 --- a/crates/starknet_gateway/src/rpc_objects.rs +++ b/crates/starknet_gateway/src/rpc_objects.rs @@ -1,4 +1,4 @@ -use blockifier::blockifier::block::{BlockInfo, GasPrices}; +use blockifier::blockifier::block::{gas_prices, BlockInfo}; use serde::{Deserialize, Serialize}; use serde_json::Value; use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp, GasPrice, NonzeroGasPrice}; @@ -86,7 +86,7 @@ impl TryInto for BlockHeader { block_number: self.block_number, sequencer_address: self.sequencer_address, block_timestamp: self.timestamp, - gas_prices: GasPrices::validated_new( + gas_prices: gas_prices( parse_gas_price(self.l1_gas_price.price_in_wei)?, parse_gas_price(self.l1_gas_price.price_in_fri)?, parse_gas_price(self.l1_data_gas_price.price_in_wei)?,