Skip to content

Commit

Permalink
chore(blockifier): separate create and validate logic on gas prices
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Nov 25, 2024
1 parent 19f082e commit 8ed2ee0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 28 deletions.
46 changes: 26 additions & 20 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,40 +35,43 @@ pub struct BlockInfo {
#[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug)]
pub struct GasPrices {
eth_gas_prices: GasPriceVector, // In wei.
strk_gas_prices: GasPriceVector, // In fri.
pub eth_gas_prices: GasPriceVector, // In wei.
pub strk_gas_prices: GasPriceVector, // In fri.
}

impl GasPrices {
pub fn 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 {
/// 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(eth_l1_gas_price.into());
if GasPrice::from(eth_l2_gas_price) != expected_eth_l2_gas_price {
.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 {eth_l2_gas_price} does not match expected eth_l2_gas_price \
{expected_eth_l2_gas_price}."
"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(strk_l1_gas_price.into());
if GasPrice::from(strk_l2_gas_price) != expected_strk_l2_gas_price {
.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 {strk_l2_gas_price} does not match expected strk_l2_gas_price \
{expected_strk_l2_gas_price}."
"strk_l2_gas_price {} does not match expected strk_l2_gas_price {}.",
self.strk_gas_prices.l2_gas_price, expected_strk_l2_gas_price
)
}
}

Self {
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,
Expand All @@ -79,7 +82,10 @@ impl GasPrices {
l1_data_gas_price: strk_l1_data_gas_price,
l2_gas_price: strk_l2_gas_price,
},
}
};
gas_prices.validate_l2_gas_price();

gas_prices
}

pub fn get_l1_gas_price_by_fee_type(&self, fee_type: &FeeType) -> NonzeroGasPrice {
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
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::new(
block_context.block_info.gas_prices = GasPrices::validated_new(
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::new(
block_info.gas_prices = GasPrices::validated_new(
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::new(
block_info.gas_prices = GasPrices::validated_new(
huge_gas_price,
huge_gas_price,
huge_gas_price,
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/test_utils/struct_impls.rs
Original file line number Diff line number Diff line change
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::new(
gas_prices: GasPrices::validated_new(
DEFAULT_ETH_L1_GAS_PRICE,
DEFAULT_STRK_L1_GAS_PRICE,
DEFAULT_ETH_L1_DATA_GAS_PRICE,
Expand Down
2 changes: 1 addition & 1 deletion crates/native_blockifier/src/py_state_diff.rs
Original file line number Diff line number Diff line change
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::new(
gas_prices: GasPrices::validated_new(
NonzeroGasPrice::try_from(block_info.l1_gas_price.price_in_wei).map_err(|_| {
NativeBlockifierInputError::InvalidNativeBlockifierInputError(
InvalidNativeBlockifierInputError::InvalidL1GasPriceWei(
Expand Down
2 changes: 1 addition & 1 deletion crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
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::new(
gas_prices: GasPrices::validated_new(
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
2 changes: 1 addition & 1 deletion crates/starknet_batcher/src/block_builder.rs
Original file line number Diff line number Diff line change
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::new(tmp_val, tmp_val, tmp_val, tmp_val, tmp_val, tmp_val)
GasPrices::validated_new(tmp_val, tmp_val, tmp_val, tmp_val, tmp_val, tmp_val)
},
use_kzg_da: block_builder_config.use_kzg_da,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/starknet_gateway/src/rpc_objects.rs
Original file line number Diff line number Diff line change
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::new(
gas_prices: GasPrices::validated_new(
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 8ed2ee0

Please sign in to comment.