1use crate::{
14 common::{
15 compression::{snappy_compress, snappy_decompress, SnappyRlpCodec},
16 decode::DecodeCompressedRlp,
17 },
18 e2s::{error::E2sError, types::Entry},
19};
20use alloy_consensus::{Block, BlockBody, Eip658Value, Header, TxType};
21use alloy_primitives::{Log, B256, U256};
22use alloy_rlp::{Decodable, Encodable, RlpDecodable, RlpEncodable};
23use sha2::{Digest, Sha256};
24
25pub const COMPRESSED_HEADER: [u8; 2] = [0x03, 0x00];
28
29pub const COMPRESSED_BODY: [u8; 2] = [0x04, 0x00];
31
32pub const COMPRESSED_SLIM_RECEIPTS: [u8; 2] = [0x0a, 0x00];
35
36pub const PROOF: [u8; 2] = [0x0b, 0x00];
39
40pub const TOTAL_DIFFICULTY: [u8; 2] = [0x06, 0x00];
42
43pub const ACCUMULATOR: [u8; 2] = [0x07, 0x00];
45
46pub const MAX_BLOCKS_PER_ERE: usize = crate::common::MAX_ENTRIES_PER_ERA as usize;
48
49#[derive(Debug, Clone)]
51pub struct CompressedHeader {
52 pub data: Vec<u8>,
54}
55
56impl CompressedHeader {
57 pub const fn new(data: Vec<u8>) -> Self {
59 Self { data }
60 }
61
62 pub fn from_rlp(rlp_data: &[u8]) -> Result<Self, E2sError> {
64 Ok(Self { data: snappy_compress(rlp_data)? })
65 }
66
67 pub fn decompress(&self) -> Result<Vec<u8>, E2sError> {
69 snappy_decompress(&self.data)
70 }
71
72 pub fn to_entry(&self) -> Entry {
74 Entry::new(COMPRESSED_HEADER, self.data.clone())
75 }
76
77 pub fn from_entry(entry: &Entry) -> Result<Self, E2sError> {
79 entry.ensure_type(COMPRESSED_HEADER, "CompressedHeader")?;
80 Ok(Self { data: entry.data.clone() })
81 }
82
83 pub fn decode_header(&self) -> Result<Header, E2sError> {
85 self.decode()
86 }
87
88 pub fn from_header<H: Encodable>(header: &H) -> Result<Self, E2sError> {
90 let encoder = SnappyRlpCodec::new();
91 let compressed = encoder.encode(header)?;
92 Ok(Self::new(compressed))
93 }
94}
95
96impl DecodeCompressedRlp for CompressedHeader {
97 fn decode<T: Decodable>(&self) -> Result<T, E2sError> {
98 let decoder = SnappyRlpCodec::<T>::new();
99 decoder.decode(&self.data)
100 }
101}
102
103#[derive(Debug, Clone)]
105pub struct CompressedBody {
106 pub data: Vec<u8>,
108}
109
110impl CompressedBody {
111 pub const fn new(data: Vec<u8>) -> Self {
113 Self { data }
114 }
115
116 pub fn from_rlp(rlp_data: &[u8]) -> Result<Self, E2sError> {
118 Ok(Self { data: snappy_compress(rlp_data)? })
119 }
120
121 pub fn decompress(&self) -> Result<Vec<u8>, E2sError> {
123 snappy_decompress(&self.data)
124 }
125
126 pub fn to_entry(&self) -> Entry {
128 Entry::new(COMPRESSED_BODY, self.data.clone())
129 }
130
131 pub fn from_entry(entry: &Entry) -> Result<Self, E2sError> {
133 entry.ensure_type(COMPRESSED_BODY, "CompressedBody")?;
134 Ok(Self { data: entry.data.clone() })
135 }
136
137 pub fn decode_body<T: Decodable, H: Decodable>(&self) -> Result<BlockBody<T, H>, E2sError> {
139 let decompressed = self.decompress()?;
140 Self::decode_body_from_decompressed(&decompressed)
141 }
142
143 pub fn decode_body_from_decompressed<T: Decodable, H: Decodable>(
145 data: &[u8],
146 ) -> Result<BlockBody<T, H>, E2sError> {
147 alloy_rlp::decode_exact::<BlockBody<T, H>>(data)
148 .map_err(|e| E2sError::Rlp(format!("Failed to decode RLP data: {e}")))
149 }
150
151 pub fn from_body<B: Encodable>(body: &B) -> Result<Self, E2sError> {
153 let encoder = SnappyRlpCodec::new();
154 let compressed = encoder.encode(body)?;
155 Ok(Self::new(compressed))
156 }
157}
158
159impl DecodeCompressedRlp for CompressedBody {
160 fn decode<T: Decodable>(&self) -> Result<T, E2sError> {
161 let decoder = SnappyRlpCodec::<T>::new();
162 decoder.decode(&self.data)
163 }
164}
165
166#[derive(Debug, Clone)]
171pub struct CompressedSlimReceipts {
172 pub data: Vec<u8>,
174}
175
176impl CompressedSlimReceipts {
177 pub const fn new(data: Vec<u8>) -> Self {
179 Self { data }
180 }
181
182 pub fn from_rlp(rlp_data: &[u8]) -> Result<Self, E2sError> {
184 Ok(Self { data: snappy_compress(rlp_data)? })
185 }
186
187 pub fn decompress(&self) -> Result<Vec<u8>, E2sError> {
189 snappy_decompress(&self.data)
190 }
191
192 pub fn to_entry(&self) -> Entry {
194 Entry::new(COMPRESSED_SLIM_RECEIPTS, self.data.clone())
195 }
196
197 pub fn from_entry(entry: &Entry) -> Result<Self, E2sError> {
199 entry.ensure_type(COMPRESSED_SLIM_RECEIPTS, "CompressedSlimReceipts")?;
200 Ok(Self { data: entry.data.clone() })
201 }
202
203 pub fn decode<T: Decodable>(&self) -> Result<T, E2sError> {
205 let decoder = SnappyRlpCodec::<T>::new();
206 decoder.decode(&self.data)
207 }
208
209 pub fn from_encodable<T: Encodable>(data: &T) -> Result<Self, E2sError> {
211 let encoder = SnappyRlpCodec::<T>::new();
212 let compressed = encoder.encode(data)?;
213 Ok(Self::new(compressed))
214 }
215
216 pub fn from_encodable_list<T: Encodable>(receipts: &[T]) -> Result<Self, E2sError> {
218 let mut rlp_data = Vec::new();
219 alloy_rlp::encode_list(receipts, &mut rlp_data);
220 Self::from_rlp(&rlp_data)
221 }
222
223 pub fn from_receipts(receipts: &[SlimReceipt]) -> Result<Self, E2sError> {
228 Self::from_encodable_list(receipts)
229 }
230
231 pub fn decode_receipts(&self) -> Result<Vec<SlimReceipt>, E2sError> {
233 self.decode()
234 }
235}
236
237impl DecodeCompressedRlp for CompressedSlimReceipts {
238 fn decode<T: Decodable>(&self) -> Result<T, E2sError> {
239 let decoder = SnappyRlpCodec::<T>::new();
240 decoder.decode(&self.data)
241 }
242}
243
244#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
252pub struct SlimReceipt {
253 pub tx_type: TxType,
255 pub status: Eip658Value,
257 pub cumulative_gas_used: u64,
259 pub logs: Vec<Log>,
261}
262
263#[derive(Debug, Clone, Copy, PartialEq, Eq)]
267#[repr(u8)]
268pub enum ProofType {
269 BlockProofHistoricalHashesAccumulator = 0,
271 BlockProofHistoricalRoots = 1,
273 BlockProofHistoricalSummariesCapella = 2,
275 BlockProofHistoricalSummariesDeneb = 3,
277}
278
279impl ProofType {
280 pub const fn from_byte(value: u8) -> Option<Self> {
282 match value {
283 0 => Some(Self::BlockProofHistoricalHashesAccumulator),
284 1 => Some(Self::BlockProofHistoricalRoots),
285 2 => Some(Self::BlockProofHistoricalSummariesCapella),
286 3 => Some(Self::BlockProofHistoricalSummariesDeneb),
287 _ => None,
288 }
289 }
290
291 pub const fn as_byte(self) -> u8 {
293 self as u8
294 }
295}
296
297#[derive(Debug, Clone)]
303pub struct Proof {
304 pub data: Vec<u8>,
306}
307
308impl Proof {
309 pub const fn new(data: Vec<u8>) -> Self {
311 Self { data }
312 }
313
314 pub fn encode(proof_type: ProofType, ssz_proof: &[u8]) -> Result<Self, E2sError> {
316 let mut payload = Vec::new();
319 proof_type.as_byte().encode(&mut payload);
320 ssz_proof.encode(&mut payload);
321
322 let mut rlp_data = Vec::new();
323 alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut rlp_data);
324 rlp_data.extend_from_slice(&payload);
325
326 Ok(Self { data: snappy_compress(&rlp_data)? })
327 }
328
329 pub fn decode(&self) -> Result<(ProofType, Vec<u8>), E2sError> {
331 let decompressed = snappy_decompress(&self.data)?;
332
333 let mut buf = decompressed.as_slice();
334 let header = alloy_rlp::Header::decode(&mut buf)
335 .map_err(|e| E2sError::Rlp(format!("Failed to decode proof RLP header: {e}")))?;
336 if !header.list {
337 return Err(E2sError::Rlp("Expected RLP list for Proof entry".to_string()));
338 }
339
340 if buf.len() != header.payload_length {
344 return Err(E2sError::Rlp(format!(
345 "Trailing bytes after Proof list: {} byte(s) beyond the list payload",
346 buf.len().saturating_sub(header.payload_length)
347 )));
348 }
349 let mut payload = &buf[..header.payload_length];
350
351 let proof_type_byte = u8::decode(&mut payload)
352 .map_err(|e| E2sError::Rlp(format!("Failed to decode proof type: {e}")))?;
353 let proof_type = ProofType::from_byte(proof_type_byte)
354 .ok_or_else(|| E2sError::Rlp(format!("Unknown proof type: {proof_type_byte}")))?;
355
356 let ssz_bytes = alloy_primitives::Bytes::decode(&mut payload)
357 .map_err(|e| E2sError::Rlp(format!("Failed to decode proof SSZ bytes: {e}")))?;
358
359 if !payload.is_empty() {
360 return Err(E2sError::Rlp("Unexpected extra items in Proof list".to_string()));
361 }
362
363 Ok((proof_type, ssz_bytes.to_vec()))
364 }
365
366 pub fn to_entry(&self) -> Entry {
368 Entry::new(PROOF, self.data.clone())
369 }
370
371 pub fn from_entry(entry: &Entry) -> Result<Self, E2sError> {
373 entry.ensure_type(PROOF, "Proof")?;
374 Ok(Self { data: entry.data.clone() })
375 }
376}
377
378#[derive(Debug, Clone)]
380pub struct TotalDifficulty {
381 pub value: U256,
383}
384
385impl TotalDifficulty {
386 pub const fn new(value: U256) -> Self {
388 Self { value }
389 }
390
391 pub fn to_entry(&self) -> Entry {
393 let data = self.value.to_le_bytes::<32>().to_vec();
395 Entry::new(TOTAL_DIFFICULTY, data)
396 }
397
398 pub fn from_entry(entry: &Entry) -> Result<Self, E2sError> {
400 entry.ensure_type(TOTAL_DIFFICULTY, "TotalDifficulty")?;
401
402 if entry.data.len() != 32 {
403 return Err(E2sError::Ssz(format!(
404 "Invalid data length for TotalDifficulty: expected 32, got {}",
405 entry.data.len()
406 )));
407 }
408
409 let value = U256::from_le_slice(&entry.data);
411
412 Ok(Self { value })
413 }
414}
415
416#[derive(Debug, Clone, PartialEq, Eq)]
419pub struct Accumulator {
420 pub root: B256,
422}
423
424impl Accumulator {
425 pub const fn new(root: B256) -> Self {
427 Self { root }
428 }
429
430 pub fn to_entry(&self) -> Entry {
432 Entry::new(ACCUMULATOR, self.root.to_vec())
433 }
434
435 pub fn from_entry(entry: &Entry) -> Result<Self, E2sError> {
437 entry.ensure_type(ACCUMULATOR, "Accumulator")?;
438
439 if entry.data.len() != 32 {
440 return Err(E2sError::Ssz(format!(
441 "Invalid data length for Accumulator: expected 32, got {}",
442 entry.data.len()
443 )));
444 }
445
446 let mut root = [0u8; 32];
447 root.copy_from_slice(&entry.data);
448
449 Ok(Self { root: B256::from(root) })
450 }
451
452 pub fn from_header_records(records: &[HeaderRecord]) -> Result<Self, E2sError> {
462 let capacity = MAX_BLOCKS_PER_ERE;
463
464 if records.len() > capacity {
465 return Err(E2sError::Ssz(format!(
466 "Too many header records: got {}, max {}",
467 records.len(),
468 capacity
469 )));
470 }
471
472 let mut leaves = Vec::with_capacity(capacity);
474 for record in records {
475 let mut data = [0u8; 64];
476 data[..32].copy_from_slice(record.block_hash.as_slice());
477 data[32..].copy_from_slice(&record.total_difficulty.to_le_bytes::<32>());
478 leaves.push(<[u8; 32]>::from(Sha256::digest(data)));
479 }
480
481 leaves.resize(capacity, [0u8; 32]);
483
484 while leaves.len() > 1 {
486 let mut next_level = Vec::with_capacity(leaves.len() / 2);
487 for pair in leaves.chunks_exact(2) {
488 let mut data = [0u8; 64];
489 data[..32].copy_from_slice(&pair[0]);
490 data[32..].copy_from_slice(&pair[1]);
491 next_level.push(<[u8; 32]>::from(Sha256::digest(data)));
492 }
493 leaves = next_level;
494 }
495
496 let merkle_root = leaves[0];
497
498 let mut mix = [0u8; 64];
500 mix[..32].copy_from_slice(&merkle_root);
501 let length = records.len() as u64;
502 mix[32..40].copy_from_slice(&length.to_le_bytes());
503 Ok(Self { root: B256::from(<[u8; 32]>::from(Sha256::digest(mix))) })
506 }
507}
508
509#[derive(Debug, Clone)]
518pub struct HeaderRecord {
519 pub block_hash: B256,
522 pub total_difficulty: U256,
525}
526
527#[derive(Debug, Clone)]
536pub struct BlockTuple {
537 pub header: CompressedHeader,
539
540 pub body: CompressedBody,
542
543 pub receipts: Option<CompressedSlimReceipts>,
545
546 pub total_difficulty: Option<TotalDifficulty>,
548
549 pub proof: Option<Proof>,
551}
552
553impl BlockTuple {
554 pub const fn new(header: CompressedHeader, body: CompressedBody) -> Self {
560 Self { header, body, receipts: None, total_difficulty: None, proof: None }
561 }
562
563 pub fn with_receipts(mut self, receipts: CompressedSlimReceipts) -> Self {
565 self.receipts = Some(receipts);
566 self
567 }
568
569 pub const fn with_total_difficulty(mut self, total_difficulty: TotalDifficulty) -> Self {
571 self.total_difficulty = Some(total_difficulty);
572 self
573 }
574
575 pub fn with_proof(mut self, proof: Proof) -> Self {
577 self.proof = Some(proof);
578 self
579 }
580
581 pub const fn component_count(&self) -> u64 {
584 2 + self.receipts.is_some() as u64 +
585 self.total_difficulty.is_some() as u64 +
586 self.proof.is_some() as u64
587 }
588
589 pub fn to_alloy_block<T: Decodable>(&self) -> Result<Block<T>, E2sError> {
591 let header: Header = self.header.decode()?;
592 let body: BlockBody<T> = self.body.decode()?;
593
594 Ok(Block::new(header, body))
595 }
596
597 pub fn from_alloy_block<T: Encodable, R: Encodable>(
599 block: &Block<T>,
600 receipts: &R,
601 total_difficulty: U256,
602 ) -> Result<Self, E2sError> {
603 let header = CompressedHeader::from_header(&block.header)?;
604 let body = CompressedBody::from_body(&block.body)?;
605
606 let compressed_receipts = CompressedSlimReceipts::from_encodable(receipts)?;
607
608 let difficulty = TotalDifficulty::new(total_difficulty);
609
610 Ok(Self::new(header, body)
611 .with_receipts(compressed_receipts)
612 .with_total_difficulty(difficulty))
613 }
614}
615
616#[cfg(test)]
617mod tests {
618 use super::*;
619 use crate::test_utils::{create_header, create_test_receipt, create_test_receipts};
620 use alloy_eips::eip4895::Withdrawals;
621 use alloy_primitives::{Bytes, U256};
622 use reth_ethereum_primitives::{Receipt, TxType};
623
624 #[test]
625 fn test_header_conversion_roundtrip() {
626 let header = create_header();
627
628 let compressed_header = CompressedHeader::from_header(&header).unwrap();
629
630 let decoded_header = compressed_header.decode_header().unwrap();
631
632 assert_eq!(header.number, decoded_header.number);
633 assert_eq!(header.difficulty, decoded_header.difficulty);
634 assert_eq!(header.timestamp, decoded_header.timestamp);
635 assert_eq!(header.gas_used, decoded_header.gas_used);
636 assert_eq!(header.parent_hash, decoded_header.parent_hash);
637 assert_eq!(header.base_fee_per_gas, decoded_header.base_fee_per_gas);
638 }
639
640 #[test]
641 fn test_block_body_conversion() {
642 let block_body: BlockBody<Bytes> =
643 BlockBody { transactions: vec![], ommers: vec![], withdrawals: None };
644
645 let compressed_body = CompressedBody::from_body(&block_body).unwrap();
646
647 let decoded_body: BlockBody<Bytes> = compressed_body.decode_body().unwrap();
648
649 assert_eq!(decoded_body.transactions.len(), 0);
650 assert_eq!(decoded_body.ommers.len(), 0);
651 assert_eq!(decoded_body.withdrawals, None);
652 }
653
654 #[test]
655 fn test_total_difficulty_roundtrip() {
656 let value = U256::from(123456789u64);
657
658 let total_difficulty = TotalDifficulty::new(value);
659
660 let entry = total_difficulty.to_entry();
661
662 assert_eq!(entry.entry_type, TOTAL_DIFFICULTY);
663
664 let recovered = TotalDifficulty::from_entry(&entry).unwrap();
665
666 assert_eq!(recovered.value, value);
667 }
668
669 #[test]
670 fn test_total_difficulty_ssz_le_encoding() {
671 let value = U256::from(1u64);
674 let td = TotalDifficulty::new(value);
675 let entry = td.to_entry();
676
677 assert_eq!(entry.data[0], 1, "First byte must be 1 (little-endian)");
679 assert_eq!(entry.data[31], 0, "Last byte must be 0 (little-endian)");
680 }
681
682 #[test]
683 fn test_compression_roundtrip() {
684 let rlp_data = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
685
686 let compressed_header = CompressedHeader::from_rlp(&rlp_data).unwrap();
688 let decompressed = compressed_header.decompress().unwrap();
689 assert_eq!(decompressed, rlp_data);
690
691 let compressed_body = CompressedBody::from_rlp(&rlp_data).unwrap();
693 let decompressed = compressed_body.decompress().unwrap();
694 assert_eq!(decompressed, rlp_data);
695
696 let compressed_receipts = CompressedSlimReceipts::from_rlp(&rlp_data).unwrap();
698 let decompressed = compressed_receipts.decompress().unwrap();
699 assert_eq!(decompressed, rlp_data);
700 }
701
702 #[test]
703 fn test_block_tuple_with_data() {
704 let header = create_header();
706
707 let transactions = vec![Bytes::from(vec![1, 2, 3, 4]), Bytes::from(vec![5, 6, 7, 8])];
708
709 let withdrawals = Some(Withdrawals(vec![]));
710
711 let block_body = BlockBody { transactions, ommers: vec![], withdrawals };
712
713 let block = Block::new(header, block_body);
714
715 let receipts: Vec<u8> = Vec::new();
716
717 let block_tuple =
718 BlockTuple::from_alloy_block(&block, &receipts, U256::from(123456u64)).unwrap();
719
720 let decoded_block: Block<Bytes> = block_tuple.to_alloy_block().unwrap();
722
723 assert_eq!(decoded_block.header.number, 100);
725 assert_eq!(decoded_block.body.transactions.len(), 2);
726 assert_eq!(decoded_block.body.transactions[0], Bytes::from(vec![1, 2, 3, 4]));
727 assert_eq!(decoded_block.body.transactions[1], Bytes::from(vec![5, 6, 7, 8]));
728 assert!(decoded_block.body.withdrawals.is_some());
729 }
730
731 #[test]
732 fn test_block_tuple_component_count() {
733 let base = BlockTuple::new(CompressedHeader::new(vec![1]), CompressedBody::new(vec![2]));
734 assert_eq!(base.component_count(), 2);
736
737 assert_eq!(
739 base.clone().with_receipts(CompressedSlimReceipts::new(vec![3])).component_count(),
740 3
741 );
742 let full = base
743 .with_receipts(CompressedSlimReceipts::new(vec![3]))
744 .with_total_difficulty(TotalDifficulty::new(U256::from(1u64)))
745 .with_proof(Proof::new(vec![4]));
746 assert_eq!(full.component_count(), 5);
747 assert!(full.receipts.is_some() && full.total_difficulty.is_some() && full.proof.is_some());
748 }
749
750 #[test]
751 fn test_single_receipt_compression_roundtrip() {
752 let test_receipt = create_test_receipt(TxType::Eip1559, true, 21000, 2);
753
754 let compressed_receipts = CompressedSlimReceipts::from_encodable(&test_receipt)
756 .expect("Failed to compress receipt");
757
758 assert!(!compressed_receipts.data.is_empty());
760
761 let decoded_receipt: Receipt =
763 compressed_receipts.decode().expect("Failed to decode compressed receipt");
764
765 assert_eq!(decoded_receipt.tx_type, test_receipt.tx_type);
767 assert_eq!(decoded_receipt.success, test_receipt.success);
768 assert_eq!(decoded_receipt.cumulative_gas_used, test_receipt.cumulative_gas_used);
769 assert_eq!(decoded_receipt.logs.len(), test_receipt.logs.len());
770
771 for (original_log, decoded_log) in test_receipt.logs.iter().zip(decoded_receipt.logs.iter())
773 {
774 assert_eq!(decoded_log.address, original_log.address);
775 assert_eq!(decoded_log.data.topics(), original_log.data.topics());
776 }
777 }
778
779 #[test]
780 fn test_slim_receipt_matches_spec_rlp() {
781 let receipt = create_test_receipt(TxType::Eip1559, true, 21000, 2);
785
786 let compressed = CompressedSlimReceipts::from_encodable(&receipt).unwrap();
787 let actual_rlp = compressed.decompress().unwrap();
788
789 let mut fields = Vec::new();
791 (receipt.tx_type as u8).encode(&mut fields);
792 receipt.success.encode(&mut fields);
793 receipt.cumulative_gas_used.encode(&mut fields);
794 receipt.logs.encode(&mut fields);
795 let mut expected = Vec::new();
796 alloy_rlp::Header { list: true, payload_length: fields.len() }.encode(&mut expected);
797 expected.extend_from_slice(&fields);
798
799 assert_eq!(
800 actual_rlp, expected,
801 "slim receipt RLP must be the 4-element list [tx-type, status, cumulative-gas, logs]"
802 );
803 }
804
805 #[test]
806 fn test_slim_receipts_typed_helpers() {
807 let receipts = vec![
810 SlimReceipt {
811 tx_type: TxType::Eip1559,
812 status: Eip658Value::Eip658(true),
813 cumulative_gas_used: 21000,
814 logs: vec![],
815 },
816 SlimReceipt {
817 tx_type: TxType::Legacy,
818 status: Eip658Value::PostState(B256::repeat_byte(0xab)),
819 cumulative_gas_used: 42000,
820 logs: vec![],
821 },
822 ];
823
824 let compressed = CompressedSlimReceipts::from_receipts(&receipts).unwrap();
825 let decoded = compressed.decode_receipts().unwrap();
826
827 assert_eq!(decoded, receipts);
828 }
829
830 #[test]
831 fn test_accumulator_from_header_records_known_vectors() {
832 let expected_empty: B256 =
835 "4a8c3a07c8d23adc5bac61157555c3c784d53d9bc110c1370809bd23cd93777d".parse().unwrap();
836 let expected_single_zero: B256 =
837 "81fd641249670887a731386e756a7a1538dc781b1b0bf016889045d350812817".parse().unwrap();
838 let expected_single_nonzero: B256 =
839 "ada35c48d81117f4fd588554cd4c4752356336e84cb41106dea1ceb4cfac8799".parse().unwrap();
840
841 let acc_empty = Accumulator::from_header_records(&[]).unwrap();
843 assert_eq!(acc_empty.root, expected_empty);
844
845 let records = vec![HeaderRecord { block_hash: B256::ZERO, total_difficulty: U256::ZERO }];
847 let acc = Accumulator::from_header_records(&records).unwrap();
848 assert_eq!(acc.root, expected_single_zero);
849
850 let records2 = vec![HeaderRecord {
852 block_hash: B256::from([1u8; 32]),
853 total_difficulty: U256::from(100u64),
854 }];
855 let acc2 = Accumulator::from_header_records(&records2).unwrap();
856 assert_eq!(acc2.root, expected_single_nonzero);
857 }
858
859 #[test]
860 fn test_accumulator_rejects_oversized_input() {
861 let records = vec![
862 HeaderRecord { block_hash: B256::ZERO, total_difficulty: U256::ZERO };
863 MAX_BLOCKS_PER_ERE + 1
864 ];
865 assert!(Accumulator::from_header_records(&records).is_err());
866 }
867
868 #[test]
869 fn test_proof_type_byte_roundtrip() {
870 for ty in [
871 ProofType::BlockProofHistoricalHashesAccumulator,
872 ProofType::BlockProofHistoricalRoots,
873 ProofType::BlockProofHistoricalSummariesCapella,
874 ProofType::BlockProofHistoricalSummariesDeneb,
875 ] {
876 assert_eq!(ProofType::from_byte(ty.as_byte()), Some(ty));
877 }
878 assert_eq!(ProofType::from_byte(4), None);
879 }
880
881 #[test]
882 fn test_proof_roundtrip() {
883 let ssz_proof = vec![0xab; 64];
884 let proof = Proof::encode(ProofType::BlockProofHistoricalRoots, &ssz_proof).unwrap();
885
886 let entry = proof.to_entry();
888 assert_eq!(entry.entry_type, PROOF);
889 let recovered = Proof::from_entry(&entry).unwrap();
890
891 let (proof_type, ssz_bytes) = recovered.decode().unwrap();
892 assert_eq!(proof_type, ProofType::BlockProofHistoricalRoots);
893 assert_eq!(ssz_bytes, ssz_proof);
894 }
895
896 #[test]
897 fn test_from_entry_rejects_wrong_type() {
898 let entry = Entry::new(COMPRESSED_BODY, vec![1, 2, 3]);
899 assert!(CompressedHeader::from_entry(&entry).is_err());
900 }
901
902 #[test]
903 fn test_decode_rejects_trailing_bytes() {
904 let mut rlp = Vec::new();
907 7u64.encode(&mut rlp);
908 rlp.push(0xff);
909 let compressed = CompressedHeader::from_rlp(&rlp).unwrap();
910 assert!(compressed.decode::<u64>().is_err());
911 }
912
913 #[test]
914 fn test_proof_decode_rejects_trailing_bytes() {
915 let valid = Proof::encode(ProofType::BlockProofHistoricalRoots, &[1, 2, 3]).unwrap();
916 let mut raw = snappy_decompress(&valid.data).unwrap();
917 raw.push(0xff); let tampered = Proof::new(snappy_compress(&raw).unwrap());
919 assert!(tampered.decode().is_err());
920 }
921
922 #[test]
923 fn test_proof_decode_rejects_extra_list_item() {
924 let mut payload = Vec::new();
926 0u8.encode(&mut payload);
927 alloy_primitives::Bytes::from(vec![1, 2, 3]).encode(&mut payload);
928 99u8.encode(&mut payload);
929 let mut rlp = Vec::new();
930 alloy_rlp::Header { list: true, payload_length: payload.len() }.encode(&mut rlp);
931 rlp.extend_from_slice(&payload);
932 let proof = Proof::new(snappy_compress(&rlp).unwrap());
933 assert!(proof.decode().is_err());
934 }
935
936 #[test]
937 fn test_receipt_list_compression() {
938 let receipts = create_test_receipts();
939
940 let compressed_receipts = CompressedSlimReceipts::from_encodable_list(&receipts)
942 .expect("Failed to compress receipt list");
943
944 let decoded_receipts: Vec<Receipt> =
947 compressed_receipts.decode().expect("Failed to decode compressed receipt list");
948
949 assert_eq!(decoded_receipts.len(), receipts.len());
951
952 for (original, decoded) in receipts.iter().zip(decoded_receipts.iter()) {
953 assert_eq!(decoded.tx_type, original.tx_type);
954 assert_eq!(decoded.success, original.success);
955 assert_eq!(decoded.cumulative_gas_used, original.cumulative_gas_used);
956 assert_eq!(decoded.logs.len(), original.logs.len());
957
958 for (original_log, decoded_log) in original.logs.iter().zip(decoded.logs.iter()) {
959 assert_eq!(decoded_log.address, original_log.address);
960 assert_eq!(decoded_log.data.topics(), original_log.data.topics());
961 }
962 }
963 }
964}