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

Commit

Permalink
Calculates messages size field
Browse files Browse the repository at this point in the history
  • Loading branch information
ayeletstarkware committed Dec 25, 2023
1 parent 6cb1e26 commit e48cefa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
22 changes: 21 additions & 1 deletion crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use starknet_api::transaction::Fee;
use super::fee_utils::{calculate_tx_l1_gas_usage, get_fee_by_l1_gas_usage};
use crate::abi::constants;
use crate::block_context::BlockContext;
use crate::execution::call_info::CallInfo;
use crate::fee::eth_gas_constants;
use crate::fee::os_resources::OS_RESOURCES;
use crate::state::cached_state::StateChangesCount;
use crate::transaction::account_transaction::AccountTransaction;
use crate::transaction::objects::{
HasRelatedFeeType, ResourcesMapping, TransactionExecutionResult, TransactionPreValidationResult,
HasRelatedFeeType, ResourcesMapping, TransactionExecutionInfo, TransactionExecutionResult,
TransactionPreValidationResult,
};

#[cfg(test)]
Expand Down Expand Up @@ -55,6 +57,24 @@ pub fn calculate_tx_gas_usage(
starknet_gas_usage + sharp_gas_usage
}

pub fn calculate_messages_size(
tx_info: &TransactionExecutionInfo,
l1_handler_payload_size: Option<usize>,
) -> usize {
let call_infos: Vec<&CallInfo> = vec![&tx_info.validate_call_info, &tx_info.execute_call_info]
.into_iter()
.flatten()
.collect::<Vec<&CallInfo>>();

let mut l2_to_l1_payloads_length = Vec::new();
for call_info in &call_infos {
l2_to_l1_payloads_length
.extend(call_info.get_sorted_l2_to_l1_payloads_length().into_iter().flatten());
}

get_message_segment_length(&l2_to_l1_payloads_length, l1_handler_payload_size)
}

/// Returns the number of felts added to the output data availability segment as a result of adding
/// a transaction to a batch. Note that constant cells - such as the one that holds the number of
/// modified contracts - are not counted.
Expand Down
17 changes: 12 additions & 5 deletions crates/native_blockifier/src/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use blockifier::block_execution::pre_process_block;
use blockifier::execution::call_info::CallInfo;
use blockifier::execution::entry_point::ExecutionResources;
use blockifier::fee::actual_cost::ActualCost;
use blockifier::fee::gas_usage::calculate_messages_size;
use blockifier::state::cached_state::{
CachedState, GlobalContractCache, StagedTransactionalState, TransactionalState,
};
Expand Down Expand Up @@ -68,7 +69,12 @@ impl<S: StateReader> TransactionExecutor<S> {
charge_fee: bool,
) -> NativeBlockifierResult<(PyTransactionExecutionInfo, PyBouncerInfo)> {
let tx: Transaction = py_tx(tx, raw_contract_class)?;

let l1_handler_payload_size: Option<usize> =
if let Transaction::L1HandlerTransaction(l1_handler_tx) = &tx {
Some(l1_handler_tx.tx.calldata.0.len() - 1)
} else {
Some(0)
};
let mut tx_executed_class_hashes = HashSet::<ClassHash>::new();
let mut transactional_state = CachedState::create_transactional(&mut self.state);
let validate = true;
Expand All @@ -78,20 +84,21 @@ impl<S: StateReader> TransactionExecutor<S> {
match tx_execution_result {
Ok(tx_execution_info) => {
tx_executed_class_hashes.extend(tx_execution_info.get_executed_class_hashes());

let py_tx_execution_info = PyTransactionExecutionInfo::from(tx_execution_info);
let messages_size =
calculate_messages_size(&tx_execution_info, l1_handler_payload_size);
let py_casm_hash_calculation_resources = get_casm_hash_calculation_resources(
&mut transactional_state,
&self.executed_class_hashes,
&tx_executed_class_hashes,
)?;
let py_bouncer_info = PyBouncerInfo {
messages_size: 0,
messages_size,
casm_hash_calculation_resources: py_casm_hash_calculation_resources,
};

self.staged_for_commit_state =
Some(transactional_state.stage(tx_executed_class_hashes));
let py_tx_execution_info = PyTransactionExecutionInfo::from(tx_execution_info);

Ok((py_tx_execution_info, py_bouncer_info))
}
Err(error) => {
Expand Down

0 comments on commit e48cefa

Please sign in to comment.