revm_context/block.rs
1//! This module contains [`BlockEnv`] and it implements [`Block`] trait.
2use context_interface::block::{BlobExcessGasAndPrice, Block};
3use primitives::{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: u64,
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: u64,
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(&mut self, excess_blob_gas: u64, is_prague: bool) {
51 self.blob_excess_gas_and_price =
52 Some(BlobExcessGasAndPrice::new(excess_blob_gas, is_prague));
53 }
54}
55
56impl Block for BlockEnv {
57 #[inline]
58 fn number(&self) -> u64 {
59 self.number
60 }
61
62 #[inline]
63 fn beneficiary(&self) -> Address {
64 self.beneficiary
65 }
66
67 #[inline]
68 fn timestamp(&self) -> u64 {
69 self.timestamp
70 }
71
72 #[inline]
73 fn gas_limit(&self) -> u64 {
74 self.gas_limit
75 }
76
77 #[inline]
78 fn basefee(&self) -> u64 {
79 self.basefee
80 }
81
82 #[inline]
83 fn difficulty(&self) -> U256 {
84 self.difficulty
85 }
86
87 #[inline]
88 fn prevrandao(&self) -> Option<B256> {
89 self.prevrandao
90 }
91
92 #[inline]
93 fn blob_excess_gas_and_price(&self) -> Option<BlobExcessGasAndPrice> {
94 self.blob_excess_gas_and_price
95 }
96}
97
98impl Default for BlockEnv {
99 fn default() -> Self {
100 Self {
101 number: 0,
102 beneficiary: Address::ZERO,
103 timestamp: 1,
104 gas_limit: u64::MAX,
105 basefee: 0,
106 difficulty: U256::ZERO,
107 prevrandao: Some(B256::ZERO),
108 blob_excess_gas_and_price: Some(BlobExcessGasAndPrice::new(0, false)),
109 }
110 }
111}