From fdf402c275f99d6634a596b789e895adc51eca13 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 | 117 +++++++----------- .../src/concurrency/fee_utils_test.rs | 2 +- crates/blockifier/src/context.rs | 3 +- crates/blockifier/src/fee/fee_checks.rs | 3 +- crates/blockifier/src/fee/fee_test.rs | 11 +- crates/blockifier/src/fee/fee_utils.rs | 3 +- crates/blockifier/src/fee/gas_usage_test.rs | 3 +- .../src/test_utils/initial_test_state.rs | 2 +- crates/blockifier/src/test_utils/prices.rs | 2 +- .../blockifier/src/test_utils/struct_impls.rs | 4 +- .../transaction/account_transactions_test.rs | 4 +- .../src/transaction/execution_flavors_test.rs | 3 +- crates/blockifier/src/transaction/objects.rs | 8 +- .../src/transaction/post_execution_test.rs | 3 +- .../blockifier/src/transaction/test_utils.rs | 4 +- .../src/transaction/transactions_test.rs | 3 +- crates/native_blockifier/src/py_state_diff.rs | 4 +- crates/papyrus_execution/src/lib.rs | 4 +- crates/papyrus_execution/src/objects.rs | 4 +- crates/starknet_api/src/block.rs | 34 +++++ crates/starknet_batcher/src/block_builder.rs | 4 +- crates/starknet_gateway/src/rpc_objects.rs | 4 +- .../src/state_reader.rs | 2 +- 23 files changed, 116 insertions(+), 115 deletions(-) diff --git a/crates/blockifier/src/blockifier/block.rs b/crates/blockifier/src/blockifier/block.rs index 4d3304666e..59a8a1ec0f 100644 --- a/crates/blockifier/src/blockifier/block.rs +++ b/crates/blockifier/src/blockifier/block.rs @@ -6,6 +6,7 @@ use starknet_api::block::{ BlockTimestamp, GasPrice, GasPriceVector, + GasPrices, NonzeroGasPrice, }; use starknet_api::core::ContractAddress; @@ -14,7 +15,6 @@ use starknet_api::state::StorageKey; 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,81 +32,54 @@ 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 eth_l2_gas_price = self.eth_gas_prices.l2_gas_price; - 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(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 - ) - } - let strk_l2_gas_price = self.strk_gas_prices.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(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 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 eth_l2_gas_price = gas_prices.eth_gas_prices.l2_gas_price; + let expected_eth_l2_gas_price = VersionedConstants::latest_constants() + .convert_l1_to_l2_gas_price_round_up(gas_prices.eth_gas_prices.l1_gas_price.into()); + 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 l1_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice { - self.gas_price_vector(fee_type).l1_gas_price - } - - pub fn l1_data_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice { - self.gas_price_vector(fee_type).l1_data_gas_price + let strk_l2_gas_price = gas_prices.strk_gas_prices.l2_gas_price; + let expected_strk_l2_gas_price = VersionedConstants::latest_constants() + .convert_l1_to_l2_gas_price_round_up(gas_prices.strk_gas_prices.l1_gas_price.into()); + 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 l2_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice { - self.gas_price_vector(fee_type).l2_gas_price - } +pub fn validated_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 gas_price_vector(&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/concurrency/fee_utils_test.rs b/crates/blockifier/src/concurrency/fee_utils_test.rs index d5884c4530..f136a8f491 100644 --- a/crates/blockifier/src/concurrency/fee_utils_test.rs +++ b/crates/blockifier/src/concurrency/fee_utils_test.rs @@ -1,5 +1,6 @@ use num_bigint::BigUint; use rstest::rstest; +use starknet_api::block::FeeType; use starknet_api::transaction::fields::{Fee, ValidResourceBounds}; use starknet_api::{felt, invoke_tx_args}; use starknet_types_core::felt::Felt; @@ -12,7 +13,6 @@ use crate::state::state_api::StateReader; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::{fund_account, test_state, test_state_inner}; use crate::test_utils::{create_trivial_calldata, CairoVersion, BALANCE}; -use crate::transaction::objects::FeeType; use crate::transaction::test_utils::{ account_invoke_tx, block_context, diff --git a/crates/blockifier/src/context.rs b/crates/blockifier/src/context.rs index 5c6700b9d0..069fcb53e6 100644 --- a/crates/blockifier/src/context.rs +++ b/crates/blockifier/src/context.rs @@ -3,7 +3,7 @@ use std::collections::BTreeMap; use papyrus_config::dumping::{append_sub_config_name, ser_param, SerializeConfig}; use papyrus_config::{ParamPath, ParamPrivacyInput, SerializedParam}; use serde::{Deserialize, Serialize}; -use starknet_api::block::GasPriceVector; +use starknet_api::block::{FeeType, GasPriceVector}; use starknet_api::core::{ChainId, ContractAddress}; use starknet_api::transaction::fields::{ AllResourceBounds, @@ -15,7 +15,6 @@ use crate::blockifier::block::BlockInfo; use crate::bouncer::BouncerConfig; use crate::transaction::objects::{ CurrentTransactionInfo, - FeeType, HasRelatedFeeType, TransactionInfo, TransactionInfoCreator, diff --git a/crates/blockifier/src/fee/fee_checks.rs b/crates/blockifier/src/fee/fee_checks.rs index f5bc38a2bd..2657fffed8 100644 --- a/crates/blockifier/src/fee/fee_checks.rs +++ b/crates/blockifier/src/fee/fee_checks.rs @@ -1,3 +1,4 @@ +use starknet_api::block::FeeType; use starknet_api::execution_resources::{GasAmount, GasVector}; use starknet_api::transaction::fields::Resource::{self, L1DataGas, L1Gas, L2Gas}; use starknet_api::transaction::fields::{ @@ -14,7 +15,7 @@ use crate::fee::fee_utils::{get_balance_and_if_covers_fee, get_fee_by_gas_vector use crate::fee::receipt::TransactionReceipt; use crate::state::state_api::StateReader; use crate::transaction::errors::TransactionExecutionError; -use crate::transaction::objects::{FeeType, TransactionExecutionResult, TransactionInfo}; +use crate::transaction::objects::{TransactionExecutionResult, TransactionInfo}; #[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Copy, Debug, Error, PartialEq)] diff --git a/crates/blockifier/src/fee/fee_test.rs b/crates/blockifier/src/fee/fee_test.rs index 2d090a7871..98ad761ef4 100644 --- a/crates/blockifier/src/fee/fee_test.rs +++ b/crates/blockifier/src/fee/fee_test.rs @@ -1,7 +1,7 @@ use assert_matches::assert_matches; use cairo_vm::types::builtin_name::BuiltinName; use rstest::rstest; -use starknet_api::block::{GasPrice, NonzeroGasPrice}; +use starknet_api::block::{FeeType, GasPrice, NonzeroGasPrice}; use starknet_api::execution_resources::{GasAmount, GasVector}; use starknet_api::invoke_tx_args; use starknet_api::transaction::fields::{ @@ -13,7 +13,7 @@ use starknet_api::transaction::fields::{ ValidResourceBounds, }; -use crate::blockifier::block::GasPrices; +use crate::blockifier::block::validated_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}; @@ -32,7 +32,6 @@ use crate::test_utils::{ DEFAULT_L2_GAS_MAX_AMOUNT, DEFAULT_STRK_L1_GAS_PRICE, }; -use crate::transaction::objects::FeeType; use crate::transaction::test_utils::{ account_invoke_tx, all_resource_bounds, @@ -178,7 +177,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 = validated_gas_prices( DEFAULT_ETH_L1_GAS_PRICE, gas_price, DEFAULT_ETH_L1_DATA_GAS_PRICE, @@ -313,7 +312,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 = validated_gas_prices( 1_u8.try_into().unwrap(), 2_u8.try_into().unwrap(), 3_u8.try_into().unwrap(), @@ -347,7 +346,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 = validated_gas_prices( huge_gas_price, huge_gas_price, huge_gas_price, diff --git a/crates/blockifier/src/fee/fee_utils.rs b/crates/blockifier/src/fee/fee_utils.rs index e217d3d6b2..32c12710b0 100644 --- a/crates/blockifier/src/fee/fee_utils.rs +++ b/crates/blockifier/src/fee/fee_utils.rs @@ -4,6 +4,7 @@ use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_bigint::BigUint; use starknet_api::abi::abi_utils::get_fee_token_var_address; +use starknet_api::block::FeeType; use starknet_api::core::ContractAddress; use starknet_api::execution_resources::GasVector; use starknet_api::state::StorageKey; @@ -16,7 +17,7 @@ use crate::context::{BlockContext, TransactionContext}; use crate::fee::resources::TransactionFeeResult; use crate::state::state_api::StateReader; use crate::transaction::errors::TransactionFeeError; -use crate::transaction::objects::{ExecutionResourcesTraits, FeeType, TransactionInfo}; +use crate::transaction::objects::{ExecutionResourcesTraits, TransactionInfo}; use crate::utils::u64_from_usize; use crate::versioned_constants::VersionedConstants; diff --git a/crates/blockifier/src/fee/gas_usage_test.rs b/crates/blockifier/src/fee/gas_usage_test.rs index 9d1bcb0f34..25dfb09958 100644 --- a/crates/blockifier/src/fee/gas_usage_test.rs +++ b/crates/blockifier/src/fee/gas_usage_test.rs @@ -3,7 +3,7 @@ use std::sync::Arc; use num_rational::Ratio; use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; -use starknet_api::block::StarknetVersion; +use starknet_api::block::{FeeType, StarknetVersion}; use starknet_api::execution_resources::{GasAmount, GasVector}; use starknet_api::invoke_tx_args; use starknet_api::transaction::fields::GasVectorComputationMode; @@ -28,7 +28,6 @@ use crate::test_utils::{ DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE, }; -use crate::transaction::objects::FeeType; use crate::transaction::test_utils::account_invoke_tx; use crate::utils::u64_from_usize; use crate::versioned_constants::{ResourceCost, VersionedConstants, VmResourceCosts}; diff --git a/crates/blockifier/src/test_utils/initial_test_state.rs b/crates/blockifier/src/test_utils/initial_test_state.rs index 094fdca6ad..0beeb1a36d 100644 --- a/crates/blockifier/src/test_utils/initial_test_state.rs +++ b/crates/blockifier/src/test_utils/initial_test_state.rs @@ -1,6 +1,7 @@ use std::collections::HashMap; use starknet_api::abi::abi_utils::get_fee_token_var_address; +use starknet_api::block::FeeType; use starknet_api::core::ContractAddress; use starknet_api::felt; use starknet_api::transaction::fields::Fee; @@ -11,7 +12,6 @@ use crate::state::cached_state::CachedState; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::dict_state_reader::DictStateReader; use crate::test_utils::CairoVersion; -use crate::transaction::objects::FeeType; /// Utility to fund an account. pub fn fund_account( diff --git a/crates/blockifier/src/test_utils/prices.rs b/crates/blockifier/src/test_utils/prices.rs index 47fba5e7eb..5b8fee6616 100644 --- a/crates/blockifier/src/test_utils/prices.rs +++ b/crates/blockifier/src/test_utils/prices.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use cached::proc_macro::cached; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use starknet_api::abi::abi_utils::{get_fee_token_var_address, selector_from_name}; +use starknet_api::block::FeeType; use starknet_api::core::ContractAddress; use starknet_api::test_utils::invoke::InvokeTxArgs; use starknet_api::transaction::constants; @@ -14,7 +15,6 @@ use crate::execution::entry_point::{CallEntryPoint, EntryPointExecutionContext}; use crate::state::state_api::State; use crate::test_utils::initial_test_state::test_state; use crate::test_utils::BALANCE; -use crate::transaction::objects::FeeType; use crate::transaction::test_utils::account_invoke_tx; /// Enum for all resource costs. diff --git a/crates/blockifier/src/test_utils/struct_impls.rs b/crates/blockifier/src/test_utils/struct_impls.rs index af42fb4eb8..71d64061f9 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::{validated_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: validated_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/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 8f901664bf..f677ada05f 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -11,7 +11,7 @@ use starknet_api::abi::abi_utils::{ get_storage_var_address, selector_from_name, }; -use starknet_api::block::GasPrice; +use starknet_api::block::{FeeType, GasPrice}; use starknet_api::core::{calculate_contract_address, ClassHash, ContractAddress}; use starknet_api::executable_transaction::{ AccountTransaction as ApiExecutableTransaction, @@ -84,7 +84,7 @@ use crate::test_utils::{ MAX_FEE, }; use crate::transaction::account_transaction::AccountTransaction; -use crate::transaction::objects::{FeeType, HasRelatedFeeType, TransactionInfoCreator}; +use crate::transaction::objects::{HasRelatedFeeType, TransactionInfoCreator}; use crate::transaction::test_utils::{ account_invoke_tx, all_resource_bounds, diff --git a/crates/blockifier/src/transaction/execution_flavors_test.rs b/crates/blockifier/src/transaction/execution_flavors_test.rs index edf7058021..fbae899ebf 100644 --- a/crates/blockifier/src/transaction/execution_flavors_test.rs +++ b/crates/blockifier/src/transaction/execution_flavors_test.rs @@ -1,6 +1,7 @@ use assert_matches::assert_matches; use pretty_assertions::assert_eq; use rstest::rstest; +use starknet_api::block::FeeType; use starknet_api::core::ContractAddress; use starknet_api::execution_resources::{GasAmount, GasVector}; use starknet_api::test_utils::invoke::InvokeTxArgs; @@ -41,7 +42,7 @@ use crate::transaction::errors::{ TransactionFeeError, TransactionPreValidationError, }; -use crate::transaction::objects::{FeeType, TransactionExecutionInfo, TransactionExecutionResult}; +use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult}; use crate::transaction::test_utils::{ account_invoke_tx, default_l1_resource_bounds, diff --git a/crates/blockifier/src/transaction/objects.rs b/crates/blockifier/src/transaction/objects.rs index 06e0e89835..0ce713a6a0 100644 --- a/crates/blockifier/src/transaction/objects.rs +++ b/crates/blockifier/src/transaction/objects.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; +use starknet_api::block::FeeType; use starknet_api::core::{ContractAddress, Nonce}; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::execution_resources::GasVector; @@ -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/blockifier/src/transaction/post_execution_test.rs b/crates/blockifier/src/transaction/post_execution_test.rs index 2b93fc5bc0..5ec93ed343 100644 --- a/crates/blockifier/src/transaction/post_execution_test.rs +++ b/crates/blockifier/src/transaction/post_execution_test.rs @@ -1,5 +1,6 @@ use assert_matches::assert_matches; use rstest::rstest; +use starknet_api::block::FeeType; use starknet_api::core::ContractAddress; use starknet_api::execution_resources::GasAmount; use starknet_api::state::StorageKey; @@ -31,7 +32,7 @@ use crate::test_utils::{ }; use crate::transaction::account_transaction::AccountTransaction; use crate::transaction::errors::TransactionExecutionError; -use crate::transaction::objects::{FeeType, HasRelatedFeeType, TransactionInfoCreator}; +use crate::transaction::objects::{HasRelatedFeeType, TransactionInfoCreator}; use crate::transaction::test_utils::{ account_invoke_tx, block_context, diff --git a/crates/blockifier/src/transaction/test_utils.rs b/crates/blockifier/src/transaction/test_utils.rs index fdefb138e6..36757124a4 100644 --- a/crates/blockifier/src/transaction/test_utils.rs +++ b/crates/blockifier/src/transaction/test_utils.rs @@ -1,6 +1,6 @@ use rstest::fixture; use starknet_api::abi::abi_utils::get_fee_token_var_address; -use starknet_api::block::GasPrice; +use starknet_api::block::{FeeType, GasPrice}; use starknet_api::contract_class::{ClassInfo, ContractClass}; use starknet_api::core::{ClassHash, ContractAddress, Nonce}; use starknet_api::execution_resources::GasAmount; @@ -43,7 +43,7 @@ use crate::test_utils::{ MAX_FEE, }; use crate::transaction::account_transaction::AccountTransaction; -use crate::transaction::objects::{FeeType, TransactionExecutionInfo, TransactionExecutionResult}; +use crate::transaction::objects::{TransactionExecutionInfo, TransactionExecutionResult}; use crate::transaction::transaction_types::TransactionType; use crate::transaction::transactions::ExecutableTransaction; diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 1d69557692..5933e4b076 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -13,7 +13,7 @@ use starknet_api::abi::abi_utils::{ selector_from_name, }; use starknet_api::abi::constants::CONSTRUCTOR_ENTRY_POINT_NAME; -use starknet_api::block::GasPriceVector; +use starknet_api::block::{FeeType, GasPriceVector}; use starknet_api::contract_class::EntryPointType; use starknet_api::core::{ChainId, ClassHash, ContractAddress, EthAddress, Nonce}; use starknet_api::executable_transaction::AccountTransaction as ApiExecutableTransaction; @@ -123,7 +123,6 @@ use crate::transaction::errors::{ TransactionPreValidationError, }; use crate::transaction::objects::{ - FeeType, HasRelatedFeeType, TransactionExecutionInfo, TransactionInfo, diff --git a/crates/native_blockifier/src/py_state_diff.rs b/crates/native_blockifier/src/py_state_diff.rs index 6a78360363..70ea8fb9d1 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::{validated_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: validated_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..6ed407c2dd 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::{pre_process_block, validated_gas_prices, 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: validated_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/papyrus_execution/src/objects.rs b/crates/papyrus_execution/src/objects.rs index d44c769310..8be701059e 100644 --- a/crates/papyrus_execution/src/objects.rs +++ b/crates/papyrus_execution/src/objects.rs @@ -9,7 +9,7 @@ use blockifier::execution::call_info::{ Retdata as BlockifierRetdata, }; use blockifier::execution::entry_point::CallType as BlockifierCallType; -use blockifier::transaction::objects::{FeeType, TransactionExecutionInfo}; +use blockifier::transaction::objects::TransactionExecutionInfo; use blockifier::utils::u64_from_usize; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources as VmExecutionResources; @@ -23,7 +23,7 @@ use papyrus_common::state::{ StorageEntry, }; use serde::{Deserialize, Serialize}; -use starknet_api::block::{BlockTimestamp, GasPrice, GasPricePerToken}; +use starknet_api::block::{BlockTimestamp, FeeType, GasPrice, GasPricePerToken}; use starknet_api::contract_class::EntryPointType; use starknet_api::core::{ ClassHash, diff --git a/crates/starknet_api/src/block.rs b/crates/starknet_api/src/block.rs index 32dde80eac..2e89212e8e 100644 --- a/crates/starknet_api/src/block.rs +++ b/crates/starknet_api/src/block.rs @@ -6,6 +6,7 @@ use itertools::Itertools; use serde::{Deserialize, Serialize}; use starknet_types_core::felt::Felt; use starknet_types_core::hash::{Poseidon, StarkHash as CoreStarkHash}; +use strum_macros::EnumIter; use crate::core::{ EventCommitment, @@ -444,6 +445,39 @@ pub struct GasPriceVector { pub l2_gas_price: NonzeroGasPrice, } +#[derive(Clone, Copy, Hash, EnumIter, Eq, PartialEq)] +pub enum FeeType { + Strk, + Eth, +} + +#[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 l1_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice { + self.gas_price_vector(fee_type).l1_gas_price + } + + pub fn l1_data_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice { + self.gas_price_vector(fee_type).l1_data_gas_price + } + + pub fn l2_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice { + self.gas_price_vector(fee_type).l2_gas_price + } + + pub fn gas_price_vector(&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_batcher/src/block_builder.rs b/crates/starknet_batcher/src/block_builder.rs index 94a8fbb69a..0e72c761ff 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::{validated_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) + validated_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 1209a4f459..5333f6e1cc 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::{validated_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: validated_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)?, diff --git a/crates/starknet_integration_tests/src/state_reader.rs b/crates/starknet_integration_tests/src/state_reader.rs index 5015d52629..ca88fb9cb3 100644 --- a/crates/starknet_integration_tests/src/state_reader.rs +++ b/crates/starknet_integration_tests/src/state_reader.rs @@ -12,7 +12,6 @@ use blockifier::test_utils::{ DEFAULT_STRK_L1_GAS_PRICE, TEST_SEQUENCER_ADDRESS, }; -use blockifier::transaction::objects::FeeType; use blockifier::versioned_constants::VersionedConstants; use cairo_lang_starknet_classes::casm_contract_class::CasmContractClass; use indexmap::IndexMap; @@ -33,6 +32,7 @@ use starknet_api::block::{ BlockHeaderWithoutHash, BlockNumber, BlockTimestamp, + FeeType, GasPricePerToken, }; use starknet_api::core::{ChainId, ClassHash, ContractAddress, Nonce, SequencerContractAddress};