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};
10use ethrex_common::seeded_hash::SeededHashMap;
11
12/// [EIP-1153]: https://eips.ethereum.org/EIPS/eip-1153#reference-implementation
13///
14/// Seeded: contracts choose these keys (see [`SeededHashMap`]).
15pub type TransientStorage = SeededHashMap<(Address, U256), U256>;
16
17#[derive(Debug, Default, Clone)]
18/// Environmental information that the execution agent must provide.
19pub struct Environment {
20    /// The sender address of the external transaction.
21    pub origin: Address,
22    /// Gas limit of the Transaction
23    pub gas_limit: u64,
24    pub config: EVMConfig,
25    pub block_number: u64,
26    /// Coinbase is the block's beneficiary - the address that receives the block rewards and fees.
27    pub coinbase: Address,
28    pub timestamp: u64,
29    pub prev_randao: Option<H256>,
30    pub difficulty: U256,
31    pub slot_number: U256,
32    pub chain_id: U256,
33    pub base_fee_per_gas: U256,
34    pub base_blob_fee_per_gas: U256,
35    pub gas_price: U256, // Effective gas price
36    pub block_excess_blob_gas: Option<u64>,
37    pub block_blob_gas_used: Option<u64>,
38    pub tx_blob_hashes: Vec<H256>,
39    pub tx_max_priority_fee_per_gas: Option<U256>,
40    pub tx_max_fee_per_gas: Option<U256>,
41    pub tx_max_fee_per_blob_gas: Option<U256>,
42    pub tx_nonce: u64,
43    pub block_gas_limit: u64,
44    pub is_privileged: bool,
45    pub fee_token: Option<Address>,
46    /// When true, skip balance deduction in `deduct_caller`. Used by the prewarmer
47    /// to avoid early reverts on insufficient balance so that warming touches more storage.
48    pub disable_balance_check: bool,
49    /// When true, skip the sender nonce-mismatch validation. Used by the simulation
50    /// RPCs (eth_call, eth_estimateGas, eth_createAccessList): call objects may omit
51    /// the nonce, and no client enforces it there. The account nonce still increments
52    /// during execution. `debug_traceCall` relies on it too (geth's
53    /// `ToMessage(_, skipNonceCheck=true)`): the synthetic call may run on top of a
54    /// mid-block state (`txIndex`) whose nonce differs from the value the caller
55    /// supplied, so enforcing the check would spuriously reject the trace.
56    /// `eth_simulateV1` sets it when `validation: false`, the mode in which the
57    /// method behaves like `eth_call`.
58    pub disable_nonce_check: bool,
59    /// When true, emit informational ETH-transfer logs from the
60    /// `TRACE_TRANSFER_ADDRESS` sentinel (`eth_simulateV1` traceTransfers).
61    /// Independent of the fork: an Amsterdam+ block carries these alongside its
62    /// consensus EIP-7708 logs rather than in place of them.
63    pub trace_eth_transfers: bool,
64    /// When true, skip the gas limits that gate a transaction's *admission* rather than
65    /// its execution: the block-level gas allowance and the EIP-7825 per-transaction cap.
66    /// Used by every simulation RPC (eth_call, eth_estimateGas, eth_createAccessList,
67    /// debug_traceCall), whose callers routinely pass a `gas` above either bound — tools
68    /// commonly pass the block gas limit — and still expect an answer, since nothing is
69    /// being submitted.
70    /// This exists so `block_gas_limit` can keep the block's real value: that field is
71    /// observable through the GASLIMIT opcode and feeds the EIP-8037 cost-per-state-byte
72    /// formula, so raising it to bypass the allowance corrupts both.
73    pub disable_gas_allowance_check: bool,
74    /// When true, skip the EIP-3607 validation that rejects a sender carrying code.
75    /// Used by every simulation RPC (eth_call, eth_estimateGas, eth_createAccessList,
76    /// debug_traceCall). EIP-3607 exists because a contract has no private key, so no
77    /// valid signature should exist for its address; a simulated call carries no
78    /// signature at all and its `from` is just an assertion by the caller, so the rule
79    /// has nothing to protect there. Enforcing it breaks the common practice of
80    /// simulating a call whose sender is a contract, such as a smart contract wallet
81    /// previewing its own transaction. Transaction admission keeps enforcing it: the
82    /// mempool rejects such a sender outright, and block execution never sets this flag.
83    pub disable_sender_eoa_check: bool,
84    /// When true, the EIP-8037 cap on the execution dimension (Amsterdam+) is lifted: the
85    /// whole gas limit is execution gas and no state-gas reservoir is funded from it, so a
86    /// simulation given more than the per-transaction execution cap can actually spend it.
87    /// Set by `eth_call` and `debug_traceCall` only, as geth does, since nothing they run is
88    /// ever submitted. `eth_estimateGas` and `eth_createAccessList` keep the cap, because
89    /// their answers must hold for a transaction that can be included. Block execution
90    /// never sets it.
91    pub disable_execution_gas_cap: bool,
92    /// When true, the tx is a pre-execution system contract call (EIP-2935, EIP-4788,
93    /// EIP-7002, EIP-7251 etc.). Skips the block-level gas-allowance check since system
94    /// calls are allowed to exceed `block_gas_limit` (their 30M cap is a separate rule).
95    pub is_system_call: bool,
96}
97
98/// This struct holds special configuration variables specific to the
99/// EVM. In most cases, at least at the time of writing (February
100/// 2025), you want to use the default blob_schedule values for the
101/// specified Fork. The "intended" way to do this is by using the `EVMConfig::canonical_values(fork: Fork)` function.
102///
103/// However, that function should NOT be used IF you want to use a
104/// custom `ForkBlobSchedule`, like it's described in [EIP-7840](https://eips.ethereum.org/EIPS/eip-7840)
105/// Values are determined by [EIP-7691](https://eips.ethereum.org/EIPS/eip-7691#specification)
106#[derive(Debug, Clone, Copy)]
107pub struct EVMConfig {
108    pub fork: Fork,
109    pub blob_schedule: ForkBlobSchedule,
110}
111
112impl EVMConfig {
113    pub fn new(fork: Fork, blob_schedule: ForkBlobSchedule) -> EVMConfig {
114        EVMConfig {
115            fork,
116            blob_schedule,
117        }
118    }
119
120    pub fn new_from_chain_config(chain_config: &ChainConfig, block_header: &BlockHeader) -> Self {
121        let fork = chain_config.fork(block_header.timestamp);
122
123        let blob_schedule = chain_config
124            .get_fork_blob_schedule(block_header.timestamp)
125            .unwrap_or_else(|| EVMConfig::canonical_values(fork));
126
127        EVMConfig::new(fork, blob_schedule)
128    }
129
130    /// This function is used for running the EF tests. If you don't
131    /// have acces to a EVMConfig (mainly in the form of a
132    /// genesis.json file) you can use this function to get the
133    /// "Default" ForkBlobSchedule for that specific Fork.
134    /// NOTE: This function could potentially be expanded to include
135    /// other types of "default"s.
136    pub fn canonical_values(fork: Fork) -> ForkBlobSchedule {
137        let max_blobs_per_block = Self::max_blobs_per_block(fork);
138        let target = Self::get_target_blob_gas_per_block_(fork);
139        let base_fee_update_fraction: u64 = Self::get_blob_base_fee_update_fraction_value(fork);
140
141        ForkBlobSchedule {
142            target,
143            max: max_blobs_per_block,
144            base_fee_update_fraction,
145        }
146    }
147
148    fn max_blobs_per_block(fork: Fork) -> u32 {
149        if fork >= Fork::Prague {
150            MAX_BLOB_COUNT_ELECTRA
151        } else {
152            MAX_BLOB_COUNT
153        }
154    }
155
156    fn get_blob_base_fee_update_fraction_value(fork: Fork) -> u64 {
157        if fork >= Fork::Prague {
158            BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE
159        } else {
160            BLOB_BASE_FEE_UPDATE_FRACTION
161        }
162    }
163
164    fn get_target_blob_gas_per_block_(fork: Fork) -> u32 {
165        if fork >= Fork::Prague {
166            TARGET_BLOB_GAS_PER_BLOCK_PECTRA
167        } else {
168            TARGET_BLOB_GAS_PER_BLOCK
169        }
170    }
171}
172
173impl Default for EVMConfig {
174    /// The default EVMConfig depends on the default Fork.
175    fn default() -> Self {
176        let fork = core::default::Default::default();
177        EVMConfig {
178            fork,
179            blob_schedule: Self::canonical_values(fork),
180        }
181    }
182}