Skip to content

Commit

Permalink
Bundle eth/strk gas prices in GasPrices (starkware-libs#894)
Browse files Browse the repository at this point in the history
Implemented exactly like with `FeeTokenAddresses`.
If this pattern is used once more, replace all three with a generic implementation.
  • Loading branch information
giladchase authored Sep 6, 2023
1 parent be7fb25 commit 1c6d2c0
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
31 changes: 19 additions & 12 deletions crates/blockifier/src/block_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ pub struct BlockContext {
pub sequencer_address: ContractAddress,
pub fee_token_addresses: FeeTokenAddresses,
pub vm_resource_fee_cost: Arc<HashMap<String, f64>>,
pub eth_l1_gas_price: u128, // In wei.
// TODO(Amos, 01/09/2023): NEW_TOKEN_SUPPORT use this gas price for V3 txs.
pub strk_l1_gas_price: u128, // In STRK.
pub gas_prices: GasPrices,

// Limits.
pub invoke_tx_max_n_steps: u32,
Expand All @@ -32,14 +30,6 @@ impl BlockContext {
pub fn fee_token_address(&self, version: &dyn HasTransactionVersion) -> ContractAddress {
self.fee_token_addresses.get_for_version(version)
}

pub fn tx_gas_price(&self, version: &dyn HasTransactionVersion) -> u128 {
if version.version() >= TransactionVersion(StarkFelt::from(3_u128)) {
self.strk_l1_gas_price
} else {
self.eth_l1_gas_price
}
}
}

#[derive(Clone, Debug)]
Expand All @@ -50,10 +40,27 @@ pub struct FeeTokenAddresses {

impl FeeTokenAddresses {
pub fn get_for_version(&self, has_version: &dyn HasTransactionVersion) -> ContractAddress {
if has_version.version() >= TransactionVersion(StarkFelt::from(3_u128)) {
if is_strk_version(has_version) {
self.strk_fee_token_address
} else {
self.eth_fee_token_address
}
}
}

#[derive(Clone, Debug)]
pub struct GasPrices {
pub eth_l1_gas_price: u128, // In wei.
// TODO(Amos, 01/09/2023): NEW_TOKEN_SUPPORT use this gas price for V3 txs.
pub strk_l1_gas_price: u128, // In STRK.
}

impl GasPrices {
pub fn get_for_version(&self, has_version: &dyn HasTransactionVersion) -> u128 {
if is_strk_version(has_version) { self.strk_l1_gas_price } else { self.eth_l1_gas_price }
}
}

fn is_strk_version(has_version: &dyn HasTransactionVersion) -> bool {
has_version.version() >= TransactionVersion(StarkFelt::from(3_u128))
}
2 changes: 1 addition & 1 deletion crates/blockifier/src/execution/entry_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl EntryPointExecutionContext {
panic!("{} must appear in `vm_resource_fee_cost`.", constants::N_STEPS_RESOURCE)
});
// TODO(Dori, 1/9/2023): NEW_TOKEN_SUPPORT gas price depends on tx version.
let max_gas = account_tx_context.max_fee.0 / block_context.eth_l1_gas_price;
let max_gas = account_tx_context.max_fee.0 / block_context.gas_prices.eth_l1_gas_price;
((max_gas as f64 / gas_per_step).floor() as usize)
.min(constants::MAX_STEPS_PER_TX)
.min(block_context.invoke_tx_max_n_steps as usize)
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@ pub fn calculate_tx_fee(
let total_l1_gas_usage = l1_gas_usage as f64 + l1_gas_by_vm_usage;

// TODO(Dori, 1/9/2023): NEW_TOKEN_SUPPORT gas price depends on transaction version.
Ok(Fee(total_l1_gas_usage.ceil() as u128 * block_context.eth_l1_gas_price))
Ok(Fee(total_l1_gas_usage.ceil() as u128 * block_context.gas_prices.eth_l1_gas_price))
}
8 changes: 5 additions & 3 deletions crates/blockifier/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use starknet_api::{calldata, class_hash, contract_address, patricia_key, stark_f

use crate::abi::abi_utils::get_storage_var_address;
use crate::abi::constants;
use crate::block_context::{BlockContext, FeeTokenAddresses};
use crate::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use crate::execution::call_info::{CallExecution, CallInfo, Retdata};
use crate::execution::contract_class::{ContractClass, ContractClassV0, ContractClassV1};
use crate::execution::entry_point::{
Expand Down Expand Up @@ -348,8 +348,10 @@ impl BlockContext {
strk_fee_token_address: contract_address!(TEST_ERC20_CONTRACT_ADDRESS2),
},
vm_resource_fee_cost: Default::default(),
eth_l1_gas_price: DEFAULT_ETH_L1_GAS_PRICE,
strk_l1_gas_price: DEFAULT_STRK_L1_GAS_PRICE,
gas_prices: GasPrices {
eth_l1_gas_price: DEFAULT_ETH_L1_GAS_PRICE,
strk_l1_gas_price: DEFAULT_STRK_L1_GAS_PRICE,
},
invoke_tx_max_n_steps: 1_000_000,
validate_max_n_steps: 1_000_000,
max_recursion_depth: 50,
Expand Down
8 changes: 5 additions & 3 deletions crates/native_blockifier/src/py_block_executor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::sync::Arc;

use blockifier::block_context::{BlockContext, FeeTokenAddresses};
use blockifier::block_context::{BlockContext, FeeTokenAddresses, GasPrices};
use blockifier::state::cached_state::GlobalContractCache;
use pyo3::prelude::*;
use starknet_api::block::{BlockNumber, BlockTimestamp};
Expand Down Expand Up @@ -291,8 +291,10 @@ pub fn into_block_context(
)?,
},
vm_resource_fee_cost: general_config.cairo_resource_fee_weights.clone(),
eth_l1_gas_price: block_info.eth_l1_gas_price,
strk_l1_gas_price: block_info.strk_l1_gas_price,
gas_prices: GasPrices {
eth_l1_gas_price: block_info.eth_l1_gas_price,
strk_l1_gas_price: block_info.strk_l1_gas_price,
},
invoke_tx_max_n_steps: general_config.invoke_tx_max_n_steps,
validate_max_n_steps: general_config.validate_max_n_steps,
max_recursion_depth,
Expand Down

0 comments on commit 1c6d2c0

Please sign in to comment.