Skip to content

Commit

Permalink
chore(blockifier): move GasPrices to snapi
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Nov 25, 2024
1 parent 3fe4ed2 commit 4139197
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 115 deletions.
117 changes: 45 additions & 72 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use starknet_api::block::{
BlockTimestamp,
GasPrice,
GasPriceVector,
GasPrices,
NonzeroGasPrice,
};
use starknet_api::core::ContractAddress;
Expand All @@ -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)]
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/concurrency/fee_utils_test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,7 +15,6 @@ use crate::blockifier::block::BlockInfo;
use crate::bouncer::BouncerConfig;
use crate::transaction::objects::{
CurrentTransactionInfo,
FeeType,
HasRelatedFeeType,
TransactionInfo,
TransactionInfoCreator,
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/fee/fee_checks.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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)]
Expand Down
11 changes: 5 additions & 6 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
@@ -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::{
Expand All @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/initial_test_state.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/transaction/execution_flavors_test.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 1 addition & 7 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Loading

0 comments on commit 4139197

Please sign in to comment.