Skip to main content

foundry_evm_core/env/
cfg.rs

1//! Configuration access and hardfork gas parameters.
2
3use revm::{
4    context::{Cfg, CfgEnv},
5    primitives::hardfork::SpecId,
6};
7use std::fmt::Debug;
8
9/// Access to a configuration's underlying environment and hardfork updates.
10pub trait FoundryCfg:
11    Cfg<Spec: Into<SpecId> + Copy + Debug> + Clone + From<CfgEnv<Self::Spec>> + Into<CfgEnv<Self::Spec>>
12{
13    /// Reference to the underlying configuration.
14    fn cfg_env(&self) -> &CfgEnv<Self::Spec>;
15
16    /// Mutable reference to the underlying configuration.
17    fn cfg_env_mut(&mut self) -> &mut CfgEnv<Self::Spec>;
18
19    /// Updates the hardfork and its gas parameters.
20    fn set_spec_and_gas_params(&mut self, spec: Self::Spec) {
21        self.cfg_env_mut().set_spec_and_mainnet_gas_params(spec);
22    }
23}
24
25impl<SPEC: Into<SpecId> + Copy + Debug> FoundryCfg for CfgEnv<SPEC> {
26    fn cfg_env(&self) -> &Self {
27        self
28    }
29
30    fn cfg_env_mut(&mut self) -> &mut Self {
31        self
32    }
33}
34
35#[cfg(feature = "monad")]
36impl FoundryCfg for monad_revm::MonadCfgEnv {
37    fn cfg_env(&self) -> &CfgEnv<Self::Spec> {
38        self.inner()
39    }
40
41    fn cfg_env_mut(&mut self) -> &mut CfgEnv<Self::Spec> {
42        self.inner_mut()
43    }
44
45    fn set_spec_and_gas_params(&mut self, spec: Self::Spec) {
46        self.inner_mut().spec = spec;
47        self.inner_mut().set_gas_params(monad_revm::instructions::monad_gas_params(spec));
48    }
49}
50
51#[cfg(all(test, feature = "monad"))]
52mod tests {
53    use super::*;
54    use crate::env::{EvmEnv, FoundryContextExt};
55    use alloy_evm::EvmFactory;
56    use monad_revm::{MonadHardfork, cfg::MONAD_MEMORY_LIMIT, instructions::monad_gas_params};
57    use revm::{context::BlockEnv, context_interface::ContextTr, database::EmptyDB};
58
59    #[test]
60    fn monad_memory_limit_follows_hardfork_transitions() {
61        const FOUNDRY_MEMORY_LIMIT: u64 = 128 * 1024 * 1024;
62
63        let mut cfg = CfgEnv::new_with_spec(MonadHardfork::MonadEight);
64        cfg.memory_limit = FOUNDRY_MEMORY_LIMIT;
65        let mut evm = alloy_monad_evm::MonadEvmFactory::default()
66            .create_evm(EmptyDB::default(), EvmEnv::new(cfg, BlockEnv::default()));
67
68        assert_eq!(evm.ctx().cfg().memory_limit(), FOUNDRY_MEMORY_LIMIT);
69
70        for (spec, memory_limit) in [
71            (MonadHardfork::MonadNine, MONAD_MEMORY_LIMIT),
72            (MonadHardfork::MonadEight, FOUNDRY_MEMORY_LIMIT),
73        ] {
74            evm.ctx_mut().set_spec_and_gas_params(spec);
75            assert_eq!(evm.ctx().cfg().inner().memory_limit, FOUNDRY_MEMORY_LIMIT);
76            assert_eq!(evm.ctx().cfg().memory_limit(), memory_limit);
77            assert_eq!(evm.ctx().cfg().inner().gas_params, monad_gas_params(spec));
78        }
79    }
80}