Skip to content

Commit

Permalink
fix(simulation): correctly handle tokio runtime for traces
Browse files Browse the repository at this point in the history
  • Loading branch information
zizou0x committed Nov 19, 2024
1 parent d12f1ba commit 62309a6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
25 changes: 16 additions & 9 deletions src/evm/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use revm::{
use revm_inspectors::tracing::{TracingInspector, TracingInspectorConfig};
use std::clone::Clone;
use strum_macros::Display;
use tokio::runtime::Runtime;
use tokio::runtime::{Handle, Runtime};
use tracing::{debug, info};

use crate::evm::simulation_db::OverriddenSimulationDB;
Expand Down Expand Up @@ -171,14 +171,21 @@ where
gas_used,
};

let runtime = tokio::runtime::Handle::try_current()
.is_err()
.then(|| Runtime::new().unwrap())
.unwrap();

runtime
.block_on(handle_traces(trace_res, &Config::default(), Some(Chain::default()), true))
.expect("failure handling traces");
tokio::task::block_in_place(|| {
let future = async {
handle_traces(trace_res, &Config::default(), Some(Chain::default()), true)
.await
.expect("failure handling traces");
};
if let Ok(handle) = Handle::try_current() {
// If successful, use the existing runtime to block on the future
handle.block_on(future)
} else {
// If no runtime is found, create a new one and block on the future
let rt = Runtime::new().expect("Failed to create a new runtime");
rt.block_on(future)
}
});
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/evm/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ pub async fn print_traces(
.as_mut()
.expect("No traces found");

println!("Traces:");
let mut traces_strings = Vec::new();
for (_, arena) in traces {
decode_trace_arena(arena, decoder).await?;
println!("{}", render_trace_arena(arena));
traces_strings.push(render_trace_arena(arena));
}
println!("Traces:");
for t in traces_strings {
println!("{}", t);
}
println!();

Expand Down

0 comments on commit 62309a6

Please sign in to comment.