Skip to content

Commit 4465f77

Browse files
committed
Add --celo flag to anvil
1 parent 81f1aa4 commit 4465f77

File tree

6 files changed

+50
-10
lines changed

6 files changed

+50
-10
lines changed

crates/anvil/src/cmd.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ impl NodeArgs {
279279
.with_max_persisted_states(self.max_persisted_states)
280280
.with_optimism(self.evm.optimism)
281281
.with_odyssey(self.evm.odyssey)
282+
.with_celo(self.evm.celo)
282283
.with_disable_default_create2_deployer(self.evm.disable_default_create2_deployer)
283284
.with_slots_in_an_epoch(self.slots_in_an_epoch)
284285
.with_memory_limit(self.evm.memory_limit)
@@ -599,6 +600,10 @@ pub struct AnvilEvmArgs {
599600
/// Enable Odyssey features
600601
#[arg(long, alias = "alphanet")]
601602
pub odyssey: bool,
603+
604+
/// Run a Celo chain
605+
#[arg(long)]
606+
pub celo: bool,
602607
}
603608

604609
/// Resolves an alias passed as fork-url to the matching url defined in the rpc_endpoints section

crates/anvil/src/config.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ pub struct NodeConfig {
194194
pub precompile_factory: Option<Arc<dyn PrecompileFactory>>,
195195
/// Enable Odyssey features.
196196
pub odyssey: bool,
197+
/// Enable Celo features.
198+
pub celo: bool,
197199
/// Do not print log messages.
198200
pub silent: bool,
199201
/// The path where states are cached.
@@ -489,6 +491,7 @@ impl Default for NodeConfig {
489491
memory_limit: None,
490492
precompile_factory: None,
491493
odyssey: false,
494+
celo: false,
492495
silent: false,
493496
cache_path: None,
494497
}
@@ -1007,6 +1010,17 @@ impl NodeConfig {
10071010
self
10081011
}
10091012

1013+
/// Sets whether to enable Celo support
1014+
#[must_use]
1015+
pub fn with_celo(mut self, celo: bool) -> Self {
1016+
self.celo = celo;
1017+
if celo {
1018+
// Celo requires Optimism support
1019+
self.enable_optimism = true;
1020+
}
1021+
self
1022+
}
1023+
10101024
/// Makes the node silent to not emit anything on stdout
10111025
#[must_use]
10121026
pub fn silent(self) -> Self {
@@ -1061,6 +1075,7 @@ impl NodeConfig {
10611075
..Default::default()
10621076
},
10631077
self.enable_optimism,
1078+
self.celo,
10641079
);
10651080

10661081
let fees = FeeManager::new(

crates/anvil/src/eth/backend/env.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ pub struct Env {
1010
pub evm_env: EvmEnv,
1111
pub tx: OpTransaction<TxEnv>,
1212
pub is_optimism: bool,
13+
pub is_celo: bool,
1314
}
1415

1516
/// Helper container type for [`EvmEnv`] and [`OpTransaction<TxEnv>`].
1617
impl Env {
17-
pub fn new(cfg: CfgEnv, block: BlockEnv, tx: OpTransaction<TxEnv>, is_optimism: bool) -> Self {
18-
Self { evm_env: EvmEnv { cfg_env: cfg, block_env: block }, tx, is_optimism }
18+
pub fn new(
19+
cfg: CfgEnv,
20+
block: BlockEnv,
21+
tx: OpTransaction<TxEnv>,
22+
is_optimism: bool,
23+
is_celo: bool,
24+
) -> Self {
25+
Self { evm_env: EvmEnv { cfg_env: cfg, block_env: block }, tx, is_optimism, is_celo }
1926
}
2027
}
2128

crates/anvil/src/eth/backend/executor.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ pub struct TransactionExecutor<'a, Db: ?Sized, V: TransactionValidator> {
120120
pub enable_steps_tracing: bool,
121121
pub odyssey: bool,
122122
pub optimism: bool,
123+
pub celo: bool,
123124
pub print_logs: bool,
124125
pub print_traces: bool,
125126
/// Recorder used for decoding traces, used together with print_traces
@@ -264,7 +265,7 @@ impl<DB: Db + ?Sized, V: TransactionValidator> TransactionExecutor<'_, DB, V> {
264265
tx_env.enveloped_tx = Some(alloy_rlp::encode(&tx.transaction.transaction).into());
265266
}
266267

267-
Env::new(self.cfg_env.clone(), self.block_env.clone(), tx_env, self.optimism)
268+
Env::new(self.cfg_env.clone(), self.block_env.clone(), tx_env, self.optimism, self.celo)
268269
}
269270
}
270271

@@ -461,7 +462,12 @@ where
461462
op_context,
462463
inspector,
463464
EthInstructions::default(),
464-
HybridPrecompileProvider::standard_only(PrecompilesMap::from_static(op_precompiles)),
465+
// Conditionally enable Celo precompile based on the is_celo flag
466+
if env.is_celo {
467+
HybridPrecompileProvider::new(PrecompilesMap::from_static(op_precompiles))
468+
} else {
469+
HybridPrecompileProvider::standard_only(PrecompilesMap::from_static(op_precompiles))
470+
},
465471
));
466472

467473
let op = OpEvm::new(op_evm, true);
@@ -492,9 +498,7 @@ where
492498
eth_context,
493499
inspector,
494500
EthInstructions::default(),
495-
// TODO: Only use standard precompiles here and pull out version with stateful
496-
// precompiles into a Celo-specific version of the Optimism branch.
497-
HybridPrecompileProvider::new(PrecompilesMap::from_static(eth_precompiles)),
501+
HybridPrecompileProvider::standard_only(PrecompilesMap::from_static(eth_precompiles)),
498502
);
499503

500504
let eth = EthEvm::new(eth_evm, true);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,11 @@ impl Backend {
834834
self.env.read().is_optimism
835835
}
836836

837+
/// Returns true if Celo features are active
838+
pub fn is_celo(&self) -> bool {
839+
self.env.read().is_celo
840+
}
841+
837842
/// Returns [`BlobParams`] corresponding to the current spec.
838843
pub fn blob_params(&self) -> BlobParams {
839844
let spec_id = self.env.read().evm_env.cfg_env.spec;
@@ -1265,6 +1270,7 @@ impl Backend {
12651270
precompile_factory: self.precompile_factory.clone(),
12661271
odyssey: self.odyssey,
12671272
optimism: self.is_optimism(),
1273+
celo: self.is_celo(),
12681274
blob_params: self.blob_params(),
12691275
};
12701276

@@ -1352,6 +1358,7 @@ impl Backend {
13521358
odyssey: self.odyssey,
13531359
precompile_factory: self.precompile_factory.clone(),
13541360
optimism: self.is_optimism(),
1361+
celo: self.is_celo(),
13551362
blob_params: self.blob_params(),
13561363
};
13571364
let executed_tx = executor.execute();

crates/anvil/src/evm/stateful_precompile.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ where
6565
is_static: bool,
6666
gas_limit: u64,
6767
) -> Result<Option<Self::Output>, String> {
68-
// Check if this is the Celo transfer precompile
69-
if address == &CELO_TRANSFER_ADDRESS {
70-
return celo_transfer_precompile(context, inputs, gas_limit);
68+
if self.stateful_precompile_addresses.contains(address) {
69+
// Check if this is the Celo transfer precompile
70+
if address == &CELO_TRANSFER_ADDRESS {
71+
return celo_transfer_precompile(context, inputs, gas_limit);
72+
}
7173
}
7274

7375
// Fall back to standard precompiles

0 commit comments

Comments
 (0)