Skip to main content

foundry_evm_core/fork/
block_env.rs

1//! The block environment recorded in fork caches and Anvil state dumps.
2//!
3//! Its serde layout matches the block environment previously written by REVM, so existing fork
4//! caches and state dumps stay valid. The fork cache compares it to decide whether a cache matches.
5
6use 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/// Excess blob gas of a block and the blob gas price derived from it.
14///
15/// Its serde layout is persisted and must stay stable.
16#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
17pub struct BlobExcessGasAndPrice {
18    /// The excess blob gas of the block.
19    pub excess_blob_gas: u64,
20    /// The blob gas price derived from `excess_blob_gas`.
21    pub blob_gasprice: u128,
22}
23
24impl BlobExcessGasAndPrice {
25    /// Creates a new instance, deriving the blob gas price from the excess blob gas.
26    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/// A persisted block environment.
39///
40/// Its serde layout is persisted and must stay stable.
41#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
42pub struct BlockEnv {
43    /// Block number.
44    pub number: U256,
45    /// Block producer.
46    pub beneficiary: Address,
47    /// Block timestamp in seconds.
48    pub timestamp: U256,
49    /// Block gas limit.
50    pub gas_limit: u64,
51    /// Block base fee.
52    pub basefee: u64,
53    /// Block difficulty, before the merge.
54    pub difficulty: U256,
55    /// Block `prevrandao`, after the merge.
56    pub prevrandao: Option<B256>,
57    /// Excess blob gas and blob gas price, after Cancun.
58    pub blob_excess_gas_and_price: Option<BlobExcessGasAndPrice>,
59    /// Slot number, after Amsterdam.
60    #[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    /// Sets the excess blob gas and derives the blob gas price from it.
85    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    /// Returns the excess blob gas, if blobs are enabled.
95    pub fn blob_excess_gas(&self) -> Option<u64> {
96        self.blob_excess_gas_and_price.map(|blob| blob.excess_blob_gas)
97    }
98
99    /// Returns the blob gas price, if blobs are enabled.
100    pub fn blob_gasprice(&self) -> Option<u128> {
101        self.blob_excess_gas_and_price.map(|blob| blob.blob_gasprice)
102    }
103}