From b9b45e7ef6e9225641395c3107c6690592f6ac35 Mon Sep 17 00:00:00 2001 From: Arni Hod Date: Wed, 17 Jan 2024 16:30:58 +0200 Subject: [PATCH] Create the function calculate_tx_blob_gas_usage. --- crates/blockifier/src/fee/actual_cost.rs | 2 +- .../blockifier/src/fee/eth_gas_constants.rs | 5 ++++ crates/blockifier/src/fee/gas_usage.rs | 6 +++++ crates/blockifier/src/fee/gas_usage_test.rs | 26 ++++++++++++++++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/crates/blockifier/src/fee/actual_cost.rs b/crates/blockifier/src/fee/actual_cost.rs index 5ae2c34a3e..19ff7f0a18 100644 --- a/crates/blockifier/src/fee/actual_cost.rs +++ b/crates/blockifier/src/fee/actual_cost.rs @@ -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; diff --git a/crates/blockifier/src/fee/eth_gas_constants.rs b/crates/blockifier/src/fee/eth_gas_constants.rs index 77943e1aec..444f87e526 100644 --- a/crates/blockifier/src/fee/eth_gas_constants.rs +++ b/crates/blockifier/src/fee/eth_gas_constants.rs @@ -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; diff --git a/crates/blockifier/src/fee/gas_usage.rs b/crates/blockifier/src/fee/gas_usage.rs index cf984fbd31..4bf8368165 100644 --- a/crates/blockifier/src/fee/gas_usage.rs +++ b/crates/blockifier/src/fee/gas_usage.rs @@ -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 diff --git a/crates/blockifier/src/fee/gas_usage_test.rs b/crates/blockifier/src/fee/gas_usage_test.rs index 47356f6603..c6e924ffff 100644 --- a/crates/blockifier/src/fee/gas_usage_test.rs +++ b/crates/blockifier/src/fee/gas_usage_test.rs @@ -1,3 +1,4 @@ +use rstest::rstest; use starknet_api::hash::StarkFelt; use starknet_api::stark_felt; use starknet_api::transaction::L2ToL1Payload; @@ -5,11 +6,34 @@ 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: