snarkvm_console_program/state_path/
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 StatePath<N> {
19    /// Reads the path from a buffer.
20    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
21        // Read the version.
22        let version = u8::read_le(&mut reader)?;
23        // Ensure the version is valid.
24        if version != 1 {
25            return Err(error("Invalid state path version"));
26        }
27
28        // Read the state path.
29        let global_state_root = N::StateRoot::read_le(&mut reader)?;
30
31        let block_path = BlockPath::read_le(&mut reader)?;
32        let block_hash = N::BlockHash::read_le(&mut reader)?;
33        let previous_block_hash = N::BlockHash::read_le(&mut reader)?;
34        let header_root = Field::read_le(&mut reader)?;
35        let header_path = HeaderPath::read_le(&mut reader)?;
36        let header_leaf = HeaderLeaf::read_le(&mut reader)?;
37        let transactions_path = TransactionsPath::read_le(&mut reader)?;
38
39        let transaction_id = FromBytes::read_le(&mut reader)?;
40        let transaction_path = FromBytes::read_le(&mut reader)?;
41        let transaction_leaf = FromBytes::read_le(&mut reader)?;
42        let transition_root = Field::read_le(&mut reader)?;
43        let tcm = FromBytes::read_le(&mut reader)?;
44        let transition_path = FromBytes::read_le(&mut reader)?;
45        let transition_leaf = FromBytes::read_le(&mut reader)?;
46
47        // Construct the state path.
48        Ok(Self::from(
49            global_state_root,
50            block_path,
51            block_hash,
52            previous_block_hash,
53            header_root,
54            header_path,
55            header_leaf,
56            transactions_path,
57            transaction_id,
58            transaction_path,
59            transaction_leaf,
60            transition_root,
61            tcm,
62            transition_path,
63            transition_leaf,
64        ))
65    }
66}
67
68impl<N: Network> ToBytes for StatePath<N> {
69    /// Writes the path to a buffer.
70    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
71        // Write the version.
72        1u8.write_le(&mut writer)?;
73
74        // Write the state path.
75        self.global_state_root.write_le(&mut writer)?;
76
77        self.block_path.write_le(&mut writer)?;
78        self.block_hash.write_le(&mut writer)?;
79        self.previous_block_hash.write_le(&mut writer)?;
80        self.header_root.write_le(&mut writer)?;
81        self.header_path.write_le(&mut writer)?;
82        self.header_leaf.write_le(&mut writer)?;
83        self.transactions_path.write_le(&mut writer)?;
84
85        self.transaction_id.write_le(&mut writer)?;
86        self.transaction_path.write_le(&mut writer)?;
87        self.transaction_leaf.write_le(&mut writer)?;
88        self.transition_root.write_le(&mut writer)?;
89        self.tcm.write_le(&mut writer)?;
90        self.transition_path.write_le(&mut writer)?;
91        self.transition_leaf.write_le(&mut writer)
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98    use snarkvm_console_network::MainnetV0;
99
100    type CurrentNetwork = MainnetV0;
101
102    const ITERATIONS: usize = 100;
103
104    #[test]
105    fn test_bytes() {
106        let mut rng = TestRng::default();
107
108        for _ in 0..ITERATIONS {
109            // Sample the state path.
110            let expected =
111                crate::state_path::test_helpers::sample_global_state_path::<CurrentNetwork>(None, &mut rng).unwrap();
112
113            // Check the byte representation.
114            let expected_bytes = expected.to_bytes_le().unwrap();
115            assert_eq!(expected, StatePath::read_le(&expected_bytes[..]).unwrap());
116            assert!(StatePath::<CurrentNetwork>::read_le(&expected_bytes[1..]).is_err());
117        }
118    }
119}