revm_context/
block.rs

1//! This module contains [`BlockEnv`] and it implements [`Block`] trait.
2use context_interface::block::{BlobExcessGasAndPrice, Block};
3use primitives::{eip4844::BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE, Address, B256, U256};
4
5/// The block environment
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8pub struct BlockEnv {
9    /// The number of ancestor blocks of this block (block height).
10    pub number: U256,
11    /// Beneficiary (Coinbase or miner) is a address that have signed the block.
12    ///
13    /// This is the receiver address of all the gas spent in the block.
14    pub beneficiary: Address,
15
16    /// The timestamp of the block in seconds since the UNIX epoch
17    pub timestamp: U256,
18    /// The gas limit of the block
19    pub gas_limit: u64,
20    /// The base fee per gas, added in the London upgrade with [EIP-1559]
21    ///
22    /// [EIP-1559]: https://eips.ethereum.org/EIPS/eip-1559
23    pub basefee: u64,
24    /// The difficulty of the block
25    ///
26    /// Unused after the Paris (AKA the merge) upgrade, and replaced by `prevrandao`.
27    pub difficulty: U256,
28    /// The output of the randomness beacon provided by the beacon chain
29    ///
30    /// Replaces `difficulty` after the Paris (AKA the merge) upgrade with [EIP-4399].
31    ///
32    /// Note: `prevrandao` can be found in a block in place of `mix_hash`.
33    ///
34    /// [EIP-4399]: https://eips.ethereum.org/EIPS/eip-4399
35    pub prevrandao: Option<B256>,
36    /// Excess blob gas and blob gasprice
37    ///
38    ///
39    /// Incorporated as part of the Cancun upgrade via [EIP-4844].
40    ///
41    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
42    pub blob_excess_gas_and_price: Option<BlobExcessGasAndPrice>,
43}
44
45impl BlockEnv {
46    /// Takes `blob_excess_gas` saves it inside env
47    /// and calculates `blob_fee` with [`BlobExcessGasAndPrice`].
48    pub fn set_blob_excess_gas_and_price(
49        &mut self,
50        excess_blob_gas: u64,
51        base_fee_update_fraction: u64,
52    ) {
53        self.blob_excess_gas_and_price = Some(BlobExcessGasAndPrice::new(
54            excess_blob_gas,
55            base_fee_update_fraction,
56        ));
57    }
58}
59
60impl Block for BlockEnv {
61    #[inline]
62    fn number(&self) -> U256 {
63        self.number
64    }
65
66    #[inline]
67    fn beneficiary(&self) -> Address {
68        self.beneficiary
69    }
70
71    #[inline]
72    fn timestamp(&self) -> U256 {
73        self.timestamp
74    }
75
76    #[inline]
77    fn gas_limit(&self) -> u64 {
78        self.gas_limit
79    }
80
81    #[inline]
82    fn basefee(&self) -> u64 {
83        self.basefee
84    }
85
86    #[inline]
87    fn difficulty(&self) -> U256 {
88        self.difficulty
89    }
90
91    #[inline]
92    fn prevrandao(&self) -> Option<B256> {
93        self.prevrandao
94    }
95
96    #[inline]
97    fn blob_excess_gas_and_price(&self) -> Option<BlobExcessGasAndPrice> {
98        self.blob_excess_gas_and_price
99    }
100}
101
102impl Default for BlockEnv {
103    fn default() -> Self {
104        Self {
105            number: U256::ZERO,
106            beneficiary: Address::ZERO,
107            timestamp: U256::ONE,
108            gas_limit: u64::MAX,
109            basefee: 0,
110            difficulty: U256::ZERO,
111            prevrandao: Some(B256::ZERO),
112            blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
113                0,
114                BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE,
115            )),
116        }
117    }
118}