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, skip the sender nonce-mismatch validation. Used by the simulation
48    /// RPCs (eth_call, eth_estimateGas, eth_createAccessList): call objects may omit
49    /// the nonce, and no client enforces it there. The account nonce still increments
50    /// during execution. `debug_traceCall` relies on it too (geth's
51    /// `ToMessage(_, skipNonceCheck=true)`): the synthetic call may run on top of a
52    /// mid-block state (`txIndex`) whose nonce differs from the value the caller
53    /// supplied, so enforcing the check would spuriously reject the trace.
54    pub disable_nonce_check: bool,
55    /// When true, skip the gas limits that gate a transaction's *admission* rather than
56    /// its execution: the block-level gas allowance and the EIP-7825 per-transaction cap.
57    /// Used by every simulation RPC (eth_call, eth_estimateGas, eth_createAccessList,
58    /// debug_traceCall), whose callers routinely pass a `gas` above either bound — tools
59    /// commonly pass the block gas limit — and still expect an answer, since nothing is
60    /// being submitted.
61    /// This exists so `block_gas_limit` can keep the block's real value: that field is
62    /// observable through the GASLIMIT opcode and feeds the EIP-8037 cost-per-state-byte
63    /// formula, so raising it to bypass the allowance corrupts both.
64    pub disable_gas_allowance_check: bool,
65    /// When true, skip the EIP-3607 validation that rejects a sender carrying code.
66    /// Used by every simulation RPC (eth_call, eth_estimateGas, eth_createAccessList,
67    /// debug_traceCall). EIP-3607 exists because a contract has no private key, so no
68    /// valid signature should exist for its address; a simulated call carries no
69    /// signature at all and its `from` is just an assertion by the caller, so the rule
70    /// has nothing to protect there. Enforcing it breaks the common practice of
71    /// simulating a call whose sender is a contract, such as a smart contract wallet
72    /// previewing its own transaction. Transaction admission keeps enforcing it: the
73    /// mempool rejects such a sender outright, and block execution never sets this flag.
74    pub disable_sender_eoa_check: bool,
75    /// When true, the tx is a pre-execution system contract call (EIP-2935, EIP-4788,
76    /// EIP-7002, EIP-7251 etc.). Skips the block-level gas-allowance check since system
77    /// calls are allowed to exceed `block_gas_limit` (their 30M cap is a separate rule).
78    pub is_system_call: bool,
79}
80
81/// This struct holds special configuration variables specific to the
82/// EVM. In most cases, at least at the time of writing (February
83/// 2025), you want to use the default blob_schedule values for the
84/// specified Fork. The "intended" way to do this is by using the `EVMConfig::canonical_values(fork: Fork)` function.
85///
86/// However, that function should NOT be used IF you want to use a
87/// custom `ForkBlobSchedule`, like it's described in [EIP-7840](https://eips.ethereum.org/EIPS/eip-7840)
88/// Values are determined by [EIP-7691](https://eips.ethereum.org/EIPS/eip-7691#specification)
89#[derive(Debug, Clone, Copy)]
90pub struct EVMConfig {
91    pub fork: Fork,
92    pub blob_schedule: ForkBlobSchedule,
93}
94
95impl EVMConfig {
96    pub fn new(fork: Fork, blob_schedule: ForkBlobSchedule) -> EVMConfig {
97        EVMConfig {
98            fork,
99            blob_schedule,
100        }
101    }
102
103    pub fn new_from_chain_config(chain_config: &ChainConfig, block_header: &BlockHeader) -> Self {
104        let fork = chain_config.fork(block_header.timestamp);
105
106        let blob_schedule = chain_config
107            .get_fork_blob_schedule(block_header.timestamp)
108            .unwrap_or_else(|| EVMConfig::canonical_values(fork));
109
110        EVMConfig::new(fork, blob_schedule)
111    }
112
113    /// This function is used for running the EF tests. If you don't
114    /// have acces to a EVMConfig (mainly in the form of a
115    /// genesis.json file) you can use this function to get the
116    /// "Default" ForkBlobSchedule for that specific Fork.
117    /// NOTE: This function could potentially be expanded to include
118    /// other types of "default"s.
119    pub fn canonical_values(fork: Fork) -> ForkBlobSchedule {
120        let max_blobs_per_block = Self::max_blobs_per_block(fork);
121        let target = Self::get_target_blob_gas_per_block_(fork);
122        let base_fee_update_fraction: u64 = Self::get_blob_base_fee_update_fraction_value(fork);
123
124        ForkBlobSchedule {
125            target,
126            max: max_blobs_per_block,
127            base_fee_update_fraction,
128        }
129    }
130
131    fn max_blobs_per_block(fork: Fork) -> u32 {
132        if fork >= Fork::Prague {
133            MAX_BLOB_COUNT_ELECTRA
134        } else {
135            MAX_BLOB_COUNT
136        }
137    }
138
139    fn get_blob_base_fee_update_fraction_value(fork: Fork) -> u64 {
140        if fork >= Fork::Prague {
141            BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE
142        } else {
143            BLOB_BASE_FEE_UPDATE_FRACTION
144        }
145    }
146
147    fn get_target_blob_gas_per_block_(fork: Fork) -> u32 {
148        if fork >= Fork::Prague {
149            TARGET_BLOB_GAS_PER_BLOCK_PECTRA
150        } else {
151            TARGET_BLOB_GAS_PER_BLOCK
152        }
153    }
154}
155
156impl Default for EVMConfig {
157    /// The default EVMConfig depends on the default Fork.
158    fn default() -> Self {
159        let fork = core::default::Default::default();
160        EVMConfig {
161            fork,
162            blob_schedule: Self::canonical_values(fork),
163        }
164    }
165}