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

Commit

Permalink
perf(execution): avoid cloning the transactions for the trace… (#1142)
Browse files Browse the repository at this point in the history
perf(execution): avoid cloning the transactions for the trace calculation
  • Loading branch information
yair-starkware authored Sep 6, 2023
1 parent 5f151a8 commit 7ca05da
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 35 deletions.
31 changes: 31 additions & 0 deletions crates/papyrus_execution/src/execution_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use blockifier::execution::contract_class::{
ContractClassV0,
ContractClassV1,
};
use blockifier::transaction::objects::TransactionExecutionInfo;
use cairo_vm::types::errors::program_errors::ProgramError;
use papyrus_storage::compiled_class::CasmStorageReader;
use papyrus_storage::db::RO;
Expand All @@ -16,6 +17,9 @@ use starknet_api::core::ClassHash;
use starknet_api::state::StateNumber;
use thiserror::Error;

use crate::objects::TransactionTrace;
use crate::ExecutableTransactionInput;

// An error that can occur during the use of the execution utils.
#[derive(Debug, Error)]
pub(crate) enum ExecutionUtilsError {
Expand Down Expand Up @@ -54,3 +58,30 @@ pub(crate) fn get_contract_class(
ContractClassV0::try_from(deprecated_class).map_err(ExecutionUtilsError::ProgramError)?,
)))
}

/// Given an ExecutableTransactionInput, returns a function that will convert the corresponding
/// TransactionExecutionInfo into the right TransactionTrace variant.
pub fn get_trace_constructor(
tx: &ExecutableTransactionInput,
) -> fn(TransactionExecutionInfo) -> TransactionTrace {
match tx {
ExecutableTransactionInput::Invoke(_) => {
|execution_info| TransactionTrace::Invoke(execution_info.into())
}
ExecutableTransactionInput::DeclareV0(_, _) => {
|execution_info| TransactionTrace::Declare(execution_info.into())
}
ExecutableTransactionInput::DeclareV1(_, _) => {
|execution_info| TransactionTrace::Declare(execution_info.into())
}
ExecutableTransactionInput::DeclareV2(_, _) => {
|execution_info| TransactionTrace::Declare(execution_info.into())
}
ExecutableTransactionInput::Deploy(_) => {
|execution_info| TransactionTrace::DeployAccount(execution_info.into())
}
ExecutableTransactionInput::L1Handler(_, _) => {
|execution_info| TransactionTrace::L1Handler(execution_info.into())
}
}
}
47 changes: 12 additions & 35 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use blockifier::transaction::transaction_execution::Transaction as BlockifierTra
use blockifier::transaction::transactions::ExecutableTransaction;
use cairo_lang_starknet::casm_contract_class::CasmContractClass;
use cairo_vm::types::errors::program_errors::ProgramError;
use execution_utils::get_trace_constructor;
use objects::TransactionTrace;
use papyrus_config::dumping::{ser_param, SerializeConfig};
use papyrus_config::{ParamPath, SerializedParam};
Expand Down Expand Up @@ -486,10 +487,9 @@ pub fn simulate_transactions(
charge_fee: bool,
validate: bool,
) -> ExecutionResult<Vec<(TransactionTrace, GasPrice, Fee)>> {
let trace_constructors = txs.iter().map(get_trace_constructor).collect::<Vec<_>>();
let (txs_execution_info, block_context) = execute_transactions(
// TODO(yair): Modify execute_transactions so it doesn't consume the txs / save the tx
// types before consuming them.
txs.clone(),
txs,
tx_hashes,
chain_id,
storage_txn,
Expand All @@ -498,37 +498,14 @@ pub fn simulate_transactions(
charge_fee,
validate,
)?;
Ok(txs
.iter()
.zip(txs_execution_info)
.map(|(tx, exec_info)| calc_trace_and_fee(tx, exec_info, &block_context))
.collect())
}

fn calc_trace_and_fee(
tx: &ExecutableTransactionInput,
execution_info: TransactionExecutionInfo,
block_context: &BlockContext,
) -> (TransactionTrace, GasPrice, Fee) {
let gas_price = GasPrice(block_context.gas_price);
let fee = execution_info.actual_fee;
let trace = match tx {
ExecutableTransactionInput::Invoke(_) => TransactionTrace::Invoke(execution_info.into()),
ExecutableTransactionInput::DeclareV0(_, _) => {
TransactionTrace::Declare(execution_info.into())
}
ExecutableTransactionInput::DeclareV1(_, _) => {
TransactionTrace::Declare(execution_info.into())
}
ExecutableTransactionInput::DeclareV2(_, _) => {
TransactionTrace::Declare(execution_info.into())
}
ExecutableTransactionInput::Deploy(_) => {
TransactionTrace::DeployAccount(execution_info.into())
}
ExecutableTransactionInput::L1Handler(_, _) => {
TransactionTrace::L1Handler(execution_info.into())
}
};
(trace, gas_price, fee)
Ok(txs_execution_info
.into_iter()
.zip(trace_constructors)
.map(|(execution_info, trace_constructor)| {
let fee = execution_info.actual_fee;
let trace = trace_constructor(execution_info);
(trace, gas_price, fee)
})
.collect())
}

0 comments on commit 7ca05da

Please sign in to comment.