snarkvm_ledger_block/header/
bytes.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<N: Network> FromBytes for Header<N> {
19    /// Reads the block header from the buffer.
20    #[inline]
21    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
22        // Read the version.
23        let version = u8::read_le(&mut reader)?;
24        // Ensure the version is valid.
25        if version != 1 {
26            return Err(error("Invalid header version"));
27        }
28
29        // Read from the buffer.
30        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        // Construct the block header.
39        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    /// Writes the block header to the buffer.
54    #[inline]
55    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
56        // Write the version.
57        1u8.write_le(&mut writer)?;
58
59        // Write to the buffer.
60        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            // Check the byte representation.
83            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}