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