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 26, 2023
1 parent 6cb1e26 commit 2e97b56
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
20 changes: 20 additions & 0 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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;
Expand Down Expand Up @@ -55,6 +56,25 @@ pub fn calculate_tx_gas_usage(
starknet_gas_usage + sharp_gas_usage
}

pub fn calculate_message_segment_size(
validate_call_info: &Option<CallInfo>,
execute_call_info: &Option<CallInfo>,
l1_handler_payload_size: Option<usize>,
) -> usize {
let call_infos: Vec<&CallInfo> = [validate_call_info, execute_call_info]
.iter()
.filter_map(|&call_info| call_info.as_ref())
.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
4 changes: 4 additions & 0 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ impl L1HandlerTransaction {
max_fee: Fee::default(),
})
}

pub fn get_payload_size(&self) -> Option<usize> {
Some(self.tx.calldata.0.len() - 1)
}
}

impl HasRelatedFeeType for L1HandlerTransaction {
Expand Down
20 changes: 15 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_message_segment_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 {
l1_handler_tx.get_payload_size()
} 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,24 @@ 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 message_segment_size = calculate_message_segment_size(
&tx_execution_info.validate_call_info,
&tx_execution_info.execute_call_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: message_segment_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 2e97b56

Please sign in to comment.