From c203584cb80d2c24c1234dff441fe19f5976d0c6 Mon Sep 17 00:00:00 2001 From: Nimrod Weiss Date: Sun, 11 Aug 2024 11:44:09 +0300 Subject: [PATCH] build(blockifier, fee): translate resources to l2 gas --- crates/blockifier/src/fee/fee_test.rs | 18 ++++----- crates/blockifier/src/fee/fee_utils.rs | 12 +++--- crates/blockifier/src/fee/gas_usage.rs | 9 +++-- crates/blockifier/src/fee/gas_usage_test.rs | 2 +- crates/blockifier/src/transaction/objects.rs | 39 ++++++++++++++------ 5 files changed, 49 insertions(+), 31 deletions(-) diff --git a/crates/blockifier/src/fee/fee_test.rs b/crates/blockifier/src/fee/fee_test.rs index 9fa32a18cc..6b9bd0c40c 100644 --- a/crates/blockifier/src/fee/fee_test.rs +++ b/crates/blockifier/src/fee/fee_test.rs @@ -10,7 +10,7 @@ use crate::abi::constants::N_STEPS_RESOURCE; use crate::context::BlockContext; use crate::fee::actual_cost::TransactionReceipt; use crate::fee::fee_checks::{FeeCheckError, FeeCheckReportFields, PostExecutionReport}; -use crate::fee::fee_utils::calculate_l1_gas_by_vm_usage; +use crate::fee::fee_utils::calculate_l2_gas_by_vm_usage; use crate::invoke_tx_args; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::initial_test_state::test_state; @@ -48,8 +48,8 @@ fn test_simple_calculate_l1_gas_by_vm_usage() { .ceil() .to_integer(); assert_eq!( - GasVector::from_l1_gas(l1_gas_by_vm_usage), - calculate_l1_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) + GasVector::from_l2_gas(l1_gas_by_vm_usage), + calculate_l2_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) .unwrap() ); @@ -60,8 +60,8 @@ fn test_simple_calculate_l1_gas_by_vm_usage() { let l1_gas_by_vm_usage = vm_resource_usage.builtin_instance_counter.get(&BuiltinName::range_check).unwrap(); assert_eq!( - GasVector::from_l1_gas(u128_from_usize(*l1_gas_by_vm_usage)), - calculate_l1_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) + GasVector::from_l2_gas(u128_from_usize(*l1_gas_by_vm_usage)), + calculate_l2_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) .unwrap() ); } @@ -80,8 +80,8 @@ fn test_float_calculate_l1_gas_by_vm_usage() { .ceil() .to_integer(); assert_eq!( - GasVector::from_l1_gas(l1_gas_by_vm_usage), - calculate_l1_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) + GasVector::from_l2_gas(l1_gas_by_vm_usage), + calculate_l2_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) .unwrap() ); @@ -98,8 +98,8 @@ fn test_float_calculate_l1_gas_by_vm_usage() { .to_integer(); assert_eq!( - GasVector::from_l1_gas(l1_gas_by_vm_usage), - calculate_l1_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) + GasVector::from_l2_gas(l1_gas_by_vm_usage), + calculate_l2_gas_by_vm_usage(&versioned_constants, &vm_resource_usage, n_reverted_steps) .unwrap() ); } diff --git a/crates/blockifier/src/fee/fee_utils.rs b/crates/blockifier/src/fee/fee_utils.rs index cce1b85227..368f2ebe44 100644 --- a/crates/blockifier/src/fee/fee_utils.rs +++ b/crates/blockifier/src/fee/fee_utils.rs @@ -28,10 +28,10 @@ use crate::versioned_constants::VersionedConstants; #[path = "fee_test.rs"] pub mod test; -/// Calculates the L1 gas consumed when submitting the underlying Cairo program to SHARP. -/// I.e., returns the heaviest Cairo resource weight (in terms of L1 gas), as the size of +/// Calculates the L2 gas consumed when submitting the underlying Cairo program to SHARP. +/// I.e., returns the heaviest Cairo resource weight (in terms of L2 gas), as the size of /// a proof is determined similarly - by the (normalized) largest segment. -pub fn calculate_l1_gas_by_vm_usage( +pub fn calculate_l2_gas_by_vm_usage( versioned_constants: &VersionedConstants, vm_resource_usage: &ExecutionResources, n_reverted_steps: usize, @@ -54,8 +54,8 @@ pub fn calculate_l1_gas_by_vm_usage( used_resource_names, ); - // Convert Cairo usage to L1 gas usage. - let vm_l1_gas_usage = vm_resource_fee_costs + // Convert Cairo usage to L2 gas usage. + let vm_l2_gas_usage = vm_resource_fee_costs .iter() .map(|(key, resource_val)| { ((*resource_val) @@ -65,7 +65,7 @@ pub fn calculate_l1_gas_by_vm_usage( }) .fold(0, u128::max); - Ok(GasVector::from_l1_gas(vm_l1_gas_usage)) + Ok(GasVector::from_l2_gas(vm_l2_gas_usage)) } /// Converts the gas vector to a fee. diff --git a/crates/blockifier/src/fee/gas_usage.rs b/crates/blockifier/src/fee/gas_usage.rs index 8451d298ef..f83385208d 100644 --- a/crates/blockifier/src/fee/gas_usage.rs +++ b/crates/blockifier/src/fee/gas_usage.rs @@ -1,6 +1,6 @@ use cairo_vm::vm::runners::cairo_runner::ExecutionResources; -use super::fee_utils::calculate_l1_gas_by_vm_usage; +use super::fee_utils::calculate_l2_gas_by_vm_usage; use crate::abi::constants; use crate::context::{BlockContext, TransactionContext}; use crate::fee::eth_gas_constants; @@ -179,8 +179,11 @@ pub fn estimate_minimal_gas_vector( + versioned_constants.os_kzg_da_resources(data_segment_length).n_steps; let resources = ExecutionResources { n_steps: os_steps_for_type, ..Default::default() }; - Ok(get_da_gas_cost(&state_changes_by_account_transaction, block_info.use_kzg_da) - + calculate_l1_gas_by_vm_usage(versioned_constants, &resources, 0)?) + // TODO(Nimrod, 30/8/2024): Don't use `ignore_l2_gas`. + Ok(GasVector::ignore_l2_gas( + get_da_gas_cost(&state_changes_by_account_transaction, block_info.use_kzg_da) + + calculate_l2_gas_by_vm_usage(versioned_constants, &resources, 0)?, + )) } /// Compute l1_gas estimation from gas_vector using the following formula: diff --git a/crates/blockifier/src/fee/gas_usage_test.rs b/crates/blockifier/src/fee/gas_usage_test.rs index f9ccee395e..8e682f9aba 100644 --- a/crates/blockifier/src/fee/gas_usage_test.rs +++ b/crates/blockifier/src/fee/gas_usage_test.rs @@ -75,7 +75,7 @@ fn test_get_event_gas_cost( }; let call_infos = vec![call_info_1, call_info_2, call_info_3]; let call_infos_iter = call_infos.iter(); - let expected = GasVector::from_l1_gas( + let expected = GasVector::from_l2_gas( // 8 keys and 11 data words overall. (data_word_cost * (event_key_factor * 8_u128 + 11_u128)).to_integer(), ); diff --git a/crates/blockifier/src/transaction/objects.rs b/crates/blockifier/src/transaction/objects.rs index db844eb075..85e8785e33 100644 --- a/crates/blockifier/src/transaction/objects.rs +++ b/crates/blockifier/src/transaction/objects.rs @@ -26,7 +26,7 @@ use crate::blockifier::block::BlockInfo; use crate::execution::call_info::{CallInfo, ExecutionSummary, MessageL1CostInfo, OrderedEvent}; use crate::fee::actual_cost::TransactionReceipt; use crate::fee::eth_gas_constants; -use crate::fee::fee_utils::{calculate_l1_gas_by_vm_usage, get_fee_by_gas_vector}; +use crate::fee::fee_utils::{calculate_l2_gas_by_vm_usage, get_fee_by_gas_vector}; use crate::fee::gas_usage::{ get_consumed_message_to_l2_emissions_cost, get_da_gas_cost, @@ -166,6 +166,18 @@ impl GasVector { Self { l1_gas: 0, l1_data_gas } } + pub fn from_l2_gas(l2_gas: u128) -> Self { + Self { l2_gas, ..Default::default() } + } + + pub fn ignore_l2_gas(gas_vector: Self) -> Self { + Self { + l1_gas: gas_vector.l1_gas + gas_vector.l2_gas, + l1_data_gas: gas_vector.l1_data_gas, + l2_gas: 0, + } + } + /// Computes the cost (in fee token units) of the gas vector (saturating on overflow). pub fn saturated_cost(&self, gas_price: u128, blob_gas_price: u128) -> Fee { let l1_gas_cost = self.l1_gas.checked_mul(gas_price).unwrap_or_else(|| { @@ -326,10 +338,10 @@ impl StarknetResources { ) -> GasVector { // 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); - let l1_gas = (versioned_constants.l2_resource_gas_costs.gas_per_data_felt + let l2_gas = (versioned_constants.l2_resource_gas_costs.gas_per_data_felt * total_data_size) .to_integer(); - GasVector::from_l1_gas(l1_gas) + GasVector::from_l2_gas(l2_gas) } /// Returns an estimation of the gas usage for processing L1<>L2 messages on L1. Accounts for @@ -379,7 +391,7 @@ impl StarknetResources { /// Returns the gas cost of declared class codes. pub fn get_code_cost(&self, versioned_constants: &VersionedConstants) -> GasVector { - GasVector::from_l1_gas( + GasVector::from_l2_gas( (versioned_constants.l2_resource_gas_costs.gas_per_code_byte * u128_from_usize(self.code_size)) .to_integer(), @@ -397,11 +409,11 @@ impl StarknetResources { let l2_resource_gas_costs = &versioned_constants.l2_resource_gas_costs; let (event_key_factor, data_word_cost) = (l2_resource_gas_costs.event_key_factor, l2_resource_gas_costs.gas_per_data_felt); - let l1_gas: u128 = (data_word_cost + let l2_gas: u128 = (data_word_cost * (event_key_factor * self.total_event_keys + self.total_event_data_size)) .to_integer(); - GasVector::from_l1_gas(l1_gas) + GasVector::from_l2_gas(l2_gas) } pub fn get_onchain_data_segment_length(&self) -> usize { @@ -448,12 +460,15 @@ impl TransactionResources { versioned_constants: &VersionedConstants, use_kzg_da: bool, ) -> TransactionFeeResult { - Ok(self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da) - + calculate_l1_gas_by_vm_usage( - versioned_constants, - &self.vm_resources, - self.n_reverted_steps, - )?) + // TODO(Nimrod, 30/8/2024): Don't use `ignore_l2_gas`. + Ok(GasVector::ignore_l2_gas( + self.starknet_resources.to_gas_vector(versioned_constants, use_kzg_da) + + calculate_l2_gas_by_vm_usage( + versioned_constants, + &self.vm_resources, + self.n_reverted_steps, + )?, + )) } pub fn to_resources_mapping(