snarkvm_ledger_block/header/
bytes.rs1use super::*;
17
18impl<N: Network> FromBytes for Header<N> {
19 #[inline]
21 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
22 let version = u8::read_le(&mut reader)?;
24 if version != 1 {
26 return Err(error("Invalid header version"));
27 }
28
29 let previous_state_root = N::StateRoot::read_le(&mut reader)?;
31 let transactions_root = Field::<N>::read_le(&mut reader)?;
32 let finalize_root = Field::<N>::read_le(&mut reader)?;
33 let ratifications_root = Field::<N>::read_le(&mut reader)?;
34 let solutions_root = Field::<N>::read_le(&mut reader)?;
35 let subdag_root = Field::<N>::read_le(&mut reader)?;
36 let metadata = Metadata::read_le(&mut reader)?;
37
38 Self::from(
40 previous_state_root,
41 transactions_root,
42 finalize_root,
43 ratifications_root,
44 solutions_root,
45 subdag_root,
46 metadata,
47 )
48 .map_err(|e| error(format!("{e:#?}")))
49 }
50}
51
52impl<N: Network> ToBytes for Header<N> {
53 #[inline]
55 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
56 1u8.write_le(&mut writer)?;
58
59 self.previous_state_root.write_le(&mut writer)?;
61 self.transactions_root.write_le(&mut writer)?;
62 self.finalize_root.write_le(&mut writer)?;
63 self.ratifications_root.write_le(&mut writer)?;
64 self.solutions_root.write_le(&mut writer)?;
65 self.subdag_root.write_le(&mut writer)?;
66 self.metadata.write_le(&mut writer)
67 }
68}
69
70#[cfg(test)]
71mod tests {
72 use super::*;
73 use console::network::MainnetV0;
74
75 type CurrentNetwork = MainnetV0;
76
77 #[test]
78 fn test_bytes() -> Result<()> {
79 let rng = &mut TestRng::default();
80
81 for expected in [crate::header::test_helpers::sample_block_header(rng)].into_iter() {
82 let expected_bytes = expected.to_bytes_le()?;
84 assert_eq!(expected, Header::read_le(&expected_bytes[..])?);
85 assert!(Header::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
86 }
87 Ok(())
88 }
89}