Skip to content

Commit

Permalink
build(fee): add l2 costs to versioned constants (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware authored Sep 9, 2024
1 parent e34425f commit 1ade15c
Show file tree
Hide file tree
Showing 15 changed files with 100 additions and 90 deletions.
10 changes: 5 additions & 5 deletions crates/blockifier/resources/versioned_constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
"max_contract_bytecode_size": 81920
},
"invoke_tx_max_n_steps": 10000000,
"l2_resource_gas_costs": {
"archival_data_gas_costs": {
"gas_per_data_felt": [
128,
1000
5120,
1
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
32,
1000
1280,
1
]
},
"disable_cairo0_redeclaration": true,
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/resources/versioned_constants_13_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"invoke_tx_max_n_steps": 4000000,
"l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
5120,
1
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
875,
1000
35000,
1
]
},
"max_recursion_depth": 50,
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/resources/versioned_constants_13_1_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"invoke_tx_max_n_steps": 4000000,
"l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
5120,
1
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
32,
1000
1280,
1
]
},
"max_recursion_depth": 50,
Expand Down
8 changes: 4 additions & 4 deletions crates/blockifier/resources/versioned_constants_13_2.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"invoke_tx_max_n_steps": 10000000,
"l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
5120,
1
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
875,
1000
35000,
1
]
},
"disable_cairo0_redeclaration": true,
Expand Down
10 changes: 5 additions & 5 deletions crates/blockifier/resources/versioned_constants_13_2_1.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@
"invoke_tx_max_n_steps": 10000000,
"l2_resource_gas_costs": {
"gas_per_data_felt": [
128,
1000
5120,
1
],
"event_key_factor": [
2,
1
],
"gas_per_code_byte": [
32,
1000
1280,
1
]
},
"disable_cairo0_redeclaration": true,
Expand Down Expand Up @@ -610,4 +610,4 @@
100
]
}
}
}
8 changes: 4 additions & 4 deletions crates/blockifier/src/blockifier/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ impl GasPrices {
strk_l2_gas_price: NonZeroU128,
) -> Self {
// 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());
let expected_eth_l2_gas_price =
VersionedConstants::latest_constants().convert_l1_to_l2_gas(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());
let expected_strk_l2_gas_price =
VersionedConstants::latest_constants().convert_l1_to_l2_gas(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! \
Expand Down
11 changes: 7 additions & 4 deletions crates/blockifier/src/fee/actual_cost_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fn versioned_constants() -> &'static VersionedConstants {
#[rstest]
fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool) {
// An empty transaction (a theoretical case for sanity check).
let versioned_constants = VersionedConstants::default();
let versioned_constants = VersionedConstants::create_for_account_testing();
let empty_tx_starknet_resources = StarknetResources::default();
let empty_tx_gas_usage_vector = empty_tx_starknet_resources.to_gas_vector(
&versioned_constants,
Expand All @@ -73,7 +73,8 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
None,
std::iter::empty(),
);
let code_gas_cost = versioned_constants.l2_resource_gas_costs.gas_per_code_byte
let code_gas_cost = versioned_constants.archival_data_gas_costs.gas_per_code_byte
* versioned_constants.l1_to_l2_gas_price_ratio()
* u128_from_usize(
(class_info.bytecode_length() + class_info.sierra_program_length())
* eth_gas_constants::WORD_WIDTH
Expand Down Expand Up @@ -110,7 +111,8 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
std::iter::empty(),
);
let calldata_and_signature_gas_cost =
versioned_constants.l2_resource_gas_costs.gas_per_data_felt
versioned_constants.archival_data_gas_costs.gas_per_data_felt
* versioned_constants.l1_to_l2_gas_price_ratio()
* u128_from_usize(calldata_length + signature_length);
let manual_starknet_gas_usage = calldata_and_signature_gas_cost.to_integer();
let manual_gas_vector = GasVector { l1_gas: manual_starknet_gas_usage, ..Default::default() }
Expand Down Expand Up @@ -143,7 +145,8 @@ fn test_calculate_tx_gas_usage_basic<'a>(#[values(false, true)] use_kzg_da: bool
// Manual calculation.
let message_segment_length = get_message_segment_length(&[], Some(l1_handler_payload_size));
let calldata_and_signature_gas_cost =
versioned_constants.l2_resource_gas_costs.gas_per_data_felt
versioned_constants.archival_data_gas_costs.gas_per_data_felt
* versioned_constants.l1_to_l2_gas_price_ratio()
* u128_from_usize(l1_handler_payload_size + signature_length);
let manual_starknet_gas_usage = message_segment_length * eth_gas_constants::GAS_PER_MEMORY_WORD
+ eth_gas_constants::GAS_PER_COUNTER_DECREASE
Expand Down
9 changes: 3 additions & 6 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn test_simple_get_vm_resource_usage() {

#[test]
fn test_float_get_vm_resource_usage() {
let versioned_constants = VersionedConstants::create_float_for_testing();
let versioned_constants = VersionedConstants::create_for_testing();
let mut vm_resource_usage = get_vm_resource_usage();

// Positive flow.
Expand Down Expand Up @@ -154,13 +154,10 @@ fn test_discounted_gas_overdraft(
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)
.convert_l1_to_l2_gas(DEFAULT_ETH_L1_GAS_PRICE)
.try_into()
.unwrap(),
VersionedConstants::latest_constants().convert_l1_to_l2_gas(gas_price).try_into().unwrap(),
);

let account = FeatureContract::AccountWithoutValidations(CairoVersion::Cairo0);
Expand Down
6 changes: 3 additions & 3 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ pub fn get_vm_resources_cost(

match computation_mode {
GasVectorComputationMode::NoL2Gas => Ok(GasVector::from_l1_gas(vm_l1_gas_usage)),
GasVectorComputationMode::All => Ok(GasVector::from_l2_gas(
versioned_constants.l1_to_l2_gas_price_conversion(vm_l1_gas_usage),
)),
GasVectorComputationMode::All => {
Ok(GasVector::from_l2_gas(versioned_constants.convert_l1_to_l2_gas(vm_l1_gas_usage)))
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ fn test_get_event_gas_cost(
versioned_constants: &VersionedConstants,
#[values(false, true)] use_kzg_da: bool,
) {
let l2_resource_gas_costs = &versioned_constants.l2_resource_gas_costs;
let archival_data_gas_costs = &versioned_constants.archival_data_gas_costs;
let (event_key_factor, data_word_cost) =
(l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt);
(archival_data_gas_costs.event_key_factor, archival_data_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 Down Expand Up @@ -82,7 +82,10 @@ 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.
(data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer(),
(data_word_cost
* versioned_constants.l1_to_l2_gas_price_ratio()
* (event_key_factor * 8_u128 + 11_u128))
.to_integer(),
);
let starknet_resources =
StarknetResources::new(0, 0, 0, StateChangesCount::default(), None, call_infos_iter);
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 @@ -163,11 +163,11 @@ impl BlockInfo {
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)
.convert_l1_to_l2_gas(DEFAULT_ETH_L1_GAS_PRICE)
.try_into()
.unwrap(),
VersionedConstants::latest_constants()
.l1_to_l2_gas_price_conversion(DEFAULT_STRK_L1_GAS_PRICE)
.convert_l1_to_l2_gas(DEFAULT_STRK_L1_GAS_PRICE)
.try_into()
.unwrap(),
),
Expand Down
38 changes: 22 additions & 16 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,28 +350,36 @@ impl StarknetResources {
versioned_constants: &VersionedConstants,
mode: &GasVectorComputationMode,
) -> GasVector {
let l2_resources_in_l1_gas = self.get_calldata_and_signature_cost(versioned_constants)
+ self.get_code_cost(versioned_constants)
+ self.get_events_cost(versioned_constants);
// Cost in L2 gas.
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),
];
match mode {
GasVectorComputationMode::All => GasVector::from_l2_gas(
versioned_constants.l1_to_l2_gas_price_conversion(l2_resources_in_l1_gas),
GasVectorComputationMode::All => {
GasVector::from_l2_gas(l2_archival_data_costs.iter().sum())
}
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(
l2_archival_data_costs
.map(|cost| versioned_constants.convert_l2_to_l1_gas(cost))
.iter()
.sum(),
),
GasVectorComputationMode::NoL2Gas => GasVector::from_l1_gas(l2_resources_in_l1_gas),
}
}

/// 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 L1 gas units.
// TODO(Nimrod, 1/10/2024): Calculate cost in L2 gas units.
/// calldata and the signature on L2. The result is given in L2 gas units.
pub fn get_calldata_and_signature_cost(
&self,
versioned_constants: &VersionedConstants,
) -> 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.l2_resource_gas_costs.gas_per_data_felt * total_data_size).to_integer()
(versioned_constants.archival_data_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 @@ -419,10 +427,9 @@ impl StarknetResources {
(message_segment_length, gas_weight)
}

/// Returns the cost of declared class codes in L1 gas units.
// TODO(Nimrod, 1/10/2024): Calculate cost in L2 gas units.
/// Returns the cost of declared class codes in L2 gas units.
pub fn get_code_cost(&self, versioned_constants: &VersionedConstants) -> u128 {
(versioned_constants.l2_resource_gas_costs.gas_per_code_byte
(versioned_constants.archival_data_gas_costs.gas_per_code_byte
* u128_from_usize(self.code_size))
.to_integer()
}
Expand All @@ -433,12 +440,11 @@ impl StarknetResources {
get_da_gas_cost(&self.state_changes_for_fee, use_kzg_da)
}

/// Returns the cost of the transaction's emmited events in L1 gas units.
// TODO(Nimrod, 1/10/2024): Calculate cost in L2 gas units.
/// Returns the cost of the transaction's emmited events in L2 gas units.
pub fn get_events_cost(&self, versioned_constants: &VersionedConstants) -> u128 {
let l2_resource_gas_costs = &versioned_constants.l2_resource_gas_costs;
let archival_data_gas_costs = &versioned_constants.archival_data_gas_costs;
let (event_key_factor, data_word_cost) =
(l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt);
(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))
.to_integer()
}
Expand Down
Loading

0 comments on commit 1ade15c

Please sign in to comment.