Skip to content

Commit

Permalink
build(blockifier, fee): translate resources to l2 gas
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-starkware committed Aug 11, 2024
1 parent d80da28 commit c203584
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 31 deletions.
18 changes: 9 additions & 9 deletions crates/blockifier/src/fee/fee_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);

Expand All @@ -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()
);
}
Expand All @@ -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()
);

Expand All @@ -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()
);
}
Expand Down
12 changes: 6 additions & 6 deletions crates/blockifier/src/fee/fee_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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.
Expand Down
9 changes: 6 additions & 3 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
39 changes: 27 additions & 12 deletions crates/blockifier/src/transaction/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(|| {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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 {
Expand Down Expand Up @@ -448,12 +460,15 @@ impl TransactionResources {
versioned_constants: &VersionedConstants,
use_kzg_da: bool,
) -> TransactionFeeResult<GasVector> {
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(
Expand Down

0 comments on commit c203584

Please sign in to comment.