Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor execution functions with proper error handling #107

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ serde_json = "1.0.116"
serde_with = "3.11.0"
serde = "1.0.197"
cairo-native = "0.2.4"
anyhow = "1.0"
# Sequencer Dependencies
starknet_api = { git = "https://github.com/lambdaclass/sequencer.git", rev = "eb3d5537d2148262ce4dc8edbd7794ec030e017b" } # replay
blockifier = { git = "https://github.com/lambdaclass/sequencer.git", rev = "eb3d5537d2148262ce4dc8edbd7794ec030e017b", features = ["cairo_native"] } # replay
Expand Down
1 change: 1 addition & 0 deletions replay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ serde = { workspace = true, optional = true }
serde_json = { workspace = true, optional = true }
serde_with = { workspace = true, optional = true }
dotenvy = "0.15.7"
anyhow.workspace = true
66 changes: 37 additions & 29 deletions replay/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use blockifier::state::cached_state::CachedState;
use blockifier::state::errors::StateError;
use blockifier::transaction::account_transaction::ExecutionFlags;
use blockifier::transaction::objects::{RevertError, TransactionExecutionInfo};
use blockifier::transaction::transactions::ExecutableTransaction;
use clap::{Parser, Subcommand};

use rpc_state_reader::execution::execute_tx_configurable;
use rpc_state_reader::execution::fetch_transaction;
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::felt;
use starknet_api::transaction::{TransactionExecutionStatus, TransactionHash};
use tracing::{debug, error, info, info_span};
use tracing_subscriber::{util::SubscriberInitExt, EnvFilter};
Expand Down Expand Up @@ -302,25 +304,32 @@ fn build_cached_state(network: &str, block_number: u64) -> CachedState<RpcStateR

fn show_execution_data(
state: &mut CachedState<RpcStateReader>,
tx_hash: String,
chain: &str,
tx_hash_str: String,
chain_str: &str,
block_number: u64,
charge_fee: bool,
) {
let _transaction_execution_span = info_span!("transaction", hash = tx_hash, chain).entered();

let _transaction_execution_span =
info_span!("transaction", hash = tx_hash_str, chain_str).entered();
info!("starting execution");

let previous_block_number = BlockNumber(block_number - 1);

let execution_info = execute_tx_configurable(
state,
&tx_hash,
previous_block_number,
false,
true,
let tx_hash = TransactionHash(felt!(tx_hash_str.as_str()));
let chain = parse_network(chain_str);
let block_number = BlockNumber(block_number);
let flags = ExecutionFlags {
only_query: false,
charge_fee,
);
validate: true,
};

let (tx, context) = match fetch_transaction(&tx_hash, block_number, chain, flags) {
Ok(x) => x,
Err(err) => {
return error!("failed to fetch transaction: {err}");
}
};

let execution_info_result = tx.execute(state, &context);

#[cfg(feature = "state_dump")]
{
Expand All @@ -334,40 +343,39 @@ fn show_execution_data(

std::fs::create_dir_all(&root).ok();

let mut path = root.join(&tx_hash);
let mut path = root.join(&tx_hash_str);
path.set_extension("json");

match &execution_info {
match &execution_info_result {
Ok(execution_info) => {
state_dump::dump_state_diff(state, execution_info, &path).unwrap();
state_dump::dump_state_diff(state, execution_info, &path)
.inspect_err(|err| error!("failed to dump state diff: {err}"))
.ok();
}
Err(err) => {
// If we have no execution info, we write the error
// to a file so that it can be compared anyway
state_dump::dump_error(err, &path).unwrap();
state_dump::dump_error(err, &path)
.inspect_err(|err| error!("failed to dump state diff: {err}"))
.ok();
}
}
}

let execution_info = match execution_info {
let execution_info = match execution_info_result {
Ok(x) => x,
Err(error_reason) => {
error!("execution failed: {}", error_reason);
Err(err) => {
error!("execution failed: {}", err);
return;
}
};

let transaction_hash = TransactionHash(StarkHash::from_hex(&tx_hash).unwrap());
match state.state.get_transaction_receipt(&transaction_hash) {
match state.state.get_transaction_receipt(&tx_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"
);
error!("failed to get transaction receipt, could not compare to rpc");
}
};
}
Expand Down
5 changes: 2 additions & 3 deletions replay/src/state_dump.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
collections::BTreeMap,
error::Error,
fs::{self, File},
path::Path,
};
Expand Down Expand Up @@ -31,7 +30,7 @@ pub fn dump_state_diff(
state: &mut CachedState<impl StateReader>,
execution_info: &TransactionExecutionInfo,
path: &Path,
) -> Result<(), Box<dyn Error>> {
) -> anyhow::Result<()> {
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
Expand All @@ -49,7 +48,7 @@ pub fn dump_state_diff(
Ok(())
}

pub fn dump_error(err: &TransactionExecutionError, path: &Path) -> Result<(), Box<dyn Error>> {
pub fn dump_error(err: &TransactionExecutionError, path: &Path) -> anyhow::Result<()> {
if let Some(parent) = path.parent() {
let _ = fs::create_dir_all(parent);
}
Expand Down
1 change: 1 addition & 0 deletions rpc-state-reader/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ blockifier = { workspace = true }
blockifier_reexecution = { workspace = true }
starknet_gateway = { workspace = true }
tracing = { workspace = true }
anyhow.workspace = true

[dev-dependencies]
pretty_assertions_sorted = "1.2.3"
Expand Down
Loading
Loading