1pub use context_interface::Cfg;
3
4use primitives::{eip170::MAX_CODE_SIZE, hardfork::SpecId};
5
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[derive(Clone, Debug, Eq, PartialEq)]
9#[non_exhaustive]
10pub struct CfgEnv<SPEC = SpecId> {
11 pub chain_id: u64,
17 pub spec: SPEC,
19 pub limit_contract_code_size: Option<usize>,
25 pub disable_nonce_check: bool,
27 pub blob_max_count: Option<u64>,
31 #[cfg(feature = "memory_limit")]
39 pub memory_limit: u64,
40 #[cfg(feature = "optional_balance_check")]
46 pub disable_balance_check: bool,
47 #[cfg(feature = "optional_block_gas_limit")]
53 pub disable_block_gas_limit: bool,
54 #[cfg(feature = "optional_eip3607")]
60 pub disable_eip3607: bool,
61 #[cfg(feature = "optional_no_base_fee")]
67 pub disable_base_fee: bool,
68}
69
70impl CfgEnv {
71 pub fn new() -> Self {
73 Self::default()
74 }
75}
76
77impl<SPEC> CfgEnv<SPEC> {
78 pub fn new_with_spec(spec: SPEC) -> Self {
80 Self {
81 chain_id: 1,
82 limit_contract_code_size: None,
83 spec,
84 disable_nonce_check: false,
85 blob_max_count: None, #[cfg(feature = "memory_limit")]
87 memory_limit: (1 << 32) - 1,
88 #[cfg(feature = "optional_balance_check")]
89 disable_balance_check: false,
90 #[cfg(feature = "optional_block_gas_limit")]
91 disable_block_gas_limit: false,
92 #[cfg(feature = "optional_eip3607")]
93 disable_eip3607: false,
94 #[cfg(feature = "optional_no_base_fee")]
95 disable_base_fee: false,
96 }
97 }
98
99 pub fn with_chain_id(mut self, chain_id: u64) -> Self {
101 self.chain_id = chain_id;
102 self
103 }
104
105 pub fn with_spec<OSPEC: Into<SpecId>>(self, spec: OSPEC) -> CfgEnv<OSPEC> {
107 CfgEnv {
108 chain_id: self.chain_id,
109 limit_contract_code_size: self.limit_contract_code_size,
110 spec,
111 disable_nonce_check: self.disable_nonce_check,
112 blob_max_count: self.blob_max_count,
113 #[cfg(feature = "memory_limit")]
114 memory_limit: self.memory_limit,
115 #[cfg(feature = "optional_balance_check")]
116 disable_balance_check: self.disable_balance_check,
117 #[cfg(feature = "optional_block_gas_limit")]
118 disable_block_gas_limit: self.disable_block_gas_limit,
119 #[cfg(feature = "optional_eip3607")]
120 disable_eip3607: self.disable_eip3607,
121 #[cfg(feature = "optional_no_base_fee")]
122 disable_base_fee: self.disable_base_fee,
123 }
124 }
125
126 pub fn with_blob_max_count(mut self, blob_max_count: u64) -> Self {
128 self.set_blob_max_count(blob_max_count);
129 self
130 }
131
132 pub fn set_blob_max_count(&mut self, blob_max_count: u64) {
134 self.blob_max_count = Some(blob_max_count);
135 }
136
137 pub fn clear_blob_max_count(&mut self) {
139 self.blob_max_count = None;
140 }
141}
142
143impl<SPEC: Into<SpecId> + Copy> Cfg for CfgEnv<SPEC> {
144 type Spec = SPEC;
145
146 fn chain_id(&self) -> u64 {
147 self.chain_id
148 }
149
150 fn spec(&self) -> Self::Spec {
151 self.spec
152 }
153
154 #[inline]
155 fn blob_max_count(&self) -> Option<u64> {
156 self.blob_max_count
157 }
158
159 fn max_code_size(&self) -> usize {
160 self.limit_contract_code_size.unwrap_or(MAX_CODE_SIZE)
161 }
162
163 fn is_eip3607_disabled(&self) -> bool {
164 cfg_if::cfg_if! {
165 if #[cfg(feature = "optional_eip3607")] {
166 self.disable_eip3607
167 } else {
168 false
169 }
170 }
171 }
172
173 fn is_balance_check_disabled(&self) -> bool {
174 cfg_if::cfg_if! {
175 if #[cfg(feature = "optional_balance_check")] {
176 self.disable_balance_check
177 } else {
178 false
179 }
180 }
181 }
182
183 fn is_block_gas_limit_disabled(&self) -> bool {
185 cfg_if::cfg_if! {
186 if #[cfg(feature = "optional_block_gas_limit")] {
187 self.disable_block_gas_limit
188 } else {
189 false
190 }
191 }
192 }
193
194 fn is_nonce_check_disabled(&self) -> bool {
195 self.disable_nonce_check
196 }
197
198 fn is_base_fee_check_disabled(&self) -> bool {
199 cfg_if::cfg_if! {
200 if #[cfg(feature = "optional_no_base_fee")] {
201 self.disable_base_fee
202 } else {
203 false
204 }
205 }
206 }
207}
208
209impl<SPEC: Default> Default for CfgEnv<SPEC> {
210 fn default() -> Self {
211 Self::new_with_spec(SPEC::default())
212 }
213}
214
215#[cfg(test)]
216mod test {
217 use super::*;
218
219 #[test]
220 fn blob_max_and_target_count() {
221 let cfg: CfgEnv = Default::default();
222 assert_eq!(cfg.blob_max_count(), None);
223 }
224}