From d12592d50d3aeb494d2e5ddabf4951794086d8c8 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 25 Aug 2025 15:04:21 -0400 Subject: [PATCH 1/2] feat: trait for inspectors that accumulate output --- src/inspectors/mod.rs | 3 +++ src/inspectors/with_output.rs | 49 +++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 src/inspectors/with_output.rs diff --git a/src/inspectors/mod.rs b/src/inspectors/mod.rs index 9e414f6..fbf7df5 100644 --- a/src/inspectors/mod.rs +++ b/src/inspectors/mod.rs @@ -10,6 +10,9 @@ pub use set::InspectorSet; mod spanning; pub use spanning::SpanningInspector; +mod with_output; +pub use with_output::InspectorWithOutput; + #[cfg(test)] mod test { use super::*; diff --git a/src/inspectors/with_output.rs b/src/inspectors/with_output.rs new file mode 100644 index 0000000..3d901c1 --- /dev/null +++ b/src/inspectors/with_output.rs @@ -0,0 +1,49 @@ +use crate::{helpers::Ctx, Trevm}; +use revm::{inspector::CountInspector, Database}; + +/// An inspector that can produce an output value after EVM execution. +pub trait InspectorWithOutput: revm::inspector::Inspector { + /// The type of output produced by the inspector. + type Output; + + /// Returns `true` if the inspector has a valid output value. + fn has_output(&self) -> bool; + + /// Reset the output value to the default state, discarding any current + /// value. + fn reset_output(&mut self) { + self.take_output(); + } + + /// Take the output value from the inspector, resetting it to the default + /// state. + fn take_output(&mut self) -> Self::Output; +} + +impl Trevm +where + Insp: InspectorWithOutput>, +{ + /// Take the output value from the inspector. + /// + /// This will also reset the output value in the inspector. + pub fn take_output(&mut self) -> Insp::Output { + self.inspector_mut().take_output() + } +} + +impl InspectorWithOutput for CountInspector { + type Output = Self; + + fn has_output(&self) -> bool { + self.total_opcodes() > 0 + } + + fn reset_output(&mut self) { + self.clear(); + } + + fn take_output(&mut self) -> Self::Output { + std::mem::take(self) + } +} From 9ce98116a94b0d3bb63694bb6e146f1300af8658 Mon Sep 17 00:00:00 2001 From: James Date: Mon, 25 Aug 2025 17:35:24 -0400 Subject: [PATCH 2/2] feat: add impl for 4 byte --- Cargo.toml | 3 +++ src/inspectors/mod.rs | 3 +++ src/inspectors/tracing.rs | 20 ++++++++++++++++++++ 3 files changed, 26 insertions(+) create mode 100644 src/inspectors/tracing.rs diff --git a/Cargo.toml b/Cargo.toml index a62e04e..2dc9ec5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ alloy = { version = "1.0.25", default-features = false, features = [ ] } revm = { version = "27.1", default-features = false } +revm-inspectors = { version = "0.27.1", optional = true } dashmap = { version = "6.1.0", optional = true } tracing = { version = "0.1.41", optional = true } @@ -69,6 +70,7 @@ default = [ "call", "concurrent-db", "estimate_gas", + "tracing-inspectors", "revm/std", "revm/c-kzg", "revm/blst", @@ -111,3 +113,4 @@ full_env_cfg = [ "optional_eip3607", "optional_no_base_fee", ] +tracing-inspectors = ["dep:revm-inspectors", "alloy/rpc-types-trace"] diff --git a/src/inspectors/mod.rs b/src/inspectors/mod.rs index fbf7df5..e54efc5 100644 --- a/src/inspectors/mod.rs +++ b/src/inspectors/mod.rs @@ -4,6 +4,9 @@ pub use layer::Layered; mod timeout; pub use timeout::TimeLimit; +#[cfg(feature = "tracing-inspectors")] +mod tracing; + mod set; pub use set::InspectorSet; diff --git a/src/inspectors/tracing.rs b/src/inspectors/tracing.rs new file mode 100644 index 0000000..628fbb2 --- /dev/null +++ b/src/inspectors/tracing.rs @@ -0,0 +1,20 @@ +use crate::{helpers::Ctx, inspectors::InspectorWithOutput}; +use alloy::rpc::types::trace::geth::FourByteFrame; +use revm::Database; +use revm_inspectors::tracing::FourByteInspector; + +impl InspectorWithOutput> for FourByteInspector { + type Output = FourByteFrame; + + fn has_output(&self) -> bool { + !self.inner().is_empty() + } + + fn reset_output(&mut self) { + *self = Self::default(); + } + + fn take_output(&mut self) -> Self::Output { + std::mem::take(self).into() + } +}