Skip to main content

reth_era/ere/types/
execution.rs

1//! Execution layer specific types for `.ere` files
2//!
3//! Contains implementations for compressed execution layer data structures:
4//! - [`CompressedHeader`] - Block header
5//! - [`CompressedBody`] - Block body
6//! - [`CompressedSlimReceipts`] - Block receipts
7//! - [`TotalDifficulty`] - Block total difficulty
8//!
9//! These types use Snappy compression to match the specification.
10//!
11//! See also <https://github.com/eth-clients/e2store-format-specs/blob/main/formats/ere.md>
12
13use 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
25// ERE-specific constants
26/// `CompressedHeader` record type
27pub const COMPRESSED_HEADER: [u8; 2] = [0x03, 0x00];
28
29/// `CompressedBody` record type
30pub const COMPRESSED_BODY: [u8; 2] = [0x04, 0x00];
31
32/// `CompressedSlimReceipts` record type (0x0a00)
33/// Slim receipts exclude bloom filters to optimize storage.
34pub const COMPRESSED_SLIM_RECEIPTS: [u8; 2] = [0x0a, 0x00];
35
36/// `Proof` record type (0x0b00)
37/// Format: `snappyFramed(rlp([proof-type, ssz(proof-object)]))`
38pub const PROOF: [u8; 2] = [0x0b, 0x00];
39
40/// `TotalDifficulty` record type
41pub const TOTAL_DIFFICULTY: [u8; 2] = [0x06, 0x00];
42
43/// `Accumulator` record type
44pub const ACCUMULATOR: [u8; 2] = [0x07, 0x00];
45
46/// Maximum number of blocks in an `ERE` file, limited by accumulator size.
47pub const MAX_BLOCKS_PER_ERE: usize = crate::common::MAX_ENTRIES_PER_ERA as usize;
48
49/// Compressed block header using `snappyFramed(rlp(header))`
50#[derive(Debug, Clone)]
51pub struct CompressedHeader {
52    /// The compressed data
53    pub data: Vec<u8>,
54}
55
56impl CompressedHeader {
57    /// Create a new [`CompressedHeader`] from compressed data
58    pub const fn new(data: Vec<u8>) -> Self {
59        Self { data }
60    }
61
62    /// Create from RLP-encoded header by compressing it with Snappy framed encoding
63    pub fn from_rlp(rlp_data: &[u8]) -> Result<Self, E2sError> {
64        Ok(Self { data: snappy_compress(rlp_data)? })
65    }
66
67    /// Decompress to get the original RLP-encoded header
68    pub fn decompress(&self) -> Result<Vec<u8>, E2sError> {
69        snappy_decompress(&self.data)
70    }
71
72    /// Convert to an [`Entry`]
73    pub fn to_entry(&self) -> Entry {
74        Entry::new(COMPRESSED_HEADER, self.data.clone())
75    }
76
77    /// Create from an [`Entry`]
78    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    /// Decode this compressed header into an `alloy_consensus::Header`
84    pub fn decode_header(&self) -> Result<Header, E2sError> {
85        self.decode()
86    }
87
88    /// Create a [`CompressedHeader`] from a header
89    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/// Compressed block body using `snappyFramed(rlp(body))`
104#[derive(Debug, Clone)]
105pub struct CompressedBody {
106    /// The compressed data
107    pub data: Vec<u8>,
108}
109
110impl CompressedBody {
111    /// Create a new [`CompressedBody`] from compressed data
112    pub const fn new(data: Vec<u8>) -> Self {
113        Self { data }
114    }
115
116    /// Create from RLP-encoded body by compressing it with Snappy framed encoding
117    pub fn from_rlp(rlp_data: &[u8]) -> Result<Self, E2sError> {
118        Ok(Self { data: snappy_compress(rlp_data)? })
119    }
120
121    /// Decompress to get the original RLP-encoded body
122    pub fn decompress(&self) -> Result<Vec<u8>, E2sError> {
123        snappy_decompress(&self.data)
124    }
125
126    /// Convert to an [`Entry`]
127    pub fn to_entry(&self) -> Entry {
128        Entry::new(COMPRESSED_BODY, self.data.clone())
129    }
130
131    /// Create from an [`Entry`]
132    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    /// Decode this [`CompressedBody`] into an `alloy_consensus::BlockBody`
138    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    /// Decode decompressed body data into an `alloy_consensus::BlockBody`
144    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    /// Create a [`CompressedBody`] from a block body (e.g.  `alloy_consensus::BlockBody`)
152    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/// Compressed slim receipts using `snappyFramed(rlp(...))`.
167///
168/// Slim receipts exclude bloom filters to optimize storage.
169/// Format: `snappyFramed(rlp([tx-type, post-state-or-status, cumulative-gas, logs]))`
170#[derive(Debug, Clone)]
171pub struct CompressedSlimReceipts {
172    /// The compressed data
173    pub data: Vec<u8>,
174}
175
176impl CompressedSlimReceipts {
177    /// Create a new [`CompressedSlimReceipts`] from compressed data
178    pub const fn new(data: Vec<u8>) -> Self {
179        Self { data }
180    }
181
182    /// Create from RLP-encoded slim receipts by compressing with Snappy framed encoding
183    pub fn from_rlp(rlp_data: &[u8]) -> Result<Self, E2sError> {
184        Ok(Self { data: snappy_compress(rlp_data)? })
185    }
186
187    /// Decompress to get the original RLP-encoded slim receipts
188    pub fn decompress(&self) -> Result<Vec<u8>, E2sError> {
189        snappy_decompress(&self.data)
190    }
191
192    /// Convert to an [`Entry`]
193    pub fn to_entry(&self) -> Entry {
194        Entry::new(COMPRESSED_SLIM_RECEIPTS, self.data.clone())
195    }
196
197    /// Create from an [`Entry`]
198    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    /// Decode this [`CompressedSlimReceipts`] into the given type
204    pub fn decode<T: Decodable>(&self) -> Result<T, E2sError> {
205        let decoder = SnappyRlpCodec::<T>::new();
206        decoder.decode(&self.data)
207    }
208
209    /// Create [`CompressedSlimReceipts`] from an encodable type
210    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    /// Encode and compress a list of slim receipts
217    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    /// Compress a block's slim receipts.
224    ///
225    /// [`SlimReceipt`] is the canonical slim receipt: its RLP encoding is the 4-element list
226    /// `[tx-type, post-state-or-status, cumulative-gas, logs]`, with no bloom filter.
227    pub fn from_receipts(receipts: &[SlimReceipt]) -> Result<Self, E2sError> {
228        Self::from_encodable_list(receipts)
229    }
230
231    /// Decompress and decode this entry into a block's slim receipts.
232    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/// A slim execution receipt as stored in an `ERE` file.
245///
246/// Per the spec, the slim form is the 4-element RLP list
247/// `[tx-type, post-state-or-status, cumulative-gas, logs]` with **no bloom filter** (the bloom is
248/// recomputable from the logs). This is a thin wrapper over alloy's field types: [`Eip658Value`]
249/// captures both the pre-Byzantium 32-byte post-state root and the post-Byzantium boolean status,
250/// so a single type decodes receipts across every fork.
251#[derive(Debug, Clone, PartialEq, Eq, RlpEncodable, RlpDecodable)]
252pub struct SlimReceipt {
253    /// Transaction type (EIP-2718).
254    pub tx_type: TxType,
255    /// Post-state root (pre-Byzantium) or success status (post-Byzantium).
256    pub status: Eip658Value,
257    /// Cumulative gas used in the block up to and including this transaction.
258    pub cumulative_gas_used: u64,
259    /// Logs emitted by the transaction.
260    pub logs: Vec<Log>,
261}
262
263/// Proof type discriminant used inside the Proof entry's RLP envelope.
264///
265/// Maps to specific Portal Network proof objects.
266#[derive(Debug, Clone, Copy, PartialEq, Eq)]
267#[repr(u8)]
268pub enum ProofType {
269    /// Pre-merge proof against the historical hashes accumulator
270    BlockProofHistoricalHashesAccumulator = 0,
271    /// Post-merge proof against historical roots
272    BlockProofHistoricalRoots = 1,
273    /// Capella-era proof against historical summaries
274    BlockProofHistoricalSummariesCapella = 2,
275    /// Deneb-era proof against historical summaries
276    BlockProofHistoricalSummariesDeneb = 3,
277}
278
279impl ProofType {
280    /// Convert from a raw byte value
281    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    /// Convert to a raw byte value
292    pub const fn as_byte(self) -> u8 {
293        self as u8
294    }
295}
296
297/// A proof entry attesting to block validity against a trusted consensus layer header.
298///
299/// Format: `snappyFramed(rlp([proof-type, ssz(proof-object)]))`
300///
301/// Multiple proof types can coexist in the same file at fork boundaries.
302#[derive(Debug, Clone)]
303pub struct Proof {
304    /// The compressed data containing `rlp([proof-type, ssz(proof-object)])`
305    pub data: Vec<u8>,
306}
307
308impl Proof {
309    /// Create a new [`Proof`] from already-compressed data
310    pub const fn new(data: Vec<u8>) -> Self {
311        Self { data }
312    }
313
314    /// Encode a [`Proof`] from a proof type and raw SSZ-encoded proof object.
315    pub fn encode(proof_type: ProofType, ssz_proof: &[u8]) -> Result<Self, E2sError> {
316        // Build the list payload first so the RLP list header gets the exact length,
317        // regardless of how each item encodes.
318        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    /// Decode the proof, returning `(proof_type, raw_ssz_proof_bytes)`.
330    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        // A proof is exactly the two-item list `[proof-type, ssz(proof-object)]`. Pin decoding to
341        // the list's own payload so nothing outside it slips through: bytes after the list end, or
342        // a third item inside it, both fail instead of being silently dropped.
343        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    /// Convert to an [`Entry`]
367    pub fn to_entry(&self) -> Entry {
368        Entry::new(PROOF, self.data.clone())
369    }
370
371    /// Create from an [`Entry`]
372    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/// Total difficulty for a block
379#[derive(Debug, Clone)]
380pub struct TotalDifficulty {
381    /// The total difficulty as U256
382    pub value: U256,
383}
384
385impl TotalDifficulty {
386    /// Create a new [`TotalDifficulty`] from a U256 value
387    pub const fn new(value: U256) -> Self {
388        Self { value }
389    }
390
391    /// Convert to an [`Entry`]
392    pub fn to_entry(&self) -> Entry {
393        // ere spec: `total-difficulty = { type: 0x0600, data: SSZ uint256 }` (little-endian)
394        let data = self.value.to_le_bytes::<32>().to_vec();
395        Entry::new(TOTAL_DIFFICULTY, data)
396    }
397
398    /// Create from an [`Entry`]
399    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        // ere spec: `total-difficulty = { type: 0x0600, data: SSZ uint256 }` (little-endian)
410        let value = U256::from_le_slice(&entry.data);
411
412        Ok(Self { value })
413    }
414}
415
416/// Accumulator is computed by constructing an SSZ list of header-records
417/// and calculating the `hash_tree_root`
418#[derive(Debug, Clone, PartialEq, Eq)]
419pub struct Accumulator {
420    /// The accumulator root hash
421    pub root: B256,
422}
423
424impl Accumulator {
425    /// Create a new [`Accumulator`] from a root hash
426    pub const fn new(root: B256) -> Self {
427        Self { root }
428    }
429
430    /// Convert to an [`Entry`]
431    pub fn to_entry(&self) -> Entry {
432        Entry::new(ACCUMULATOR, self.root.to_vec())
433    }
434
435    /// Create from an [`Entry`]
436    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    /// Compute the accumulator from a list of header records.
453    ///
454    /// Implements `hash_tree_root(List[HeaderRecord, 8192])` per the spec:
455    /// - Each leaf is `sha256(block_hash || total_difficulty_le_bytes32)`
456    /// - Leaves are padded to `MAX_BLOCKS_PER_ERE` (8192) with zero hashes
457    /// - Binary Merkle tree is computed bottom-up
458    /// - Final root is `sha256(merkle_root || le_bytes32(actual_count))`
459    ///
460    /// Returns `Err` if `records` exceeds [`MAX_BLOCKS_PER_ERE`].
461    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        // Compute leaf hash for each header record
473        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        // Pad to capacity with zero hashes
482        leaves.resize(capacity, [0u8; 32]);
483
484        // Binary Merkle tree bottom-up (capacity is always a power of two)
485        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        // mix_in_length: sha256(merkle_root || le_bytes32(actual_length))
499        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        // remaining bytes stay zero (uint256 LE padding)
504
505        Ok(Self { root: B256::from(<[u8; 32]>::from(Sha256::digest(mix))) })
506    }
507}
508
509/// The minimal per-block commitment used to build the accumulator.
510///
511/// This is **not** a block header: it is the 64-byte leaf
512/// `{ block-hash: Bytes32, total-difficulty: Uint256 }` that
513/// [`Accumulator::from_header_records`] merkleizes as `hash_tree_root(List[HeaderRecord, 8192])`.
514/// The full header is stored separately as [`CompressedHeader`].
515///
516/// Only meaningful pre-merge, since `total-difficulty` stops advancing after the merge.
517#[derive(Debug, Clone)]
518pub struct HeaderRecord {
519    /// The canonical block hash, i.e. `keccak256(rlp(header))` — the hash *of* the full block
520    /// header, which serves as this leaf's identity in the accumulator.
521    pub block_hash: B256,
522    /// The **cumulative** total difficulty through this block (the running sum of every block's
523    /// difficulty up to and including it), not the header's own per-block `difficulty` field.
524    pub total_difficulty: U256,
525}
526
527/// A single block's components in an `ERE` file.
528///
529/// Only the header and body are mandatory; receipts, total difficulty, and the proof are optional,
530/// so subset profiles or post-merge blocks can omit them.
531/// [`component_count`](Self::component_count) reports how many are present, matching the file's
532/// `DynamicBlockIndex` `component-count`.
533///
534/// See also <https://github.com/eth-clients/e2store-format-specs/blob/main/formats/ere.md#specification>
535#[derive(Debug, Clone)]
536pub struct BlockTuple {
537    /// Compressed block header
538    pub header: CompressedHeader,
539
540    /// Compressed block body
541    pub body: CompressedBody,
542
543    /// Compressed slim receipts, omitted by the `noreceipts` profile
544    pub receipts: Option<CompressedSlimReceipts>,
545
546    /// Total difficulty, absent once it stops advancing after the merge
547    pub total_difficulty: Option<TotalDifficulty>,
548
549    /// Proof of block validity, omitted by the `noproofs` profile
550    pub proof: Option<Proof>,
551}
552
553impl BlockTuple {
554    /// Create a new [`BlockTuple`] with only the mandatory header and body.
555    ///
556    /// Attach the optional components with [`with_receipts`](Self::with_receipts),
557    /// [`with_total_difficulty`](Self::with_total_difficulty), and
558    /// [`with_proof`](Self::with_proof).
559    pub const fn new(header: CompressedHeader, body: CompressedBody) -> Self {
560        Self { header, body, receipts: None, total_difficulty: None, proof: None }
561    }
562
563    /// Attach compressed slim receipts.
564    pub fn with_receipts(mut self, receipts: CompressedSlimReceipts) -> Self {
565        self.receipts = Some(receipts);
566        self
567    }
568
569    /// Attach the total difficulty.
570    pub const fn with_total_difficulty(mut self, total_difficulty: TotalDifficulty) -> Self {
571        self.total_difficulty = Some(total_difficulty);
572        self
573    }
574
575    /// Attach a validity proof.
576    pub fn with_proof(mut self, proof: Proof) -> Self {
577        self.proof = Some(proof);
578        self
579    }
580
581    /// Number of index components this block contributes: the mandatory header and body plus each
582    /// optional component present. Always in the range 2-5, matching the file's `component-count`.
583    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    /// Convert to an `alloy_consensus::Block`
590    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    /// Create from an `alloy_consensus::Block`, attaching the given receipts and total difficulty.
598    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        // Verify that total-difficulty is encoded as SSZ uint256 (little-endian).
672        // See https://github.com/eth-clients/e2store-format-specs/blob/main/formats/ere.md
673        let value = U256::from(1u64);
674        let td = TotalDifficulty::new(value);
675        let entry = td.to_entry();
676
677        // Little-endian: least significant byte first [1, 0, 0, ..., 0]
678        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        // Test header compression/decompression
687        let compressed_header = CompressedHeader::from_rlp(&rlp_data).unwrap();
688        let decompressed = compressed_header.decompress().unwrap();
689        assert_eq!(decompressed, rlp_data);
690
691        // Test body compression/decompression
692        let compressed_body = CompressedBody::from_rlp(&rlp_data).unwrap();
693        let decompressed = compressed_body.decompress().unwrap();
694        assert_eq!(decompressed, rlp_data);
695
696        // Test receipts compression/decompression
697        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        // Create block with transactions and withdrawals
705        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        // Convert back to Block
721        let decoded_block: Block<Bytes> = block_tuple.to_alloy_block().unwrap();
722
723        // Verify block components
724        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        // Mandatory header + body only.
735        assert_eq!(base.component_count(), 2);
736
737        // Each optional component bumps the count, up to the 5-component maximum.
738        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        // Compress the receipt
755        let compressed_receipts = CompressedSlimReceipts::from_encodable(&test_receipt)
756            .expect("Failed to compress receipt");
757
758        // Verify compression
759        assert!(!compressed_receipts.data.is_empty());
760
761        // Decode the compressed receipt back
762        let decoded_receipt: Receipt =
763            compressed_receipts.decode().expect("Failed to decode compressed receipt");
764
765        // Verify that the decoded receipt matches the original
766        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        // Verify each log
772        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        // Spec: CompressedSlimReceipts.data = snappyFramed(rlp([tx-type, status, cumulative-gas,
782        // logs])), with no bloom filter. Prove the inner RLP of `EthereumReceipt` is exactly that
783        // 4-element list, byte for byte.
784        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        // Hand-build rlp([tx-type, status, cumulative-gas, logs]) in spec field order.
790        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        // Cover both status variants: post-Byzantium boolean status and a pre-Byzantium 32-byte
808        // post-state root, proving a single `SlimReceipt` type round-trips across forks.
809        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        // Known-answer vectors computed from the SSZ spec:
833        //   hash_tree_root(List[HeaderRecord, 8192])
834        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        // Empty list
842        let acc_empty = Accumulator::from_header_records(&[]).unwrap();
843        assert_eq!(acc_empty.root, expected_empty);
844
845        // Single record with zero values
846        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        // Single record with non-zero values
851        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        // Roundtrip through an Entry
887        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        // A record is exactly `snappyFramed(rlp(...))`; an extra byte after the RLP value
905        // must be rejected, not silently ignored.
906        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); // byte beyond the RLP list
918        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        // Build rlp([proof-type, ssz, extra]) — a third item must be rejected.
925        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        // Compress the list of receipts
941        let compressed_receipts = CompressedSlimReceipts::from_encodable_list(&receipts)
942            .expect("Failed to compress receipt list");
943
944        // Decode the compressed receipts back. ERE always stores slim receipts (no bloom), so the
945        // bare `Receipt` (`alloy_consensus::EthereumReceipt`) is the canonical decode target.
946        let decoded_receipts: Vec<Receipt> =
947            compressed_receipts.decode().expect("Failed to decode compressed receipt list");
948
949        // Verify that the decoded receipts match the original
950        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}