Skip to content

Commit

Permalink
refactor: block override without block_env fn
Browse files Browse the repository at this point in the history
  • Loading branch information
onbjerg authored and gakonst committed Mar 22, 2022
1 parent e3c498e commit 88c5786
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ codegen-units = 1
panic = "abort"
debug = true

[patch."https://github.com/bluealloy/revm"]
revm = { git = "https://github.com/onbjerg/revm", branch = "onbjerg/override-block-env" }

## Patch ethers-rs with a local checkout then run `cargo update -p ethers`
#[patch."https://github.com/gakonst/ethers-rs"]
#ethers = { path = "../ethers-rs" }
Expand Down
6 changes: 3 additions & 3 deletions evm/src/executor/inspector/cheatcodes/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,15 @@ pub fn apply<DB: Database>(
) -> Option<Result<Bytes, Bytes>> {
Some(match call {
HEVMCalls::Warp(inner) => {
state.block.timestamp = inner.0;
data.env.block.timestamp = inner.0;
Ok(Bytes::new())
}
HEVMCalls::Roll(inner) => {
state.block.number = inner.0;
data.env.block.number = inner.0;
Ok(Bytes::new())
}
HEVMCalls::Fee(inner) => {
state.block.basefee = inner.0;
data.env.block.basefee = inner.0;
Ok(Bytes::new())
}
HEVMCalls::Store(inner) => {
Expand Down
23 changes: 17 additions & 6 deletions evm/src/executor/inspector/cheatcodes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Cheatcodes {
///
/// Used in the cheatcode handler to overwrite the block environment separately from the
/// execution block environment.
pub block: BlockEnv,
pub block: Option<BlockEnv>,

/// Address labels
pub labels: BTreeMap<Address, String>,
Expand All @@ -63,7 +63,7 @@ pub struct Cheatcodes {

impl Cheatcodes {
pub fn new(ffi: bool, block: BlockEnv) -> Self {
Self { ffi, block, ..Default::default() }
Self { ffi, block: Some(block), ..Default::default() }
}

fn apply_cheatcode<DB: Database>(
Expand All @@ -89,10 +89,6 @@ impl<DB> Inspector<DB> for Cheatcodes
where
DB: Database,
{
fn block_env(&self) -> Option<&BlockEnv> {
Some(&self.block)
}

fn call(
&mut self,
data: &mut EVMData<'_, DB>,
Expand Down Expand Up @@ -147,6 +143,21 @@ where
}
}

fn initialize_interp(
&mut self,
_: &mut Interpreter,
data: &mut EVMData<'_, DB>,
_: bool,
) -> Return {
// When the first interpreter is initialized we've circumvented the balance and gas checks,
// so we apply our actual block data with the correct fees and all.
if let Some(block) = self.block.take() {
data.env.block = block;
}

Return::Continue
}

fn step(&mut self, interpreter: &mut Interpreter, _: &mut EVMData<'_, DB>, _: bool) -> Return {
// Record writes and reads if `record` has been called
if let Some(storage_accesses) = &mut self.accesses {
Expand Down
11 changes: 1 addition & 10 deletions evm/src/executor/inspector/stack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use super::{Cheatcodes, Debugger, LogCollector, Tracer};
use bytes::Bytes;
use ethers::types::{Address, H256};
use revm::{
db::Database, BlockEnv, CallInputs, CreateInputs, EVMData, Gas, Inspector, Interpreter, Return,
};
use revm::{db::Database, CallInputs, CreateInputs, EVMData, Gas, Inspector, Interpreter, Return};

/// Helper macro to call the same method on multiple inspectors without resorting to dynamic
/// dispatch
Expand Down Expand Up @@ -41,13 +39,6 @@ impl<DB> Inspector<DB> for InspectorStack
where
DB: Database,
{
fn block_env(&self) -> Option<&BlockEnv> {
match &self.cheatcodes {
Some(cheatcodes) => Inspector::<DB>::block_env(cheatcodes),
None => None,
}
}

fn initialize_interp(
&mut self,
interpreter: &mut Interpreter,
Expand Down
4 changes: 1 addition & 3 deletions evm/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,9 +295,7 @@ where
};

// Persist the changed block environment
if let Some(ref cheats) = inspector.cheatcodes {
self.inspector_config.block = cheats.block.clone();
}
self.inspector_config.block = evm.env.block.clone();

let InspectorData { logs, labels, traces, debug } = collect_inspector_states(inspector);
Ok(RawCallResult {
Expand Down

0 comments on commit 88c5786

Please sign in to comment.