Skip to content

Commit

Permalink
feat(blockifier): add n_allocated_aliases to tx receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs committed Nov 11, 2024
1 parent 5c1e4fa commit 3397acc
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 14 deletions.
1 change: 1 addition & 0 deletions crates/blockifier/src/blockifier/stateful_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<S: StateReader> StatefulValidator<S> {
&execution_resources,
CallInfo::summarize_many(validate_call_info.iter()),
0,
0,
);

Ok((validate_call_info, tx_receipt))
Expand Down
1 change: 1 addition & 0 deletions crates/blockifier/src/execution.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod alias_keys;
pub mod call_info;
pub mod common_hints;
pub mod contract_address;
Expand Down
11 changes: 11 additions & 0 deletions crates/blockifier/src/execution/alias_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::state::cached_state::{CachedState, StateChanges};
use crate::state::state_api::{StateReader, StateResult};

/// Returns the number of aliases we charge the transaction for.
pub fn n_charged_invoke_aliases<S: StateReader>(
_state: &CachedState<S>,
_state_changes: &StateChanges,
) -> StateResult<usize> {
// TODO: Implement this function
Ok(0)
}
12 changes: 11 additions & 1 deletion crates/blockifier/src/fee/receipt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ struct TransactionReceiptParameters<'a> {
execution_resources: &'a ExecutionResources,
tx_type: TransactionType,
reverted_steps: usize,
n_aliases_for_fee: usize,
}

// TODO(Gilad): Use everywhere instead of passing the `actual_{fee,resources}` tuple, which often
Expand Down Expand Up @@ -61,13 +62,19 @@ impl TransactionReceipt {
execution_resources,
tx_type,
reverted_steps,
n_aliases_for_fee,
} = 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_aliases_for_fee,
),
l1_handler_payload_size,
execution_summary_without_fee_transfer,
);
Expand Down Expand Up @@ -128,6 +135,7 @@ impl TransactionReceipt {
execution_resources,
tx_type: TransactionType::L1Handler,
reverted_steps: 0,
n_aliases_for_fee: 0,
})
}

Expand All @@ -139,6 +147,7 @@ impl TransactionReceipt {
execution_resources: &'a ExecutionResources,
execution_summary_without_fee_transfer: ExecutionSummary,
reverted_steps: usize,
n_aliases_for_fee: usize,
) -> Self {
Self::from_params(TransactionReceiptParameters {
tx_context,
Expand All @@ -152,6 +161,7 @@ impl TransactionReceipt {
execution_resources,
tx_type: account_tx.tx_type(),
reverted_steps,
n_aliases_for_fee,
})
}
}
14 changes: 9 additions & 5 deletions crates/blockifier/src/fee/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,28 @@ impl StarknetResources {
#[cfg_attr(feature = "transaction_serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Default, PartialEq)]
pub struct StateResources {
/// The state changes takes into account n_aliases_for_fee.
state_changes_for_fee: StateChangesCount,
pub n_aliases_for_fee: usize,
}

impl StateResources {
pub fn new(
state_changes: &StateChanges,
sender_address: Option<ContractAddress>,
fee_token_address: ContractAddress,
n_aliases_for_fee: usize,
) -> Self {
Self {
state_changes_for_fee: state_changes
.count_for_fee_charge(sender_address, fee_token_address),
}
let mut state_changes_for_fee =
state_changes.count_for_fee_charge(sender_address, fee_token_address);
state_changes_for_fee.n_storage_updates += n_aliases_for_fee;
Self { state_changes_for_fee, n_aliases_for_fee }

}

#[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_aliases_for_fee: 0 }
}

/// Returns the gas cost of the transaction's state changes.
Expand Down
41 changes: 34 additions & 7 deletions crates/blockifier/src/transaction/account_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down Expand Up @@ -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_aliases_for_fee =
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_aliases_for_fee,
);

let post_execution_report =
Expand Down Expand Up @@ -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_aliases_for_fee = 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_aliases_for_fee,
);
// Post-execution checks.
let post_execution_report = PostExecutionReport::new(
Expand Down Expand Up @@ -789,6 +799,23 @@ impl AccountTransaction {

self.run_revertible(state, tx_context, remaining_gas, validate, charge_fee)
}

fn n_charged_aliases<S: StateReader>(
&self,
tx_context: Arc<TransactionContext>,
state: &CachedState<S>,
state_changes: &StateChanges,
) -> StateResult<usize> {
if tx_context.as_ref().block_context.versioned_constants.enable_stateful_compression {
Ok(match self {
Self::Declare(_) => 1, // Charge for introducing a new class hash.
Self::DeployAccount(_) => 1, // Charge for introducing a new contract address.
Self::Invoke(_) => n_charged_invoke_aliases(state, state_changes)?,
})
} else {
Ok(0)
}
}
}

impl<U: UpdatableState> ExecutableTransaction<U> for AccountTransaction {
Expand Down
3 changes: 2 additions & 1 deletion crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,14 +1498,15 @@ 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(),
StateResources::new_for_testing(state_changes_for_fee),
None,
ExecutionSummary::default(),
);
starknet_resources.state.n_aliases_for_fee = 1; // Declare tx.
let account_tx = declare_tx(
declare_tx_args! {
max_fee: MAX_FEE,
Expand Down

0 comments on commit 3397acc

Please sign in to comment.