Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(blockifier): add l2_gas_price to blockinfo #463

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::num::NonZeroU128;

use log::warn;
use starknet_api::block::{BlockHash, BlockNumber, BlockTimestamp};
use starknet_api::core::ContractAddress;
use starknet_api::state::StorageKey;
Expand All @@ -9,7 +10,7 @@ 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, VersionedConstantsOverrides};
use crate::versioned_constants::VersionedConstants;

#[cfg(test)]
#[path = "block_test.rs"]
Expand Down Expand Up @@ -42,26 +43,27 @@ impl GasPrices {
strk_l1_gas_price: NonZeroU128,
eth_l1_data_gas_price: NonZeroU128,
strk_l1_data_gas_price: NonZeroU128,
eth_l2_gas_price: NonZeroU128,
strk_l2_gas_price: NonZeroU128,
) -> Self {
// 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).");
// TODO(Aner): fix backwards compatibility.
let expected_eth_l2_gas_price = VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(eth_l1_gas_price.into());
if u128::from(eth_l2_gas_price) != expected_eth_l2_gas_price {
warn!(
"eth_l2_gas_price does not match expected! eth_l2_gas_price:{eth_l2_gas_price}, \
expected:{expected_eth_l2_gas_price}."
)
}
let expected_strk_l2_gas_price = VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(strk_l1_gas_price.into());
if u128::from(strk_l2_gas_price) != expected_strk_l2_gas_price {
warn!(
"strk_l2_gas_price does not match expected! \
strk_l2_gas_price:{strk_l2_gas_price}, expected:{expected_strk_l2_gas_price}."
)
}

GasPrices {
eth_l1_gas_price,
strk_l1_gas_price,
Expand Down
8 changes: 8 additions & 0 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ fn test_discounted_gas_overdraft(
gas_price.try_into().unwrap(),
DEFAULT_ETH_L1_DATA_GAS_PRICE.try_into().unwrap(),
data_gas_price.try_into().unwrap(),
VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(DEFAULT_ETH_L1_GAS_PRICE)
.try_into()
.unwrap(),
VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(gas_price)
.try_into()
.unwrap(),
);

let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0);
Expand Down
8 changes: 8 additions & 0 deletions crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,14 @@ impl BlockInfo {
DEFAULT_STRK_L1_GAS_PRICE.try_into().unwrap(),
DEFAULT_ETH_L1_DATA_GAS_PRICE.try_into().unwrap(),
DEFAULT_STRK_L1_DATA_GAS_PRICE.try_into().unwrap(),
VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(DEFAULT_ETH_L1_GAS_PRICE)
.try_into()
.unwrap(),
VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(DEFAULT_STRK_L1_GAS_PRICE)
.try_into()
.unwrap(),
),
use_kzg_da: false,
}
Expand Down
3 changes: 3 additions & 0 deletions crates/gateway/src/rpc_objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ impl TryInto<BlockInfo> for BlockHeader {
parse_gas_price(self.l1_gas_price.price_in_fri)?,
parse_gas_price(self.l1_data_gas_price.price_in_wei)?,
parse_gas_price(self.l1_data_gas_price.price_in_fri)?,
// TODO(Aner): add to BlockHeader and take the value from it.
NonZeroU128::MIN,
NonZeroU128::MIN,
),
use_kzg_da: matches!(self.l1_da_mode, L1DataAvailabilityMode::Blob),
})
Expand Down
12 changes: 8 additions & 4 deletions crates/native_blockifier/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ pub enum InvalidNativeBlockifierInputError {
#[error("Invalid builtin count: {0:?}.")]
InvalidBuiltinCounts(BuiltinCount),
#[error("Invalid Wei gas price: {0}.")]
InvalidGasPriceWei(u128),
InvalidL1GasPriceWei(u128),
#[error("Invalid Fri gas price: {0}.")]
InvalidGasPriceFri(u128),
InvalidL1GasPriceFri(u128),
#[error("Invalid Wei data gas price: {0}.")]
InvalidDataGasPriceWei(u128),
InvalidL1DataGasPriceWei(u128),
#[error("Invalid Fri data gas price: {0}.")]
InvalidDataGasPriceFri(u128),
InvalidL1DataGasPriceFri(u128),
#[error("Invalid Wei l2 gas price: {0}.")]
InvalidL2GasPriceWei(u128),
#[error("Invalid Fri l2 gas price: {0}.")]
InvalidL2GasPriceFri(u128),
}

create_exception!(native_blockifier, UndeclaredClassHashError, PyException);
30 changes: 26 additions & 4 deletions crates/native_blockifier/src/py_state_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use blockifier::test_utils::{
DEFAULT_STRK_L1_DATA_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
};
use blockifier::versioned_constants::VersionedConstants;
use indexmap::IndexMap;
use pyo3::prelude::*;
use pyo3::FromPyObject;
Expand Down Expand Up @@ -140,6 +141,7 @@ pub struct PyBlockInfo {
pub block_timestamp: u64,
pub l1_gas_price: PyResourcePrice,
pub l1_data_gas_price: PyResourcePrice,
pub l2_gas_price: PyResourcePrice,
pub sequencer_address: PyFelt,
pub use_kzg_da: bool,
}
Expand All @@ -158,6 +160,12 @@ impl Default for PyBlockInfo {
price_in_wei: DEFAULT_ETH_L1_DATA_GAS_PRICE,
price_in_fri: DEFAULT_STRK_L1_DATA_GAS_PRICE,
},
l2_gas_price: PyResourcePrice {
price_in_wei: VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(DEFAULT_ETH_L1_GAS_PRICE),
price_in_fri: VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(DEFAULT_STRK_L1_GAS_PRICE),
},
sequencer_address: PyFelt::default(),
use_kzg_da: bool::default(),
}
Expand All @@ -175,32 +183,46 @@ impl TryFrom<PyBlockInfo> for BlockInfo {
gas_prices: GasPrices::new(
block_info.l1_gas_price.price_in_wei.try_into().map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidGasPriceWei(
InvalidNativeBlockifierInputError::InvalidL1GasPriceWei(
block_info.l1_gas_price.price_in_wei,
),
)
})?,
block_info.l1_gas_price.price_in_fri.try_into().map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidGasPriceFri(
InvalidNativeBlockifierInputError::InvalidL1GasPriceFri(
block_info.l1_gas_price.price_in_fri,
),
)
})?,
block_info.l1_data_gas_price.price_in_wei.try_into().map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidDataGasPriceWei(
InvalidNativeBlockifierInputError::InvalidL1DataGasPriceWei(
block_info.l1_data_gas_price.price_in_wei,
),
)
})?,
block_info.l1_data_gas_price.price_in_fri.try_into().map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidDataGasPriceFri(
InvalidNativeBlockifierInputError::InvalidL1DataGasPriceFri(
block_info.l1_data_gas_price.price_in_fri,
),
)
})?,
block_info.l2_gas_price.price_in_wei.try_into().map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidL2GasPriceWei(
block_info.l2_gas_price.price_in_wei,
),
)
})?,
block_info.l2_gas_price.price_in_fri.try_into().map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidL2GasPriceFri(
block_info.l2_gas_price.price_in_fri,
),
)
})?,
),
use_kzg_da: block_info.use_kzg_da,
})
Expand Down
3 changes: 3 additions & 0 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ fn create_block_context(
NonZeroU128::new(l1_gas_price.price_in_fri.0).unwrap_or(NonZeroU128::MIN),
NonZeroU128::new(l1_data_gas_price.price_in_wei.0).unwrap_or(NonZeroU128::MIN),
NonZeroU128::new(l1_data_gas_price.price_in_fri.0).unwrap_or(NonZeroU128::MIN),
// TODO(Aner - Shahak): fix to come from pending_data/block_header.
NonZeroU128::MIN,
NonZeroU128::MIN,
),
};
let chain_info = ChainInfo {
Expand Down
Loading