Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Create the function calculate_tx_blob_gas_usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jan 17, 2024
1 parent 88bc886 commit b9b45e7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion crates/blockifier/src/fee/actual_cost.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use starknet_api::core::ContractAddress;
use starknet_api::transaction::Fee;

use super::gas_usage::calculate_tx_gas_usage;
use super::gas_usage::{calculate_tx_blob_gas_usage, calculate_tx_gas_usage};
use crate::abi::constants as abi_constants;
use crate::block_context::BlockContext;
use crate::execution::call_info::CallInfo;
Expand Down
5 changes: 5 additions & 0 deletions crates/blockifier/src/fee/eth_gas_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ pub const GAS_PER_MEMORY_BYTE: usize = 16;
pub const WORD_WIDTH: usize = 32;
pub const GAS_PER_MEMORY_WORD: usize = GAS_PER_MEMORY_BYTE * WORD_WIDTH;

// 4844 Data.
pub const FIELD_ELEMENTS_PER_BLOB: usize = 1 << 12;
pub const DATA_GAS_PER_BLOB: usize = 1 << 17;
pub const DATA_GAS_PER_FIELD_ELEMENT: usize = DATA_GAS_PER_BLOB / FIELD_ELEMENTS_PER_BLOB;

// Storage.
pub const GAS_PER_ZERO_TO_NONZERO_STORAGE_SET: usize = 20000;
pub const GAS_PER_COLD_STORAGE_ACCESS: usize = 2100;
Expand Down
6 changes: 6 additions & 0 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ fn calculate_l2_to_l1_payloads_length_and_message_segment_length<'a>(
Ok((l2_to_l1_payloads_length, message_segment_length))
}

/// Returns the blob-gas cost of publishing the on chain data to a KZG blob.
pub fn calculate_tx_blob_gas_usage(state_changes_count: StateChangesCount) -> usize {
let onchain_data_segment_length = get_onchain_data_segment_length(state_changes_count);
onchain_data_segment_length * eth_gas_constants::DATA_GAS_PER_FIELD_ELEMENT
}

/// Returns an estimation of the L1 gas amount that will be used (by Starknet's update state and
/// the verifier) following the addition of a transaction with the given parameters to a batch;
/// e.g., a message from L2 to L1 is followed by a storage write operation in Starknet L1 contract
Expand Down
26 changes: 25 additions & 1 deletion crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
use rstest::rstest;
use starknet_api::hash::StarkFelt;
use starknet_api::stark_felt;
use starknet_api::transaction::L2ToL1Payload;

use crate::execution::call_info::{CallExecution, CallInfo, MessageToL1, OrderedL2ToL1Message};
use crate::fee::eth_gas_constants;
use crate::fee::gas_usage::{
calculate_tx_gas_usage, get_consumed_message_to_l2_emissions_cost,
calculate_tx_blob_gas_usage, calculate_tx_gas_usage, get_consumed_message_to_l2_emissions_cost,
get_log_message_to_l1_emissions_cost, get_message_segment_length, get_onchain_data_cost,
get_onchain_data_segment_length,
};
use crate::state::cached_state::StateChangesCount;

#[rstest]
fn test_calculate_tx_blob_gas_usage_basic(
#[values(0, 1)] n_storage_updates: usize,
#[values(0, 1)] n_class_hash_updates: usize,
#[values(0, 1)] n_compiled_class_hash_updates: usize,
#[values(0, 1)] n_modified_contracts: usize,
) {
let state_changes_count = StateChangesCount {
n_storage_updates,
n_class_hash_updates,
n_compiled_class_hash_updates,
n_modified_contracts,
};

// Manual calculation.
let on_chain_data_segment_length = get_onchain_data_segment_length(state_changes_count);
let maunal_blob_gas_usage =
on_chain_data_segment_length * eth_gas_constants::DATA_GAS_PER_FIELD_ELEMENT;

assert_eq!(maunal_blob_gas_usage, calculate_tx_blob_gas_usage(state_changes_count));
}

/// This test goes over five cases. In each case, we calculate the gas usage given the parameters.
/// We then perform the same calculation manually, each time using only the relevant parameters.
/// The five cases are:
Expand Down

0 comments on commit b9b45e7

Please sign in to comment.