Skip to content

Commit f3fff94

Browse files
authored
feat: missing tools for inspector manipulation (#118)
* feat: missing tools for inspector manipulation * feat: replace_inspector * chore: bump version
1 parent a114df7 commit f3fff94

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
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.3"
3+
version = "0.27.4"
44
rust-version = "1.83.0"
55
edition = "2021"
66
authors = ["init4"]

src/evm.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{
22
db::{StateAcc, TryStateAcc},
33
driver::DriveBlockResult,
44
helpers::{Ctx, Evm, Instruction},
5+
inspectors::Layered,
56
Block, BlockDriver, BundleDriver, Cfg, ChainDriver, DriveBundleResult, DriveChainResult,
67
ErroredState, EvmErrored, EvmExtUnchecked, EvmNeedsBlock, EvmNeedsCfg, EvmNeedsTx, EvmReady,
78
EvmTransacted, HasBlock, HasCfg, HasTx, NeedsCfg, NeedsTx, TransactedState, Tx,
@@ -101,6 +102,92 @@ where
101102
self.inner.ctx.journaled_state.database
102103
}
103104

105+
/// Get a reference to the inspector.
106+
pub fn inspector(&self) -> &Insp {
107+
&self.inner.inspector
108+
}
109+
110+
/// Get a mutable reference to the inspector.
111+
pub fn inspector_mut(&mut self) -> &mut Insp {
112+
&mut self.inner.inspector
113+
}
114+
115+
/// Replace the current inspector with a new inspector of the same type.
116+
pub fn replace_inspector(mut self, inspector: Insp) -> Self
117+
where
118+
TrevmState: Copy,
119+
{
120+
*self.inspector_mut() = inspector;
121+
self
122+
}
123+
124+
/// Layer a new inspector on top of the current one.
125+
pub fn layer_inspector<Insp2>(
126+
self,
127+
inspector: Insp2,
128+
) -> Trevm<Db, Layered<Insp2, Insp>, TrevmState>
129+
where
130+
Insp2: Inspector<Ctx<Db>>,
131+
TrevmState: Copy,
132+
{
133+
let inner = Box::new(Evm {
134+
ctx: self.inner.ctx,
135+
inspector: Layered::new(inspector, self.inner.inspector),
136+
instruction: self.inner.instruction,
137+
precompiles: self.inner.precompiles,
138+
frame_stack: self.inner.frame_stack,
139+
});
140+
Trevm { inner, state: self.state }
141+
}
142+
143+
/// Take the inspector out of the Trevm, replacing it with a
144+
/// [`NoOpInspector`].
145+
pub fn take_inspector(self) -> (Insp, Trevm<Db, NoOpInspector, TrevmState>) {
146+
let inspector = self.inner.inspector;
147+
let inner = Box::new(Evm {
148+
ctx: self.inner.ctx,
149+
inspector: NoOpInspector,
150+
instruction: self.inner.instruction,
151+
precompiles: self.inner.precompiles,
152+
frame_stack: self.inner.frame_stack,
153+
});
154+
(inspector, Trevm { inner, state: self.state })
155+
}
156+
157+
/// Replace the current inspector with a new one, dropping the old one.
158+
pub fn set_inspector<Insp2>(self, inspector: Insp2) -> Trevm<Db, Insp2, TrevmState>
159+
where
160+
Insp2: Inspector<Ctx<Db>>,
161+
TrevmState: Copy,
162+
{
163+
let inner = Box::new(Evm {
164+
ctx: self.inner.ctx,
165+
inspector,
166+
instruction: self.inner.instruction,
167+
precompiles: self.inner.precompiles,
168+
frame_stack: self.inner.frame_stack,
169+
});
170+
Trevm { inner, state: self.state }
171+
}
172+
173+
/// Run the closure with a different inspector, then restore the previous
174+
/// one.
175+
pub fn with_inspector<Insp2, F, NewState>(
176+
self,
177+
inspector: Insp2,
178+
f: F,
179+
) -> Trevm<Db, Insp, NewState>
180+
where
181+
Insp2: Inspector<Ctx<Db>>,
182+
F: FnOnce(Trevm<Db, Insp2, TrevmState>) -> Trevm<Db, Insp2, NewState>,
183+
TrevmState: Copy,
184+
NewState: Copy,
185+
{
186+
let (old, this) = self.take_inspector();
187+
let this = f(this.set_inspector(inspector));
188+
this.set_inspector(old)
189+
}
190+
104191
/// Get the id of the currently running hardfork spec.
105192
pub fn spec_id(&self) -> SpecId {
106193
self.inner.ctx.cfg().spec()

0 commit comments

Comments
 (0)