diff --git a/crates/blockifier/src/fee/gas_usage_test.rs b/crates/blockifier/src/fee/gas_usage_test.rs index 4b2809502bb..9d1bcb0f345 100644 --- a/crates/blockifier/src/fee/gas_usage_test.rs +++ b/crates/blockifier/src/fee/gas_usage_test.rs @@ -71,12 +71,15 @@ fn starknet_resources() -> StarknetResources { .collect(); let execution_summary = CallInfo::summarize_many(call_infos.iter(), VersionedConstants::latest_constants()); - let state_resources = StateResources::new_for_testing(StateChangesCount { - n_storage_updates: 7, - n_class_hash_updates: 11, - n_compiled_class_hash_updates: 13, - n_modified_contracts: 17, - }); + let state_resources = StateResources::new_for_testing( + StateChangesCount { + n_storage_updates: 7, + n_class_hash_updates: 11, + n_compiled_class_hash_updates: 13, + n_modified_contracts: 17, + }, + 19, + ); StarknetResources::new(2_usize, 3_usize, 4_usize, state_resources, 6.into(), execution_summary) } diff --git a/crates/blockifier/src/fee/receipt_test.rs b/crates/blockifier/src/fee/receipt_test.rs index b15916a12cb..496c2b6bdcd 100644 --- a/crates/blockifier/src/fee/receipt_test.rs +++ b/crates/blockifier/src/fee/receipt_test.rs @@ -118,7 +118,7 @@ fn test_calculate_tx_gas_usage_basic<'a>( calldata_length, signature_length, 0, - StateResources::new_for_testing(deploy_account_state_changes_count), + StateResources::new_for_testing(deploy_account_state_changes_count, 0), None, ExecutionSummary::default(), ); @@ -228,7 +228,7 @@ fn test_calculate_tx_gas_usage_basic<'a>( 0, 0, 0, - StateResources::new_for_testing(l2_to_l1_state_changes_count), + StateResources::new_for_testing(l2_to_l1_state_changes_count, 0), None, execution_summary.clone(), ); @@ -261,7 +261,7 @@ fn test_calculate_tx_gas_usage_basic<'a>( assert_eq!(l2_to_l1_messages_gas_usage_vector, manual_gas_computation); - // Any calculation with storage writings.t + // Any calculation with storage writings. let n_modified_contracts = 7; let n_storage_updates = 11; @@ -275,7 +275,7 @@ fn test_calculate_tx_gas_usage_basic<'a>( 0, 0, 0, - StateResources::new_for_testing(storage_writes_state_changes_count), + StateResources::new_for_testing(storage_writes_state_changes_count, n_storage_updates / 2), None, ExecutionSummary::default(), ); @@ -304,7 +304,10 @@ fn test_calculate_tx_gas_usage_basic<'a>( l1_handler_payload_size, signature_length, 0, - StateResources::new_for_testing(combined_state_changes_count), + StateResources::new_for_testing( + combined_state_changes_count, + storage_writes_state_changes_count.n_storage_updates / 2, + ), Some(l1_handler_payload_size), execution_summary.clone(), ); @@ -379,7 +382,7 @@ fn test_calculate_tx_gas_usage( calldata_length, signature_length, 0, - StateResources::new_for_testing(state_changes_count), + StateResources::new_for_testing(state_changes_count, 0), None, ExecutionSummary::default(), ); @@ -437,7 +440,7 @@ fn test_calculate_tx_gas_usage( calldata_length, signature_length, 0, - StateResources::new_for_testing(state_changes_count), + StateResources::new_for_testing(state_changes_count, 0), None, // The transfer entrypoint emits an event - pass the call info to count its resources. execution_summary, diff --git a/crates/blockifier/src/fee/resources.rs b/crates/blockifier/src/fee/resources.rs index 2468ab041a3..78620c93a58 100644 --- a/crates/blockifier/src/fee/resources.rs +++ b/crates/blockifier/src/fee/resources.rs @@ -13,7 +13,9 @@ use crate::fee::gas_usage::{ get_message_segment_length, get_onchain_data_segment_length, }; -use crate::state::cached_state::{StateChanges, StateChangesCount}; +#[cfg(any(test, feature = "testing"))] +use crate::state::cached_state::StateChangesCount; +use crate::state::cached_state::{StateChanges, StateChangesCountForFee}; use crate::transaction::errors::TransactionFeeError; use crate::utils::u64_from_usize; use crate::versioned_constants::{ArchivalDataGasCosts, VersionedConstants}; @@ -157,7 +159,7 @@ impl StarknetResources { #[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))] #[derive(Clone, Debug, Default, PartialEq)] pub struct StateResources { - state_changes_for_fee: StateChangesCount, + pub state_changes_for_fee: StateChangesCountForFee, } impl StateResources { @@ -173,18 +175,27 @@ impl StateResources { } #[cfg(any(test, feature = "testing"))] - pub fn new_for_testing(state_changes_for_fee: StateChangesCount) -> Self { - Self { state_changes_for_fee } + pub fn new_for_testing( + state_changes_count: StateChangesCount, + n_allocated_keys: usize, + ) -> Self { + Self { + state_changes_for_fee: StateChangesCountForFee { + state_changes_count, + n_allocated_keys, + }, + } } /// Returns the gas cost of the transaction's state changes. pub fn to_gas_vector(&self, use_kzg_da: bool) -> GasVector { // TODO(Nimrod, 29/3/2024): delete `get_da_gas_cost` and move it's logic here. - get_da_gas_cost(&self.state_changes_for_fee, use_kzg_da) + // TODO(Yoav): Add the cost of allocating keys. + get_da_gas_cost(&self.state_changes_for_fee.state_changes_count, use_kzg_da) } pub fn get_onchain_data_segment_length(&self) -> usize { - get_onchain_data_segment_length(&self.state_changes_for_fee) + get_onchain_data_segment_length(&self.state_changes_for_fee.state_changes_count) } } diff --git a/crates/blockifier/src/state/cached_state.rs b/crates/blockifier/src/state/cached_state.rs index 85c79048b7e..59cb99b5ad0 100644 --- a/crates/blockifier/src/state/cached_state.rs +++ b/crates/blockifier/src/state/cached_state.rs @@ -733,7 +733,7 @@ impl StateChanges { &self, sender_address: Option, fee_token_address: ContractAddress, - ) -> StateChangesCount { + ) -> StateChangesCountForFee { let mut modified_contracts = self.state_maps.get_modified_contracts(); // For account transactions, we need to compute the transaction fee before we can execute @@ -753,11 +753,15 @@ impl StateChanges { // block. modified_contracts.remove(&fee_token_address); - StateChangesCount { - n_storage_updates, - n_class_hash_updates: self.state_maps.class_hashes.len(), - n_compiled_class_hash_updates: self.state_maps.compiled_class_hashes.len(), - n_modified_contracts: modified_contracts.len(), + StateChangesCountForFee { + state_changes_count: StateChangesCount { + n_storage_updates, + n_class_hash_updates: self.state_maps.class_hashes.len(), + n_compiled_class_hash_updates: self.state_maps.compiled_class_hashes.len(), + n_modified_contracts: modified_contracts.len(), + }, + // TODO: Set number of allocated keys. + n_allocated_keys: self.allocated_keys.len(), } } } @@ -771,3 +775,11 @@ pub struct StateChangesCount { pub n_compiled_class_hash_updates: usize, pub n_modified_contracts: usize, } + +/// Holds the number of state changes for fee. +#[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub struct StateChangesCountForFee { + pub state_changes_count: StateChangesCount, + pub n_allocated_keys: usize, +} diff --git a/crates/blockifier/src/state/cached_state_test.rs b/crates/blockifier/src/state/cached_state_test.rs index f260250115d..bdec8176785 100644 --- a/crates/blockifier/src/state/cached_state_test.rs +++ b/crates/blockifier/src/state/cached_state_test.rs @@ -323,7 +323,6 @@ fn create_state_changes_for_test( let sender_balance_key = get_fee_token_var_address(sender_address); state.set_storage_at(fee_token_address, sender_balance_key, felt!("0x1999")).unwrap(); } - state.get_actual_state_changes().unwrap() } @@ -336,12 +335,15 @@ fn test_from_state_changes_for_fee_charge( let state_changes = create_state_changes_for_test(&mut state, sender_address, fee_token_address); let state_changes_count = state_changes.count_for_fee_charge(sender_address, fee_token_address); - let expected_state_changes_count = StateChangesCount { + let expected_state_changes_count = StateChangesCountForFee { // 1 for storage update + 1 for sender balance update if sender is defined. - n_storage_updates: 1 + usize::from(sender_address.is_some()), - n_class_hash_updates: 1, - n_compiled_class_hash_updates: 1, - n_modified_contracts: 2, + state_changes_count: StateChangesCount { + n_storage_updates: 1 + usize::from(sender_address.is_some()), + n_class_hash_updates: 1, + n_compiled_class_hash_updates: 1, + n_modified_contracts: 2, + }, + n_allocated_keys: 0, }; assert_eq!(state_changes_count, expected_state_changes_count); } diff --git a/crates/blockifier/src/transaction/account_transactions_test.rs b/crates/blockifier/src/transaction/account_transactions_test.rs index 52e040fcdec..f180cf85783 100644 --- a/crates/blockifier/src/transaction/account_transactions_test.rs +++ b/crates/blockifier/src/transaction/account_transactions_test.rs @@ -59,7 +59,7 @@ use crate::execution::entry_point::EntryPointExecutionContext; use crate::execution::syscalls::SyscallSelector; use crate::fee::fee_utils::{get_fee_by_gas_vector, get_sequencer_balance_keys}; use crate::fee::gas_usage::estimate_minimal_gas_vector; -use crate::state::cached_state::{StateChangesCount, TransactionalState}; +use crate::state::cached_state::{StateChangesCount, StateChangesCountForFee, TransactionalState}; use crate::state::state_api::{State, StateReader}; use crate::test_utils::contracts::FeatureContract; use crate::test_utils::declare::declare_tx; @@ -1386,13 +1386,16 @@ fn test_count_actual_storage_changes( let state_changes_count_1 = state_changes_1.clone().count_for_fee_charge(Some(account_address), fee_token_address); - let expected_state_changes_count_1 = StateChangesCount { - // See expected storage updates. - n_storage_updates: 3, - // The contract address (storage update) and the account address (nonce update). Does not - // include the fee token address as a modified contract. - n_modified_contracts: 2, - ..Default::default() + let expected_state_changes_count_1 = StateChangesCountForFee { + state_changes_count: StateChangesCount { + // See expected storage updates. + n_storage_updates: 3, + // The contract address (storage update) and the account address (nonce update). Does + // not include the fee token address as a modified contract. + n_modified_contracts: 2, + ..Default::default() + }, + n_allocated_keys: 0, }; assert_eq!(expected_modified_contracts, state_changes_1.state_maps.get_modified_contracts()); @@ -1423,13 +1426,16 @@ fn test_count_actual_storage_changes( let state_changes_count_2 = state_changes_2.clone().count_for_fee_charge(Some(account_address), fee_token_address); - let expected_state_changes_count_2 = StateChangesCount { - // See expected storage updates. - n_storage_updates: 2, - // The account address (nonce update). Does not include the fee token address as a modified - // contract. - n_modified_contracts: 1, - ..Default::default() + let expected_state_changes_count_2 = StateChangesCountForFee { + state_changes_count: StateChangesCount { + // See expected storage updates. + n_storage_updates: 2, + // The account address (nonce update). Does not include the fee token address as a + // modified contract. + n_modified_contracts: 1, + ..Default::default() + }, + n_allocated_keys: 0, }; assert_eq!(expected_modified_contracts_2, state_changes_2.state_maps.get_modified_contracts()); @@ -1469,13 +1475,16 @@ fn test_count_actual_storage_changes( let state_changes_count_3 = state_changes_transfer .clone() .count_for_fee_charge(Some(account_address), fee_token_address); - let expected_state_changes_count_3 = StateChangesCount { - // See expected storage updates. - n_storage_updates: 3, - // The account address (nonce update). Does not include the fee token address as a modified - // contract. - n_modified_contracts: 1, - ..Default::default() + let expected_state_changes_count_3 = StateChangesCountForFee { + state_changes_count: StateChangesCount { + // See expected storage updates. + n_storage_updates: 3, + // The account address (nonce update). Does not include the fee token address as a + // modified contract. + n_modified_contracts: 1, + ..Default::default() + }, + n_allocated_keys: 0, }; assert_eq!( diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 5539a10e36c..f07b2c8ecbb 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -470,7 +470,7 @@ fn test_invoke_tx( calldata_length, signature_length, 0, - StateResources::new_for_testing(state_changes_for_fee), + StateResources::new_for_testing(state_changes_for_fee, 0), None, ExecutionSummary::default(), ); @@ -1506,7 +1506,7 @@ fn test_declare_tx( 0, 0, class_info.code_size(), - StateResources::new_for_testing(state_changes_for_fee), + StateResources::new_for_testing(state_changes_for_fee, 0), None, ExecutionSummary::default(), );