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