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 Jan 16, 2024
1 parent 88bc886 commit 57140bb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
4 changes: 1 addition & 3 deletions crates/blockifier/src/fee/gas_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ use crate::transaction::objects::{
#[path = "gas_usage_test.rs"]
pub mod test;

// TODO(Ayelet, 10/1/2024): Use to calculate message segment length in transaction_executer's
// execute
fn calculate_l2_to_l1_payloads_length_and_message_segment_length<'a>(
pub fn calculate_l2_to_l1_payloads_length_and_message_segment_length<'a>(
call_infos: impl Iterator<Item = &'a CallInfo>,
l1_handler_payload_size: Option<usize>,
) -> TransactionExecutionResult<(Vec<usize>, usize)> {
Expand Down
3 changes: 1 addition & 2 deletions crates/blockifier/src/transaction/transaction_execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl<S: StateReader> ExecutableTransaction<S> for L1HandlerTransaction {
let mut remaining_gas = Transaction::initial_gas();
let execute_call_info =
self.run_execute(state, &mut execution_resources, &mut context, &mut remaining_gas)?;
// The calldata includes the "from" field, which is not a part of the payload.
let l1_handler_payload_size = self.tx.calldata.0.len() - 1;
let l1_handler_payload_size = self.payload_size();

let ActualCost { actual_fee, actual_resources } =
ActualCost::builder_for_l1_handler(block_context, tx_context, l1_handler_payload_size)
Expand Down
5 changes: 5 additions & 0 deletions crates/blockifier/src/transaction/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ impl L1HandlerTransaction {
max_fee: Fee::default(),
})
}

pub fn payload_size(&self) -> usize {
// The calldata includes the "from" field, which is not a part of the payload.
self.tx.calldata.0.len() - 1
}
}

impl HasRelatedFeeType for L1HandlerTransaction {
Expand Down
22 changes: 19 additions & 3 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_l2_to_l1_payloads_length_and_message_segment_length;
use blockifier::state::cached_state::{
CachedState, GlobalContractCache, StagedTransactionalState, StorageEntry, TransactionalState,
};
Expand Down Expand Up @@ -74,7 +75,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: usize =
if let Transaction::L1HandlerTransaction(l1_handler_tx) = &tx {
l1_handler_tx.payload_size()
} else {
0
};
let mut tx_executed_class_hashes = HashSet::<ClassHash>::new();
let mut tx_visited_storage_entries = HashSet::<StorageEntry>::new();
let mut transactional_state = CachedState::create_transactional(&mut self.state);
Expand All @@ -88,9 +94,19 @@ impl<S: StateReader> TransactionExecutor<S> {
// TODO(Elin, 01/06/2024): consider traversing the calls to collect data once.
tx_executed_class_hashes.extend(tx_execution_info.get_executed_class_hashes());
tx_visited_storage_entries.extend(tx_execution_info.get_visited_storage_entries());

let call_infos: Vec<&CallInfo> =
[&tx_execution_info.validate_call_info, &tx_execution_info.execute_call_info]
.iter()
.filter_map(|&call_info| call_info.as_ref())
.collect::<Vec<&CallInfo>>();
let (_l2_to_l1_payloads_length, message_segment_length) =
calculate_l2_to_l1_payloads_length_and_message_segment_length(
call_infos.into_iter(),
Some(l1_handler_payload_size),
)?;
// TODO(Elin, 01/06/2024): consider moving Bouncer logic to a function.
let py_tx_execution_info = PyTransactionExecutionInfo::from(tx_execution_info);

let mut additional_os_resources = get_casm_hash_calculation_resources(
&mut transactional_state,
&self.executed_class_hashes,
Expand All @@ -101,7 +117,7 @@ impl<S: StateReader> TransactionExecutor<S> {
&tx_visited_storage_entries,
)?;
let py_bouncer_info = PyBouncerInfo {
message_segment_length: 0,
message_segment_length,
kzg_resources: PyKzgResources { state_diff_size: 0 },
additional_os_resources: PyVmExecutionResources::from(additional_os_resources),
};
Expand Down

0 comments on commit 57140bb

Please sign in to comment.