ethrex_levm/
environment.rs1use 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;
12pub type TransientStorage = FxHashMap<(Address, U256), U256>;
14
15#[derive(Debug, Default, Clone)]
16pub struct Environment {
18 pub origin: Address,
20 pub gas_limit: u64,
22 pub config: EVMConfig,
23 pub block_number: u64,
24 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, 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 pub disable_balance_check: bool,
47 pub disable_nonce_check: bool,
52 pub is_system_call: bool,
56}
57
58#[derive(Debug, Clone, Copy)]
67pub struct EVMConfig {
68 pub fork: Fork,
69 pub blob_schedule: ForkBlobSchedule,
70}
71
72impl EVMConfig {
73 pub fn new(fork: Fork, blob_schedule: ForkBlobSchedule) -> EVMConfig {
74 EVMConfig {
75 fork,
76 blob_schedule,
77 }
78 }
79
80 pub fn new_from_chain_config(chain_config: &ChainConfig, block_header: &BlockHeader) -> Self {
81 let fork = chain_config.fork(block_header.timestamp);
82
83 let blob_schedule = chain_config
84 .get_fork_blob_schedule(block_header.timestamp)
85 .unwrap_or_else(|| EVMConfig::canonical_values(fork));
86
87 EVMConfig::new(fork, blob_schedule)
88 }
89
90 pub fn canonical_values(fork: Fork) -> ForkBlobSchedule {
97 let max_blobs_per_block = Self::max_blobs_per_block(fork);
98 let target = Self::get_target_blob_gas_per_block_(fork);
99 let base_fee_update_fraction: u64 = Self::get_blob_base_fee_update_fraction_value(fork);
100
101 ForkBlobSchedule {
102 target,
103 max: max_blobs_per_block,
104 base_fee_update_fraction,
105 }
106 }
107
108 fn max_blobs_per_block(fork: Fork) -> u32 {
109 if fork >= Fork::Prague {
110 MAX_BLOB_COUNT_ELECTRA
111 } else {
112 MAX_BLOB_COUNT
113 }
114 }
115
116 fn get_blob_base_fee_update_fraction_value(fork: Fork) -> u64 {
117 if fork >= Fork::Prague {
118 BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE
119 } else {
120 BLOB_BASE_FEE_UPDATE_FRACTION
121 }
122 }
123
124 fn get_target_blob_gas_per_block_(fork: Fork) -> u32 {
125 if fork >= Fork::Prague {
126 TARGET_BLOB_GAS_PER_BLOCK_PECTRA
127 } else {
128 TARGET_BLOB_GAS_PER_BLOCK
129 }
130 }
131}
132
133impl Default for EVMConfig {
134 fn default() -> Self {
136 let fork = core::default::Default::default();
137 EVMConfig {
138 fork,
139 blob_schedule: Self::canonical_values(fork),
140 }
141 }
142}