Skip to main content

foundry_evm_core/
opts.rs

1use crate::{
2    EvmEnv, FoundryBlock, FoundryTransaction,
3    constants::DEFAULT_CREATE2_DEPLOYER,
4    fork::CreateFork,
5    utils::{apply_chain_and_block_specific_env_changes, block_env_from_header},
6};
7use alloy_chains::NamedChain;
8use alloy_consensus::BlockHeader;
9use alloy_network::{AnyNetwork, BlockResponse, Network};
10use alloy_primitives::{Address, B256, BlockNumber, ChainId, U256};
11use alloy_provider::{Provider, RootProvider};
12use alloy_rpc_types::{BlockNumberOrTag, anvil::NodeInfo};
13use eyre::WrapErr;
14use foundry_common::{ALCHEMY_FREE_TIER_CUPS, NON_ARCHIVE_NODE_WARNING, provider::ProviderBuilder};
15use foundry_config::{Chain, Config, GasLimit};
16use foundry_evm_networks::NetworkConfigs;
17use revm::{context::CfgEnv, primitives::hardfork::SpecId};
18use serde::{Deserialize, Serialize};
19use std::fmt::Write;
20use url::Url;
21
22#[derive(Clone, Debug, Serialize, Deserialize)]
23pub struct EvmOpts {
24    /// The EVM environment configuration.
25    #[serde(flatten)]
26    pub env: Env,
27
28    /// Fetch state over a remote instead of starting from empty state.
29    #[serde(rename = "eth_rpc_url")]
30    pub fork_url: Option<String>,
31
32    /// Pins the block number for the state fork.
33    pub fork_block_number: Option<u64>,
34
35    /// The number of retries.
36    pub fork_retries: Option<u32>,
37
38    /// Initial retry backoff.
39    pub fork_retry_backoff: Option<u64>,
40
41    /// Headers to use with `fork_url`
42    pub fork_headers: Option<Vec<String>>,
43
44    /// The available compute units per second.
45    ///
46    /// See also <https://docs.alchemy.com/reference/compute-units#what-are-cups-compute-units-per-second>
47    pub compute_units_per_second: Option<u64>,
48
49    /// Disables RPC rate limiting entirely.
50    pub no_rpc_rate_limit: bool,
51
52    /// Disables storage caching entirely.
53    pub no_storage_caching: bool,
54
55    /// The initial balance of each deployed test contract.
56    pub initial_balance: U256,
57
58    /// The address which will be executing all tests.
59    pub sender: Address,
60
61    /// Enables the FFI cheatcode.
62    pub ffi: bool,
63
64    /// Use the create 2 factory in all cases including tests and non-broadcasting scripts.
65    pub always_use_create_2_factory: bool,
66
67    /// Verbosity mode of EVM output as number of occurrences.
68    pub verbosity: u8,
69
70    /// The memory limit per EVM execution in bytes.
71    /// If this limit is exceeded, a `MemoryLimitOOG` result is thrown.
72    pub memory_limit: u64,
73
74    /// Whether to enable isolation of calls.
75    pub isolate: bool,
76
77    /// Whether to disable block gas limit checks.
78    pub disable_block_gas_limit: bool,
79
80    /// Whether to enable tx gas limit checks as imposed by Osaka (EIP-7825).
81    pub enable_tx_gas_limit: bool,
82
83    #[serde(flatten)]
84    /// Networks with enabled features.
85    pub networks: NetworkConfigs,
86
87    /// The CREATE2 deployer's address.
88    pub create2_deployer: Address,
89}
90
91impl Default for EvmOpts {
92    fn default() -> Self {
93        Self {
94            env: Env::default(),
95            fork_url: None,
96            fork_block_number: None,
97            fork_retries: None,
98            fork_retry_backoff: None,
99            fork_headers: None,
100            compute_units_per_second: None,
101            no_rpc_rate_limit: false,
102            no_storage_caching: false,
103            initial_balance: U256::default(),
104            sender: Address::default(),
105            ffi: false,
106            always_use_create_2_factory: false,
107            verbosity: 0,
108            memory_limit: 0,
109            isolate: false,
110            disable_block_gas_limit: false,
111            enable_tx_gas_limit: false,
112            networks: NetworkConfigs::default(),
113            create2_deployer: DEFAULT_CREATE2_DEPLOYER,
114        }
115    }
116}
117
118impl EvmOpts {
119    /// Returns a `RootProvider` for the given fork URL configured with options in `self` and
120    /// annotated `Network` type.
121    pub fn fork_provider_with_url<N: Network>(
122        &self,
123        fork_url: &str,
124    ) -> eyre::Result<RootProvider<N>> {
125        ProviderBuilder::new(fork_url)
126            .maybe_max_retry(self.fork_retries)
127            .maybe_initial_backoff(self.fork_retry_backoff)
128            .maybe_headers(self.fork_headers.clone())
129            .compute_units_per_second(self.get_compute_units_per_second())
130            .build()
131    }
132
133    /// Infers the network configuration from the fork chain ID if not already set.
134    ///
135    /// When a fork URL is configured and the network has not been explicitly set,
136    /// this fetches the chain ID from the remote endpoint and calls
137    /// [`NetworkConfigs::with_chain_id`] to auto-enable the correct network
138    /// (e.g. Tempo, OP Stack) based on the chain ID.
139    pub async fn infer_network_from_fork(&mut self) {
140        #[cfg(feature = "optimism")]
141        let already_op = self.networks.is_optimism();
142        #[cfg(not(feature = "optimism"))]
143        let already_op = false;
144        if !self.networks.is_tempo()
145            && !already_op
146            && let Some(ref fork_url) = self.fork_url
147            && let Ok(provider) = self.fork_provider_with_url::<AnyNetwork>(fork_url)
148            && let Ok(chain_id) = provider.get_chain_id().await
149        {
150            // If Anvil's chain, request anvil_nodeInfo to determine if the network is Tempo.
151            if chain_id == NamedChain::AnvilHardhat as u64 {
152                if let Ok(node_info) =
153                    provider.raw_request::<_, NodeInfo>("anvil_nodeInfo".into(), ()).await
154                    && node_info.network.is_some_and(|network| network == "tempo")
155                {
156                    self.networks = NetworkConfigs::with_tempo();
157                }
158            } else {
159                self.networks = self.networks.with_chain_id(chain_id);
160            }
161        }
162    }
163
164    /// Returns a tuple with [`EvmEnv`], `TxEnv`, and the actual fork block number.
165    ///
166    /// If a `fork_url` is set, creates a provider and passes it to both `EvmOpts::fork_evm_env`
167    /// and `EvmOpts::fork_tx_env`. Falls back to local settings when no fork URL is configured.
168    ///
169    /// The fork block number is returned separately because on some L2s (e.g., Arbitrum) the
170    /// `block_env.number` may be remapped (to the L1 block number) and therefore cannot be used
171    /// to pin the fork.
172    pub async fn env<
173        SPEC: Into<SpecId> + Default + Copy,
174        BLOCK: FoundryBlock + Default,
175        TX: FoundryTransaction + Default,
176    >(
177        &self,
178    ) -> eyre::Result<(EvmEnv<SPEC, BLOCK>, TX, Option<BlockNumber>)> {
179        if let Some(ref fork_url) = self.fork_url {
180            let provider = self.fork_provider_with_url::<AnyNetwork>(fork_url)?;
181            let ((evm_env, block_number), tx) =
182                tokio::try_join!(self.fork_evm_env(&provider), self.fork_tx_env(&provider))?;
183            Ok((evm_env, tx, Some(block_number)))
184        } else {
185            Ok((self.local_evm_env(), self.local_tx_env(), None))
186        }
187    }
188
189    /// Returns the [`EvmEnv`] (cfg + block) and [`BlockNumber`] fetched from the fork endpoint via
190    /// provider
191    pub async fn fork_evm_env<
192        SPEC: Into<SpecId> + Default + Copy,
193        BLOCK: FoundryBlock + Default,
194        N: Network,
195        P: Provider<N>,
196    >(
197        &self,
198        provider: &P,
199    ) -> eyre::Result<(EvmEnv<SPEC, BLOCK>, BlockNumber)> {
200        trace!(
201            memory_limit = %self.memory_limit,
202            override_chain_id = ?self.env.chain_id,
203            pin_block = ?self.fork_block_number,
204            origin = %self.sender,
205            disable_block_gas_limit = %self.disable_block_gas_limit,
206            enable_tx_gas_limit = %self.enable_tx_gas_limit,
207            configs = ?self.networks,
208            "creating fork environment"
209        );
210
211        let bn = match self.fork_block_number {
212            Some(bn) => BlockNumberOrTag::Number(bn),
213            None => BlockNumberOrTag::Latest,
214        };
215
216        let (chain_id, block) = tokio::try_join!(
217            option_try_or_else(self.env.chain_id, async || provider.get_chain_id().await),
218            provider.get_block_by_number(bn)
219        )
220        .wrap_err_with(|| {
221            let mut msg = "could not instantiate forked environment".to_string();
222            if let Some(fork_url) = self.fork_url.as_deref()
223                && let Ok(url) = Url::parse(fork_url)
224                && let Some(host) = url.host()
225            {
226                write!(msg, " with provider {host}").unwrap();
227            }
228            msg
229        })?;
230
231        let Some(block) = block else {
232            let bn_msg = match bn {
233                BlockNumberOrTag::Number(bn) => format!("block number: {bn}"),
234                bn => format!("{bn} block"),
235            };
236            let latest_msg = if let Ok(latest_block) = provider.get_block_number().await {
237                if let Some(block_number) = self.fork_block_number
238                    && block_number <= latest_block
239                {
240                    error!("{NON_ARCHIVE_NODE_WARNING}");
241                }
242                format!("; latest block number: {latest_block}")
243            } else {
244                Default::default()
245            };
246            eyre::bail!("failed to get {bn_msg}{latest_msg}");
247        };
248
249        let block_number = block.header().number();
250        let mut evm_env = EvmEnv {
251            cfg_env: self.cfg_env(chain_id),
252            block_env: block_env_from_header(block.header()),
253        };
254
255        apply_chain_and_block_specific_env_changes::<N, _, _>(&mut evm_env, &block, self.networks);
256
257        Ok((evm_env, block_number))
258    }
259
260    /// Returns the [`EvmEnv`] configured with only local settings.
261    fn local_evm_env<SPEC: Into<SpecId> + Default + Clone, BLOCK: FoundryBlock + Default>(
262        &self,
263    ) -> EvmEnv<SPEC, BLOCK> {
264        let cfg_env = self.cfg_env(self.env.chain_id.unwrap_or(foundry_common::DEV_CHAIN_ID));
265        let mut block_env = BLOCK::default();
266        block_env.set_number(self.env.block_number);
267        block_env.set_beneficiary(self.env.block_coinbase);
268        block_env.set_timestamp(self.env.block_timestamp);
269        block_env.set_difficulty(U256::from(self.env.block_difficulty));
270        block_env.set_prevrandao(Some(self.env.block_prevrandao));
271        block_env.set_basefee(self.env.block_base_fee_per_gas);
272        block_env.set_gas_limit(self.gas_limit());
273        EvmEnv::new(cfg_env, block_env)
274    }
275
276    /// Returns the `TxEnv` with gas price and chain id resolved from provider.
277    async fn fork_tx_env<TX: FoundryTransaction + Default, N: Network, P: Provider<N>>(
278        &self,
279        provider: &P,
280    ) -> eyre::Result<TX> {
281        let (gas_price, chain_id) = tokio::try_join!(
282            option_try_or_else(self.env.gas_price.map(|v| v as u128), async || {
283                provider.get_gas_price().await
284            }),
285            option_try_or_else(self.env.chain_id, async || provider.get_chain_id().await),
286        )?;
287        let mut tx_env = TX::default();
288        tx_env.set_caller(self.sender);
289        tx_env.set_chain_id(Some(chain_id));
290        tx_env.set_gas_price(gas_price);
291        tx_env.set_gas_limit(self.gas_limit());
292        Ok(tx_env)
293    }
294
295    /// Returns the `TxEnv` configured from local settings only.
296    fn local_tx_env<TX: FoundryTransaction + Default>(&self) -> TX {
297        let mut tx_env = TX::default();
298        tx_env.set_caller(self.sender);
299        tx_env.set_gas_price(self.env.gas_price.unwrap_or_default().into());
300        tx_env.set_gas_limit(self.gas_limit());
301        tx_env
302    }
303
304    /// Builds a [`CfgEnv`] from the options, using the provided [`ChainId`].
305    fn cfg_env<SPEC: Into<SpecId> + Default + Clone>(&self, chain_id: ChainId) -> CfgEnv<SPEC> {
306        let mut cfg = CfgEnv::default();
307        cfg.chain_id = chain_id;
308        cfg.memory_limit = self.memory_limit;
309        cfg.limit_contract_code_size = self.env.code_size_limit.or(Some(usize::MAX));
310        // EIP-3607 rejects transactions from senders with deployed code.
311        // If EIP-3607 is enabled it can cause issues during fuzz/invariant tests if the caller
312        // is a contract. So we disable the check by default.
313        cfg.disable_eip3607 = true;
314        cfg.disable_block_gas_limit = self.disable_block_gas_limit;
315        cfg.disable_nonce_check = true;
316        // By default do not enforce transaction gas limits imposed by Osaka (EIP-7825).
317        // Users can opt-in to enable these limits by setting `enable_tx_gas_limit` to true.
318        if !self.enable_tx_gas_limit {
319            cfg.tx_gas_limit_cap = Some(u64::MAX);
320        }
321        cfg
322    }
323
324    /// Helper function that returns the [CreateFork] to use, if any.
325    ///
326    /// storage caching for the [CreateFork] will be enabled if
327    ///   - `fork_url` is present
328    ///   - `fork_block_number` is present
329    ///   - `StorageCachingConfig` allows the `fork_url` + chain ID pair
330    ///   - storage is allowed (`no_storage_caching = false`)
331    ///
332    /// If all these criteria are met, then storage caching is enabled and storage info will be
333    /// written to `<Config::foundry_cache_dir()>/<str(chainid)>/<block>/storage.json`.
334    ///
335    /// for `mainnet` and `--fork-block-number 14435000` on mac the corresponding storage cache will
336    /// be at `~/.foundry/cache/mainnet/14435000/storage.json`.
337    /// `fork_block_number` is the actual block number to pin the fork to. This must be the
338    /// real chain block number, not a remapped value. On some L2s (e.g., Arbitrum)
339    /// `block_env.number` is remapped to the L1 block number, so callers must pass the
340    /// original block number returned by [`EvmOpts::env`] instead.
341    pub fn get_fork(
342        &self,
343        config: &Config,
344        chain_id: u64,
345        fork_block_number: Option<BlockNumber>,
346    ) -> Option<CreateFork> {
347        let url = self.fork_url.clone()?;
348        let enable_caching = config.enable_caching(&url, chain_id);
349
350        // Pin fork_block_number to the block that was already fetched in env, so subsequent
351        // fork operations use the same block. This prevents inconsistencies when forking at
352        // "latest" where the chain could advance between calls.
353        let mut evm_opts = self.clone();
354        evm_opts.fork_block_number = evm_opts.fork_block_number.or(fork_block_number);
355
356        Some(CreateFork { url, enable_caching, evm_opts })
357    }
358
359    /// Returns the gas limit to use
360    pub fn gas_limit(&self) -> u64 {
361        self.env.block_gas_limit.unwrap_or(self.env.gas_limit).0
362    }
363
364    /// Returns the available compute units per second, which will be
365    /// - u64::MAX, if `no_rpc_rate_limit` if set (as rate limiting is disabled)
366    /// - the assigned compute units, if `compute_units_per_second` is set
367    /// - ALCHEMY_FREE_TIER_CUPS (330) otherwise
368    const fn get_compute_units_per_second(&self) -> u64 {
369        if self.no_rpc_rate_limit {
370            u64::MAX
371        } else if let Some(cups) = self.compute_units_per_second {
372            cups
373        } else {
374            ALCHEMY_FREE_TIER_CUPS
375        }
376    }
377
378    /// Returns the chain ID from the RPC, if any.
379    pub async fn get_remote_chain_id(&self) -> Option<Chain> {
380        if let Some(url) = &self.fork_url
381            && let Ok(provider) = self.fork_provider_with_url::<AnyNetwork>(url)
382        {
383            trace!(?url, "retrieving chain via eth_chainId");
384
385            if let Ok(id) = provider.get_chain_id().await {
386                return Some(Chain::from(id));
387            }
388
389            // Provider URLs could be of the format `{CHAIN_IDENTIFIER}-mainnet`
390            // (e.g. Alchemy `opt-mainnet`, `arb-mainnet`), fallback to this method only
391            // if we're not able to retrieve chain id from `RetryProvider`.
392            if url.contains("mainnet") {
393                trace!(?url, "auto detected mainnet chain");
394                return Some(Chain::mainnet());
395            }
396        }
397
398        None
399    }
400}
401
402#[derive(Clone, Debug, Default, Serialize, Deserialize)]
403pub struct Env {
404    /// The block gas limit.
405    pub gas_limit: GasLimit,
406
407    /// The `CHAINID` opcode value.
408    pub chain_id: Option<u64>,
409
410    /// the tx.gasprice value during EVM execution
411    ///
412    /// This is an Option, so we can determine in fork mode whether to use the config's gas price
413    /// (if set by user) or the remote client's gas price.
414    #[serde(default, skip_serializing_if = "Option::is_none")]
415    pub gas_price: Option<u64>,
416
417    /// the base fee in a block
418    pub block_base_fee_per_gas: u64,
419
420    /// the tx.origin value during EVM execution
421    pub tx_origin: Address,
422
423    /// the block.coinbase value during EVM execution
424    pub block_coinbase: Address,
425
426    /// the block.timestamp value during EVM execution
427    #[serde(
428        deserialize_with = "foundry_config::deserialize_u64_to_u256",
429        serialize_with = "foundry_config::serialize_u64_or_u256"
430    )]
431    pub block_timestamp: U256,
432
433    /// the block.number value during EVM execution"
434    #[serde(
435        deserialize_with = "foundry_config::deserialize_u64_to_u256",
436        serialize_with = "foundry_config::serialize_u64_or_u256"
437    )]
438    pub block_number: U256,
439
440    /// the block.difficulty value during EVM execution
441    pub block_difficulty: u64,
442
443    /// Previous block beacon chain random value. Before merge this field is used for mix_hash
444    pub block_prevrandao: B256,
445
446    /// the block.gaslimit value during EVM execution
447    #[serde(default, skip_serializing_if = "Option::is_none")]
448    pub block_gas_limit: Option<GasLimit>,
449
450    /// EIP-170: Contract code size limit in bytes. Useful to increase this because of tests.
451    #[serde(default, skip_serializing_if = "Option::is_none")]
452    pub code_size_limit: Option<usize>,
453}
454
455async fn option_try_or_else<T, E>(
456    option: Option<T>,
457    f: impl AsyncFnOnce() -> Result<T, E>,
458) -> Result<T, E> {
459    if let Some(value) = option { Ok(value) } else { f().await }
460}
461
462#[cfg(test)]
463mod tests {
464    use revm::context::{BlockEnv, TxEnv};
465
466    use super::*;
467
468    #[tokio::test(flavor = "multi_thread")]
469    async fn infer_network_default_anvil_selects_ethereum() {
470        let (_api, handle) = anvil::spawn(anvil::NodeConfig::test()).await;
471
472        let config = Config::figment();
473        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
474        evm_opts.fork_url = Some(handle.http_endpoint());
475        assert_eq!(evm_opts.networks, NetworkConfigs::default());
476
477        evm_opts.infer_network_from_fork().await;
478
479        // Plain anvil (chain id 31337) without tempo flag -> Ethereum (no network flags set).
480        assert!(!evm_opts.networks.is_tempo());
481        #[cfg(feature = "optimism")]
482        assert!(!evm_opts.networks.is_optimism());
483        assert!(!evm_opts.networks.is_celo());
484        assert_eq!(evm_opts.networks, NetworkConfigs::default());
485    }
486
487    #[tokio::test(flavor = "multi_thread")]
488    async fn infer_network_tempo_anvil_via_node_info() {
489        let (_api, handle) = anvil::spawn(anvil::NodeConfig::test_tempo()).await;
490
491        let config = Config::figment();
492        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
493        evm_opts.fork_url = Some(handle.http_endpoint());
494        // Networks not set -> should query anvil_nodeInfo to discover tempo.
495        assert_eq!(evm_opts.networks, NetworkConfigs::default());
496
497        evm_opts.infer_network_from_fork().await;
498
499        assert!(evm_opts.networks.is_tempo(), "should detect tempo via anvil_nodeInfo");
500    }
501
502    #[tokio::test(flavor = "multi_thread")]
503    async fn infer_network_tempo_anvil_skips_rpc_when_already_set() {
504        // Use a URL that would fail if any RPC call were attempted (connection refused).
505        // This proves the early-return guard prevents all network requests.
506        let config = Config::figment();
507        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
508        evm_opts.fork_url = Some("http://127.0.0.1:1".to_string());
509        // Explicitly set tempo before calling infer (simulates --tempo CLI flag).
510        evm_opts.networks = NetworkConfigs::with_tempo();
511
512        evm_opts.infer_network_from_fork().await;
513
514        // Should still be tempo, the early-return guard skips the RPC call.
515        assert!(evm_opts.networks.is_tempo());
516    }
517
518    #[tokio::test(flavor = "multi_thread")]
519    async fn flaky_infer_network_tempo_moderato_rpc() {
520        let config = Config::figment();
521        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
522        evm_opts.fork_url = Some("https://rpc.moderato.tempo.xyz".to_string());
523        assert_eq!(evm_opts.networks, NetworkConfigs::default());
524
525        evm_opts.infer_network_from_fork().await;
526
527        // Tempo Moderato has a known Tempo chain ID -> should be inferred via with_chain_id.
528        assert!(evm_opts.networks.is_tempo(), "should detect tempo from Moderato chain ID");
529    }
530
531    #[tokio::test(flavor = "multi_thread")]
532    async fn get_fork_pins_block_number_from_env() {
533        let endpoint = foundry_test_utils::rpc::next_http_rpc_endpoint();
534
535        let config = Config::figment();
536        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
537        evm_opts.fork_url = Some(endpoint.clone());
538        // Explicitly leave fork_block_number as None to simulate --fork-url without --block-number
539        assert!(evm_opts.fork_block_number.is_none());
540
541        // Fetch the environment (this resolves "latest" to an actual block number)
542        let (evm_env, _, fork_block) = evm_opts.env::<SpecId, BlockEnv, TxEnv>().await.unwrap();
543        assert!(fork_block.is_some(), "should have resolved a fork block number");
544        let resolved_block = fork_block.unwrap();
545        assert!(resolved_block > 0, "should have resolved to a real block number");
546
547        // Create the fork - this should pin the block number
548        let fork =
549            evm_opts.get_fork(&Config::default(), evm_env.cfg_env.chain_id, fork_block).unwrap();
550
551        // The fork's evm_opts should now have fork_block_number set to the resolved block
552        assert_eq!(
553            fork.evm_opts.fork_block_number,
554            Some(resolved_block),
555            "get_fork should pin fork_block_number to the block from env"
556        );
557    }
558
559    // Regression test for https://github.com/foundry-rs/foundry/issues/13576
560    // On Arbitrum, `block_env.number` is remapped to the L1 block number by
561    // `apply_chain_and_block_specific_env_changes`. The fork block number returned
562    // by `env()` must be the actual L2 block number, not the remapped L1 value.
563    #[tokio::test(flavor = "multi_thread")]
564    async fn flaky_get_fork_uses_l2_block_number_on_arbitrum() {
565        let endpoint =
566            foundry_test_utils::rpc::next_rpc_endpoint(foundry_config::NamedChain::Arbitrum);
567
568        let config = Config::figment();
569        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
570        evm_opts.fork_url = Some(endpoint.clone());
571        assert!(evm_opts.fork_block_number.is_none());
572
573        let (evm_env, _, fork_block) = evm_opts.env::<SpecId, BlockEnv, TxEnv>().await.unwrap();
574        let fork_block = fork_block.expect("should have resolved a fork block number");
575
576        // On Arbitrum, block_env.number is the L1 block number (much smaller).
577        // The fork_block should be the actual L2 block number (much larger).
578        let block_env_number: u64 = evm_env.block_env.number.to();
579        assert!(
580            fork_block > block_env_number,
581            "fork_block ({fork_block}) should be the L2 block, which is larger than \
582             block_env.number ({block_env_number}) which is the L1 block on Arbitrum"
583        );
584
585        // Verify get_fork pins to the correct L2 block number
586        let fork = evm_opts
587            .get_fork(&Config::default(), evm_env.cfg_env.chain_id, Some(fork_block))
588            .unwrap();
589        assert_eq!(
590            fork.evm_opts.fork_block_number,
591            Some(fork_block),
592            "get_fork should pin to the L2 block number, not the L1 block number"
593        );
594    }
595
596    #[tokio::test(flavor = "multi_thread")]
597    async fn get_fork_preserves_explicit_block_number() {
598        let endpoint = foundry_test_utils::rpc::next_http_rpc_endpoint();
599
600        let config = Config::figment();
601        let mut evm_opts = config.extract::<EvmOpts>().unwrap();
602        evm_opts.fork_url = Some(endpoint.clone());
603        // Set an explicit block number
604        evm_opts.fork_block_number = Some(12345678);
605
606        let (evm_env, _, fork_block) = evm_opts.env::<SpecId, BlockEnv, TxEnv>().await.unwrap();
607
608        let fork =
609            evm_opts.get_fork(&Config::default(), evm_env.cfg_env.chain_id, fork_block).unwrap();
610
611        // Should preserve the explicit block number, not override it
612        assert_eq!(
613            fork.evm_opts.fork_block_number,
614            Some(12345678),
615            "get_fork should preserve explicitly set fork_block_number"
616        );
617    }
618}