Skip to content

Commit

Permalink
feat(anvil): Include CREATE2 deployer by default on new instances (
Browse files Browse the repository at this point in the history
…foundry-rs#5391)

* chore: install create_2_deployer

* chore: docs

* chore: comment codes

* fix: set proper runtime code

* fmt
  • Loading branch information
Evalir authored and mikelodder7 committed Sep 12, 2023
1 parent 462e0ee commit 06f7e7e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
17 changes: 16 additions & 1 deletion crates/anvil/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use foundry_common::{
};
use foundry_config::Config;
use foundry_evm::{
executor::fork::{BlockchainDb, BlockchainDbMeta, SharedBackend},
executor::{
fork::{BlockchainDb, BlockchainDbMeta, SharedBackend},
inspector::DEFAULT_CREATE2_DEPLOYER,
},
revm,
revm::primitives::{BlockEnv, CfgEnv, SpecId, TxEnv, U256 as rU256},
utils::{apply_chain_and_block_specific_env_changes, h256_to_b256, u256_to_ru256},
Expand Down Expand Up @@ -163,6 +166,8 @@ pub struct NodeConfig {
pub init_state: Option<SerializableState>,
/// max number of blocks with transactions in memory
pub transaction_block_keeper: Option<usize>,
/// Disable the default CREATE2 deployer
pub disable_default_create2_deployer: bool,
}

impl NodeConfig {
Expand Down Expand Up @@ -398,6 +403,7 @@ impl Default for NodeConfig {
prune_history: Default::default(),
init_state: None,
transaction_block_keeper: None,
disable_default_create2_deployer: false,
}
}
}
Expand Down Expand Up @@ -1005,6 +1011,15 @@ latest block number: {latest_block}"
)
.await;

// Writes the default create2 deployer to the backend,
// if the option is not disabled and we are not forking.
if !self.disable_default_create2_deployer && self.eth_rpc_url.is_none() {
backend
.set_create2_deployer(DEFAULT_CREATE2_DEPLOYER)
.await
.expect("Failed to create default create2 deployer");
}

if let Some(ref state) = self.init_state {
backend
.get_db()
Expand Down
8 changes: 8 additions & 0 deletions crates/anvil/src/eth/backend/mem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ use foundry_evm::{
executor::{
backend::{DatabaseError, DatabaseResult},
inspector::AccessListTracer,
DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE,
},
revm::{
self,
Expand Down Expand Up @@ -235,6 +236,13 @@ impl Backend {
backend
}

/// Writes the CREATE2 deployer code directly to the database at the address provided.
pub async fn set_create2_deployer(&self, address: Address) -> DatabaseResult<()> {
self.set_code(address, Bytes::from_static(DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE)).await?;

Ok(())
}

/// Updates memory limits that should be more strict when auto-mine is enabled
pub(crate) fn update_interval_mine_block_time(&self, block_time: Duration) {
self.states.write().update_interval_mine_block_time(block_time)
Expand Down
3 changes: 3 additions & 0 deletions crates/evm/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ pub use builder::ExecutorBuilder;
/// A mapping of addresses to their changed state.
pub type StateChangeset = HashMap<B160, Account>;

/// The initcode of the default create2 deployer.
pub const DEFAULT_CREATE2_DEPLOYER_CODE: &[u8] = &hex!("604580600e600039806000f350fe7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3");
/// The runtime code of the default create2 deployer.
pub const DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE: &[u8] = &hex!("7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe03601600081602082378035828234f58015156039578182fd5b8082525050506014600cf3");

/// A type that can execute calls
///
Expand Down

0 comments on commit 06f7e7e

Please sign in to comment.