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