revm_context/
cfg.rs

1//! This module contains [`CfgEnv`] and implements [`Cfg`] trait for it.
2pub use context_interface::Cfg;
3
4use primitives::{eip170::MAX_CODE_SIZE, hardfork::SpecId};
5
6/// EVM configuration
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[derive(Clone, Debug, Eq, PartialEq)]
9#[non_exhaustive]
10pub struct CfgEnv<SPEC = SpecId> {
11    /// Chain ID of the EVM
12    ///
13    /// `chain_id` will be compared to the transaction's Chain ID.
14    ///
15    /// Chain ID is introduced EIP-155.
16    pub chain_id: u64,
17    /// Specification for EVM represent the hardfork
18    pub spec: SPEC,
19    /// If some it will effects EIP-170: Contract code size limit.
20    ///
21    /// Useful to increase this because of tests.
22    ///
23    /// By default it is `0x6000` (~25kb).
24    pub limit_contract_code_size: Option<usize>,
25    /// Skips the nonce validation against the account's nonce
26    pub disable_nonce_check: bool,
27    /// Blob max count. EIP-7840 Add blob schedule to EL config files.
28    ///
29    /// If this config is not set, the check for max blobs will be skipped.
30    pub blob_max_count: Option<u64>,
31    /// A hard memory limit in bytes beyond which
32    /// [OutOfGasError::Memory][context_interface::result::OutOfGasError::Memory] cannot be resized.
33    ///
34    /// In cases where the gas limit may be extraordinarily high, it is recommended to set this to
35    /// a sane value to prevent memory allocation panics.
36    ///
37    /// Defaults to `2^32 - 1` bytes per EIP-1985.
38    #[cfg(feature = "memory_limit")]
39    pub memory_limit: u64,
40    /// Skip balance checks if `true`
41    ///
42    /// Adds transaction cost to balance to ensure execution doesn't fail.
43    ///
44    /// By default, it is set to `false`.
45    #[cfg(feature = "optional_balance_check")]
46    pub disable_balance_check: bool,
47    /// There are use cases where it's allowed to provide a gas limit that's higher than a block's gas limit.
48    ///
49    /// To that end, you can disable the block gas limit validation.
50    ///
51    /// By default, it is set to `false`.
52    #[cfg(feature = "optional_block_gas_limit")]
53    pub disable_block_gas_limit: bool,
54    /// EIP-3607 rejects transactions from senders with deployed code
55    ///
56    /// In development, it can be desirable to simulate calls from contracts, which this setting allows.
57    ///
58    /// By default, it is set to `false`.
59    #[cfg(feature = "optional_eip3607")]
60    pub disable_eip3607: bool,
61    /// Disables base fee checks for EIP-1559 transactions
62    ///
63    /// This is useful for testing method calls with zero gas price.
64    ///
65    /// By default, it is set to `false`.
66    #[cfg(feature = "optional_no_base_fee")]
67    pub disable_base_fee: bool,
68}
69
70impl CfgEnv {
71    /// Creates new `CfgEnv` with default values.
72    pub fn new() -> Self {
73        Self::default()
74    }
75}
76
77impl<SPEC> CfgEnv<SPEC> {
78    /// Create new `CfgEnv` with default values and specified spec.
79    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, //vec![(SpecId::CANCUN, 3, 6), (SpecId::PRAGUE, 6, 9)],
86            #[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    /// Consumes `self` and returns a new `CfgEnv` with the specified chain ID.
100    pub fn with_chain_id(mut self, chain_id: u64) -> Self {
101        self.chain_id = chain_id;
102        self
103    }
104
105    /// Consumes `self` and returns a new `CfgEnv` with the specified spec.
106    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    /// Sets the blob target
127    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    /// Sets the blob target
133    pub fn set_blob_max_count(&mut self, blob_max_count: u64) {
134        self.blob_max_count = Some(blob_max_count);
135    }
136
137    /// Clears the blob target and max count over hardforks.
138    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    /// Returns `true` if the block gas limit is disabled.
184    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}