Skip to content

Commit c4eb11c

Browse files
authored
feat: more inspector layering tools (#121)
* feat: more inspector layering tools * chore: bump version * lint: cluppy
1 parent 72c4a83 commit c4eb11c

File tree

5 files changed

+122
-4
lines changed

5 files changed

+122
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "trevm"
3-
version = "0.27.7"
3+
version = "0.27.8"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/evm.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,66 @@ where
828828
}
829829
}
830830

831+
// Layered inspector
832+
impl<Db, Outer, Inner, TrevmState> Trevm<Db, Layered<Outer, Inner>, TrevmState>
833+
where
834+
Db: Database,
835+
Outer: Inspector<Ctx<Db>>,
836+
Inner: Inspector<Ctx<Db>>,
837+
{
838+
/// Remove the outer-layer inspector, leaving the inner-layer inspector in
839+
/// place.
840+
pub fn take_outer(self) -> (Outer, Trevm<Db, Inner, TrevmState>) {
841+
let (outer, inner) = self.inner.inspector.into_parts();
842+
843+
(
844+
outer,
845+
Trevm {
846+
inner: Box::new(Evm {
847+
ctx: self.inner.ctx,
848+
inspector: inner,
849+
instruction: self.inner.instruction,
850+
precompiles: self.inner.precompiles,
851+
frame_stack: self.inner.frame_stack,
852+
}),
853+
state: self.state,
854+
},
855+
)
856+
}
857+
858+
/// Remove the outer-layer inspector, leaving the inner-layer inspector in
859+
/// place.
860+
pub fn remove_outer(self) -> Trevm<Db, Inner, TrevmState> {
861+
self.take_outer().1
862+
}
863+
864+
/// Remove the inner-layer inspector, leaving the outer-layer inspector in
865+
/// place.
866+
pub fn take_inner(self) -> (Inner, Trevm<Db, Outer, TrevmState>) {
867+
let (outer, inner) = self.inner.inspector.into_parts();
868+
869+
(
870+
inner,
871+
Trevm {
872+
inner: Box::new(Evm {
873+
ctx: self.inner.ctx,
874+
inspector: outer,
875+
instruction: self.inner.instruction,
876+
precompiles: self.inner.precompiles,
877+
frame_stack: self.inner.frame_stack,
878+
}),
879+
state: self.state,
880+
},
881+
)
882+
}
883+
884+
/// Remove the inner-layer inspector, leaving the outer-layer inspector in
885+
/// place.
886+
pub fn remove_inner(self) -> Trevm<Db, Outer, TrevmState> {
887+
self.take_inner().1
888+
}
889+
}
890+
831891
// --- ALL STATES, WITH State<Db>
832892

833893
impl<Db, Insp, TrevmState> Trevm<Db, Insp, TrevmState>

src/inspectors/layer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,18 @@ impl<Outer, Inner> Layered<Outer, Inner> {
4141
Layered { outer: self, inner }
4242
}
4343

44-
/// Get a reference to the current inspector.
44+
/// Decompose the [`Layered`] inspector into its outer and inner
45+
/// inspectors.
46+
pub fn into_parts(self) -> (Outer, Inner) {
47+
(self.outer, self.inner)
48+
}
49+
50+
/// Get a reference to the outer inspector.
4551
pub const fn outer(&self) -> &Outer {
4652
&self.outer
4753
}
4854

49-
/// Get a mutable reference to the current inspector.
55+
/// Get a mutable reference to the outer inspector.
5056
pub const fn outer_mut(&mut self) -> &mut Outer {
5157
&mut self.outer
5258
}

src/inspectors/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pub use timeout::TimeLimit;
66

77
#[cfg(feature = "tracing-inspectors")]
88
mod tracing;
9+
#[cfg(feature = "tracing-inspectors")]
10+
pub use tracing::TracingInspectorOutput;
911

1012
mod set;
1113
pub use set::InspectorSet;

src/inspectors/tracing.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,57 @@
11
use crate::{helpers::Ctx, inspectors::InspectorWithOutput};
22
use alloy::rpc::types::trace::geth::FourByteFrame;
33
use revm::Database;
4-
use revm_inspectors::tracing::FourByteInspector;
4+
use revm_inspectors::tracing::{
5+
CallTraceArena, FourByteInspector, GethTraceBuilder, ParityTraceBuilder, TracingInspector,
6+
};
7+
8+
/// Output produced by the [`TracingInspector`].
9+
#[derive(Clone, Debug)]
10+
pub struct TracingInspectorOutput {
11+
traces: CallTraceArena,
12+
}
13+
14+
impl TracingInspectorOutput {
15+
/// Creates a new output instance.
16+
pub const fn new(traces: CallTraceArena) -> Self {
17+
Self { traces }
18+
}
19+
20+
/// Returns a reference to the traces produced by the inspector.
21+
pub const fn traces(&self) -> &CallTraceArena {
22+
&self.traces
23+
}
24+
25+
/// Consumes the output and produces a Parity trace builder.
26+
pub fn into_parity_builder(self) -> ParityTraceBuilder {
27+
// NB: the second arguments are actually currently unused. This is
28+
// weird.
29+
ParityTraceBuilder::new(self.traces.into_nodes(), None, Default::default())
30+
}
31+
32+
/// Consumes the output and produces a Geth trace builder.
33+
pub fn into_geth_builder(self) -> GethTraceBuilder<'static> {
34+
GethTraceBuilder::new(self.traces.into_nodes())
35+
}
36+
}
37+
38+
impl<Db: Database> InspectorWithOutput<Ctx<Db>> for TracingInspector {
39+
type Output = TracingInspectorOutput;
40+
41+
fn has_output(&self) -> bool {
42+
!self.traces().nodes().is_empty()
43+
}
44+
45+
fn reset_output(&mut self) {
46+
self.fuse();
47+
}
48+
49+
fn take_output(&mut self) -> Self::Output {
50+
let this = self.clone();
51+
self.fuse();
52+
TracingInspectorOutput::new(this.into_traces())
53+
}
54+
}
555

656
impl<Db: Database> InspectorWithOutput<Ctx<Db>> for FourByteInspector {
757
type Output = FourByteFrame;

0 commit comments

Comments
 (0)