Skip to content

Commit 72c4a83

Browse files
authored
feat: trait for inspectors that accumulate output (#120)
* feat: trait for inspectors that accumulate output * feat: add impl for 4 byte
1 parent 78dc81e commit 72c4a83

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ alloy = { version = "1.0.25", default-features = false, features = [
4545
] }
4646

4747
revm = { version = "27.1", default-features = false }
48+
revm-inspectors = { version = "0.27.1", optional = true }
4849

4950
dashmap = { version = "6.1.0", optional = true }
5051
tracing = { version = "0.1.41", optional = true }
@@ -69,6 +70,7 @@ default = [
6970
"call",
7071
"concurrent-db",
7172
"estimate_gas",
73+
"tracing-inspectors",
7274
"revm/std",
7375
"revm/c-kzg",
7476
"revm/blst",
@@ -111,3 +113,4 @@ full_env_cfg = [
111113
"optional_eip3607",
112114
"optional_no_base_fee",
113115
]
116+
tracing-inspectors = ["dep:revm-inspectors", "alloy/rpc-types-trace"]

src/inspectors/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@ pub use layer::Layered;
44
mod timeout;
55
pub use timeout::TimeLimit;
66

7+
#[cfg(feature = "tracing-inspectors")]
8+
mod tracing;
9+
710
mod set;
811
pub use set::InspectorSet;
912

1013
mod spanning;
1114
pub use spanning::SpanningInspector;
1215

16+
mod with_output;
17+
pub use with_output::InspectorWithOutput;
18+
1319
#[cfg(test)]
1420
mod test {
1521
use super::*;

src/inspectors/tracing.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::{helpers::Ctx, inspectors::InspectorWithOutput};
2+
use alloy::rpc::types::trace::geth::FourByteFrame;
3+
use revm::Database;
4+
use revm_inspectors::tracing::FourByteInspector;
5+
6+
impl<Db: Database> InspectorWithOutput<Ctx<Db>> for FourByteInspector {
7+
type Output = FourByteFrame;
8+
9+
fn has_output(&self) -> bool {
10+
!self.inner().is_empty()
11+
}
12+
13+
fn reset_output(&mut self) {
14+
*self = Self::default();
15+
}
16+
17+
fn take_output(&mut self) -> Self::Output {
18+
std::mem::take(self).into()
19+
}
20+
}

src/inspectors/with_output.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::{helpers::Ctx, Trevm};
2+
use revm::{inspector::CountInspector, Database};
3+
4+
/// An inspector that can produce an output value after EVM execution.
5+
pub trait InspectorWithOutput<CTX>: revm::inspector::Inspector<CTX> {
6+
/// The type of output produced by the inspector.
7+
type Output;
8+
9+
/// Returns `true` if the inspector has a valid output value.
10+
fn has_output(&self) -> bool;
11+
12+
/// Reset the output value to the default state, discarding any current
13+
/// value.
14+
fn reset_output(&mut self) {
15+
self.take_output();
16+
}
17+
18+
/// Take the output value from the inspector, resetting it to the default
19+
/// state.
20+
fn take_output(&mut self) -> Self::Output;
21+
}
22+
23+
impl<Db: Database, Insp> Trevm<Db, Insp>
24+
where
25+
Insp: InspectorWithOutput<Ctx<Db>>,
26+
{
27+
/// Take the output value from the inspector.
28+
///
29+
/// This will also reset the output value in the inspector.
30+
pub fn take_output(&mut self) -> Insp::Output {
31+
self.inspector_mut().take_output()
32+
}
33+
}
34+
35+
impl<Ctx> InspectorWithOutput<Ctx> for CountInspector {
36+
type Output = Self;
37+
38+
fn has_output(&self) -> bool {
39+
self.total_opcodes() > 0
40+
}
41+
42+
fn reset_output(&mut self) {
43+
self.clear();
44+
}
45+
46+
fn take_output(&mut self) -> Self::Output {
47+
std::mem::take(self)
48+
}
49+
}

0 commit comments

Comments
 (0)