revm_primitives/env/
handler_cfg.rs
1use super::{BlockEnv, CfgEnv, Env, SpecId, TxEnv};
2use core::ops::{Deref, DerefMut};
3use std::boxed::Box;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
9pub struct HandlerCfg {
10 pub spec_id: SpecId,
12 #[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 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 #[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 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#[derive(Clone, Debug, Eq, PartialEq)]
64pub struct CfgEnvWithHandlerCfg {
65 pub cfg_env: CfgEnv,
67 pub handler_cfg: HandlerCfg,
69}
70
71impl CfgEnvWithHandlerCfg {
72 pub fn new(cfg_env: CfgEnv, handler_cfg: HandlerCfg) -> Self {
74 Self {
75 cfg_env,
76 handler_cfg,
77 }
78 }
79
80 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 #[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#[derive(Clone, Debug, Default, Eq, PartialEq)]
110pub struct EnvWithHandlerCfg {
111 pub env: Box<Env>,
113 pub handler_cfg: HandlerCfg,
115}
116
117impl EnvWithHandlerCfg {
118 pub fn new(env: Box<Env>, handler_cfg: HandlerCfg) -> Self {
120 Self { env, handler_cfg }
121 }
122
123 pub fn new_with_spec_id(env: Box<Env>, spec_id: SpecId) -> Self {
127 Self::new(env, HandlerCfg::new(spec_id))
128 }
129
130 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 pub const fn spec_id(&self) -> SpecId {
137 self.handler_cfg.spec_id
138 }
139
140 #[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}