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 2038d5b commit d0e9ff5
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 92 deletions.
120 changes: 50 additions & 70 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use log::warn;
use serde::{Deserialize, Serialize};
pub use starknet_api::block::GasPrices;
use starknet_api::block::{
BlockHashAndNumber,
BlockNumber,
Expand All @@ -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)]
Expand All @@ -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.gas_price_vector(&FeeType::Eth).l1_gas_price.into(),
);
let eth_l2_gas_price = gas_prices.gas_price_vector(&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 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 expected_strk_l2_gas_price = VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(
gas_prices.gas_price_vector(&FeeType::Strk).l1_gas_price.into(),
);
let strk_l2_gas_price = gas_prices.gas_price_vector(&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 l2_gas_price(&self, fee_type: &FeeType) -> NonzeroGasPrice {
self.gas_price_vector(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 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
8 changes: 4 additions & 4 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
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::{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: gas_prices(
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
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 @@ -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,
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
4 changes: 2 additions & 2 deletions crates/native_blockifier/src/py_state_diff.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -182,7 +182,7 @@ impl TryFrom<PyBlockInfo> 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(
Expand Down
4 changes: 2 additions & 2 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down
29 changes: 28 additions & 1 deletion crates/starknet_api/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 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,
Expand Down
6 changes: 6 additions & 0 deletions crates/starknet_api/src/transaction/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,9 @@ impl AccountDeploymentData {
self.0.is_empty()
}
}

#[derive(Clone, Copy, Hash, EnumIter, Eq, PartialEq)]
pub enum FeeType {
Strk,
Eth,
}
4 changes: 2 additions & 2 deletions crates/starknet_batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
};
Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_gateway/src/rpc_objects.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -86,7 +86,7 @@ impl TryInto<BlockInfo> 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)?,
Expand Down

0 comments on commit d0e9ff5

Please sign in to comment.