revm_primitives/env/
handler_cfg.rs

1use super::{BlockEnv, CfgEnv, Env, SpecId, TxEnv};
2use core::ops::{Deref, DerefMut};
3use std::boxed::Box;
4
5/// Handler configuration fields. It is used to configure the handler.
6/// It contains specification id and the Optimism related field if
7/// optimism feature is enabled.
8#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
9pub struct HandlerCfg {
10    /// Specification identification.
11    pub spec_id: SpecId,
12    /// Optimism related field, it will append the Optimism handle register to the EVM.
13    #[cfg(feature = "optimism")]
14    pub is_optimism: bool,
15}
16
17impl Default for HandlerCfg {
18    fn default() -> Self {
19        Self::new(SpecId::default())
20    }
21}
22
23impl HandlerCfg {
24    /// Creates new `HandlerCfg` instance.
25    pub fn new(spec_id: SpecId) -> Self {
26        cfg_if::cfg_if! {
27            if #[cfg(all(feature = "optimism-default-handler",
28                not(feature = "negate-optimism-default-handler")))] {
29                    let is_optimism = true;
30            } else if #[cfg(feature = "optimism")] {
31                let is_optimism = false;
32            }
33        }
34        Self {
35            spec_id,
36            #[cfg(feature = "optimism")]
37            is_optimism,
38        }
39    }
40
41    /// Creates new `HandlerCfg` instance with the optimism feature.
42    #[cfg(feature = "optimism")]
43    pub fn new_with_optimism(spec_id: SpecId, is_optimism: bool) -> Self {
44        Self {
45            spec_id,
46            is_optimism,
47        }
48    }
49
50    /// Returns `true` if the optimism feature is enabled and flag is set to `true`.
51    pub fn is_optimism(&self) -> bool {
52        cfg_if::cfg_if! {
53            if #[cfg(feature = "optimism")] {
54                self.is_optimism
55            } else {
56                false
57            }
58        }
59    }
60}
61
62/// Configuration environment with the chain spec id.
63#[derive(Clone, Debug, Eq, PartialEq)]
64pub struct CfgEnvWithHandlerCfg {
65    /// Configuration environment.
66    pub cfg_env: CfgEnv,
67    /// Handler configuration fields.
68    pub handler_cfg: HandlerCfg,
69}
70
71impl CfgEnvWithHandlerCfg {
72    /// Returns new instance of `CfgEnvWithHandlerCfg` with the handler configuration.
73    pub fn new(cfg_env: CfgEnv, handler_cfg: HandlerCfg) -> Self {
74        Self {
75            cfg_env,
76            handler_cfg,
77        }
78    }
79
80    /// Returns new `CfgEnvWithHandlerCfg` instance with the chain spec id.
81    ///
82    /// is_optimism will be set to default value depending on `optimism-default-handler` feature.
83    pub fn new_with_spec_id(cfg_env: CfgEnv, spec_id: SpecId) -> Self {
84        Self::new(cfg_env, HandlerCfg::new(spec_id))
85    }
86
87    /// Enables the optimism feature.
88    #[cfg(feature = "optimism")]
89    pub fn enable_optimism(&mut self) {
90        self.handler_cfg.is_optimism = true;
91    }
92}
93
94impl DerefMut for CfgEnvWithHandlerCfg {
95    fn deref_mut(&mut self) -> &mut Self::Target {
96        &mut self.cfg_env
97    }
98}
99
100impl Deref for CfgEnvWithHandlerCfg {
101    type Target = CfgEnv;
102
103    fn deref(&self) -> &Self::Target {
104        &self.cfg_env
105    }
106}
107
108/// Evm environment with the chain spec id.
109#[derive(Clone, Debug, Default, Eq, PartialEq)]
110pub struct EnvWithHandlerCfg {
111    /// Evm environment.
112    pub env: Box<Env>,
113    /// Handler configuration fields.
114    pub handler_cfg: HandlerCfg,
115}
116
117impl EnvWithHandlerCfg {
118    /// Returns new `EnvWithHandlerCfg` instance.
119    pub fn new(env: Box<Env>, handler_cfg: HandlerCfg) -> Self {
120        Self { env, handler_cfg }
121    }
122
123    /// Returns new `EnvWithHandlerCfg` instance with the chain spec id.
124    ///
125    /// is_optimism will be set to default value depending on `optimism-default-handler` feature.
126    pub fn new_with_spec_id(env: Box<Env>, spec_id: SpecId) -> Self {
127        Self::new(env, HandlerCfg::new(spec_id))
128    }
129
130    /// Takes `CfgEnvWithHandlerCfg` and returns new `EnvWithHandlerCfg` instance.
131    pub fn new_with_cfg_env(cfg: CfgEnvWithHandlerCfg, block: BlockEnv, tx: TxEnv) -> Self {
132        Self::new(Env::boxed(cfg.cfg_env, block, tx), cfg.handler_cfg)
133    }
134
135    /// Returns the specification id.
136    pub const fn spec_id(&self) -> SpecId {
137        self.handler_cfg.spec_id
138    }
139
140    /// Enables the optimism handle register.
141    #[cfg(feature = "optimism")]
142    pub fn enable_optimism(&mut self) {
143        self.handler_cfg.is_optimism = true;
144    }
145}
146
147impl DerefMut for EnvWithHandlerCfg {
148    fn deref_mut(&mut self) -> &mut Self::Target {
149        &mut self.env
150    }
151}
152
153impl Deref for EnvWithHandlerCfg {
154    type Target = Env;
155
156    fn deref(&self) -> &Self::Target {
157        &self.env
158    }
159}