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

Commit

Permalink
Create the function calculate_tx_blob_gas_usage. (starkware-libs#1346)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jan 22, 2024
1 parent 22f9c14 commit 4a2a92d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
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;

// Blob 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 (data-gas) needed to publish the transaction's state diff in a 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
45 changes: 44 additions & 1 deletion crates/blockifier/src/fee/gas_usage_test.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,58 @@
use pretty_assertions::assert_eq;
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,
};
use crate::state::cached_state::StateChangesCount;

#[rstest]
#[case::storage_write(StateChangesCount {
n_storage_updates: 1,
n_class_hash_updates:0,
n_compiled_class_hash_updates:0,
n_modified_contracts:0,
})
]
#[case::deploy_account(StateChangesCount {
n_storage_updates: 0,
n_class_hash_updates:1,
n_compiled_class_hash_updates:0,
n_modified_contracts:1,
})
]
#[case::declare(StateChangesCount {
n_storage_updates: 0,
n_class_hash_updates:0,
n_compiled_class_hash_updates:1,
n_modified_contracts:0,
})
]
#[case::general_scenario(StateChangesCount {
n_storage_updates: 7,
n_class_hash_updates:11,
n_compiled_class_hash_updates:13,
n_modified_contracts:17,
})
]
fn test_calculate_tx_blob_gas_usage_basic(#[case] state_changes_count: StateChangesCount) {
// Manual calculation.
let on_chain_data_segment_length = state_changes_count.n_storage_updates * 2
+ state_changes_count.n_class_hash_updates
+ state_changes_count.n_compiled_class_hash_updates * 2
+ state_changes_count.n_modified_contracts * 2;
let manual_blob_gas_usage =
on_chain_data_segment_length * eth_gas_constants::DATA_GAS_PER_FIELD_ELEMENT;

assert_eq!(manual_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 4a2a92d

Please sign in to comment.