Skip to content

Commit

Permalink
fix(blockifier): revert l2_to_l1 conversion for backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
aner-starkware committed Sep 23, 2024
1 parent 00fb0cb commit 27a1fe9
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 55 deletions.
14 changes: 14 additions & 0 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
"max_contract_bytecode_size": 81920
},
"invoke_tx_max_n_steps": 10000000,
"deprecated_l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
32,
1000
]
},
"archival_data_gas_costs": {
"gas_per_data_felt": [
5120,
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_0.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@
"max_keys_length": 1000000000,
"max_n_emitted_events": 1000000000
},
"deprecated_l2_resource_gas_costs": {
"gas_per_data_felt": [
0,
1
],
"event_key_factor": [
0,
1
],
"gas_per_code_byte": [
0,
1
]
},
"archival_data_gas_costs": {
"gas_per_data_felt": [
0,
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
"max_contract_bytecode_size": 81920
},
"invoke_tx_max_n_steps": 4000000,
"deprecated_l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
875,
1000
]
},
"archival_data_gas_costs": {
"gas_per_data_felt": [
5120,
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_1_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
"max_contract_bytecode_size": 81920
},
"invoke_tx_max_n_steps": 4000000,
"deprecated_l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
32,
1000
]
},
"archival_data_gas_costs": {
"gas_per_data_felt": [
5120,
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
"max_contract_bytecode_size": 81920
},
"invoke_tx_max_n_steps": 10000000,
"deprecated_l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
875,
1000
]
},
"archival_data_gas_costs": {
"gas_per_data_felt": [
5120,
Expand Down
14 changes: 14 additions & 0 deletions crates/blockifier/resources/versioned_constants_13_2_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@
"max_contract_bytecode_size": 81920
},
"invoke_tx_max_n_steps": 10000000,
"deprecated_l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
32,
1000
]
},
"archival_data_gas_costs": {
"gas_per_data_felt": [
5120,
Expand Down
4 changes: 2 additions & 2 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl GasPrices {
let expected_eth_l2_gas_price = VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(eth_l1_gas_price.into());
if u128::from(eth_l2_gas_price) != expected_eth_l2_gas_price {
// TODO!(Aner): change to panic!
// 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:{expected_eth_l2_gas_price}."
Expand All @@ -59,7 +59,7 @@ impl GasPrices {
let expected_strk_l2_gas_price = VersionedConstants::latest_constants()
.convert_l1_to_l2_gas_price_round_up(strk_l1_gas_price.into());
if u128::from(strk_l2_gas_price) != expected_strk_l2_gas_price {
// TODO!(Aner): change to panic!
// 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:{expected_strk_l2_gas_price}."
Expand Down
15 changes: 8 additions & 7 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::fee::fee_utils::get_fee_by_gas_vector;
use crate::fee::gas_usage::{get_da_gas_cost, get_message_segment_length};
use crate::state::cached_state::StateChangesCount;
use crate::test_utils::{DEFAULT_ETH_L1_DATA_GAS_PRICE, DEFAULT_ETH_L1_GAS_PRICE};
use crate::transaction::objects::GasVectorComputationMode::NoL2Gas;
use crate::transaction::objects::{
FeeType,
GasVector,
Expand All @@ -31,11 +32,13 @@ fn versioned_constants() -> &'static VersionedConstants {
#[rstest]
fn test_get_event_gas_cost(
versioned_constants: &VersionedConstants,
// TODO!(Aner): add `All` computation mode.
#[values(NoL2Gas)] gas_vector_computation_mode: GasVectorComputationMode,
#[values(false, true)] use_kzg_da: bool,
) {
let archival_data_gas_costs = &versioned_constants.archival_data_gas_costs;
let l2_resource_gas_costs = &versioned_constants.deprecated_l2_resource_gas_costs;
let (event_key_factor, data_word_cost) =
(archival_data_gas_costs.event_key_factor, archival_data_gas_costs.gas_per_data_felt);
(l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt);
let call_infos = vec![CallInfo::default(), CallInfo::default(), CallInfo::default()];
let call_infos_iter = call_infos.iter();
let starknet_resources =
Expand All @@ -45,7 +48,7 @@ fn test_get_event_gas_cost(
starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas
&gas_vector_computation_mode
)
);

Expand Down Expand Up @@ -82,16 +85,14 @@ fn test_get_event_gas_cost(
let call_infos_iter = call_infos.iter();
let expected = GasVector::from_l1_gas(
// 8 keys and 11 data words overall.
versioned_constants.convert_l2_to_l1_gas_amount_round_up(
(data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer(),
),
(data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer(),
);
let starknet_resources =
StarknetResources::new(0, 0, 0, StateChangesCount::default(), None, call_infos_iter);
let gas_vector = starknet_resources.to_gas_vector(
versioned_constants,
use_kzg_da,
&GasVectorComputationMode::NoL2Gas,
&gas_vector_computation_mode,
);
assert_eq!(expected, gas_vector);
assert_ne!(GasVector::default(), gas_vector)
Expand Down
75 changes: 41 additions & 34 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::transaction::errors::{
TransactionPreValidationError,
};
use crate::utils::{u128_div_ceil, u128_from_usize, usize_from_u128};
use crate::versioned_constants::VersionedConstants;
use crate::versioned_constants::{ArchivalDataGasCosts, VersionedConstants};

#[cfg(test)]
#[path = "objects_test.rs"]
Expand Down Expand Up @@ -350,38 +350,36 @@ impl StarknetResources {
versioned_constants: &VersionedConstants,
mode: &GasVectorComputationMode,
) -> GasVector {
match mode {
// Cost in L2 gas.
GasVectorComputationMode::All => {
let l2_archival_data_costs = [
self.get_calldata_and_signature_cost(versioned_constants),
self.get_code_cost(versioned_constants),
self.get_events_cost(versioned_constants),
];
GasVector::from_l2_gas(l2_archival_data_costs.iter().sum())
}
let archival_gas_costs = match mode {
// Computation is in L2 gas units.
GasVectorComputationMode::All => &versioned_constants.archival_data_gas_costs,
// Computation is in L1 gas units.
GasVectorComputationMode::NoL2Gas => {
let l1_archival_data_costs = [
self.get_calldata_and_signature_cost(versioned_constants),
self.get_code_cost(versioned_constants),
self.get_events_cost(versioned_constants),
];
GasVector::from_l1_gas(l1_archival_data_costs.iter().sum())
&versioned_constants.deprecated_l2_resource_gas_costs
}
}
};
mode.to_l1_or_l2_gas_vector(
[
self.get_calldata_and_signature_gas_cost(archival_gas_costs),
self.get_code_gas_cost(archival_gas_costs),
self.get_events_gas_cost(archival_gas_costs),
]
.into_iter()
.sum(),
)
}

/// Returns the cost for transaction calldata and transaction signature. Each felt costs a
/// fixed and configurable amount of gas. This cost represents the cost of storing the
/// calldata and the signature on L2. The result is given in L2 gas units.
pub fn get_calldata_and_signature_cost(
/// calldata and the signature on L2. The result is given in L1/L2 gas units, depending on the
/// mode.
pub fn get_calldata_and_signature_gas_cost(
&self,
versioned_constants: &VersionedConstants,
archival_gas_costs: &ArchivalDataGasCosts,
) -> u128 {
// TODO(Avi, 20/2/2024): Calculate the number of bytes instead of the number of felts.
let total_data_size = u128_from_usize(self.calldata_length + self.signature_length);
(versioned_constants.archival_data_gas_costs.gas_per_data_felt * total_data_size)
.to_integer()
(archival_gas_costs.gas_per_data_felt * total_data_size).to_integer()
}

/// Returns an estimation of the gas usage for processing L1<>L2 messages on L1. Accounts for
Expand Down Expand Up @@ -429,11 +427,9 @@ impl StarknetResources {
(message_segment_length, gas_weight)
}

/// Returns the cost of declared class codes in L2 gas units.
pub fn get_code_cost(&self, versioned_constants: &VersionedConstants) -> u128 {
(versioned_constants.archival_data_gas_costs.gas_per_code_byte
* u128_from_usize(self.code_size))
.to_integer()
/// Returns the cost of declared class codes in L1/L2 gas units, depending on the mode.
pub fn get_code_gas_cost(&self, archival_gas_costs: &ArchivalDataGasCosts) -> u128 {
(archival_gas_costs.gas_per_code_byte * u128_from_usize(self.code_size)).to_integer()
}

/// Returns the gas cost of the transaction's state changes.
Expand All @@ -442,12 +438,12 @@ impl StarknetResources {
get_da_gas_cost(&self.state_changes_for_fee, use_kzg_da)
}

/// Returns the cost of the transaction's emmited events in L2 gas units.
pub fn get_events_cost(&self, versioned_constants: &VersionedConstants) -> u128 {
let archival_data_gas_costs = &versioned_constants.archival_data_gas_costs;
let (event_key_factor, data_word_cost) =
(archival_data_gas_costs.event_key_factor, archival_data_gas_costs.gas_per_data_felt);
(data_word_cost * (event_key_factor * self.total_event_keys + self.total_event_data_size))
/// Returns the cost of the transaction's emmited events in L1/L2 gas units, depending on the
/// mode.
pub fn get_events_gas_cost(&self, archival_gas_costs: &ArchivalDataGasCosts) -> u128 {
(archival_gas_costs.gas_per_data_felt
* (archival_gas_costs.event_key_factor * self.total_event_keys
+ self.total_event_data_size))
.to_integer()
}

Expand Down Expand Up @@ -493,6 +489,17 @@ pub enum GasVectorComputationMode {
NoL2Gas,
}

impl GasVectorComputationMode {
// Transforms the gas amount from a u128 of L1\L2 (depending on the computation mode) units, to
// a GasVector.
pub fn to_l1_or_l2_gas_vector(&self, gas_amount: u128) -> GasVector {
match self {
GasVectorComputationMode::All => GasVector::from_l2_gas(gas_amount),
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(gas_amount),
}
}
}

impl TransactionResources {
/// Computes and returns the total gas consumption. The L2 gas amount may be converted
/// to L1 gas (depending on the gas vector computation mode).
Expand Down
21 changes: 9 additions & 12 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::execution::errors::PostExecutionError;
use crate::execution::execution_utils::poseidon_hash_many_cost;
use crate::execution::syscalls::SyscallSelector;
use crate::transaction::errors::TransactionExecutionError;
use crate::transaction::objects::StarknetResources;
use crate::transaction::objects::{GasVectorComputationMode, StarknetResources};
use crate::transaction::transaction_types::TransactionType;

#[cfg(test)]
Expand Down Expand Up @@ -94,6 +94,7 @@ pub struct VersionedConstants {
// Limits.
pub tx_event_limits: EventLimits,
pub invoke_tx_max_n_steps: u32,
pub deprecated_l2_resource_gas_costs: ArchivalDataGasCosts,
pub archival_data_gas_costs: ArchivalDataGasCosts,
pub max_recursion_depth: usize,
pub validate_max_n_steps: u32,
Expand Down Expand Up @@ -149,17 +150,6 @@ impl VersionedConstants {
*(self.l1_to_l2_gas_price_ratio().inv() * l1_gas_amount).ceil().numer()
}

/// Converts from L2 gas price to L1 gas price with **upward rounding**.
pub fn convert_l2_to_l1_gas_price_round_up(&self, l2_gas_price: u128) -> u128 {
*(self.l1_to_l2_gas_price_ratio().inv() * l2_gas_price).ceil().numer()
}

/// Converts from L2 gas amount to L1 gas amount with **upward rounding**.
pub fn convert_l2_to_l1_gas_amount_round_up(&self, l2_gas_amount: u128) -> u128 {
// The amount ratio is the inverse of the price ratio.
*(self.l1_to_l2_gas_price_ratio() * l2_gas_amount).ceil().numer()
}

/// Returns the following ratio: L2_gas_price/L1_gas_price.
fn l1_to_l2_gas_price_ratio(&self) -> ResourceCost {
Ratio::new(1, u128::from(self.os_constants.gas_costs.step_gas_cost))
Expand Down Expand Up @@ -277,6 +267,13 @@ impl VersionedConstants {
..Self::latest_constants().clone()
}
}

pub fn get_archival_gas_costs(&self, mode: &GasVectorComputationMode) -> &ArchivalDataGasCosts {
match mode {
GasVectorComputationMode::All => &self.archival_data_gas_costs,
GasVectorComputationMode::NoL2Gas => &self.deprecated_l2_resource_gas_costs,
}
}
}

impl TryFrom<&Path> for VersionedConstants {
Expand Down

0 comments on commit 27a1fe9

Please sign in to comment.