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    /// See also [`calc_excess_blob_gas`][context_interface::block::calc_excess_blob_gas]
39    /// and [`calc_blob_gasprice`][context_interface::block::blob::calc_blob_gasprice].
40    ///
41    /// Incorporated as part of the Cancun upgrade via [EIP-4844].
42    ///
43    /// [EIP-4844]: https://eips.ethereum.org/EIPS/eip-4844
44    pub blob_excess_gas_and_price: Option<BlobExcessGasAndPrice>,
45}
46
47impl BlockEnv {
48    /// Takes `blob_excess_gas` saves it inside env
49    /// and calculates `blob_fee` with [`BlobExcessGasAndPrice`].
50    pub fn set_blob_excess_gas_and_price(
51        &mut self,
52        excess_blob_gas: u64,
53        base_fee_update_fraction: u64,
54    ) {
55        self.blob_excess_gas_and_price = Some(BlobExcessGasAndPrice::new(
56            excess_blob_gas,
57            base_fee_update_fraction,
58        ));
59    }
60}
61
62impl Block for BlockEnv {
63    #[inline]
64    fn number(&self) -> U256 {
65        self.number
66    }
67
68    #[inline]
69    fn beneficiary(&self) -> Address {
70        self.beneficiary
71    }
72
73    #[inline]
74    fn timestamp(&self) -> U256 {
75        self.timestamp
76    }
77
78    #[inline]
79    fn gas_limit(&self) -> u64 {
80        self.gas_limit
81    }
82
83    #[inline]
84    fn basefee(&self) -> u64 {
85        self.basefee
86    }
87
88    #[inline]
89    fn difficulty(&self) -> U256 {
90        self.difficulty
91    }
92
93    #[inline]
94    fn prevrandao(&self) -> Option<B256> {
95        self.prevrandao
96    }
97
98    #[inline]
99    fn blob_excess_gas_and_price(&self) -> Option<BlobExcessGasAndPrice> {
100        self.blob_excess_gas_and_price
101    }
102}
103
104impl Default for BlockEnv {
105    fn default() -> Self {
106        Self {
107            number: U256::ZERO,
108            beneficiary: Address::ZERO,
109            timestamp: U256::ONE,
110            gas_limit: u64::MAX,
111            basefee: 0,
112            difficulty: U256::ZERO,
113            prevrandao: Some(B256::ZERO),
114            blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(
115                0,
116                BLOB_BASE_FEE_UPDATE_FRACTION_PRAGUE,
117            )),
118        }
119    }
120}