1use super::transaction::{TransactionInfo, TypedReceipt};
2use alloy_consensus::{EMPTY_OMMER_ROOT_HASH, Header, proofs::calculate_transaction_root};
3use alloy_primitives::{Address, B64, B256, Bloom, Bytes, U256};
4use alloy_rlp::{RlpDecodable, RlpEncodable};
5
6type Transaction = crate::eth::transaction::MaybeImpersonatedTransaction;
8
9#[derive(Clone, Debug)]
11pub struct BlockInfo {
12 pub block: Block,
13 pub transactions: Vec<TransactionInfo>,
14 pub receipts: Vec<TypedReceipt>,
15}
16
17#[derive(Clone, Debug, PartialEq, Eq, RlpEncodable, RlpDecodable)]
19pub struct Block {
20 pub header: Header,
21 pub transactions: Vec<Transaction>,
22 pub ommers: Vec<Header>,
23}
24
25impl Block {
26 pub fn new<T>(partial_header: PartialHeader, transactions: impl IntoIterator<Item = T>) -> Self
31 where
32 T: Into<Transaction>,
33 {
34 let transactions: Vec<_> = transactions.into_iter().map(Into::into).collect();
35 let transactions_root = calculate_transaction_root(&transactions);
36
37 Self {
38 header: Header {
39 parent_hash: partial_header.parent_hash,
40 beneficiary: partial_header.beneficiary,
41 ommers_hash: EMPTY_OMMER_ROOT_HASH,
42 state_root: partial_header.state_root,
43 transactions_root,
44 receipts_root: partial_header.receipts_root,
45 logs_bloom: partial_header.logs_bloom,
46 difficulty: partial_header.difficulty,
47 number: partial_header.number,
48 gas_limit: partial_header.gas_limit,
49 gas_used: partial_header.gas_used,
50 timestamp: partial_header.timestamp,
51 extra_data: partial_header.extra_data,
52 mix_hash: partial_header.mix_hash,
53 withdrawals_root: partial_header.withdrawals_root,
54 blob_gas_used: partial_header.blob_gas_used,
55 excess_blob_gas: partial_header.excess_blob_gas,
56 parent_beacon_block_root: partial_header.parent_beacon_block_root,
57 nonce: partial_header.nonce,
58 base_fee_per_gas: partial_header.base_fee,
59 requests_hash: partial_header.requests_hash,
60 },
61 transactions,
62 ommers: vec![],
63 }
64 }
65}
66
67#[derive(Clone, Debug, Default, PartialEq, Eq)]
69pub struct PartialHeader {
70 pub parent_hash: B256,
71 pub beneficiary: Address,
72 pub state_root: B256,
73 pub receipts_root: B256,
74 pub logs_bloom: Bloom,
75 pub difficulty: U256,
76 pub number: u64,
77 pub gas_limit: u64,
78 pub gas_used: u64,
79 pub timestamp: u64,
80 pub extra_data: Bytes,
81 pub mix_hash: B256,
82 pub blob_gas_used: Option<u64>,
83 pub excess_blob_gas: Option<u64>,
84 pub parent_beacon_block_root: Option<B256>,
85 pub nonce: B64,
86 pub base_fee: Option<u64>,
87 pub withdrawals_root: Option<B256>,
88 pub requests_hash: Option<B256>,
89}
90
91impl From<Header> for PartialHeader {
92 fn from(value: Header) -> Self {
93 Self {
94 parent_hash: value.parent_hash,
95 beneficiary: value.beneficiary,
96 state_root: value.state_root,
97 receipts_root: value.receipts_root,
98 logs_bloom: value.logs_bloom,
99 difficulty: value.difficulty,
100 number: value.number,
101 gas_limit: value.gas_limit,
102 gas_used: value.gas_used,
103 timestamp: value.timestamp,
104 extra_data: value.extra_data,
105 mix_hash: value.mix_hash,
106 nonce: value.nonce,
107 base_fee: value.base_fee_per_gas,
108 blob_gas_used: value.blob_gas_used,
109 excess_blob_gas: value.excess_blob_gas,
110 parent_beacon_block_root: value.parent_beacon_block_root,
111 requests_hash: value.requests_hash,
112 withdrawals_root: value.withdrawals_root,
113 }
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use alloy_primitives::{
120 b256,
121 hex::{self, FromHex},
122 };
123 use alloy_rlp::Decodable;
124
125 use super::*;
126 use std::str::FromStr;
127
128 #[test]
129 fn header_rlp_roundtrip() {
130 let mut header = Header {
131 parent_hash: Default::default(),
132 ommers_hash: Default::default(),
133 beneficiary: Default::default(),
134 state_root: Default::default(),
135 transactions_root: Default::default(),
136 receipts_root: Default::default(),
137 logs_bloom: Default::default(),
138 difficulty: Default::default(),
139 number: 124u64,
140 gas_limit: Default::default(),
141 gas_used: 1337u64,
142 timestamp: 0,
143 extra_data: Default::default(),
144 mix_hash: Default::default(),
145 nonce: B64::with_last_byte(99),
146 withdrawals_root: Default::default(),
147 blob_gas_used: Default::default(),
148 excess_blob_gas: Default::default(),
149 parent_beacon_block_root: Default::default(),
150 base_fee_per_gas: None,
151 requests_hash: None,
152 };
153
154 let encoded = alloy_rlp::encode(&header);
155 let decoded: Header = Header::decode(&mut encoded.as_ref()).unwrap();
156 assert_eq!(header, decoded);
157
158 header.base_fee_per_gas = Some(12345u64);
159
160 let encoded = alloy_rlp::encode(&header);
161 let decoded: Header = Header::decode(&mut encoded.as_ref()).unwrap();
162 assert_eq!(header, decoded);
163 }
164
165 #[test]
166 fn test_encode_block_header() {
167 use alloy_rlp::Encodable;
168
169 let expected = hex::decode("f901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000").unwrap();
170 let mut data = vec![];
171 let header = Header {
172 parent_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
173 ommers_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
174 beneficiary: Address::from_str("0000000000000000000000000000000000000000").unwrap(),
175 state_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
176 transactions_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
177 receipts_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
178 logs_bloom: Bloom::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(),
179 difficulty: U256::from(2222),
180 number: 0xd05u64,
181 gas_limit: 0x115cu64,
182 gas_used: 0x15b3u64,
183 timestamp: 0x1a0au64,
184 extra_data: hex::decode("7788").unwrap().into(),
185 mix_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
186 withdrawals_root: None,
187 blob_gas_used: None,
188 excess_blob_gas: None,
189 parent_beacon_block_root: None,
190 nonce: B64::ZERO,
191 base_fee_per_gas: None,
192 requests_hash: None,
193 };
194
195 header.encode(&mut data);
196 assert_eq!(hex::encode(&data), hex::encode(expected));
197 assert_eq!(header.length(), data.len());
198 }
199
200 #[test]
201 fn test_decode_block_header() {
203 let data = hex::decode("f901f9a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000940000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000000b90100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008208ae820d0582115c8215b3821a0a827788a00000000000000000000000000000000000000000000000000000000000000000880000000000000000").unwrap();
204 let expected = Header {
205 parent_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
206 ommers_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
207 beneficiary: Address::from_str("0000000000000000000000000000000000000000").unwrap(),
208 state_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
209 transactions_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
210 receipts_root: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
211 logs_bloom: <[u8; 256]>::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap().into(),
212 difficulty: U256::from(2222),
213 number: 0xd05u64,
214 gas_limit: 0x115cu64,
215 gas_used: 0x15b3u64,
216 timestamp: 0x1a0au64,
217 extra_data: hex::decode("7788").unwrap().into(),
218 mix_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
219 nonce: B64::ZERO,
220 withdrawals_root: None,
221 blob_gas_used: None,
222 excess_blob_gas: None,
223 parent_beacon_block_root: None,
224 base_fee_per_gas: None,
225 requests_hash: None,
226 };
227 let header = Header::decode(&mut data.as_slice()).unwrap();
228 assert_eq!(header, expected);
229 }
230
231 #[test]
232 fn test_eip1559_block_header_hash() {
234 let expected_hash =
235 b256!("0x6a251c7c3c5dca7b42407a3752ff48f3bbca1fab7f9868371d9918daf1988d1f");
236 let header = Header {
237 parent_hash: B256::from_str("e0a94a7a3c9617401586b1a27025d2d9671332d22d540e0af72b069170380f2a").unwrap(),
238 ommers_hash: B256::from_str("1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347").unwrap(),
239 beneficiary: Address::from_str("ba5e000000000000000000000000000000000000").unwrap(),
240 state_root: B256::from_str("ec3c94b18b8a1cff7d60f8d258ec723312932928626b4c9355eb4ab3568ec7f7").unwrap(),
241 transactions_root: B256::from_str("50f738580ed699f0469702c7ccc63ed2e51bc034be9479b7bff4e68dee84accf").unwrap(),
242 receipts_root: B256::from_str("29b0562f7140574dd0d50dee8a271b22e1a0a7b78fca58f7c60370d8317ba2a9").unwrap(),
243 logs_bloom: Bloom::from_hex("00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000").unwrap(),
244 difficulty: U256::from(0x020000),
245 number: 1u64,
246 gas_limit: U256::from(0x016345785d8a0000u128).to::<u64>(),
247 gas_used: U256::from(0x015534).to::<u64>(),
248 timestamp: 0x079e,
249 extra_data: hex::decode("42").unwrap().into(),
250 mix_hash: B256::from_str("0000000000000000000000000000000000000000000000000000000000000000").unwrap(),
251 nonce: B64::ZERO,
252 base_fee_per_gas: Some(875),
253 withdrawals_root: None,
254 blob_gas_used: None,
255 excess_blob_gas: None,
256 parent_beacon_block_root: None,
257 requests_hash: None,
258 };
259 assert_eq!(header.hash_slow(), expected_hash);
260 }
261
262 #[test]
263 fn block_network_roundtrip() {
265 use alloy_rlp::Encodable;
266
267 let data = hex::decode("f9034df90348a0fbdbd8d2d0ac5f14bd5fa90e547fe6f1d15019c724f8e7b60972d381cd5d9cf8a01dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d4934794c9577e7945db22e38fc060909f2278c7746b0f9ba05017cfa3b0247e35197215ae8d610265ffebc8edca8ea66d6567eb0adecda867a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421b9010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018355bb7b871fffffffffffff808462bd0e1ab9014bf90148a00000000000000000000000000000000000000000000000000000000000000000f85494319fa8f1bc4e53410e92d10d918659b16540e60a945a573efb304d04c1224cd012313e827eca5dce5d94a9c831c5a268031176ebf5f3de5051e8cba0dbfe94c9577e7945db22e38fc060909f2278c7746b0f9b808400000000f8c9b841a6946f2d16f68338cbcbd8b117374ab421128ce422467088456bceba9d70c34106128e6d4564659cf6776c08a4186063c0a05f7cffd695c10cf26a6f301b67f800b8412b782100c18c35102dc0a37ece1a152544f04ad7dc1868d18a9570f744ace60870f822f53d35e89a2ea9709ccbf1f4a25ee5003944faa845d02dde0a41d5704601b841d53caebd6c8a82456e85c2806a9e08381f959a31fb94a77e58f00e38ad97b2e0355b8519ab2122662cbe022f2a4ef7ff16adc0b2d5dcd123181ec79705116db300a063746963616c2062797a616e74696e65206661756c7420746f6c6572616e6365880000000000000000c0c0").unwrap();
268
269 let block = Block::decode(&mut data.as_slice()).unwrap();
270
271 let mut encoded = Vec::new();
273 block.encode(&mut encoded);
274 assert_eq!(data, encoded);
275
276 assert_eq!(block.length(), encoded.len());
278 }
279}