Skip to content

Commit 73b4fcc

Browse files
authored
docs(era): use lighthouse decoding support for era types (#20147)
1 parent 6fc752d commit 73b4fcc

File tree

2 files changed

+64
-43
lines changed

2 files changed

+64
-43
lines changed

crates/era/src/common/decode.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,9 @@
22
33
use crate::e2s::error::E2sError;
44
use alloy_rlp::Decodable;
5-
use ssz::Decode;
65

76
/// Extension trait for generic decoding from compressed data
87
pub trait DecodeCompressedRlp {
98
/// Decompress and decode the data into the given type
109
fn decode<T: Decodable>(&self) -> Result<T, E2sError>;
1110
}
12-
13-
/// Extension trait for generic decoding from compressed ssz data
14-
pub trait DecodeCompressedSsz {
15-
/// Decompress and decode the SSZ data into the given type
16-
fn decode<T: Decode>(&self) -> Result<T, E2sError>;
17-
}

crates/era/src/era/types/consensus.rs

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
11
//! Consensus types for Era post-merge history files
2-
3-
use crate::{
4-
common::decode::DecodeCompressedSsz,
5-
e2s::{error::E2sError, types::Entry},
6-
};
2+
//!
3+
//! # Decoding
4+
//!
5+
//! This crate only handles compression/decompression.
6+
//! To decode the SSZ data into concrete beacon types, use the [Lighthouse `types`](https://github.com/sigp/lighthouse/tree/stable/consensus/types)
7+
//! crate or another SSZ-compatible library.
8+
//!
9+
//! # Examples
10+
//!
11+
//! ## Decoding a [`CompressedBeaconState`]
12+
//!
13+
//! ```ignore
14+
//! use types::{BeaconState, ChainSpec, MainnetEthSpec};
15+
//! use reth_era::era::types::consensus::CompressedBeaconState;
16+
//!
17+
//! fn decode_state(
18+
//! compressed_state: &CompressedBeaconState,
19+
//! ) -> Result<(), Box<dyn std::error::Error>> {
20+
//! let spec = ChainSpec::mainnet();
21+
//!
22+
//! // Decompress to get SSZ bytes
23+
//! let ssz_bytes = compressed_state.decompress()?;
24+
//!
25+
//! // Decode with fork-aware method, chainSpec determines fork from slot in SSZ
26+
//! let state = BeaconState::<MainnetEthSpec>::from_ssz_bytes(&ssz_bytes, &spec)
27+
//! .map_err(|e| format!("{:?}", e))?;
28+
//!
29+
//! println!("State slot: {}", state.slot());
30+
//! println!("Fork: {:?}", state.fork_name_unchecked());
31+
//! println!("Validators: {}", state.validators().len());
32+
//! println!("Finalized checkpoint: {:?}", state.finalized_checkpoint());
33+
//! Ok(())
34+
//! }
35+
//! ```
36+
//!
37+
//! ## Decoding a [`CompressedSignedBeaconBlock`]
38+
//!
39+
//! ```ignore
40+
//! use consensus_types::{ForkName, ForkVersionDecode, MainnetEthSpec, SignedBeaconBlock};
41+
//! use reth_era::era::types::consensus::CompressedSignedBeaconBlock;
42+
//!
43+
//! // Decode using fork-aware decoding, fork must be known beforehand
44+
//! fn decode_block(
45+
//! compressed: &CompressedSignedBeaconBlock,
46+
//! fork: ForkName,
47+
//! ) -> Result<(), Box<dyn std::error::Error>> {
48+
//! // Decompress to get SSZ bytes
49+
//! let ssz_bytes = compressed.decompress()?;
50+
//!
51+
//! let block = SignedBeaconBlock::<MainnetEthSpec>::from_ssz_bytes_by_fork(&ssz_bytes, fork)
52+
//! .map_err(|e| format!("{:?}", e))?;
53+
//!
54+
//! println!("Block slot: {}", block.message().slot());
55+
//! println!("Proposer index: {}", block.message().proposer_index());
56+
//! println!("Parent root: {:?}", block.message().parent_root());
57+
//! println!("State root: {:?}", block.message().state_root());
58+
//!
59+
//! Ok(())
60+
//! }
61+
//! ```
62+
63+
use crate::e2s::{error::E2sError, types::Entry};
764
use snap::{read::FrameDecoder, write::FrameEncoder};
8-
use ssz::Decode;
965
use std::io::{Read, Write};
1066

1167
/// `CompressedSignedBeaconBlock` record type: [0x01, 0x00]
@@ -76,20 +132,6 @@ impl CompressedSignedBeaconBlock {
76132

77133
Ok(Self { data: entry.data.clone() })
78134
}
79-
80-
/// Decode the compressed signed beacon block into ssz bytes
81-
pub fn decode_to_ssz(&self) -> Result<Vec<u8>, E2sError> {
82-
self.decompress()
83-
}
84-
}
85-
86-
impl DecodeCompressedSsz for CompressedSignedBeaconBlock {
87-
fn decode<T: Decode>(&self) -> Result<T, E2sError> {
88-
let ssz_bytes = self.decompress()?;
89-
T::from_ssz_bytes(&ssz_bytes).map_err(|e| {
90-
E2sError::Ssz(format!("Failed to decode SSZ data into target type: {e:?}"))
91-
})
92-
}
93135
}
94136

95137
/// Compressed beacon state
@@ -154,20 +196,6 @@ impl CompressedBeaconState {
154196

155197
Ok(Self { data: entry.data.clone() })
156198
}
157-
158-
/// Decode the compressed beacon state into ssz bytes
159-
pub fn decode_to_ssz(&self) -> Result<Vec<u8>, E2sError> {
160-
self.decompress()
161-
}
162-
}
163-
164-
impl DecodeCompressedSsz for CompressedBeaconState {
165-
fn decode<T: Decode>(&self) -> Result<T, E2sError> {
166-
let ssz_bytes = self.decompress()?;
167-
T::from_ssz_bytes(&ssz_bytes).map_err(|e| {
168-
E2sError::Ssz(format!("Failed to decode SSZ data into target type: {e:?}"))
169-
})
170-
}
171199
}
172200

173201
#[cfg(test)]
@@ -203,7 +231,7 @@ mod tests {
203231
assert_eq!(entry.entry_type, COMPRESSED_SIGNED_BEACON_BLOCK);
204232

205233
let recovered = CompressedSignedBeaconBlock::from_entry(&entry).unwrap();
206-
let recovered_ssz = recovered.decode_to_ssz().unwrap();
234+
let recovered_ssz = recovered.decompress().unwrap();
207235

208236
assert_eq!(recovered_ssz, ssz_data);
209237
}
@@ -217,7 +245,7 @@ mod tests {
217245
assert_eq!(entry.entry_type, COMPRESSED_BEACON_STATE);
218246

219247
let recovered = CompressedBeaconState::from_entry(&entry).unwrap();
220-
let recovered_ssz = recovered.decode_to_ssz().unwrap();
248+
let recovered_ssz = recovered.decompress().unwrap();
221249

222250
assert_eq!(recovered_ssz, ssz_data);
223251
}

0 commit comments

Comments
 (0)