foundry_evm_core/fork/
block_env.rs1use alloy_eips::{
7 eip4844::{BLOB_TX_MIN_BLOB_GASPRICE, fake_exponential},
8 eip7691::BLOB_GASPRICE_UPDATE_FRACTION_PECTRA,
9};
10use alloy_primitives::{Address, B256, U256};
11use serde::{Deserialize, Serialize};
12
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
17pub struct BlobExcessGasAndPrice {
18 pub excess_blob_gas: u64,
20 pub blob_gasprice: u128,
22}
23
24impl BlobExcessGasAndPrice {
25 pub fn new(excess_blob_gas: u64, blob_base_fee_update_fraction: u64) -> Self {
27 Self {
28 excess_blob_gas,
29 blob_gasprice: fake_exponential(
30 BLOB_TX_MIN_BLOB_GASPRICE,
31 excess_blob_gas as u128,
32 blob_base_fee_update_fraction as u128,
33 ),
34 }
35 }
36}
37
38#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
42pub struct BlockEnv {
43 pub number: U256,
45 pub beneficiary: Address,
47 pub timestamp: U256,
49 pub gas_limit: u64,
51 pub basefee: u64,
53 pub difficulty: U256,
55 pub prevrandao: Option<B256>,
57 pub blob_excess_gas_and_price: Option<BlobExcessGasAndPrice>,
59 #[serde(default)]
61 pub slot_num: u64,
62}
63
64impl Default for BlockEnv {
65 fn default() -> Self {
66 Self {
67 number: U256::ZERO,
68 beneficiary: Address::ZERO,
69 timestamp: U256::ONE,
70 gas_limit: u64::MAX,
71 basefee: 0,
72 difficulty: U256::ZERO,
73 prevrandao: Some(B256::ZERO),
74 blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
75 0,
76 BLOB_GASPRICE_UPDATE_FRACTION_PECTRA as u64,
77 )),
78 slot_num: 0,
79 }
80 }
81}
82
83impl BlockEnv {
84 pub fn set_blob_excess_gas_and_price(
86 &mut self,
87 excess_blob_gas: u64,
88 base_fee_update_fraction: u64,
89 ) {
90 self.blob_excess_gas_and_price =
91 Some(BlobExcessGasAndPrice::new(excess_blob_gas, base_fee_update_fraction));
92 }
93
94 pub fn blob_excess_gas(&self) -> Option<u64> {
96 self.blob_excess_gas_and_price.map(|blob| blob.excess_blob_gas)
97 }
98
99 pub fn blob_gasprice(&self) -> Option<u128> {
101 self.blob_excess_gas_and_price.map(|blob| blob.blob_gasprice)
102 }
103}