From dca476b61080d2dfba2a9a828cd995db4a42b07d Mon Sep 17 00:00:00 2001 From: Yoav Gross Date: Sun, 10 Nov 2024 15:38:08 +0200 Subject: [PATCH] feat(blockifier): add n_allocated_aliases to tx receipt --- .../src/blockifier/stateful_validator.rs | 1 + crates/blockifier/src/execution.rs | 1 + crates/blockifier/src/execution/alias_keys.rs | 11 +++++ crates/blockifier/src/fee/receipt.rs | 12 +++++- crates/blockifier/src/fee/resources.rs | 5 ++- .../src/transaction/account_transaction.rs | 41 +++++++++++++++---- .../src/transaction/transactions_test.rs | 3 +- 7 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 crates/blockifier/src/execution/alias_keys.rs diff --git a/crates/blockifier/src/blockifier/stateful_validator.rs b/crates/blockifier/src/blockifier/stateful_validator.rs index 14964e1a56..6a52f7b094 100644 --- a/crates/blockifier/src/blockifier/stateful_validator.rs +++ b/crates/blockifier/src/blockifier/stateful_validator.rs @@ -135,6 +135,7 @@ impl StatefulValidator { &execution_resources, CallInfo::summarize_many(validate_call_info.iter()), 0, + 0, ); Ok((validate_call_info, tx_receipt)) diff --git a/crates/blockifier/src/execution.rs b/crates/blockifier/src/execution.rs index e3b5567d77..f2ddf27dce 100644 --- a/crates/blockifier/src/execution.rs +++ b/crates/blockifier/src/execution.rs @@ -1,3 +1,4 @@ +pub mod alias_keys; pub mod call_info; pub mod common_hints; pub mod contract_address; diff --git a/crates/blockifier/src/execution/alias_keys.rs b/crates/blockifier/src/execution/alias_keys.rs new file mode 100644 index 0000000000..dbf90e6874 --- /dev/null +++ b/crates/blockifier/src/execution/alias_keys.rs @@ -0,0 +1,11 @@ +use crate::state::cached_state::{CachedState, StateChanges}; +use crate::state::state_api::{StateReader, StateResult}; + +/// Returns the number of charged new allocated aliases. +pub fn n_charged_invoke_aliases( + _state: &CachedState, + _state_changes: &StateChanges, +) -> StateResult { + // TODO: Implement this function + Ok(0) +} diff --git a/crates/blockifier/src/fee/receipt.rs b/crates/blockifier/src/fee/receipt.rs index 5720417d50..af2c98314e 100644 --- a/crates/blockifier/src/fee/receipt.rs +++ b/crates/blockifier/src/fee/receipt.rs @@ -33,6 +33,7 @@ struct TransactionReceiptParameters<'a> { execution_resources: &'a ExecutionResources, tx_type: TransactionType, reverted_steps: usize, + n_allocated_aliases: usize, } // TODO(Gilad): Use everywhere instead of passing the `actual_{fee,resources}` tuple, which often @@ -61,13 +62,19 @@ impl TransactionReceipt { execution_resources, tx_type, reverted_steps, + n_allocated_aliases, } = tx_receipt_params; let starknet_resources = StarknetResources::new( calldata_length, signature_length, code_size, - StateResources::new(state_changes, sender_address, tx_context.fee_token_address()), + StateResources::new( + state_changes, + sender_address, + tx_context.fee_token_address(), + n_allocated_aliases, + ), l1_handler_payload_size, execution_summary_without_fee_transfer, ); @@ -128,6 +135,7 @@ impl TransactionReceipt { execution_resources, tx_type: TransactionType::L1Handler, reverted_steps: 0, + n_allocated_aliases: 0, }) } @@ -139,6 +147,7 @@ impl TransactionReceipt { execution_resources: &'a ExecutionResources, execution_summary_without_fee_transfer: ExecutionSummary, reverted_steps: usize, + n_allocated_aliases: usize, ) -> Self { Self::from_params(TransactionReceiptParameters { tx_context, @@ -152,6 +161,7 @@ impl TransactionReceipt { execution_resources, tx_type: account_tx.tx_type(), reverted_steps, + n_allocated_aliases, }) } } diff --git a/crates/blockifier/src/fee/resources.rs b/crates/blockifier/src/fee/resources.rs index 9944a736b9..99cf1ebcd8 100644 --- a/crates/blockifier/src/fee/resources.rs +++ b/crates/blockifier/src/fee/resources.rs @@ -143,6 +143,7 @@ impl StarknetResources { #[derive(Clone, Debug, Default, PartialEq)] pub struct StateResources { state_changes_for_fee: StateChangesCount, + pub n_allocated_aliases: usize, } impl StateResources { @@ -150,16 +151,18 @@ impl StateResources { state_changes: &StateChanges, sender_address: Option, fee_token_address: ContractAddress, + n_allocated_aliases: usize, ) -> Self { Self { state_changes_for_fee: state_changes .count_for_fee_charge(sender_address, fee_token_address), + n_allocated_aliases, } } #[cfg(any(test, feature = "testing"))] pub fn new_for_testing(state_changes_for_fee: StateChangesCount) -> Self { - Self { state_changes_for_fee } + Self { state_changes_for_fee, n_allocated_aliases: 0 } } /// Returns the gas cost of the transaction's state changes. diff --git a/crates/blockifier/src/transaction/account_transaction.rs b/crates/blockifier/src/transaction/account_transaction.rs index 09335d4fa2..f1d38aa4c2 100644 --- a/crates/blockifier/src/transaction/account_transaction.rs +++ b/crates/blockifier/src/transaction/account_transaction.rs @@ -23,6 +23,7 @@ use starknet_types_core::felt::Felt; use crate::abi::abi_utils::selector_from_name; use crate::context::{BlockContext, TransactionContext}; +use crate::execution::alias_keys::n_charged_invoke_aliases; use crate::execution::call_info::CallInfo; use crate::execution::contract_class::RunnableContractClass; use crate::execution::entry_point::{CallEntryPoint, CallType, EntryPointExecutionContext}; @@ -36,8 +37,8 @@ use crate::fee::fee_utils::{ use crate::fee::gas_usage::estimate_minimal_gas_vector; use crate::fee::receipt::TransactionReceipt; use crate::retdata; -use crate::state::cached_state::{StateChanges, TransactionalState}; -use crate::state::state_api::{State, StateReader, UpdatableState}; +use crate::state::cached_state::{CachedState, StateChanges, TransactionalState}; +use crate::state::state_api::{State, StateReader, StateResult, UpdatableState}; use crate::transaction::constants; use crate::transaction::errors::{ TransactionExecutionError, @@ -611,13 +612,17 @@ impl AccountTransaction { self.run_execute(state, &mut resources, &mut execution_context, remaining_gas)?; } + let state_changes = state.get_actual_state_changes()?; + let n_allocated_aliases = + self.n_charged_aliases(tx_context.clone(), state, &state_changes)?; let tx_receipt = TransactionReceipt::from_account_tx( self, &tx_context, - &state.get_actual_state_changes()?, + &state_changes, &resources, CallInfo::summarize_many(validate_call_info.iter().chain(execute_call_info.iter())), 0, + n_allocated_aliases, ); let post_execution_report = @@ -685,24 +690,29 @@ impl AccountTransaction { &resources, CallInfo::summarize_many(validate_call_info.iter()), execution_steps_consumed, + 0, ); match execution_result { Ok(execute_call_info) => { + let execution_state_changes = execution_state.get_actual_state_changes()?; + let n_allocated_aliases = self.n_charged_aliases( + tx_context.clone(), + &execution_state, + &execution_state_changes, + )?; // When execution succeeded, calculate the actual required fee before committing the // transactional state. If max_fee is insufficient, revert the `run_execute` part. let tx_receipt = TransactionReceipt::from_account_tx( self, &tx_context, - &StateChanges::merge(vec![ - validate_state_changes, - execution_state.get_actual_state_changes()?, - ]), + &StateChanges::merge(vec![validate_state_changes, execution_state_changes]), &execution_resources, CallInfo::summarize_many( validate_call_info.iter().chain(execute_call_info.iter()), ), 0, + n_allocated_aliases, ); // Post-execution checks. let post_execution_report = PostExecutionReport::new( @@ -789,6 +799,23 @@ impl AccountTransaction { self.run_revertible(state, tx_context, remaining_gas, validate, charge_fee) } + + fn n_charged_aliases( + &self, + tx_context: Arc, + state: &CachedState, + state_changes: &StateChanges, + ) -> StateResult { + if tx_context.as_ref().block_context.versioned_constants.enable_stateful_compression { + Ok(match self { + Self::Declare(_) => 1, + Self::DeployAccount(_) => 1, + Self::Invoke(_) => n_charged_invoke_aliases(state, state_changes)?, + }) + } else { + Ok(0) + } + } } impl ExecutableTransaction for AccountTransaction { diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index 956c228b65..4a15e2d583 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -1498,7 +1498,7 @@ fn test_declare_tx( let sender_address = account.get_instance_address(0); let mut nonce_manager = NonceManager::default(); let state_changes_for_fee = declare_expected_state_changes_count(tx_version); - let starknet_resources = StarknetResources::new( + let mut starknet_resources = StarknetResources::new( 0, 0, class_info.code_size(), @@ -1506,6 +1506,7 @@ fn test_declare_tx( None, ExecutionSummary::default(), ); + starknet_resources.state.n_allocated_aliases = 1; // Declare tx. let account_tx = declare_tx( declare_tx_args! { max_fee: MAX_FEE,