Skip to main content

ethrex_levm/
environment.rs

1use ethrex_common::{
2    Address, H256, U256,
3    types::{BlockHeader, ChainConfig, Fork, ForkBlobSchedule},
4};
5
6use crate::constants::{
7    BLOB_BASE_FEE_UPDATE_FRACTION, BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE, MAX_BLOB_COUNT,
8    MAX_BLOB_COUNT_ELECTRA, TARGET_BLOB_GAS_PER_BLOCK, TARGET_BLOB_GAS_PER_BLOCK_PECTRA,
9};
10
11use rustc_hash::FxHashMap;
12/// [EIP-1153]: https://eips.ethereum.org/EIPS/eip-1153#reference-implementation
13pub type TransientStorage = FxHashMap<(Address, U256), U256>;
14
15#[derive(Debug, Default, Clone)]
16/// Environmental information that the execution agent must provide.
17pub struct Environment {
18    /// The sender address of the external transaction.
19    pub origin: Address,
20    /// Gas limit of the Transaction
21    pub gas_limit: u64,
22    pub config: EVMConfig,
23    pub block_number: u64,
24    /// Coinbase is the block's beneficiary - the address that receives the block rewards and fees.
25    pub coinbase: Address,
26    pub timestamp: u64,
27    pub prev_randao: Option<H256>,
28    pub difficulty: U256,
29    pub slot_number: U256,
30    pub chain_id: U256,
31    pub base_fee_per_gas: U256,
32    pub base_blob_fee_per_gas: U256,
33    pub gas_price: U256, // Effective gas price
34    pub block_excess_blob_gas: Option<u64>,
35    pub block_blob_gas_used: Option<u64>,
36    pub tx_blob_hashes: Vec<H256>,
37    pub tx_max_priority_fee_per_gas: Option<U256>,
38    pub tx_max_fee_per_gas: Option<U256>,
39    pub tx_max_fee_per_blob_gas: Option<U256>,
40    pub tx_nonce: u64,
41    pub block_gas_limit: u64,
42    pub is_privileged: bool,
43    pub fee_token: Option<Address>,
44    /// When true, skip balance deduction in `deduct_caller`. Used by the prewarmer
45    /// to avoid early reverts on insufficient balance so that warming touches more storage.
46    pub disable_balance_check: bool,
47    /// When true, the tx is a pre-execution system contract call (EIP-2935, EIP-4788,
48    /// EIP-7002, EIP-7251 etc.). Skips the block-level gas-allowance check since system
49    /// calls are allowed to exceed `block_gas_limit` (their 30M cap is a separate rule).
50    pub is_system_call: bool,
51}
52
53/// This struct holds special configuration variables specific to the
54/// EVM. In most cases, at least at the time of writing (February
55/// 2025), you want to use the default blob_schedule values for the
56/// specified Fork. The "intended" way to do this is by using the `EVMConfig::canonical_values(fork: Fork)` function.
57///
58/// However, that function should NOT be used IF you want to use a
59/// custom `ForkBlobSchedule`, like it's described in [EIP-7840](https://eips.ethereum.org/EIPS/eip-7840)
60/// Values are determined by [EIP-7691](https://eips.ethereum.org/EIPS/eip-7691#specification)
61#[derive(Debug, Clone, Copy)]
62pub struct EVMConfig {
63    pub fork: Fork,
64    pub blob_schedule: ForkBlobSchedule,
65}
66
67impl EVMConfig {
68    pub fn new(fork: Fork, blob_schedule: ForkBlobSchedule) -> EVMConfig {
69        EVMConfig {
70            fork,
71            blob_schedule,
72        }
73    }
74
75    pub fn new_from_chain_config(chain_config: &ChainConfig, block_header: &BlockHeader) -> Self {
76        let fork = chain_config.fork(block_header.timestamp);
77
78        let blob_schedule = chain_config
79            .get_fork_blob_schedule(block_header.timestamp)
80            .unwrap_or_else(|| EVMConfig::canonical_values(fork));
81
82        EVMConfig::new(fork, blob_schedule)
83    }
84
85    /// This function is used for running the EF tests. If you don't
86    /// have acces to a EVMConfig (mainly in the form of a
87    /// genesis.json file) you can use this function to get the
88    /// "Default" ForkBlobSchedule for that specific Fork.
89    /// NOTE: This function could potentially be expanded to include
90    /// other types of "default"s.
91    pub fn canonical_values(fork: Fork) -> ForkBlobSchedule {
92        let max_blobs_per_block = Self::max_blobs_per_block(fork);
93        let target = Self::get_target_blob_gas_per_block_(fork);
94        let base_fee_update_fraction: u64 = Self::get_blob_base_fee_update_fraction_value(fork);
95
96        ForkBlobSchedule {
97            target,
98            max: max_blobs_per_block,
99            base_fee_update_fraction,
100        }
101    }
102
103    fn max_blobs_per_block(fork: Fork) -> u32 {
104        if fork >= Fork::Prague {
105            MAX_BLOB_COUNT_ELECTRA
106        } else {
107            MAX_BLOB_COUNT
108        }
109    }
110
111    fn get_blob_base_fee_update_fraction_value(fork: Fork) -> u64 {
112        if fork >= Fork::Prague {
113            BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE
114        } else {
115            BLOB_BASE_FEE_UPDATE_FRACTION
116        }
117    }
118
119    fn get_target_blob_gas_per_block_(fork: Fork) -> u32 {
120        if fork >= Fork::Prague {
121            TARGET_BLOB_GAS_PER_BLOCK_PECTRA
122        } else {
123            TARGET_BLOB_GAS_PER_BLOCK
124        }
125    }
126}
127
128impl Default for EVMConfig {
129    /// The default EVMConfig depends on the default Fork.
130    fn default() -> Self {
131        let fork = core::default::Default::default();
132        EVMConfig {
133            fork,
134            blob_schedule: Self::canonical_values(fork),
135        }
136    }
137}