From 9fd48c9d811466459526089d0bb728ce39c4b53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Gonz=C3=A1lez=20Calder=C3=B3n?= Date: Wed, 23 Oct 2024 17:49:57 -0300 Subject: [PATCH] Fix unwrap in transaction receipt --- Cargo.lock | 7 --- replay/src/main.rs | 80 ++++++++++++++++++++----------- rpc-state-reader/src/execution.rs | 10 +--- 3 files changed, 53 insertions(+), 44 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c919305..14aa128 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2023,12 +2023,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "dotenv" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" - [[package]] name = "downcast" version = "0.11.0" @@ -5144,7 +5138,6 @@ dependencies = [ "cairo-lang-utils", "cairo-native", "cairo-vm", - "dotenv", "flate2", "pretty_assertions_sorted", "serde", diff --git a/replay/src/main.rs b/replay/src/main.rs index ba63dd2..69b0852 100644 --- a/replay/src/main.rs +++ b/replay/src/main.rs @@ -2,11 +2,14 @@ use std::str::FromStr; use blockifier::state::cached_state::CachedState; use blockifier::state::errors::StateError; +use blockifier::transaction::objects::TransactionExecutionInfo; use clap::{Parser, Subcommand}; use rpc_state_reader::execution::execute_tx_configurable; +use rpc_state_reader::objects::RpcTransactionReceipt; use rpc_state_reader::reader::{RpcChain, RpcStateReader}; use starknet_api::block::BlockNumber; +use starknet_api::hash::StarkHash; use starknet_api::transaction::{TransactionExecutionStatus, TransactionHash}; use tracing::{debug, error, info, info_span}; use tracing_subscriber::filter::Directive; @@ -277,7 +280,7 @@ fn show_execution_data( let previous_block_number = BlockNumber(block_number - 1); - let (execution_info, _trace, rpc_receipt) = match execute_tx_configurable( + let execution_info = match execute_tx_configurable( state, &tx_hash, previous_block_number, @@ -292,7 +295,41 @@ fn show_execution_data( } }; - let reverted = execution_info.is_reverted(); + #[cfg(feature = "state_dump")] + { + use std::path::Path; + #[cfg(feature = "only_cairo_vm")] + let root = Path::new("state_dumps/vm"); + #[cfg(not(feature = "only_cairo_vm"))] + let root = Path::new("state_dumps/native"); + let root = root.join(format!("block{}", block_number)); + + let mut path = root.join(&tx_hash); + path.set_extension("json"); + + state_dump::dump_state_diff(state, &execution_info, &path).unwrap(); + } + + let transaction_hash = TransactionHash(StarkHash::from_hex(&tx_hash).unwrap()); + match state.state.get_transaction_receipt(&transaction_hash) { + Ok(rpc_receipt) => { + compare_execution(execution_info, rpc_receipt); + } + Err(_) => { + error!( + transaction_hash = tx_hash, + chain = chain, + "failed to get transaction receipt, could not compare to rpc" + ); + } + }; +} + +fn compare_execution( + execution: TransactionExecutionInfo, + rpc_receipt: RpcTransactionReceipt, +) -> bool { + let reverted = execution.is_reverted(); let rpc_reverted = matches!( rpc_receipt.execution_status, TransactionExecutionStatus::Reverted(_) @@ -300,13 +337,13 @@ fn show_execution_data( let status_matches = reverted == rpc_reverted; - let da_gas = &execution_info.receipt.da_gas; + let da_gas = &execution.receipt.da_gas; let da_gas_str = format!( "{{ l1_da_gas: {}, l1_gas: {} }}", da_gas.l1_data_gas, da_gas.l1_gas ); - let exec_rsc = &execution_info.receipt.resources.starknet_resources; + let exec_rsc = &execution.receipt.resources.starknet_resources; let events_and_msgs = format!( "{{ events_number: {}, l2_to_l1_messages_number: {} }}", @@ -336,6 +373,10 @@ fn show_execution_data( state_changes.n_storage_updates ); + let execution_gas = execution.receipt.fee; + let rpc_gas = rpc_receipt.actual_fee; + debug!(?execution_gas, ?rpc_gas, "execution actual fee"); + if !status_matches || !events_msgs_match { let root_of_error = if !status_matches { "EXECUTION STATUS DIVERGED" @@ -348,51 +389,32 @@ fn show_execution_data( }; error!( - transaction_hash = tx_hash, - chain = chain, reverted, rpc_reverted, root_of_error = root_of_error, - execution_error_message = execution_info.revert_error, + execution_error_message = execution.revert_error, n_events_and_messages = events_and_msgs, rpc_n_events_and_msgs = rpc_events_and_msgs, da_gas = da_gas_str, state_changes_for_fee_str, "rpc and execution status diverged" - ) + ); + + false } else { info!( - transaction_hash = tx_hash, - chain = chain, reverted, rpc_reverted, - execution_error_message = execution_info.revert_error, + execution_error_message = execution.revert_error, n_events_and_messages = events_and_msgs, rpc_n_events_and_msgs = rpc_events_and_msgs, da_gas = da_gas_str, state_changes_for_fee_str, "execution finished successfully" ); - } - - #[cfg(feature = "state_dump")] - { - use std::path::Path; - #[cfg(feature = "only_cairo_vm")] - let root = Path::new("state_dumps/vm"); - #[cfg(not(feature = "only_cairo_vm"))] - let root = Path::new("state_dumps/native"); - let root = root.join(format!("block{}", block_number)); - - let mut path = root.join(tx_hash); - path.set_extension("json"); - state_dump::dump_state_diff(state, &execution_info, &path).unwrap(); + true } - - let execution_gas = execution_info.receipt.fee; - let rpc_gas = rpc_receipt.actual_fee; - debug!(?execution_gas, ?rpc_gas, "execution actual fee"); } fn get_transaction_hashes( diff --git a/rpc-state-reader/src/execution.rs b/rpc-state-reader/src/execution.rs index 92e8dc3..a7d8786 100644 --- a/rpc-state-reader/src/execution.rs +++ b/rpc-state-reader/src/execution.rs @@ -255,11 +255,7 @@ pub fn execute_tx_configurable( skip_validate: bool, skip_nonce_check: bool, charge_fee: bool, -) -> TransactionExecutionResult<( - TransactionExecutionInfo, - RpcTransactionTrace, - RpcTransactionReceipt, -)> { +) -> TransactionExecutionResult { let tx_hash = TransactionHash(StarkHash::from_hex(tx_hash).unwrap()); let tx = state.state.get_transaction(&tx_hash).unwrap(); let block_info = state.state.get_block_info().unwrap(); @@ -272,9 +268,7 @@ pub fn execute_tx_configurable( charge_fee, state, )?; - let trace = state.state.get_transaction_trace(&tx_hash).unwrap(); - let receipt = state.state.get_transaction_receipt(&tx_hash).unwrap(); - Ok((blockifier_exec_info, trace, receipt)) + Ok(blockifier_exec_info) } /// Executes a transaction with blockifier