Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: inject call to CREATE2 factory through custom revm handler #7653

Merged
merged 10 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
review fixes
  • Loading branch information
klkvr committed Apr 17, 2024
commit c94034f021dd12b59e8eaa426f48d7898f56a854
2 changes: 1 addition & 1 deletion crates/anvil/src/eth/backend/mem/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use foundry_evm::{
EvmContext,
},
traces::TracingInspectorConfig,
InspectorExt,
};
use foundry_evm_core::InspectorExt;

/// The [`revm::Inspector`] used when transacting in the evm
#[derive(Clone, Debug, Default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ use foundry_evm::{
},
},
utils::new_evm_with_inspector_ref,
InspectorExt,
};
use foundry_evm_core::InspectorExt;
use futures::channel::mpsc::{unbounded, UnboundedSender};
use parking_lot::{Mutex, RwLock};
use revm::{
Expand Down
7 changes: 0 additions & 7 deletions crates/cheatcodes/src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1503,13 +1503,6 @@ impl<DB: DatabaseExt> Inspector<DB> for Cheatcodes {
}

impl<DB: DatabaseExt> InspectorExt<DB> for Cheatcodes {
/// Determines whether the `DEFAULT_CREATE2_DEPLOYER` should be used for this CREATE2 frame.
///
/// This function is invoked during the contract creation process and updates the caller of the
/// contract creation transaction to be the `DEFAULT_CREATE2_DEPLOYER` if the `CreateScheme` is
/// `Create2` and the current execution depth matches the depth at which the `prank` or
/// `broadcast` was started, or the default depth of 1 if no prank or broadcast is currently
/// active.
fn should_use_create2_factory(
&mut self,
ecx: &mut EvmContext<DB>,
Expand Down
6 changes: 6 additions & 0 deletions crates/evm/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@ pub mod opts;
pub mod snapshot;
pub mod utils;

/// An extension trait that allows us to add additional hooks to Inspector for later use in
/// handlers.
#[auto_impl(&mut, Box)]
pub trait InspectorExt<DB: Database>: Inspector<DB> {
klkvr marked this conversation as resolved.
Show resolved Hide resolved
DaniPopes marked this conversation as resolved.
Show resolved Hide resolved
/// Determines whether the `DEFAULT_CREATE2_DEPLOYER` should be used for a CREATE2 frame.
///
/// If this function returns true, we'll replace CREATE2 frame with a CALL frame to CREATE2
/// factory.
fn should_use_create2_factory(
&mut self,
_context: &mut EvmContext<DB>,
Expand Down
14 changes: 9 additions & 5 deletions crates/evm/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,15 @@ pub fn create2_handler_register<DB: revm::Database, I: InspectorExt<DB>>(

// Decode address from output.
let address = match outcome.instruction_result() {
return_ok!() => {
// SAFETY: we assume here that successful CREATE2 factory call will always
// return a valid address.
Some(Address::try_from(outcome.output().as_ref()).unwrap())
}
return_ok!() => Address::try_from(outcome.output().as_ref())
.map_err(|_| {
outcome.result = InterpreterResult {
result: InstructionResult::Revert,
output: "invalid CREATE2 factory output".into(),
gas: Gas::new(call_inputs.gas_limit),
};
})
.ok(),
_ => None,
};
frame
Expand Down
2 changes: 1 addition & 1 deletion crates/evm/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ extern crate tracing;
pub mod executors;
pub mod inspectors;

pub use foundry_evm_core::{backend, constants, debug, decode, fork, opts, utils};
pub use foundry_evm_core::{backend, constants, debug, decode, fork, opts, utils, InspectorExt};
pub use foundry_evm_coverage as coverage;
pub use foundry_evm_fuzz as fuzz;
pub use foundry_evm_traces as traces;
Expand Down