Skip to content

Commit 70bea9d

Browse files
DaniPopesmikelodder7
authored andcommitted
refactor: clean up evm inspectors (foundry-rs#5675)
* refactor: clean up evm inspectors * tests
1 parent 5d5f8be commit 70bea9d

File tree

30 files changed

+596
-676
lines changed

30 files changed

+596
-676
lines changed

crates/anvil/src/eth/backend/mem/inspector.rs

Lines changed: 36 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,15 @@ use foundry_evm::{
99
executor::inspector::{LogCollector, Tracer},
1010
revm,
1111
revm::{
12-
inspectors::GasInspector,
1312
interpreter::{CallInputs, CreateInputs, Gas, InstructionResult, Interpreter},
1413
primitives::{B160, B256},
1514
EVMData,
1615
},
1716
};
18-
use std::{cell::RefCell, rc::Rc};
1917

2018
/// The [`revm::Inspector`] used when transacting in the evm
2119
#[derive(Debug, Clone, Default)]
2220
pub struct Inspector {
23-
pub gas: Option<Rc<RefCell<GasInspector>>>,
2421
pub tracer: Option<Tracer>,
2522
/// collects all `console.sol` logs
2623
pub log_collector: LogCollector,
@@ -42,109 +39,83 @@ impl Inspector {
4239
self
4340
}
4441

45-
/// Enables steps recording for `Tracer` and attaches `GasInspector` to it
46-
/// If `Tracer` wasn't configured before, configures it automatically
42+
/// Enables steps recording for `Tracer`.
4743
pub fn with_steps_tracing(mut self) -> Self {
48-
if self.tracer.is_none() {
49-
self = self.with_tracing()
50-
}
51-
let gas_inspector = Rc::new(RefCell::new(GasInspector::default()));
52-
self.gas = Some(gas_inspector.clone());
53-
self.tracer = self.tracer.map(|tracer| tracer.with_steps_recording(gas_inspector));
54-
44+
let tracer = self.tracer.get_or_insert_with(Default::default);
45+
tracer.record_steps();
5546
self
5647
}
5748
}
5849

5950
impl<DB: Database> revm::Inspector<DB> for Inspector {
51+
#[inline]
6052
fn initialize_interp(
6153
&mut self,
6254
interp: &mut Interpreter,
6355
data: &mut EVMData<'_, DB>,
6456
is_static: bool,
6557
) -> InstructionResult {
66-
call_inspectors!(
67-
inspector,
68-
[&mut self.gas.as_deref().map(|gas| gas.borrow_mut()), &mut self.tracer],
69-
{ inspector.initialize_interp(interp, data, is_static) }
70-
);
58+
call_inspectors!([&mut self.tracer], |inspector| {
59+
inspector.initialize_interp(interp, data, is_static);
60+
});
7161
InstructionResult::Continue
7262
}
7363

64+
#[inline]
7465
fn step(
7566
&mut self,
7667
interp: &mut Interpreter,
7768
data: &mut EVMData<'_, DB>,
7869
is_static: bool,
7970
) -> InstructionResult {
80-
call_inspectors!(
81-
inspector,
82-
[&mut self.gas.as_deref().map(|gas| gas.borrow_mut()), &mut self.tracer],
83-
{
84-
inspector.step(interp, data, is_static);
85-
}
86-
);
71+
call_inspectors!([&mut self.tracer], |inspector| {
72+
inspector.step(interp, data, is_static);
73+
});
8774
InstructionResult::Continue
8875
}
8976

77+
#[inline]
9078
fn log(
9179
&mut self,
9280
evm_data: &mut EVMData<'_, DB>,
9381
address: &B160,
9482
topics: &[B256],
9583
data: &Bytes,
9684
) {
97-
call_inspectors!(
98-
inspector,
99-
[
100-
&mut self.gas.as_deref().map(|gas| gas.borrow_mut()),
101-
&mut self.tracer,
102-
Some(&mut self.log_collector)
103-
],
104-
{
105-
inspector.log(evm_data, address, topics, data);
106-
}
107-
);
85+
call_inspectors!([&mut self.tracer, Some(&mut self.log_collector)], |inspector| {
86+
inspector.log(evm_data, address, topics, data);
87+
});
10888
}
10989

90+
#[inline]
11091
fn step_end(
11192
&mut self,
11293
interp: &mut Interpreter,
11394
data: &mut EVMData<'_, DB>,
11495
is_static: bool,
11596
eval: InstructionResult,
11697
) -> InstructionResult {
117-
call_inspectors!(
118-
inspector,
119-
[&mut self.gas.as_deref().map(|gas| gas.borrow_mut()), &mut self.tracer],
120-
{
121-
inspector.step_end(interp, data, is_static, eval);
122-
}
123-
);
98+
call_inspectors!([&mut self.tracer], |inspector| {
99+
inspector.step_end(interp, data, is_static, eval);
100+
});
124101
eval
125102
}
126103

104+
#[inline]
127105
fn call(
128106
&mut self,
129107
data: &mut EVMData<'_, DB>,
130108
call: &mut CallInputs,
131109
is_static: bool,
132110
) -> (InstructionResult, Gas, Bytes) {
133-
call_inspectors!(
134-
inspector,
135-
[
136-
&mut self.gas.as_deref().map(|gas| gas.borrow_mut()),
137-
&mut self.tracer,
138-
Some(&mut self.log_collector)
139-
],
140-
{
141-
inspector.call(data, call, is_static);
142-
}
143-
);
111+
call_inspectors!([&mut self.tracer, Some(&mut self.log_collector)], |inspector| {
112+
inspector.call(data, call, is_static);
113+
});
144114

145115
(InstructionResult::Continue, Gas::new(call.gas_limit), Bytes::new())
146116
}
147117

118+
#[inline]
148119
fn call_end(
149120
&mut self,
150121
data: &mut EVMData<'_, DB>,
@@ -154,32 +125,26 @@ impl<DB: Database> revm::Inspector<DB> for Inspector {
154125
out: Bytes,
155126
is_static: bool,
156127
) -> (InstructionResult, Gas, Bytes) {
157-
call_inspectors!(
158-
inspector,
159-
[&mut self.gas.as_deref().map(|gas| gas.borrow_mut()), &mut self.tracer],
160-
{
161-
inspector.call_end(data, inputs, remaining_gas, ret, out.clone(), is_static);
162-
}
163-
);
128+
call_inspectors!([&mut self.tracer], |inspector| {
129+
inspector.call_end(data, inputs, remaining_gas, ret, out.clone(), is_static);
130+
});
164131
(ret, remaining_gas, out)
165132
}
166133

134+
#[inline]
167135
fn create(
168136
&mut self,
169137
data: &mut EVMData<'_, DB>,
170138
call: &mut CreateInputs,
171139
) -> (InstructionResult, Option<B160>, Gas, Bytes) {
172-
call_inspectors!(
173-
inspector,
174-
[&mut self.gas.as_deref().map(|gas| gas.borrow_mut()), &mut self.tracer],
175-
{
176-
inspector.create(data, call);
177-
}
178-
);
140+
call_inspectors!([&mut self.tracer], |inspector| {
141+
inspector.create(data, call);
142+
});
179143

180144
(InstructionResult::Continue, None, Gas::new(call.gas_limit), Bytes::new())
181145
}
182146

147+
#[inline]
183148
fn create_end(
184149
&mut self,
185150
data: &mut EVMData<'_, DB>,
@@ -189,18 +154,15 @@ impl<DB: Database> revm::Inspector<DB> for Inspector {
189154
gas: Gas,
190155
retdata: Bytes,
191156
) -> (InstructionResult, Option<B160>, Gas, Bytes) {
192-
call_inspectors!(
193-
inspector,
194-
[&mut self.gas.as_deref().map(|gas| gas.borrow_mut()), &mut self.tracer],
195-
{
196-
inspector.create_end(data, inputs, status, address, gas, retdata.clone());
197-
}
198-
);
157+
call_inspectors!([&mut self.tracer], |inspector| {
158+
inspector.create_end(data, inputs, status, address, gas, retdata.clone());
159+
});
199160
(status, address, gas, retdata)
200161
}
201162
}
202163

203164
/// Prints all the logs
165+
#[inline]
204166
pub fn print_logs(logs: &[Log]) {
205167
for log in decode_console_logs(logs) {
206168
node_info!("{}", log);

crates/anvil/src/eth/backend/mem/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl Backend {
347347
}
348348

349349
pub fn precompiles(&self) -> Vec<Address> {
350-
get_precompiles_for(self.env().read().cfg.spec_id)
350+
get_precompiles_for(self.env.read().cfg.spec_id)
351351
}
352352

353353
/// Resets the fork to a fresh state
@@ -541,12 +541,12 @@ impl Backend {
541541

542542
/// Returns the block gas limit
543543
pub fn gas_limit(&self) -> U256 {
544-
self.env().read().block.gas_limit.into()
544+
self.env.read().block.gas_limit.into()
545545
}
546546

547547
/// Sets the block gas limit
548548
pub fn set_gas_limit(&self, gas_limit: U256) {
549-
self.env().write().block.gas_limit = gas_limit.into();
549+
self.env.write().block.gas_limit = gas_limit.into();
550550
}
551551

552552
/// Returns the current base fee
@@ -791,7 +791,7 @@ impl Backend {
791791
let (outcome, header, block_hash) = {
792792
let current_base_fee = self.base_fee();
793793

794-
let mut env = self.env().read().clone();
794+
let mut env = self.env.read().clone();
795795

796796
if env.block.basefee == revm::primitives::U256::ZERO {
797797
// this is an edge case because the evm fails if `tx.effective_gas_price < base_fee`
@@ -1631,7 +1631,7 @@ impl Backend {
16311631
// So this provides calls the given provided function `f` with a genesis aware database
16321632
if let Some(fork) = self.get_fork() {
16331633
if block_number == U256::from(fork.block_number()) {
1634-
let mut block = self.env().read().block.clone();
1634+
let mut block = self.env.read().block.clone();
16351635
let db = self.db.read().await;
16361636
let gen_db = self.genesis.state_db_at_genesis(Box::new(&*db));
16371637

@@ -1651,7 +1651,7 @@ impl Backend {
16511651
}
16521652

16531653
let db = self.db.read().await;
1654-
let block = self.env().read().block.clone();
1654+
let block = self.env.read().block.clone();
16551655
Ok(f(Box::new(&*db), block))
16561656
}
16571657

crates/chisel/src/executor.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,14 +290,15 @@ impl SessionSource {
290290
};
291291

292292
// Build a new executor
293-
let executor = ExecutorBuilder::default()
294-
.with_config(env)
295-
.with_chisel_state(final_pc)
296-
.set_tracing(true)
297-
.with_spec(foundry_evm::utils::evm_spec(&self.config.foundry_config.evm_version))
298-
.with_gas_limit(self.config.evm_opts.gas_limit())
299-
.with_cheatcodes(CheatsConfig::new(&self.config.foundry_config, &self.config.evm_opts))
300-
.build(backend);
293+
let executor = ExecutorBuilder::new()
294+
.inspectors(|stack| {
295+
stack.chisel_state(final_pc).trace(true).cheatcodes(
296+
CheatsConfig::new(&self.config.foundry_config, &self.config.evm_opts).into(),
297+
)
298+
})
299+
.gas_limit(self.config.evm_opts.gas_limit())
300+
.spec(foundry_evm::utils::evm_spec(self.config.foundry_config.evm_version))
301+
.build(env, backend);
301302

302303
// Create a [ChiselRunner] with a default balance of [U256::MAX] and
303304
// the sender [Address::zero].

crates/chisel/src/runner.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,19 @@ impl ChiselRunner {
132132
value: U256,
133133
commit: bool,
134134
) -> eyre::Result<ChiselResult> {
135-
let fs_commit_changed =
136-
if let Some(ref mut cheatcodes) = self.executor.inspector_config_mut().cheatcodes {
137-
let original_fs_commit = cheatcodes.fs_commit;
138-
cheatcodes.fs_commit = false;
139-
original_fs_commit != cheatcodes.fs_commit
140-
} else {
141-
false
142-
};
135+
let fs_commit_changed = if let Some(cheatcodes) = &mut self.executor.inspector.cheatcodes {
136+
let original_fs_commit = cheatcodes.fs_commit;
137+
cheatcodes.fs_commit = false;
138+
original_fs_commit != cheatcodes.fs_commit
139+
} else {
140+
false
141+
};
143142

144143
let mut res = self.executor.call_raw(from, to, calldata.0.clone(), value)?;
145144
let mut gas_used = res.gas_used;
146145
if matches!(res.exit_reason, return_ok!()) {
147146
// store the current gas limit and reset it later
148-
let init_gas_limit = self.executor.env_mut().tx.gas_limit;
147+
let init_gas_limit = self.executor.env.tx.gas_limit;
149148

150149
// the executor will return the _exact_ gas value this transaction consumed, setting
151150
// this value as gas limit will result in `OutOfGas` so to come up with a
@@ -156,7 +155,7 @@ impl ChiselRunner {
156155
let mut last_highest_gas_limit = highest_gas_limit;
157156
while (highest_gas_limit - lowest_gas_limit) > 1 {
158157
let mid_gas_limit = (highest_gas_limit + lowest_gas_limit) / 2;
159-
self.executor.env_mut().tx.gas_limit = mid_gas_limit;
158+
self.executor.env.tx.gas_limit = mid_gas_limit;
160159
let res = self.executor.call_raw(from, to, calldata.0.clone(), value)?;
161160
match res.exit_reason {
162161
InstructionResult::Revert |
@@ -182,13 +181,13 @@ impl ChiselRunner {
182181
}
183182
}
184183
// reset gas limit in the
185-
self.executor.env_mut().tx.gas_limit = init_gas_limit;
184+
self.executor.env.tx.gas_limit = init_gas_limit;
186185
}
187186

188187
// if we changed `fs_commit` during gas limit search, re-execute the call with original
189188
// value
190189
if fs_commit_changed {
191-
if let Some(ref mut cheatcodes) = self.executor.inspector_config_mut().cheatcodes {
190+
if let Some(cheatcodes) = &mut self.executor.inspector.cheatcodes {
192191
cheatcodes.fs_commit = !cheatcodes.fs_commit;
193192
}
194193

0 commit comments

Comments
 (0)