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