Skip to content

Commit 1e6f441

Browse files
authored
feat(anvil): Include CREATE2 deployer by default on new instances (#5391)
* chore: install create_2_deployer * chore: docs * chore: comment codes * fix: set proper runtime code * fmt
1 parent f64fe13 commit 1e6f441

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

crates/anvil/src/config.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ use foundry_common::{
3232
};
3333
use foundry_config::Config;
3434
use foundry_evm::{
35-
executor::fork::{BlockchainDb, BlockchainDbMeta, SharedBackend},
35+
executor::{
36+
fork::{BlockchainDb, BlockchainDbMeta, SharedBackend},
37+
inspector::DEFAULT_CREATE2_DEPLOYER,
38+
},
3639
revm,
3740
revm::primitives::{BlockEnv, CfgEnv, SpecId, TxEnv, U256 as rU256},
3841
utils::{apply_chain_and_block_specific_env_changes, h256_to_b256, u256_to_ru256},
@@ -163,6 +166,8 @@ pub struct NodeConfig {
163166
pub init_state: Option<SerializableState>,
164167
/// max number of blocks with transactions in memory
165168
pub transaction_block_keeper: Option<usize>,
169+
/// Disable the default CREATE2 deployer
170+
pub disable_default_create2_deployer: bool,
166171
}
167172

168173
impl NodeConfig {
@@ -398,6 +403,7 @@ impl Default for NodeConfig {
398403
prune_history: Default::default(),
399404
init_state: None,
400405
transaction_block_keeper: None,
406+
disable_default_create2_deployer: false,
401407
}
402408
}
403409
}
@@ -1005,6 +1011,15 @@ latest block number: {latest_block}"
10051011
)
10061012
.await;
10071013

1014+
// Writes the default create2 deployer to the backend,
1015+
// if the option is not disabled and we are not forking.
1016+
if !self.disable_default_create2_deployer && self.eth_rpc_url.is_none() {
1017+
backend
1018+
.set_create2_deployer(DEFAULT_CREATE2_DEPLOYER)
1019+
.await
1020+
.expect("Failed to create default create2 deployer");
1021+
}
1022+
10081023
if let Some(ref state) = self.init_state {
10091024
backend
10101025
.get_db()

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use foundry_evm::{
6060
executor::{
6161
backend::{DatabaseError, DatabaseResult},
6262
inspector::AccessListTracer,
63+
DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE,
6364
},
6465
revm::{
6566
self,
@@ -235,6 +236,13 @@ impl Backend {
235236
backend
236237
}
237238

239+
/// Writes the CREATE2 deployer code directly to the database at the address provided.
240+
pub async fn set_create2_deployer(&self, address: Address) -> DatabaseResult<()> {
241+
self.set_code(address, Bytes::from_static(DEFAULT_CREATE2_DEPLOYER_RUNTIME_CODE)).await?;
242+
243+
Ok(())
244+
}
245+
238246
/// Updates memory limits that should be more strict when auto-mine is enabled
239247
pub(crate) fn update_interval_mine_block_time(&self, block_time: Duration) {
240248
self.states.write().update_interval_mine_block_time(block_time)

crates/evm/src/executor/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ pub use builder::ExecutorBuilder;
6464
/// A mapping of addresses to their changed state.
6565
pub type StateChangeset = HashMap<B160, Account>;
6666

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

6972
/// A type that can execute calls
7073
///

0 commit comments

Comments
 (0)