revm-context 16.0.1

Revm context crates
Documentation
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! This module contains [`CfgEnv`] and implements [`Cfg`] trait for it.
pub use context_interface::Cfg;

use context_interface::cfg::GasParams;
use primitives::{eip170, eip3860, eip7825, eip7954, hardfork::SpecId};

/// EVM configuration
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub struct CfgEnv<SPEC = SpecId> {
    /// Specification for EVM represent the hardfork
    ///
    /// [`CfgEnv::new_with_spec`] is going to set both gas params and spec.
    ///
    /// As GasParams is spec dependent, it is recommended to use one of following function to set both of them.
    /// [`CfgEnv::set_spec_and_mainnet_gas_params`], [`CfgEnv::with_mainnet_gas_params`], [`CfgEnv::with_mainnet_gas_params`]
    pub spec: SPEC,

    /// Gas params for the EVM. Use [`CfgEnv::set_gas_params`] to set the gas params.
    /// If gas_params was not set it will be set to the default gas params for the spec.
    pub gas_params: GasParams,

    /// Chain ID of the EVM. Used in CHAINID opcode and transaction's chain ID check.
    ///
    /// Chain ID is introduced EIP-155.
    pub chain_id: u64,

    /// Whether to check the transaction's chain ID.
    ///
    /// If set to `false`, the transaction's chain ID check will be skipped.
    pub tx_chain_id_check: bool,

    /// Contract code size limit override.
    ///
    /// If None, the limit will be determined by the SpecId (EIP-170 or EIP-7954) at runtime.
    /// If Some, this specific limit will be used regardless of SpecId.
    ///
    /// Useful to increase this because of tests.
    pub limit_contract_code_size: Option<usize>,
    /// Contract initcode size limit override.
    ///
    /// If None, the limit will check if `limit_contract_code_size` is set.
    /// If it is set, it will double it for a limit.
    /// If it is not set, the limit will be determined by the SpecId (EIP-170 or EIP-7954) at runtime.
    ///
    /// Useful to increase this because of tests.
    pub limit_contract_initcode_size: Option<usize>,
    /// Skips the nonce validation against the account's nonce
    pub disable_nonce_check: bool,
    /// Blob max count. EIP-7840 Add blob schedule to EL config files.
    ///
    /// If this config is not set, the check for max blobs will be skipped.
    pub max_blobs_per_tx: Option<u64>,
    /// Blob base fee update fraction. EIP-4844 Blob base fee update fraction.
    ///
    /// If this config is not set, the blob base fee update fraction will be set to the default value.
    /// See also [CfgEnv::blob_base_fee_update_fraction].
    ///
    /// Default values for Cancun is [`primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN`]
    /// and for Prague is [`primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE`].
    pub blob_base_fee_update_fraction: Option<u64>,
    /// Configures the gas limit cap for the transaction.
    ///
    /// If `None`, default value defined by spec will be used.
    ///
    /// Introduced in Osaka in [EIP-7825: Transaction Gas Limit Cap](https://eips.ethereum.org/EIPS/eip-7825)
    /// with initials cap of 30M.
    pub tx_gas_limit_cap: Option<u64>,
    /// A hard memory limit in bytes beyond which
    /// [OutOfGasError::Memory][context_interface::result::OutOfGasError::Memory] cannot be resized.
    ///
    /// In cases where the gas limit may be extraordinarily high, it is recommended to set this to
    /// a sane value to prevent memory allocation panics.
    ///
    /// Defaults to `2^32 - 1` bytes per EIP-1985.
    #[cfg(feature = "memory_limit")]
    pub memory_limit: u64,
    /// Skip balance checks if `true`
    ///
    /// Adds transaction cost to balance to ensure execution doesn't fail.
    ///
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_balance_check")]
    pub disable_balance_check: bool,
    /// There are use cases where it's allowed to provide a gas limit that's higher than a block's gas limit.
    ///
    /// To that end, you can disable the block gas limit validation.
    ///
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_block_gas_limit")]
    pub disable_block_gas_limit: bool,
    /// EIP-3541 rejects the creation of contracts that starts with 0xEF
    ///
    /// This is useful for chains that do not implement EIP-3541.
    ///
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_eip3541")]
    pub disable_eip3541: bool,
    /// EIP-3607 rejects transactions from senders with deployed code
    ///
    /// In development, it can be desirable to simulate calls from contracts, which this setting allows.
    ///
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_eip3607")]
    pub disable_eip3607: bool,
    /// EIP-7623 increases calldata cost.
    ///
    /// This EIP can be considered irrelevant in the context of an EVM-compatible L2 rollup,
    /// if it does not make use of blobs.
    ///
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_eip7623")]
    pub disable_eip7623: bool,
    /// Disables base fee checks for EIP-1559 transactions
    ///
    /// This is useful for testing method calls with zero gas price.
    ///
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_no_base_fee")]
    pub disable_base_fee: bool,
    /// Disables "max fee must be less than or equal to max priority fee" check for EIP-1559 transactions.
    /// This is useful because some chains (e.g. Arbitrum) do not enforce this check.
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_priority_fee_check")]
    pub disable_priority_fee_check: bool,
    /// Disables fee charging for transactions.
    /// This is useful when executing `eth_call` for example, on OP-chains where setting the base fee
    /// to 0 isn't sufficient.
    /// By default, it is set to `false`.
    #[cfg(feature = "optional_fee_charge")]
    pub disable_fee_charge: bool,
    /// Enables EIP-8037 (Amsterdam) state creation gas cost increase.
    ///
    /// EIP-8037 introduces dual gas limits: regular gas for execution and state gas
    /// for storage creation. State gas is tracked via a reservoir model.
    /// It specifies concrete gas values based on `cost_per_state_byte` and adds
    /// a hash cost for deployed bytecode.
    ///
    /// By default, it is set to `false`.
    pub enable_amsterdam_eip8037: bool,
    /// Disables EIP-7708 (ETH transfers emit logs).
    ///
    /// By default, it is set to `false`.
    pub amsterdam_eip7708_disabled: bool,
    /// Disables EIP-7708 delayed burn logging.
    ///
    /// When enabled, revm tracks all self-destructed addresses and emits logs for
    /// accounts that still have remaining balance at the end of the transaction.
    /// This can be disabled for performance reasons as it requires storing and
    /// iterating over all self-destructed accounts. When disabled, the logging
    /// can be done outside of revm when applying accounts to database state.
    ///
    /// By default, it is set to `false`.
    pub amsterdam_eip7708_delayed_burn_disabled: bool,
}

impl CfgEnv {
    /// Creates new `CfgEnv` with default values.
    pub fn new() -> Self {
        Self::default()
    }
}

impl<SPEC> CfgEnv<SPEC> {
    /// Returns the spec for the `CfgEnv`.
    #[inline]
    pub fn spec(&self) -> &SPEC {
        &self.spec
    }

    /// Consumes `self` and returns a new `CfgEnv` with the specified chain ID.
    pub fn with_chain_id(mut self, chain_id: u64) -> Self {
        self.chain_id = chain_id;
        self
    }

    /// Sets the gas params for the `CfgEnv`.
    #[inline]
    pub fn with_gas_params(mut self, gas_params: GasParams) -> Self {
        self.set_gas_params(gas_params);
        self
    }

    /// Sets the spec for the `CfgEnv`.
    #[inline]
    #[deprecated(note = "Use [`CfgEnv::set_spec_and_mainnet_gas_params`] instead")]
    pub fn set_spec(&mut self, spec: SPEC) {
        self.spec = spec;
    }

    /// Sets the gas params for the `CfgEnv`.
    #[inline]
    pub fn set_gas_params(&mut self, gas_params: GasParams) {
        self.gas_params = gas_params;
    }

    /// Enables the transaction's chain ID check.
    pub fn enable_tx_chain_id_check(mut self) -> Self {
        self.tx_chain_id_check = true;
        self
    }

    /// Disables the transaction's chain ID check.
    pub fn disable_tx_chain_id_check(mut self) -> Self {
        self.tx_chain_id_check = false;
        self
    }

    /// Sets the spec for the `CfgEnv`.
    #[inline]
    #[deprecated(note = "Use [`CfgEnv::with_spec_and_mainnet_gas_params`] instead")]
    pub fn with_spec(mut self, spec: SPEC) -> Self {
        self.spec = spec;
        self
    }

    /// Sets the spec for the `CfgEnv` and the gas params to the mainnet gas params.
    ///
    /// Automatically enables EIP-8037 for AMSTERDAM and later.
    pub fn with_spec_and_mainnet_gas_params<OSPEC: Into<SpecId> + Clone>(
        self,
        spec: OSPEC,
    ) -> CfgEnv<OSPEC> {
        let is_amsterdam = spec.clone().into().is_enabled_in(SpecId::AMSTERDAM);
        let enable_amsterdam_eip8037 = self.enable_amsterdam_eip8037 || is_amsterdam;
        let mut cfg = self.with_spec_and_gas_params(spec.clone(), GasParams::new_spec(spec.into()));
        cfg.enable_amsterdam_eip8037 = enable_amsterdam_eip8037;
        cfg
    }

    /// Consumes `self` and returns a new `CfgEnv` with the specified spec.
    ///
    /// Resets the gas params override function as it is generic over SPEC.
    pub fn with_spec_and_gas_params<OSPEC: Into<SpecId> + Clone>(
        self,
        spec: OSPEC,
        gas_params: GasParams,
    ) -> CfgEnv<OSPEC> {
        CfgEnv {
            chain_id: self.chain_id,
            tx_chain_id_check: self.tx_chain_id_check,
            limit_contract_code_size: self.limit_contract_code_size,
            limit_contract_initcode_size: self.limit_contract_initcode_size,
            spec,
            disable_nonce_check: self.disable_nonce_check,
            tx_gas_limit_cap: self.tx_gas_limit_cap,
            max_blobs_per_tx: self.max_blobs_per_tx,
            blob_base_fee_update_fraction: self.blob_base_fee_update_fraction,
            gas_params,
            #[cfg(feature = "memory_limit")]
            memory_limit: self.memory_limit,
            #[cfg(feature = "optional_balance_check")]
            disable_balance_check: self.disable_balance_check,
            #[cfg(feature = "optional_block_gas_limit")]
            disable_block_gas_limit: self.disable_block_gas_limit,
            #[cfg(feature = "optional_eip3541")]
            disable_eip3541: self.disable_eip3541,
            #[cfg(feature = "optional_eip3607")]
            disable_eip3607: self.disable_eip3607,
            #[cfg(feature = "optional_eip7623")]
            disable_eip7623: self.disable_eip7623,
            #[cfg(feature = "optional_no_base_fee")]
            disable_base_fee: self.disable_base_fee,
            #[cfg(feature = "optional_priority_fee_check")]
            disable_priority_fee_check: self.disable_priority_fee_check,
            #[cfg(feature = "optional_fee_charge")]
            disable_fee_charge: self.disable_fee_charge,
            enable_amsterdam_eip8037: self.enable_amsterdam_eip8037,
            amsterdam_eip7708_disabled: self.amsterdam_eip7708_disabled,
            amsterdam_eip7708_delayed_burn_disabled: self.amsterdam_eip7708_delayed_burn_disabled,
        }
    }

    /// Sets the blob target
    pub fn with_max_blobs_per_tx(mut self, max_blobs_per_tx: u64) -> Self {
        self.set_max_blobs_per_tx(max_blobs_per_tx);
        self
    }

    /// Sets the blob target
    pub fn set_max_blobs_per_tx(&mut self, max_blobs_per_tx: u64) {
        self.max_blobs_per_tx = Some(max_blobs_per_tx);
    }

    /// Clears the blob target and max count over hardforks.
    pub fn clear_max_blobs_per_tx(&mut self) {
        self.max_blobs_per_tx = None;
    }

    /// Sets the disable priority fee check flag.
    #[cfg(feature = "optional_priority_fee_check")]
    pub fn with_disable_priority_fee_check(mut self, disable: bool) -> Self {
        self.disable_priority_fee_check = disable;
        self
    }

    /// Sets the disable fee charge flag.
    #[cfg(feature = "optional_fee_charge")]
    pub fn with_disable_fee_charge(mut self, disable: bool) -> Self {
        self.disable_fee_charge = disable;
        self
    }

    /// Sets the disable eip7623 flag.
    #[cfg(feature = "optional_eip7623")]
    pub fn with_disable_eip7623(mut self, disable: bool) -> Self {
        self.disable_eip7623 = disable;
        self
    }

    /// Sets the enable EIP-8037 (Amsterdam) state creation gas cost flag.
    pub fn with_enable_amsterdam_eip8037(mut self, enable: bool) -> Self {
        self.enable_amsterdam_eip8037 = enable;
        self
    }
}

impl<SPEC: Into<SpecId> + Clone> CfgEnv<SPEC> {
    /// Create new `CfgEnv` with default values and specified spec.
    pub fn new_with_spec_and_gas_params(spec: SPEC, gas_params: GasParams) -> Self {
        let is_amsterdam = spec.clone().into().is_enabled_in(SpecId::AMSTERDAM);
        Self {
            chain_id: 1,
            tx_chain_id_check: true,
            limit_contract_code_size: None,
            limit_contract_initcode_size: None,
            spec,
            disable_nonce_check: false,
            max_blobs_per_tx: None,
            tx_gas_limit_cap: None,
            blob_base_fee_update_fraction: None,
            gas_params,
            #[cfg(feature = "memory_limit")]
            memory_limit: (1 << 32) - 1,
            #[cfg(feature = "optional_balance_check")]
            disable_balance_check: false,
            #[cfg(feature = "optional_block_gas_limit")]
            disable_block_gas_limit: false,
            #[cfg(feature = "optional_eip3541")]
            disable_eip3541: false,
            #[cfg(feature = "optional_eip3607")]
            disable_eip3607: false,
            #[cfg(feature = "optional_eip7623")]
            disable_eip7623: false,
            #[cfg(feature = "optional_no_base_fee")]
            disable_base_fee: false,
            #[cfg(feature = "optional_priority_fee_check")]
            disable_priority_fee_check: false,
            #[cfg(feature = "optional_fee_charge")]
            disable_fee_charge: false,
            enable_amsterdam_eip8037: is_amsterdam,
            amsterdam_eip7708_disabled: false,
            amsterdam_eip7708_delayed_burn_disabled: false,
        }
    }

    /// Returns the blob base fee update fraction from [CfgEnv::blob_base_fee_update_fraction].
    ///
    /// If this field is not set, return the default value for the spec.
    ///
    /// Default values for Cancun is [`primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN`]
    /// and for Prague is [`primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE`].
    pub fn blob_base_fee_update_fraction(&mut self) -> u64 {
        self.blob_base_fee_update_fraction.unwrap_or_else(|| {
            let spec: SpecId = self.spec.clone().into();
            if spec.is_enabled_in(SpecId::PRAGUE) {
                primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE
            } else {
                primitives::eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_CANCUN
            }
        })
    }

    /// Create new `CfgEnv` with default values and specified spec.
    /// It will create a new gas params based on mainnet spec.
    ///
    /// Internally it will call [`CfgEnv::new_with_spec_and_gas_params`] with the mainnet gas params.
    pub fn new_with_spec(spec: SPEC) -> Self {
        Self::new_with_spec_and_gas_params(spec.clone(), GasParams::new_spec(spec.into()))
    }

    /// Sets the gas params for the `CfgEnv` to the mainnet gas params.
    ///
    /// If spec gets changed, calling this function would use this spec to set the mainnetF gas params.
    pub fn with_mainnet_gas_params(mut self) -> Self {
        self.set_gas_params(GasParams::new_spec(self.spec.clone().into()));
        self
    }

    /// Sets the spec for the `CfgEnv` and the gas params to the mainnet gas params.
    ///
    /// Automatically enables EIP-8037 for AMSTERDAM and later.
    #[inline]
    pub fn set_spec_and_mainnet_gas_params(&mut self, spec: SPEC) {
        self.spec = spec.clone();
        self.set_gas_params(GasParams::new_spec(spec.clone().into()));
        // EIP-8037: Enable EIP-8037 for AMSTERDAM and later
        if spec.into().is_enabled_in(SpecId::AMSTERDAM) {
            self.enable_amsterdam_eip8037 = true;
        }
    }
}

impl<SPEC: Into<SpecId> + Clone> Cfg for CfgEnv<SPEC> {
    type Spec = SPEC;

    #[inline]
    fn chain_id(&self) -> u64 {
        self.chain_id
    }

    #[inline]
    fn spec(&self) -> Self::Spec {
        self.spec.clone()
    }

    #[inline]
    fn tx_chain_id_check(&self) -> bool {
        self.tx_chain_id_check
    }

    #[inline]
    fn tx_gas_limit_cap(&self) -> u64 {
        self.tx_gas_limit_cap
            .unwrap_or(if self.spec.clone().into().is_enabled_in(SpecId::OSAKA) {
                eip7825::TX_GAS_LIMIT_CAP
            } else {
                u64::MAX
            })
    }

    #[inline]
    fn max_blobs_per_tx(&self) -> Option<u64> {
        self.max_blobs_per_tx
    }

    fn max_code_size(&self) -> usize {
        self.limit_contract_code_size.unwrap_or(
            if self.spec.clone().into().is_enabled_in(SpecId::AMSTERDAM) {
                eip7954::MAX_CODE_SIZE
            } else {
                eip170::MAX_CODE_SIZE
            },
        )
    }

    fn max_initcode_size(&self) -> usize {
        self.limit_contract_initcode_size
            .or_else(|| {
                self.limit_contract_code_size
                    .map(|size| size.saturating_mul(2))
            })
            .unwrap_or(
                if self.spec.clone().into().is_enabled_in(SpecId::AMSTERDAM) {
                    eip7954::MAX_INITCODE_SIZE
                } else {
                    eip3860::MAX_INITCODE_SIZE
                },
            )
    }

    fn is_eip3541_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_eip3541")] {
                self.disable_eip3541
            } else {
                false
            }
        }
    }

    fn is_eip3607_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_eip3607")] {
                self.disable_eip3607
            } else {
                false
            }
        }
    }

    fn is_eip7623_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_eip7623")] {
                self.disable_eip7623
            } else {
                false
            }
        }
    }

    fn is_balance_check_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_balance_check")] {
                self.disable_balance_check
            } else {
                false
            }
        }
    }

    /// Returns `true` if the block gas limit is disabled.
    fn is_block_gas_limit_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_block_gas_limit")] {
                self.disable_block_gas_limit
            } else {
                false
            }
        }
    }

    fn is_nonce_check_disabled(&self) -> bool {
        self.disable_nonce_check
    }

    fn is_base_fee_check_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_no_base_fee")] {
                self.disable_base_fee
            } else {
                false
            }
        }
    }

    fn is_priority_fee_check_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_priority_fee_check")] {
                self.disable_priority_fee_check
            } else {
                false
            }
        }
    }

    fn is_fee_charge_disabled(&self) -> bool {
        cfg_if::cfg_if! {
            if #[cfg(feature = "optional_fee_charge")] {
                self.disable_fee_charge
            } else {
                false
            }
        }
    }

    fn is_eip7708_disabled(&self) -> bool {
        self.amsterdam_eip7708_disabled
    }

    fn is_eip7708_delayed_burn_disabled(&self) -> bool {
        self.amsterdam_eip7708_delayed_burn_disabled
    }

    fn memory_limit(&self) -> u64 {
        cfg_if::cfg_if! {
            if #[cfg(feature = "memory_limit")] {
                self.memory_limit
            } else {
                u64::MAX
            }
        }
    }

    #[inline]
    fn gas_params(&self) -> &GasParams {
        &self.gas_params
    }

    fn is_amsterdam_eip8037_enabled(&self) -> bool {
        self.enable_amsterdam_eip8037
    }
}

impl<SPEC: Default + Into<SpecId> + Clone> Default for CfgEnv<SPEC> {
    fn default() -> Self {
        Self::new_with_spec_and_gas_params(
            SPEC::default(),
            GasParams::new_spec(SPEC::default().into()),
        )
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn blob_max_and_target_count() {
        let cfg: CfgEnv = Default::default();
        assert_eq!(cfg.max_blobs_per_tx(), None);
    }
}