1use crate::Transaction;
4use alloc::{collections::BTreeMap, vec::Vec};
5use alloy_consensus::{error::ValueError, BlockBody, BlockHeader, Sealed, TxEnvelope};
6use alloy_eips::{eip4895::Withdrawals, eip7840::BlobParams, Encodable2718};
7use alloy_network_primitives::{
8 BlockResponse, BlockTransactions, HeaderResponse, TransactionResponse,
9};
10use alloy_primitives::{Address, BlockHash, Bloom, Bytes, Sealable, B256, B64, U256};
11use alloy_rlp::Encodable;
12use core::ops::{Deref, DerefMut};
13
14pub use alloy_eips::{
15 calc_blob_gasprice, calc_excess_blob_gas, BlockHashOrNumber, BlockId, BlockNumHash,
16 BlockNumberOrTag, ForkBlock, RpcBlockHash,
17};
18
19#[derive(Clone, Debug, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
23pub struct Block<T = Transaction<TxEnvelope>, H = Header> {
24 #[cfg_attr(feature = "serde", serde(flatten))]
26 pub header: H,
27 #[cfg_attr(feature = "serde", serde(default))]
29 pub uncles: Vec<B256>,
30 #[cfg_attr(
33 feature = "serde",
34 serde(
35 default = "BlockTransactions::uncle",
36 skip_serializing_if = "BlockTransactions::is_uncle"
37 )
38 )]
39 pub transactions: BlockTransactions<T>,
40 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
42 pub withdrawals: Option<Withdrawals>,
43}
44
45impl<T, H: Default> Default for Block<T, H> {
47 fn default() -> Self {
48 Self {
49 header: Default::default(),
50 uncles: Default::default(),
51 transactions: Default::default(),
52 withdrawals: Default::default(),
53 }
54 }
55}
56
57impl<T, H> Block<T, H> {
58 pub const fn empty(header: H) -> Self {
60 Self::new(header, BlockTransactions::Full(vec![]))
61 }
62
63 pub const fn new(header: H, transactions: BlockTransactions<T>) -> Self {
78 Self { header, uncles: vec![], transactions, withdrawals: None }
79 }
80
81 pub fn number(&self) -> u64
83 where
84 H: BlockHeader,
85 {
86 self.header.number()
87 }
88
89 pub fn apply<F>(self, f: F) -> Self
91 where
92 F: FnOnce(Self) -> Self,
93 {
94 f(self)
95 }
96
97 pub fn with_transactions(mut self, transactions: BlockTransactions<T>) -> Self {
99 self.transactions = transactions;
100 self
101 }
102
103 pub fn with_withdrawals(mut self, withdrawals: Option<Withdrawals>) -> Self {
105 self.withdrawals = withdrawals;
106 self
107 }
108
109 pub fn with_uncles(mut self, uncles: Vec<B256>) -> Self {
111 self.uncles = uncles;
112 self
113 }
114
115 pub fn try_into_transactions(self) -> Result<Vec<T>, ValueError<BlockTransactions<T>>> {
119 self.transactions.try_into_transactions()
120 }
121
122 pub fn into_transactions_vec(self) -> Vec<T> {
126 self.transactions.into_transactions_vec()
127 }
128
129 pub fn into_hashes_vec(self) -> Vec<B256>
133 where
134 T: TransactionResponse,
135 {
136 self.transactions.into_hashes_vec()
137 }
138
139 pub fn try_into_block_body(self) -> Result<BlockBody<T, H>, ValueError<Self>> {
143 if !self.uncles.is_empty() {
144 return Err(ValueError::new_static(self, "uncles not empty"));
145 }
146 if !self.transactions.is_full() {
147 return Err(ValueError::new_static(self, "transactions not full"));
148 }
149
150 Ok(self.into_block_body_unchecked())
151 }
152
153 pub fn into_block_body_unchecked(self) -> BlockBody<T, H> {
159 BlockBody {
160 transactions: self.transactions.into_transactions_vec(),
161 ommers: Default::default(),
162 withdrawals: self.withdrawals,
163 }
164 }
165
166 pub fn into_consensus_block(self) -> alloy_consensus::Block<T, H> {
177 alloy_consensus::BlockBody {
178 transactions: self.transactions.into_transactions_vec(),
179 ommers: vec![],
180 withdrawals: self.withdrawals,
181 }
182 .into_block(self.header)
183 }
184
185 pub fn map_header<U>(self, f: impl FnOnce(H) -> U) -> Block<T, U> {
187 Block {
188 header: f(self.header),
189 uncles: self.uncles,
190 transactions: self.transactions,
191 withdrawals: self.withdrawals,
192 }
193 }
194
195 pub fn into_header(self) -> H {
199 self.header
200 }
201
202 pub fn try_convert_header<U>(self) -> Result<Block<T, U>, U::Error>
204 where
205 U: TryFrom<H>,
206 {
207 self.try_map_header(U::try_from)
208 }
209
210 pub fn try_map_header<U, E>(self, f: impl FnOnce(H) -> Result<U, E>) -> Result<Block<T, U>, E> {
212 Ok(Block {
213 header: f(self.header)?,
214 uncles: self.uncles,
215 transactions: self.transactions,
216 withdrawals: self.withdrawals,
217 })
218 }
219
220 pub fn convert_transactions<U>(self) -> Block<U, H>
222 where
223 U: From<T>,
224 {
225 self.map_transactions(U::from)
226 }
227
228 pub fn try_convert_transactions<U>(self) -> Result<Block<U, H>, U::Error>
232 where
233 U: TryFrom<T>,
234 {
235 self.try_map_transactions(U::try_from)
236 }
237
238 pub fn map_transactions<U>(self, f: impl FnMut(T) -> U) -> Block<U, H> {
242 Block {
243 header: self.header,
244 uncles: self.uncles,
245 transactions: self.transactions.map(f),
246 withdrawals: self.withdrawals,
247 }
248 }
249
250 pub fn try_map_transactions<U, E>(
255 self,
256 f: impl FnMut(T) -> Result<U, E>,
257 ) -> Result<Block<U, H>, E> {
258 Ok(Block {
259 header: self.header,
260 uncles: self.uncles,
261 transactions: self.transactions.try_map(f)?,
262 withdrawals: self.withdrawals,
263 })
264 }
265
266 pub fn calculate_transactions_root(&self) -> Option<B256>
270 where
271 T: Encodable2718,
272 {
273 self.transactions.calculate_transactions_root()
274 }
275}
276
277impl<T: TransactionResponse, H> Block<T, H> {
278 pub fn into_full_block(self, txs: Vec<T>) -> Self {
280 Self { transactions: txs.into(), ..self }
281 }
282}
283
284impl<T, H: Sealable + Encodable> Block<T, Header<H>> {
285 pub fn uncle_from_header(header: H) -> Self {
290 let block = alloy_consensus::Block::<TxEnvelope, H>::uncle(header);
291 let size = U256::from(block.length());
292 Self {
293 uncles: vec![],
294 header: Header::from_consensus(block.header.seal_slow(), None, Some(size)),
295 transactions: BlockTransactions::Uncle,
296 withdrawals: None,
297 }
298 }
299}
300
301impl<T> Block<T> {
302 pub const fn hash(&self) -> B256 {
304 self.header.hash
305 }
306
307 pub const fn sealed_header(&self) -> Sealed<&alloy_consensus::Header> {
309 Sealed::new_unchecked(&self.header.inner, self.header.hash)
310 }
311
312 pub fn into_sealed_header(self) -> Sealed<alloy_consensus::Header> {
314 self.header.into_sealed()
315 }
316
317 pub fn into_consensus_header(self) -> alloy_consensus::Header {
320 self.header.into_consensus()
321 }
322
323 pub fn from_consensus(block: alloy_consensus::Block<T>, total_difficulty: Option<U256>) -> Self
325 where
326 T: Encodable,
327 {
328 let size = U256::from(block.length());
329 let alloy_consensus::Block {
330 header,
331 body: alloy_consensus::BlockBody { transactions, ommers, withdrawals },
332 } = block;
333
334 Self {
335 header: Header::from_consensus(header.seal_slow(), total_difficulty, Some(size)),
336 uncles: ommers.into_iter().map(|h| h.hash_slow()).collect(),
337 transactions: BlockTransactions::Full(transactions),
338 withdrawals,
339 }
340 }
341
342 pub fn into_consensus(self) -> alloy_consensus::Block<T> {
350 let Self { header, transactions, withdrawals, .. } = self;
351 alloy_consensus::BlockBody {
352 transactions: transactions.into_transactions_vec(),
353 ommers: vec![],
354 withdrawals,
355 }
356 .into_block(header.into_consensus())
357 }
358
359 pub fn into_consensus_sealed(self) -> Sealed<alloy_consensus::Block<T>> {
361 let hash = self.header.hash;
362 Sealed::new_unchecked(self.into_consensus(), hash)
363 }
364}
365
366impl<T, S> From<Block<T>> for alloy_consensus::Block<S>
367where
368 S: From<T>,
369{
370 fn from(block: Block<T>) -> Self {
371 block.into_consensus().convert_transactions()
372 }
373}
374
375#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
379#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
380#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
381#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
382pub struct Header<H = alloy_consensus::Header> {
383 pub hash: BlockHash,
385 #[cfg_attr(feature = "serde", serde(flatten))]
387 pub inner: H,
388 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
392 pub total_difficulty: Option<U256>,
393 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
395 pub size: Option<U256>,
396}
397
398impl<H> Header<H> {
399 pub fn new(inner: H) -> Self
403 where
404 H: Sealable,
405 {
406 Self::from_sealed(Sealed::new(inner))
407 }
408
409 pub fn from_sealed(header: Sealed<H>) -> Self {
413 let (inner, hash) = header.into_parts();
414 Self { hash, inner, total_difficulty: None, size: None }
415 }
416
417 pub fn into_sealed(self) -> Sealed<H> {
419 Sealed::new_unchecked(self.inner, self.hash)
420 }
421
422 pub fn into_consensus(self) -> H {
424 self.inner
425 }
426
427 pub fn from_consensus(
429 header: Sealed<H>,
430 total_difficulty: Option<U256>,
431 size: Option<U256>,
432 ) -> Self {
433 let (inner, hash) = header.into_parts();
434 Self { hash, inner, total_difficulty, size }
435 }
436
437 pub const fn with_total_difficulty(mut self, total_difficulty: Option<U256>) -> Self {
439 self.total_difficulty = total_difficulty;
440 self
441 }
442
443 pub const fn with_size(mut self, size: Option<U256>) -> Self {
445 self.size = size;
446 self
447 }
448
449 #[expect(clippy::use_self)]
451 pub fn map<H1>(self, f: impl FnOnce(H) -> H1) -> Header<H1> {
452 let Header { hash, inner, total_difficulty, size } = self;
453
454 Header { hash, inner: f(inner), total_difficulty, size }
455 }
456
457 #[expect(clippy::use_self)]
459 pub fn try_map<H1, E>(self, f: impl FnOnce(H) -> Result<H1, E>) -> Result<Header<H1>, E> {
460 let Header { hash, inner, total_difficulty, size } = self;
461
462 Ok(Header { hash, inner: f(inner)?, total_difficulty, size })
463 }
464}
465
466impl<H> Deref for Header<H> {
467 type Target = H;
468
469 fn deref(&self) -> &Self::Target {
470 &self.inner
471 }
472}
473
474impl<H> DerefMut for Header<H> {
475 fn deref_mut(&mut self) -> &mut Self::Target {
476 &mut self.inner
477 }
478}
479
480impl<H> AsRef<H> for Header<H> {
481 fn as_ref(&self) -> &H {
482 &self.inner
483 }
484}
485
486impl<H: BlockHeader> Header<H> {
487 pub fn blob_fee(&self) -> Option<u128> {
491 self.inner.excess_blob_gas().map(calc_blob_gasprice)
492 }
493
494 pub fn next_block_blob_fee(&self, blob_params: BlobParams) -> Option<u128> {
500 self.inner.next_block_blob_fee(blob_params)
501 }
502
503 pub fn next_block_excess_blob_gas(&self, blob_params: BlobParams) -> Option<u64> {
508 self.inner.next_block_excess_blob_gas(blob_params)
509 }
510}
511
512impl<H: BlockHeader> BlockHeader for Header<H> {
513 fn parent_hash(&self) -> B256 {
514 self.inner.parent_hash()
515 }
516
517 fn ommers_hash(&self) -> B256 {
518 self.inner.ommers_hash()
519 }
520
521 fn beneficiary(&self) -> Address {
522 self.inner.beneficiary()
523 }
524
525 fn state_root(&self) -> B256 {
526 self.inner.state_root()
527 }
528
529 fn transactions_root(&self) -> B256 {
530 self.inner.transactions_root()
531 }
532
533 fn receipts_root(&self) -> B256 {
534 self.inner.receipts_root()
535 }
536
537 fn withdrawals_root(&self) -> Option<B256> {
538 self.inner.withdrawals_root()
539 }
540
541 fn logs_bloom(&self) -> Bloom {
542 self.inner.logs_bloom()
543 }
544
545 fn difficulty(&self) -> U256 {
546 self.inner.difficulty()
547 }
548
549 fn number(&self) -> u64 {
550 self.inner.number()
551 }
552
553 fn gas_limit(&self) -> u64 {
554 self.inner.gas_limit()
555 }
556
557 fn gas_used(&self) -> u64 {
558 self.inner.gas_used()
559 }
560
561 fn timestamp(&self) -> u64 {
562 self.inner.timestamp()
563 }
564
565 fn mix_hash(&self) -> Option<B256> {
566 self.inner.mix_hash()
567 }
568
569 fn nonce(&self) -> Option<B64> {
570 self.inner.nonce()
571 }
572
573 fn base_fee_per_gas(&self) -> Option<u64> {
574 self.inner.base_fee_per_gas()
575 }
576
577 fn blob_gas_used(&self) -> Option<u64> {
578 self.inner.blob_gas_used()
579 }
580
581 fn excess_blob_gas(&self) -> Option<u64> {
582 self.inner.excess_blob_gas()
583 }
584
585 fn parent_beacon_block_root(&self) -> Option<B256> {
586 self.inner.parent_beacon_block_root()
587 }
588
589 fn requests_hash(&self) -> Option<B256> {
590 self.inner.requests_hash()
591 }
592
593 fn block_access_list_hash(&self) -> Option<B256> {
594 self.inner.block_access_list_hash()
595 }
596
597 fn slot_number(&self) -> Option<u64> {
598 self.inner.slot_number()
599 }
600
601 fn extra_data(&self) -> &Bytes {
602 self.inner.extra_data()
603 }
604}
605
606impl<H: BlockHeader> HeaderResponse for Header<H> {
607 fn hash(&self) -> BlockHash {
608 self.hash
609 }
610}
611
612impl From<Header> for alloy_consensus::Header {
613 fn from(header: Header) -> Self {
614 header.into_consensus()
615 }
616}
617
618impl<H> From<Header<H>> for Sealed<H> {
619 fn from(value: Header<H>) -> Self {
620 value.into_sealed()
621 }
622}
623
624#[derive(Clone, Copy, Debug, thiserror::Error)]
626pub enum BlockError {
627 #[error("transaction failed sender recovery")]
629 InvalidSignature,
630 #[error("failed to decode raw block {0}")]
632 RlpDecodeRawBlock(alloy_rlp::Error),
633}
634
635#[cfg(feature = "serde")]
636impl<T, H> From<Block<T, H>> for alloy_serde::WithOtherFields<Block<T, H>> {
637 fn from(inner: Block<T, H>) -> Self {
638 Self { inner, other: Default::default() }
639 }
640}
641
642#[cfg(feature = "serde")]
643impl From<Header> for alloy_serde::WithOtherFields<Header> {
644 fn from(inner: Header) -> Self {
645 Self { inner, other: Default::default() }
646 }
647}
648
649#[derive(Clone, Debug, Default, PartialEq, Eq)]
651#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
652#[cfg_attr(feature = "serde", serde(default, rename_all = "camelCase"))]
653pub struct BlockOverrides {
654 #[cfg_attr(
660 feature = "serde",
661 serde(default, skip_serializing_if = "Option::is_none", alias = "blockNumber")
662 )]
663 pub number: Option<U256>,
664 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
666 pub difficulty: Option<U256>,
667 #[cfg_attr(
670 feature = "serde",
671 serde(
672 default,
673 skip_serializing_if = "Option::is_none",
674 alias = "timestamp",
675 with = "alloy_serde::quantity::opt"
676 )
677 )]
678 pub time: Option<u64>,
679 #[cfg_attr(
681 feature = "serde",
682 serde(
683 default,
684 skip_serializing_if = "Option::is_none",
685 with = "alloy_serde::quantity::opt"
686 )
687 )]
688 pub gas_limit: Option<u64>,
689 #[cfg_attr(
691 feature = "serde",
692 serde(default, skip_serializing_if = "Option::is_none", alias = "feeRecipient")
693 )]
694 pub coinbase: Option<Address>,
695 #[cfg_attr(
697 feature = "serde",
698 serde(default, skip_serializing_if = "Option::is_none", alias = "prevRandao")
699 )]
700 pub random: Option<B256>,
701 #[cfg_attr(
703 feature = "serde",
704 serde(default, skip_serializing_if = "Option::is_none", alias = "baseFeePerGas")
705 )]
706 pub base_fee: Option<U256>,
707 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
709 pub blob_base_fee: Option<U256>,
710 #[cfg_attr(
714 feature = "serde",
715 serde(default, skip_serializing_if = "Option::is_none", alias = "parentBeaconBlockRoot")
716 )]
717 pub beacon_root: Option<B256>,
718 #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "Option::is_none"))]
721 pub block_hash: Option<BTreeMap<u64, B256>>,
722}
723
724impl BlockOverrides {
725 pub const fn is_empty(&self) -> bool {
727 self.number.is_none()
728 && self.difficulty.is_none()
729 && self.time.is_none()
730 && self.gas_limit.is_none()
731 && self.coinbase.is_none()
732 && self.random.is_none()
733 && self.base_fee.is_none()
734 && self.blob_base_fee.is_none()
735 && self.beacon_root.is_none()
736 && self.block_hash.is_none()
737 }
738
739 pub const fn with_number(mut self, number: U256) -> Self {
741 self.number = Some(number);
742 self
743 }
744
745 pub const fn with_difficulty(mut self, difficulty: U256) -> Self {
747 self.difficulty = Some(difficulty);
748 self
749 }
750
751 pub const fn with_time(mut self, time: u64) -> Self {
753 self.time = Some(time);
754 self
755 }
756
757 pub const fn with_gas_limit(mut self, gas_limit: u64) -> Self {
759 self.gas_limit = Some(gas_limit);
760 self
761 }
762
763 pub const fn with_coinbase(mut self, coinbase: Address) -> Self {
765 self.coinbase = Some(coinbase);
766 self
767 }
768
769 pub const fn with_random(mut self, random: B256) -> Self {
771 self.random = Some(random);
772 self
773 }
774
775 pub const fn with_base_fee(mut self, base_fee: U256) -> Self {
777 self.base_fee = Some(base_fee);
778 self
779 }
780
781 pub const fn with_blob_base_fee(mut self, blob_base_fee: U256) -> Self {
783 self.blob_base_fee = Some(blob_base_fee);
784 self
785 }
786
787 pub const fn with_beacon_root(mut self, beacon_root: B256) -> Self {
789 self.beacon_root = Some(beacon_root);
790 self
791 }
792
793 pub fn append_block_hash(mut self, block_number: u64, hash: B256) -> Self {
795 let hash_map = self.block_hash.get_or_insert_with(Default::default);
796 hash_map.insert(block_number, hash);
797 self
798 }
799
800 pub fn with_block_hash_overrides<I>(mut self, hashes: I) -> Self
802 where
803 I: IntoIterator<Item = (u64, B256)>,
804 {
805 let map = self.block_hash.get_or_insert_with(Default::default);
806 map.extend(hashes);
807 self
808 }
809}
810
811impl<T: TransactionResponse, H> BlockResponse for Block<T, H> {
812 type Header = H;
813 type Transaction = T;
814
815 fn header(&self) -> &Self::Header {
816 &self.header
817 }
818
819 fn transactions(&self) -> &BlockTransactions<T> {
820 &self.transactions
821 }
822
823 fn transactions_mut(&mut self) -> &mut BlockTransactions<Self::Transaction> {
824 &mut self.transactions
825 }
826}
827
828#[derive(Clone, Debug, Default, PartialEq, Eq)]
830#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
831#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
832pub struct BadBlock<B = Block> {
833 pub block: B,
835 pub hash: BlockHash,
837 pub rlp: Bytes,
839}
840
841#[cfg(test)]
842mod tests {
843 use super::*;
844 use alloy_primitives::{hex, keccak256, Bloom, B64};
845 use arbitrary::Arbitrary;
846 use rand::Rng;
847 use similar_asserts::assert_eq;
848
849 #[test]
850 fn arbitrary_header() {
851 let mut bytes = [0u8; 1024];
852 rand::thread_rng().fill(bytes.as_mut_slice());
853 let _: Header = Header::arbitrary(&mut arbitrary::Unstructured::new(&bytes)).unwrap();
854 }
855
856 #[test]
857 #[cfg(feature = "serde")]
858 fn serde_json_header() {
859 #[derive(serde::Deserialize)]
860 #[allow(dead_code)]
861 struct SubParams<T> {
862 result: T,
863 }
864 #[derive(serde::Deserialize)]
865 #[allow(dead_code)]
866 struct SubNotification<T> {
867 params: SubParams<T>,
868 }
869
870 let resp = r#"{"jsonrpc":"2.0","method":"eth_subscribe","params":{"subscription":"0x7eef37ff35d471f8825b1c8f67a5d3c0","result":{"hash":"0x7a7ada12e140961a32395059597764416499f4178daf1917193fad7bd2cc6386","parentHash":"0xdedbd831f496e705e7f2ec3c8dcb79051040a360bf1455dbd7eb8ea6ad03b751","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","miner":"0x0000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","number":"0x8","gasUsed":"0x0","gasLimit":"0x1c9c380","extraData":"0x","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x642aa48f","difficulty":"0x0","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000"}}}"#;
871 let _header: SubNotification<Header> = serde_json::from_str(resp).unwrap();
872
873 let resp = r#"{"jsonrpc":"2.0","method":"eth_subscription","params":{"subscription":"0x1a14b6bdcf4542fabf71c4abee244e47","result":{"author":"0x000000568b9b5a365eaa767d42e74ed88915c204","difficulty":"0x1","extraData":"0x4e65746865726d696e6420312e392e32322d302d6463373666616366612d32308639ad8ff3d850a261f3b26bc2a55e0f3a718de0dd040a19a4ce37e7b473f2d7481448a1e1fd8fb69260825377c0478393e6055f471a5cf839467ce919a6ad2700","gasLimit":"0x7a1200","gasUsed":"0x0","hash":"0xa4856602944fdfd18c528ef93cc52a681b38d766a7e39c27a47488c8461adcb0","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x434822","parentHash":"0x1a9bdc31fc785f8a95efeeb7ae58f40f6366b8e805f47447a52335c95f4ceb49","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x261","stateRoot":"0xf38c4bf2958e541ec6df148e54ce073dc6b610f8613147ede568cb7b5c2d81ee","totalDifficulty":"0x633ebd","timestamp":"0x604726b0","transactions":[],"transactionsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","uncles":[]}}}"#;
874 let _header: SubNotification<Header> = serde_json::from_str(resp).unwrap();
875 }
876
877 #[test]
878 #[cfg(feature = "serde")]
879 fn serde_block() {
880 use alloy_primitives::B64;
881
882 let block = Block {
883 header: Header {
884 hash: B256::with_last_byte(1),
885 inner: alloy_consensus::Header {
886 parent_hash: B256::with_last_byte(2),
887 ommers_hash: B256::with_last_byte(3),
888 beneficiary: Address::with_last_byte(4),
889 state_root: B256::with_last_byte(5),
890 transactions_root: B256::with_last_byte(6),
891 receipts_root: B256::with_last_byte(7),
892 withdrawals_root: Some(B256::with_last_byte(8)),
893 number: 9,
894 gas_used: 10,
895 gas_limit: 11,
896 extra_data: vec![1, 2, 3].into(),
897 logs_bloom: Default::default(),
898 timestamp: 12,
899 difficulty: U256::from(13),
900 mix_hash: B256::with_last_byte(14),
901 nonce: B64::with_last_byte(15),
902 base_fee_per_gas: Some(20),
903 blob_gas_used: None,
904 excess_blob_gas: None,
905 parent_beacon_block_root: None,
906 requests_hash: None,
907 block_access_list_hash: None,
908 slot_number: None,
909 },
910 total_difficulty: Some(U256::from(100000)),
911 size: None,
912 },
913 uncles: vec![B256::with_last_byte(17)],
914 transactions: vec![B256::with_last_byte(18)].into(),
915 withdrawals: Some(Default::default()),
916 };
917 let serialized = serde_json::to_string(&block).unwrap();
918 similar_asserts::assert_eq!(
919 serialized,
920 r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","totalDifficulty":"0x186a0","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"withdrawals":[]}"#
921 );
922 let deserialized: Block = serde_json::from_str(&serialized).unwrap();
923 similar_asserts::assert_eq!(block, deserialized);
924 }
925
926 #[test]
927 #[cfg(feature = "serde")]
928 fn serde_uncle_block() {
929 use alloy_primitives::B64;
930
931 let block = Block {
932 header: Header {
933 hash: B256::with_last_byte(1),
934 inner: alloy_consensus::Header {
935 parent_hash: B256::with_last_byte(2),
936 ommers_hash: B256::with_last_byte(3),
937 beneficiary: Address::with_last_byte(4),
938 state_root: B256::with_last_byte(5),
939 transactions_root: B256::with_last_byte(6),
940 receipts_root: B256::with_last_byte(7),
941 withdrawals_root: Some(B256::with_last_byte(8)),
942 number: 9,
943 gas_used: 10,
944 gas_limit: 11,
945 extra_data: vec![1, 2, 3].into(),
946 logs_bloom: Default::default(),
947 timestamp: 12,
948 difficulty: U256::from(13),
949 mix_hash: B256::with_last_byte(14),
950 nonce: B64::with_last_byte(15),
951 base_fee_per_gas: Some(20),
952 blob_gas_used: None,
953 excess_blob_gas: None,
954 parent_beacon_block_root: None,
955 requests_hash: None,
956 block_access_list_hash: None,
957 slot_number: None,
958 },
959 size: None,
960 total_difficulty: Some(U256::from(100000)),
961 },
962 uncles: vec![],
963 transactions: BlockTransactions::Uncle,
964 withdrawals: None,
965 };
966 let serialized = serde_json::to_string(&block).unwrap();
967 assert_eq!(
968 serialized,
969 r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","totalDifficulty":"0x186a0","uncles":[]}"#
970 );
971 let deserialized: Block = serde_json::from_str(&serialized).unwrap();
972 assert_eq!(block, deserialized);
973 }
974
975 #[test]
976 #[cfg(feature = "serde")]
977 fn serde_block_with_withdrawals_set_as_none() {
978 let block = Block {
979 header: Header {
980 hash: B256::with_last_byte(1),
981 inner: alloy_consensus::Header {
982 parent_hash: B256::with_last_byte(2),
983 ommers_hash: B256::with_last_byte(3),
984 beneficiary: Address::with_last_byte(4),
985 state_root: B256::with_last_byte(5),
986 transactions_root: B256::with_last_byte(6),
987 receipts_root: B256::with_last_byte(7),
988 withdrawals_root: None,
989 number: 9,
990 gas_used: 10,
991 gas_limit: 11,
992 extra_data: vec![1, 2, 3].into(),
993 logs_bloom: Bloom::default(),
994 timestamp: 12,
995 difficulty: U256::from(13),
996 mix_hash: B256::with_last_byte(14),
997 nonce: B64::with_last_byte(15),
998 base_fee_per_gas: Some(20),
999 blob_gas_used: None,
1000 excess_blob_gas: None,
1001 parent_beacon_block_root: None,
1002 requests_hash: None,
1003 block_access_list_hash: None,
1004 slot_number: None,
1005 },
1006 total_difficulty: Some(U256::from(100000)),
1007 size: None,
1008 },
1009 uncles: vec![B256::with_last_byte(17)],
1010 transactions: vec![B256::with_last_byte(18)].into(),
1011 withdrawals: None,
1012 };
1013 let serialized = serde_json::to_string(&block).unwrap();
1014 assert_eq!(
1015 serialized,
1016 r#"{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","totalDifficulty":"0x186a0","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"]}"#
1017 );
1018 let deserialized: Block = serde_json::from_str(&serialized).unwrap();
1019 assert_eq!(block, deserialized);
1020 }
1021
1022 #[test]
1023 #[cfg(feature = "serde")]
1024 fn block_overrides() {
1025 let s = r#"{"blockNumber": "0xe39dd0"}"#;
1026 let _overrides = serde_json::from_str::<BlockOverrides>(s).unwrap();
1027 }
1028
1029 #[test]
1030 fn block_overrides_is_empty() {
1031 let default_overrides = BlockOverrides::default();
1033 assert!(default_overrides.is_empty());
1034
1035 let overrides_with_number = BlockOverrides::default().with_number(U256::from(42));
1037 assert!(!overrides_with_number.is_empty());
1038
1039 let overrides_with_difficulty = BlockOverrides::default().with_difficulty(U256::from(100));
1040 assert!(!overrides_with_difficulty.is_empty());
1041
1042 let overrides_with_time = BlockOverrides::default().with_time(12345);
1043 assert!(!overrides_with_time.is_empty());
1044
1045 let overrides_with_gas_limit = BlockOverrides::default().with_gas_limit(21000);
1046 assert!(!overrides_with_gas_limit.is_empty());
1047
1048 let overrides_with_coinbase =
1049 BlockOverrides::default().with_coinbase(Address::with_last_byte(1));
1050 assert!(!overrides_with_coinbase.is_empty());
1051
1052 let overrides_with_random = BlockOverrides::default().with_random(B256::with_last_byte(1));
1053 assert!(!overrides_with_random.is_empty());
1054
1055 let overrides_with_base_fee = BlockOverrides::default().with_base_fee(U256::from(20));
1056 assert!(!overrides_with_base_fee.is_empty());
1057
1058 let overrides_with_block_hash =
1059 BlockOverrides::default().append_block_hash(1, B256::with_last_byte(1));
1060 assert!(!overrides_with_block_hash.is_empty());
1061
1062 let overrides_with_beacon_root =
1063 BlockOverrides::default().with_beacon_root(B256::with_last_byte(1));
1064 assert!(!overrides_with_beacon_root.is_empty());
1065 }
1066
1067 #[test]
1068 #[cfg(feature = "serde")]
1069 fn serde_rich_block() {
1070 let s = r#"{
1071 "hash": "0xb25d0e54ca0104e3ebfb5a1dcdf9528140854d609886a300946fd6750dcb19f4",
1072 "parentHash": "0x9400ec9ef59689c157ac89eeed906f15ddd768f94e1575e0e27d37c241439a5d",
1073 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
1074 "miner": "0x829bd824b016326a401d083b33d092293333a830",
1075 "stateRoot": "0x546e330050c66d02923e7f1f3e925efaf64e4384eeecf2288f40088714a77a84",
1076 "transactionsRoot": "0xd5eb3ad6d7c7a4798cc5fb14a6820073f44a941107c5d79dac60bd16325631fe",
1077 "receiptsRoot": "0xb21c41cbb3439c5af25304e1405524c885e733b16203221900cb7f4b387b62f0",
1078 "logsBloom": "0x1f304e641097eafae088627298685d20202004a4a59e4d8900914724e2402b028c9d596660581f361240816e82d00fa14250c9ca89840887a381efa600288283d170010ab0b2a0694c81842c2482457e0eb77c2c02554614007f42aaf3b4dc15d006a83522c86a240c06d241013258d90540c3008888d576a02c10120808520a2221110f4805200302624d22092b2c0e94e849b1e1aa80bc4cc3206f00b249d0a603ee4310216850e47c8997a20aa81fe95040a49ca5a420464600e008351d161dc00d620970b6a801535c218d0b4116099292000c08001943a225d6485528828110645b8244625a182c1a88a41087e6d039b000a180d04300d0680700a15794",
1079 "difficulty": "0xc40faff9c737d",
1080 "number": "0xa9a230",
1081 "gasLimit": "0xbe5a66",
1082 "gasUsed": "0xbe0fcc",
1083 "timestamp": "0x5f93b749",
1084 "totalDifficulty": "0x3dc957fd8167fb2684a",
1085 "extraData": "0x7070796520e4b883e5bda9e7a59ee4bb99e9b1bc0103",
1086 "mixHash": "0xd5e2b7b71fbe4ddfe552fb2377bf7cddb16bbb7e185806036cee86994c6e97fc",
1087 "nonce": "0x4722f2acd35abe0f",
1088 "uncles": [],
1089 "transactions": [
1090 "0xf435a26acc2a9ef73ac0b73632e32e29bd0e28d5c4f46a7e18ed545c93315916"
1091 ],
1092 "size": "0xaeb6"
1093}"#;
1094
1095 let block = serde_json::from_str::<alloy_serde::WithOtherFields<Block>>(s).unwrap();
1096 let serialized = serde_json::to_string(&block).unwrap();
1097 let block2 =
1098 serde_json::from_str::<alloy_serde::WithOtherFields<Block>>(&serialized).unwrap();
1099 assert_eq!(block, block2);
1100 }
1101
1102 #[test]
1103 #[cfg(feature = "serde")]
1104 fn serde_missing_uncles_block() {
1105 let s = r#"{
1106 "baseFeePerGas":"0x886b221ad",
1107 "blobGasUsed":"0x0",
1108 "difficulty":"0x0",
1109 "excessBlobGas":"0x0",
1110 "extraData":"0x6265617665726275696c642e6f7267",
1111 "gasLimit":"0x1c9c380",
1112 "gasUsed":"0xb0033c",
1113 "hash":"0x85cdcbe36217fd57bf2c33731d8460657a7ce512401f49c9f6392c82a7ccf7ac",
1114 "logsBloom":"0xc36919406572730518285284f2293101104140c0d42c4a786c892467868a8806f40159d29988002870403902413a1d04321320308da2e845438429e0012a00b419d8ccc8584a1c28f82a415d04eab8a5ae75c00d07761acf233414c08b6d9b571c06156086c70ea5186e9b989b0c2d55c0213c936805cd2ab331589c90194d070c00867549b1e1be14cb24500b0386cd901197c1ef5a00da453234fa48f3003dcaa894e3111c22b80e17f7d4388385a10720cda1140c0400f9e084ca34fc4870fb16b472340a2a6a63115a82522f506c06c2675080508834828c63defd06bc2331b4aa708906a06a560457b114248041e40179ebc05c6846c1e922125982f427",
1115 "miner":"0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5",
1116 "mixHash":"0x4c068e902990f21f92a2456fc75c59bec8be03b7f13682b6ebd27da56269beb5",
1117 "nonce":"0x0000000000000000",
1118 "number":"0x128c6df",
1119 "parentBeaconBlockRoot":"0x2843cb9f7d001bd58816a915e685ed96a555c9aeec1217736bd83a96ebd409cc",
1120 "parentHash":"0x90926e0298d418181bd20c23b332451e35fd7d696b5dcdc5a3a0a6b715f4c717",
1121 "receiptsRoot":"0xd43aa19ecb03571d1b86d89d9bb980139d32f2f2ba59646cd5c1de9e80c68c90",
1122 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
1123 "size":"0xdcc3",
1124 "stateRoot":"0x707875120a7103621fb4131df59904cda39de948dfda9084a1e3da44594d5404",
1125 "timestamp":"0x65f5f4c3",
1126 "transactionsRoot":"0x889a1c26dc42ba829dab552b779620feac231cde8a6c79af022bdc605c23a780",
1127 "withdrawals":[
1128 {
1129 "index":"0x24d80e6",
1130 "validatorIndex":"0x8b2b6",
1131 "address":"0x7cd1122e8e118b12ece8d25480dfeef230da17ff",
1132 "amount":"0x1161f10"
1133 }
1134 ],
1135 "withdrawalsRoot":"0x360c33f20eeed5efbc7d08be46e58f8440af5db503e40908ef3d1eb314856ef7"
1136 }"#;
1137
1138 let block = serde_json::from_str::<Block>(s).unwrap();
1139 let serialized = serde_json::to_string(&block).unwrap();
1140 let block2 = serde_json::from_str::<Block>(&serialized).unwrap();
1141 assert_eq!(block, block2);
1142 }
1143
1144 #[test]
1145 #[cfg(feature = "serde")]
1146 fn serde_block_containing_uncles() {
1147 let s = r#"{
1148 "baseFeePerGas":"0x886b221ad",
1149 "blobGasUsed":"0x0",
1150 "difficulty":"0x0",
1151 "excessBlobGas":"0x0",
1152 "extraData":"0x6265617665726275696c642e6f7267",
1153 "gasLimit":"0x1c9c380",
1154 "gasUsed":"0xb0033c",
1155 "hash":"0x85cdcbe36217fd57bf2c33731d8460657a7ce512401f49c9f6392c82a7ccf7ac",
1156 "logsBloom":"0xc36919406572730518285284f2293101104140c0d42c4a786c892467868a8806f40159d29988002870403902413a1d04321320308da2e845438429e0012a00b419d8ccc8584a1c28f82a415d04eab8a5ae75c00d07761acf233414c08b6d9b571c06156086c70ea5186e9b989b0c2d55c0213c936805cd2ab331589c90194d070c00867549b1e1be14cb24500b0386cd901197c1ef5a00da453234fa48f3003dcaa894e3111c22b80e17f7d4388385a10720cda1140c0400f9e084ca34fc4870fb16b472340a2a6a63115a82522f506c06c2675080508834828c63defd06bc2331b4aa708906a06a560457b114248041e40179ebc05c6846c1e922125982f427",
1157 "miner":"0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5",
1158 "mixHash":"0x4c068e902990f21f92a2456fc75c59bec8be03b7f13682b6ebd27da56269beb5",
1159 "nonce":"0x0000000000000000",
1160 "number":"0x128c6df",
1161 "parentBeaconBlockRoot":"0x2843cb9f7d001bd58816a915e685ed96a555c9aeec1217736bd83a96ebd409cc",
1162 "parentHash":"0x90926e0298d418181bd20c23b332451e35fd7d696b5dcdc5a3a0a6b715f4c717",
1163 "receiptsRoot":"0xd43aa19ecb03571d1b86d89d9bb980139d32f2f2ba59646cd5c1de9e80c68c90",
1164 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
1165 "size":"0xdcc3",
1166 "stateRoot":"0x707875120a7103621fb4131df59904cda39de948dfda9084a1e3da44594d5404",
1167 "timestamp":"0x65f5f4c3",
1168 "transactionsRoot":"0x889a1c26dc42ba829dab552b779620feac231cde8a6c79af022bdc605c23a780",
1169 "uncles": ["0x123a1c26dc42ba829dab552b779620feac231cde8a6c79af022bdc605c23a780", "0x489a1c26dc42ba829dab552b779620feac231cde8a6c79af022bdc605c23a780"],
1170 "withdrawals":[
1171 {
1172 "index":"0x24d80e6",
1173 "validatorIndex":"0x8b2b6",
1174 "address":"0x7cd1122e8e118b12ece8d25480dfeef230da17ff",
1175 "amount":"0x1161f10"
1176 }
1177 ],
1178 "withdrawalsRoot":"0x360c33f20eeed5efbc7d08be46e58f8440af5db503e40908ef3d1eb314856ef7"
1179 }"#;
1180
1181 let block = serde_json::from_str::<Block>(s).unwrap();
1182 assert_eq!(block.uncles.len(), 2);
1183 let serialized = serde_json::to_string(&block).unwrap();
1184 let block2 = serde_json::from_str::<Block>(&serialized).unwrap();
1185 assert_eq!(block, block2);
1186 }
1187
1188 #[test]
1189 #[cfg(feature = "serde")]
1190 fn serde_empty_block() {
1191 let s = r#"{
1192 "hash": "0xb25d0e54ca0104e3ebfb5a1dcdf9528140854d609886a300946fd6750dcb19f4",
1193 "parentHash": "0x9400ec9ef59689c157ac89eeed906f15ddd768f94e1575e0e27d37c241439a5d",
1194 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
1195 "miner": "0x829bd824b016326a401d083b33d092293333a830",
1196 "stateRoot": "0x546e330050c66d02923e7f1f3e925efaf64e4384eeecf2288f40088714a77a84",
1197 "transactionsRoot": "0xd5eb3ad6d7c7a4798cc5fb14a6820073f44a941107c5d79dac60bd16325631fe",
1198 "receiptsRoot": "0xb21c41cbb3439c5af25304e1405524c885e733b16203221900cb7f4b387b62f0",
1199 "logsBloom": "0x1f304e641097eafae088627298685d20202004a4a59e4d8900914724e2402b028c9d596660581f361240816e82d00fa14250c9ca89840887a381efa600288283d170010ab0b2a0694c81842c2482457e0eb77c2c02554614007f42aaf3b4dc15d006a83522c86a240c06d241013258d90540c3008888d576a02c10120808520a2221110f4805200302624d22092b2c0e94e849b1e1aa80bc4cc3206f00b249d0a603ee4310216850e47c8997a20aa81fe95040a49ca5a420464600e008351d161dc00d620970b6a801535c218d0b4116099292000c08001943a225d6485528828110645b8244625a182c1a88a41087e6d039b000a180d04300d0680700a15794",
1200 "difficulty": "0xc40faff9c737d",
1201 "number": "0xa9a230",
1202 "gasLimit": "0xbe5a66",
1203 "gasUsed": "0xbe0fcc",
1204 "timestamp": "0x5f93b749",
1205 "totalDifficulty": "0x3dc957fd8167fb2684a",
1206 "extraData": "0x7070796520e4b883e5bda9e7a59ee4bb99e9b1bc0103",
1207 "mixHash": "0xd5e2b7b71fbe4ddfe552fb2377bf7cddb16bbb7e185806036cee86994c6e97fc",
1208 "nonce": "0x4722f2acd35abe0f",
1209 "uncles": [],
1210 "transactions": [],
1211 "size": "0xaeb6"
1212}"#;
1213
1214 let block = serde_json::from_str::<Block>(s).unwrap();
1215 assert!(block.transactions.is_empty());
1216 assert!(block.transactions.as_transactions().is_some());
1217 }
1218
1219 #[test]
1220 #[cfg(feature = "serde")]
1221 fn recompute_block_hash() {
1222 let s = r#"{
1223 "hash": "0xb25d0e54ca0104e3ebfb5a1dcdf9528140854d609886a300946fd6750dcb19f4",
1224 "parentHash": "0x9400ec9ef59689c157ac89eeed906f15ddd768f94e1575e0e27d37c241439a5d",
1225 "sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
1226 "miner": "0x829bd824b016326a401d083b33d092293333a830",
1227 "stateRoot": "0x546e330050c66d02923e7f1f3e925efaf64e4384eeecf2288f40088714a77a84",
1228 "transactionsRoot": "0xd5eb3ad6d7c7a4798cc5fb14a6820073f44a941107c5d79dac60bd16325631fe",
1229 "receiptsRoot": "0xb21c41cbb3439c5af25304e1405524c885e733b16203221900cb7f4b387b62f0",
1230 "logsBloom": "0x1f304e641097eafae088627298685d20202004a4a59e4d8900914724e2402b028c9d596660581f361240816e82d00fa14250c9ca89840887a381efa600288283d170010ab0b2a0694c81842c2482457e0eb77c2c02554614007f42aaf3b4dc15d006a83522c86a240c06d241013258d90540c3008888d576a02c10120808520a2221110f4805200302624d22092b2c0e94e849b1e1aa80bc4cc3206f00b249d0a603ee4310216850e47c8997a20aa81fe95040a49ca5a420464600e008351d161dc00d620970b6a801535c218d0b4116099292000c08001943a225d6485528828110645b8244625a182c1a88a41087e6d039b000a180d04300d0680700a15794",
1231 "difficulty": "0xc40faff9c737d",
1232 "number": "0xa9a230",
1233 "gasLimit": "0xbe5a66",
1234 "gasUsed": "0xbe0fcc",
1235 "timestamp": "0x5f93b749",
1236 "totalDifficulty": "0x3dc957fd8167fb2684a",
1237 "extraData": "0x7070796520e4b883e5bda9e7a59ee4bb99e9b1bc0103",
1238 "mixHash": "0xd5e2b7b71fbe4ddfe552fb2377bf7cddb16bbb7e185806036cee86994c6e97fc",
1239 "nonce": "0x4722f2acd35abe0f",
1240 "uncles": [],
1241 "transactions": [],
1242 "size": "0xaeb6"
1243}"#;
1244 let block = serde_json::from_str::<Block>(s).unwrap();
1245 let recomputed_hash = keccak256(alloy_rlp::encode(&block.header.inner));
1246 assert_eq!(recomputed_hash, block.header.hash);
1247
1248 let s2 = r#"{
1249 "baseFeePerGas":"0x886b221ad",
1250 "blobGasUsed":"0x0",
1251 "difficulty":"0x0",
1252 "excessBlobGas":"0x0",
1253 "extraData":"0x6265617665726275696c642e6f7267",
1254 "gasLimit":"0x1c9c380",
1255 "gasUsed":"0xb0033c",
1256 "hash":"0x85cdcbe36217fd57bf2c33731d8460657a7ce512401f49c9f6392c82a7ccf7ac",
1257 "logsBloom":"0xc36919406572730518285284f2293101104140c0d42c4a786c892467868a8806f40159d29988002870403902413a1d04321320308da2e845438429e0012a00b419d8ccc8584a1c28f82a415d04eab8a5ae75c00d07761acf233414c08b6d9b571c06156086c70ea5186e9b989b0c2d55c0213c936805cd2ab331589c90194d070c00867549b1e1be14cb24500b0386cd901197c1ef5a00da453234fa48f3003dcaa894e3111c22b80e17f7d4388385a10720cda1140c0400f9e084ca34fc4870fb16b472340a2a6a63115a82522f506c06c2675080508834828c63defd06bc2331b4aa708906a06a560457b114248041e40179ebc05c6846c1e922125982f427",
1258 "miner":"0x95222290dd7278aa3ddd389cc1e1d165cc4bafe5",
1259 "mixHash":"0x4c068e902990f21f92a2456fc75c59bec8be03b7f13682b6ebd27da56269beb5",
1260 "nonce":"0x0000000000000000",
1261 "number":"0x128c6df",
1262 "parentBeaconBlockRoot":"0x2843cb9f7d001bd58816a915e685ed96a555c9aeec1217736bd83a96ebd409cc",
1263 "parentHash":"0x90926e0298d418181bd20c23b332451e35fd7d696b5dcdc5a3a0a6b715f4c717",
1264 "receiptsRoot":"0xd43aa19ecb03571d1b86d89d9bb980139d32f2f2ba59646cd5c1de9e80c68c90",
1265 "sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
1266 "size":"0xdcc3",
1267 "stateRoot":"0x707875120a7103621fb4131df59904cda39de948dfda9084a1e3da44594d5404",
1268 "timestamp":"0x65f5f4c3",
1269 "transactionsRoot":"0x889a1c26dc42ba829dab552b779620feac231cde8a6c79af022bdc605c23a780",
1270 "withdrawals":[
1271 {
1272 "index":"0x24d80e6",
1273 "validatorIndex":"0x8b2b6",
1274 "address":"0x7cd1122e8e118b12ece8d25480dfeef230da17ff",
1275 "amount":"0x1161f10"
1276 }
1277 ],
1278 "withdrawalsRoot":"0x360c33f20eeed5efbc7d08be46e58f8440af5db503e40908ef3d1eb314856ef7"
1279 }"#;
1280 let block2 = serde_json::from_str::<Block>(s2).unwrap();
1281 let recomputed_hash = keccak256(alloy_rlp::encode(&block2.header.inner));
1282 assert_eq!(recomputed_hash, block2.header.hash);
1283 }
1284
1285 #[test]
1286 fn header_roundtrip_conversion() {
1287 let rpc_header = Header {
1289 hash: B256::with_last_byte(1),
1290 inner: alloy_consensus::Header {
1291 parent_hash: B256::with_last_byte(2),
1292 ommers_hash: B256::with_last_byte(3),
1293 beneficiary: Address::with_last_byte(4),
1294 state_root: B256::with_last_byte(5),
1295 transactions_root: B256::with_last_byte(6),
1296 receipts_root: B256::with_last_byte(7),
1297 withdrawals_root: None,
1298 number: 9,
1299 gas_used: 10,
1300 gas_limit: 11,
1301 extra_data: vec![1, 2, 3].into(),
1302 logs_bloom: Bloom::default(),
1303 timestamp: 12,
1304 difficulty: U256::from(13),
1305 mix_hash: B256::with_last_byte(14),
1306 nonce: B64::with_last_byte(15),
1307 base_fee_per_gas: Some(20),
1308 blob_gas_used: None,
1309 excess_blob_gas: None,
1310 parent_beacon_block_root: None,
1311 requests_hash: None,
1312 block_access_list_hash: None,
1313 slot_number: None,
1314 },
1315 size: None,
1316 total_difficulty: None,
1317 };
1318
1319 let primitive_header = rpc_header.inner.clone();
1321
1322 let sealed_header: Sealed<alloy_consensus::Header> =
1324 primitive_header.seal(B256::with_last_byte(1));
1325
1326 let roundtrip_rpc_header = Header::from_consensus(sealed_header, None, None);
1328
1329 assert_eq!(rpc_header, roundtrip_rpc_header);
1331 }
1332
1333 #[test]
1334 fn test_consensus_header_to_rpc_block() {
1335 let header = Header {
1337 hash: B256::with_last_byte(1),
1338 inner: alloy_consensus::Header {
1339 parent_hash: B256::with_last_byte(2),
1340 ommers_hash: B256::with_last_byte(3),
1341 beneficiary: Address::with_last_byte(4),
1342 state_root: B256::with_last_byte(5),
1343 transactions_root: B256::with_last_byte(6),
1344 receipts_root: B256::with_last_byte(7),
1345 withdrawals_root: None,
1346 number: 9,
1347 gas_used: 10,
1348 gas_limit: 11,
1349 extra_data: vec![1, 2, 3].into(),
1350 logs_bloom: Bloom::default(),
1351 timestamp: 12,
1352 difficulty: U256::from(13),
1353 mix_hash: B256::with_last_byte(14),
1354 nonce: B64::with_last_byte(15),
1355 base_fee_per_gas: Some(20),
1356 blob_gas_used: None,
1357 excess_blob_gas: None,
1358 parent_beacon_block_root: None,
1359 requests_hash: None,
1360 block_access_list_hash: None,
1361 slot_number: None,
1362 },
1363 total_difficulty: None,
1364 size: Some(U256::from(505)),
1365 };
1366
1367 let primitive_header = header.inner.clone();
1369
1370 let block: Block<Transaction> = Block::uncle_from_header(primitive_header);
1372
1373 assert_eq!(
1375 block,
1376 Block {
1377 header: Header {
1378 hash: B256::from(hex!(
1379 "379bd1414cf69a9b86fb4e0e6b05a2e4b14cb3d5af057e13ccdc2192cb9780b2"
1380 )),
1381 ..header
1382 },
1383 uncles: vec![],
1384 transactions: BlockTransactions::Uncle,
1385 withdrawals: None,
1386 }
1387 );
1388 }
1389
1390 #[test]
1391 #[cfg(feature = "serde")]
1392 fn serde_bad_block() {
1393 use alloy_primitives::B64;
1394
1395 let block = Block {
1396 header: Header {
1397 hash: B256::with_last_byte(1),
1398 inner: alloy_consensus::Header {
1399 parent_hash: B256::with_last_byte(2),
1400 ommers_hash: B256::with_last_byte(3),
1401 beneficiary: Address::with_last_byte(4),
1402 state_root: B256::with_last_byte(5),
1403 transactions_root: B256::with_last_byte(6),
1404 receipts_root: B256::with_last_byte(7),
1405 withdrawals_root: Some(B256::with_last_byte(8)),
1406 number: 9,
1407 gas_used: 10,
1408 gas_limit: 11,
1409 extra_data: vec![1, 2, 3].into(),
1410 logs_bloom: Default::default(),
1411 timestamp: 12,
1412 difficulty: U256::from(13),
1413 mix_hash: B256::with_last_byte(14),
1414 nonce: B64::with_last_byte(15),
1415 base_fee_per_gas: Some(20),
1416 blob_gas_used: None,
1417 excess_blob_gas: None,
1418 parent_beacon_block_root: None,
1419 requests_hash: None,
1420 block_access_list_hash: None,
1421 slot_number: None,
1422 },
1423 total_difficulty: Some(U256::from(100000)),
1424 size: Some(U256::from(19)),
1425 },
1426 uncles: vec![B256::with_last_byte(17)],
1427 transactions: vec![B256::with_last_byte(18)].into(),
1428 withdrawals: Some(Default::default()),
1429 };
1430 let hash = block.header.hash;
1431 let rlp = Bytes::from("header");
1432
1433 let bad_block = BadBlock { block, hash, rlp };
1434
1435 let serialized = serde_json::to_string(&bad_block).unwrap();
1436 assert_eq!(
1437 serialized,
1438 r#"{"block":{"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000002","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000003","miner":"0x0000000000000000000000000000000000000004","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000005","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000006","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000007","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","difficulty":"0xd","number":"0x9","gasLimit":"0xb","gasUsed":"0xa","timestamp":"0xc","extraData":"0x010203","mixHash":"0x000000000000000000000000000000000000000000000000000000000000000e","nonce":"0x000000000000000f","baseFeePerGas":"0x14","withdrawalsRoot":"0x0000000000000000000000000000000000000000000000000000000000000008","totalDifficulty":"0x186a0","size":"0x13","uncles":["0x0000000000000000000000000000000000000000000000000000000000000011"],"transactions":["0x0000000000000000000000000000000000000000000000000000000000000012"],"withdrawals":[]},"hash":"0x0000000000000000000000000000000000000000000000000000000000000001","rlp":"0x686561646572"}"#
1439 );
1440
1441 let deserialized: BadBlock = serde_json::from_str(&serialized).unwrap();
1442 similar_asserts::assert_eq!(bad_block, deserialized);
1443 }
1444
1445 #[test]
1447 #[cfg(feature = "serde")]
1448 fn deserde_tenderly_block() {
1449 let s = include_str!("../testdata/tenderly.sepolia.json");
1450 let _block: Block = serde_json::from_str(s).unwrap();
1451 }
1452}