Skip to content

Commit fbd3abe

Browse files
authored
chore: more StackInspector cleanups (#11105)
* chore: InspectorStack::as_inspector * chore: clean InspectorExt::tracing_inspector * chore: dont use trait
1 parent b57f7c1 commit fbd3abe

File tree

8 files changed

+31
-25
lines changed

8 files changed

+31
-25
lines changed

crates/cheatcodes/src/evm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ impl Cheatcode for setBlockhashCall {
874874

875875
impl Cheatcode for startDebugTraceRecordingCall {
876876
fn apply_full(&self, ccx: &mut CheatsCtxt, executor: &mut dyn CheatcodesExecutor) -> Result {
877-
let Some(tracer) = executor.tracing_inspector().and_then(|t| t.as_mut()) else {
877+
let Some(tracer) = executor.tracing_inspector() else {
878878
return Err(Error::from("no tracer initiated, consider adding -vvv flag"));
879879
};
880880

@@ -905,7 +905,7 @@ impl Cheatcode for startDebugTraceRecordingCall {
905905

906906
impl Cheatcode for stopAndReturnDebugTraceRecordingCall {
907907
fn apply_full(&self, ccx: &mut CheatsCtxt, executor: &mut dyn CheatcodesExecutor) -> Result {
908-
let Some(tracer) = executor.tracing_inspector().and_then(|t| t.as_mut()) else {
908+
let Some(tracer) = executor.tracing_inspector() else {
909909
return Err(Error::from("no tracer initiated, consider adding -vvv flag"));
910910
};
911911

crates/cheatcodes/src/inspector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub trait CheatcodesExecutor {
111111
}
112112

113113
/// Returns a mutable reference to the tracing inspector if it is available.
114-
fn tracing_inspector(&mut self) -> Option<&mut Option<Box<TracingInspector>>> {
114+
fn tracing_inspector(&mut self) -> Option<&mut TracingInspector> {
115115
None
116116
}
117117
}

crates/cheatcodes/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl Cheatcode for pauseTracingCall {
157157
ccx: &mut crate::CheatsCtxt,
158158
executor: &mut dyn CheatcodesExecutor,
159159
) -> Result {
160-
let Some(tracer) = executor.tracing_inspector().and_then(|t| t.as_ref()) else {
160+
let Some(tracer) = executor.tracing_inspector() else {
161161
// No tracer -> nothing to pause
162162
return Ok(Default::default());
163163
};
@@ -180,7 +180,7 @@ impl Cheatcode for resumeTracingCall {
180180
ccx: &mut crate::CheatsCtxt,
181181
executor: &mut dyn CheatcodesExecutor,
182182
) -> Result {
183-
let Some(tracer) = executor.tracing_inspector().and_then(|t| t.as_ref()) else {
183+
let Some(tracer) = executor.tracing_inspector() else {
184184
// No tracer -> nothing to unpause
185185
return Ok(Default::default());
186186
};

crates/evm/core/src/backend/cow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ impl<'a> CowBackend<'a> {
6767
pub fn inspect<I: InspectorExt>(
6868
&mut self,
6969
env: &mut Env,
70-
inspector: &mut I,
70+
inspector: I,
7171
) -> eyre::Result<ResultAndState> {
7272
// this is a new call to inspect with a new env, so even if we've cloned the backend
7373
// already, we reset the initialized state

crates/evm/core/src/backend/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ impl Backend {
770770
pub fn inspect<I: InspectorExt>(
771771
&mut self,
772772
env: &mut Env,
773-
inspector: &mut I,
773+
inspector: I,
774774
) -> eyre::Result<ResultAndState> {
775775
self.initialize(env);
776776
let mut evm = crate::evm::new_evm_with_inspector(self, env.to_owned(), inspector);

crates/evm/core/src/evm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ use revm::{
3838
primitives::hardfork::SpecId,
3939
};
4040

41-
pub fn new_evm_with_inspector<'i, 'db, I: InspectorExt + ?Sized>(
41+
pub fn new_evm_with_inspector<'db, I: InspectorExt>(
4242
db: &'db mut dyn DatabaseExt,
4343
env: Env,
44-
inspector: &'i mut I,
45-
) -> FoundryEvm<'db, &'i mut I> {
44+
inspector: I,
45+
) -> FoundryEvm<'db, I> {
4646
let mut ctx = EthEvmContext {
4747
journaled_state: {
4848
let mut journal = Journal::new(db);

crates/evm/evm/src/executors/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use alloy_primitives::{
2020
};
2121
use alloy_sol_types::{SolCall, sol};
2222
use foundry_evm_core::{
23-
EvmEnv, InspectorExt,
23+
EvmEnv,
2424
backend::{Backend, BackendError, BackendResult, CowBackend, DatabaseExt, GLOBAL_FAIL_SLOT},
2525
constants::{
2626
CALLER, CHEATCODE_ADDRESS, CHEATCODE_CONTRACT_HASH, DEFAULT_CREATE2_DEPLOYER,
@@ -312,7 +312,7 @@ impl Executor {
312312

313313
#[inline]
314314
pub fn create2_deployer(&self) -> Address {
315-
self.inspector().create2_deployer()
315+
self.inspector().create2_deployer
316316
}
317317

318318
/// Deploys a contract and commits the new state to the underlying database.
@@ -489,20 +489,20 @@ impl Executor {
489489
/// The state after the call is **not** persisted.
490490
#[instrument(name = "call", level = "debug", skip_all)]
491491
pub fn call_with_env(&self, mut env: Env) -> eyre::Result<RawCallResult> {
492-
let mut inspector = self.inspector().clone();
492+
let mut stack = self.inspector().clone();
493493
let mut backend = CowBackend::new_borrowed(self.backend());
494-
let result = backend.inspect(&mut env, &mut inspector)?;
495-
convert_executed_result(env, inspector, result, backend.has_state_snapshot_failure())
494+
let result = backend.inspect(&mut env, stack.as_inspector())?;
495+
convert_executed_result(env, stack, result, backend.has_state_snapshot_failure())
496496
}
497497

498498
/// Execute the transaction configured in `env.tx`.
499499
#[instrument(name = "transact", level = "debug", skip_all)]
500500
pub fn transact_with_env(&mut self, mut env: Env) -> eyre::Result<RawCallResult> {
501-
let mut inspector = self.inspector().clone();
501+
let mut stack = self.inspector().clone();
502502
let backend = self.backend_mut();
503-
let result = backend.inspect(&mut env, &mut inspector)?;
503+
let result = backend.inspect(&mut env, stack.as_inspector())?;
504504
let mut result =
505-
convert_executed_result(env, inspector, result, backend.has_state_snapshot_failure())?;
505+
convert_executed_result(env, stack, result, backend.has_state_snapshot_failure())?;
506506
self.commit(&mut result);
507507
Ok(result)
508508
}

crates/evm/evm/src/inspectors/stack.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ impl CheatcodesExecutor for InspectorStackInner {
337337
Box::new(InspectorStackRefMut { cheatcodes: Some(cheats), inner: self })
338338
}
339339

340-
fn tracing_inspector(&mut self) -> Option<&mut Option<Box<TracingInspector>>> {
341-
Some(&mut self.tracer)
340+
fn tracing_inspector(&mut self) -> Option<&mut TracingInspector> {
341+
self.tracer.as_deref_mut()
342342
}
343343
}
344344

@@ -478,6 +478,17 @@ impl InspectorStack {
478478
script_address;
479479
}
480480

481+
#[inline(always)]
482+
fn as_mut(&mut self) -> InspectorStackRefMut<'_> {
483+
InspectorStackRefMut { cheatcodes: self.cheatcodes.as_deref_mut(), inner: &mut self.inner }
484+
}
485+
486+
/// Returns an [`InspectorExt`] using this stack's inspectors.
487+
#[inline]
488+
pub fn as_inspector(&mut self) -> impl InspectorExt + '_ {
489+
self
490+
}
491+
481492
/// Collects all the data gathered during inspection into a single struct.
482493
pub fn collect(self) -> InspectorData {
483494
let Self {
@@ -526,11 +537,6 @@ impl InspectorStack {
526537
reverter,
527538
}
528539
}
529-
530-
#[inline(always)]
531-
fn as_mut(&mut self) -> InspectorStackRefMut<'_> {
532-
InspectorStackRefMut { cheatcodes: self.cheatcodes.as_deref_mut(), inner: &mut self.inner }
533-
}
534540
}
535541

536542
impl InspectorStackRefMut<'_> {

0 commit comments

Comments
 (0)