Skip to content

Commit

Permalink
tracing: implement FlatCallTracer (#240)
Browse files Browse the repository at this point in the history
closes #201

BTW, this will also change the public method `try_into_mux_frame`, so
need to adjust in reth's side.

---------

Signed-off-by: jsvisa <delweng@gmail.com>
  • Loading branch information
jsvisa authored Nov 26, 2024
1 parent bbe7a7c commit f54634d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
25 changes: 23 additions & 2 deletions src/tracing/mux.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::tracing::{FourByteInspector, TracingInspector, TracingInspectorConfig};
use alloy_primitives::{map::HashMap, Address, Log, U256};
use alloy_rpc_types_eth::TransactionInfo;
use alloy_rpc_types_trace::geth::{
mux::{MuxConfig, MuxFrame},
CallConfig, FourByteFrame, GethDebugBuiltInTracerType, NoopFrame, PreStateConfig,
CallConfig, FlatCallConfig, FourByteFrame, GethDebugBuiltInTracerType, NoopFrame,
PreStateConfig,
};
use revm::{
interpreter::{
Expand All @@ -29,6 +31,7 @@ pub struct MuxInspector {
enum TraceConfig {
Call(CallConfig),
PreState(PreStateConfig),
FlatCall(FlatCallConfig),
Noop,
}

Expand Down Expand Up @@ -73,7 +76,13 @@ impl MuxInspector {
configs.push((tracer_type, TraceConfig::Noop));
}
GethDebugBuiltInTracerType::FlatCallTracer => {
return Err(Error::UnexpectedConfig(tracer_type));
let flatcall_config = tracer_config
.ok_or(Error::MissingConfig(tracer_type))?
.into_flat_call_config()?;

inspector_config
.merge(TracingInspectorConfig::from_flat_call_config(&flatcall_config));
configs.push((tracer_type, TraceConfig::FlatCall(flatcall_config)));
}
GethDebugBuiltInTracerType::MuxTracer => {
return Err(Error::UnexpectedConfig(tracer_type));
Expand All @@ -91,6 +100,7 @@ impl MuxInspector {
&self,
result: &ResultAndState,
db: &DB,
tx_info: TransactionInfo,
) -> Result<MuxFrame, DB::Error> {
let mut frame = HashMap::with_capacity_and_hasher(self.configs.len(), Default::default());

Expand All @@ -116,6 +126,17 @@ impl MuxInspector {
continue;
}
}
TraceConfig::FlatCall(_flatcall_config) => {
if let Some(inspector) = &self.tracing {
inspector
.clone()
.into_parity_builder()
.into_localized_transaction_traces(tx_info)
.into()
} else {
continue;
}
}
TraceConfig::Noop => NoopFrame::default().into(),
};

Expand Down
30 changes: 26 additions & 4 deletions tests/it/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
use crate::utils::{deploy_contract, inspect};
use alloy_primitives::{hex, map::HashMap, Address, Bytes};
use alloy_rpc_types_eth::TransactionInfo;
use alloy_rpc_types_trace::geth::{
mux::MuxConfig, CallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig, GethTrace,
PreStateConfig, PreStateFrame,
mux::MuxConfig, CallConfig, FlatCallConfig, GethDebugBuiltInTracerType, GethDebugTracerConfig,
GethTrace, PreStateConfig, PreStateFrame,
};
use revm::{
db::{CacheDB, EmptyDB},
Expand Down Expand Up @@ -137,6 +138,8 @@ fn test_geth_mux_tracer() {
let (addr, mut evm) = deploy_contract(code.into(), deployer, SpecId::LONDON);

let call_config = CallConfig { only_top_call: Some(false), with_log: Some(true) };
let flatcall_config =
FlatCallConfig { convert_parity_errors: Some(true), include_precompiles: None };
let prestate_config = PreStateConfig { diff_mode: Some(false), ..Default::default() };

let config = MuxConfig(HashMap::from_iter([
Expand All @@ -149,6 +152,10 @@ fn test_geth_mux_tracer() {
GethDebugBuiltInTracerType::PreStateTracer,
Some(GethDebugTracerConfig(serde_json::to_value(prestate_config).unwrap())),
),
(
GethDebugBuiltInTracerType::FlatCallTracer,
Some(GethDebugTracerConfig(serde_json::to_value(flatcall_config).unwrap())),
),
]));

let mut insp = MuxInspector::try_from_config(config.clone()).unwrap();
Expand All @@ -164,12 +171,13 @@ fn test_geth_mux_tracer() {
let (res, _) = inspect(&mut evm.db, env, &mut insp).unwrap();
assert!(res.result.is_success());

let frame = insp.try_into_mux_frame(&res, &evm.db).unwrap();
let frame = insp.try_into_mux_frame(&res, &evm.db, TransactionInfo::default()).unwrap();

assert_eq!(frame.0.len(), 3);
assert_eq!(frame.0.len(), 4);
assert!(frame.0.contains_key(&GethDebugBuiltInTracerType::FourByteTracer));
assert!(frame.0.contains_key(&GethDebugBuiltInTracerType::CallTracer));
assert!(frame.0.contains_key(&GethDebugBuiltInTracerType::PreStateTracer));
assert!(frame.0.contains_key(&GethDebugBuiltInTracerType::FlatCallTracer));

let four_byte_frame = frame.0[&GethDebugBuiltInTracerType::FourByteTracer].clone();
match four_byte_frame {
Expand Down Expand Up @@ -203,6 +211,20 @@ fn test_geth_mux_tracer() {
}
_ => panic!("Expected PreStateTracer"),
}

let flatcall_frame = frame.0[&GethDebugBuiltInTracerType::FlatCallTracer].clone();
match flatcall_frame {
GethTrace::FlatCallTracer(traces) => {
assert_eq!(traces.len(), 6);
assert!(traces[0].trace.error.is_none());
assert!(traces[1].trace.error.is_some());
assert!(traces[2].trace.error.is_some());
assert!(traces[3].trace.error.is_none());
assert!(traces[4].trace.error.is_none());
assert!(traces[5].trace.error.is_none());
}
_ => panic!("Expected FlatCallTracer"),
}
}

#[test]
Expand Down

0 comments on commit f54634d

Please sign in to comment.