Skip to main content

snarkvm_console_program/state_path/transition_leaf/
bytes.rs

1// Copyright (c) 2019-2026 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 TransitionLeaf<N> {
19    /// Reads the transition leaf from a buffer.
20    fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
21        // Read the version.
22        let version = FromBytes::read_le(&mut reader)?;
23        // Read the index.
24        let index = FromBytes::read_le(&mut reader)?;
25        // Read the variant.
26        let variant = FromBytes::read_le(&mut reader)?;
27        // Read the ID.
28        let id = FromBytes::read_le(&mut reader)?;
29        // Return the transition leaf.
30        Self::from(version, index, variant, id).map_err(|e| error(e.to_string()))
31    }
32}
33
34impl<N: Network> ToBytes for TransitionLeaf<N> {
35    /// Writes the transition leaf to a buffer.
36    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
37        // Write the version.
38        self.version.write_le(&mut writer)?;
39        // Write the index.
40        self.index.write_le(&mut writer)?;
41        // Write the variant.
42        self.variant.write_le(&mut writer)?;
43        // Write the ID.
44        self.id.write_le(&mut writer)
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use snarkvm_console_network::MainnetV0;
52
53    type CurrentNetwork = MainnetV0;
54
55    const ITERATIONS: u64 = 1000;
56
57    #[test]
58    fn test_bytes() -> Result<()> {
59        let mut rng = TestRng::default();
60
61        for _ in 0..ITERATIONS {
62            // Sample a static leaf (version 1).
63            let expected = test_helpers::sample_leaf(&mut rng);
64
65            // Check the byte representation.
66            let expected_bytes = expected.to_bytes_le()?;
67            assert_eq!(expected, TransitionLeaf::read_le(&expected_bytes[..])?);
68
69            // Sample a dynamic leaf (version 2).
70            let expected_dynamic = test_helpers::sample_dynamic_leaf(&mut rng);
71
72            // Check the byte representation.
73            let expected_dynamic_bytes = expected_dynamic.to_bytes_le()?;
74            assert_eq!(expected_dynamic, TransitionLeaf::read_le(&expected_dynamic_bytes[..])?);
75        }
76        Ok(())
77    }
78
79    #[test]
80    fn test_version_variant_validation() -> Result<()> {
81        let mut rng = TestRng::default();
82        let id = Uniform::rand(&mut rng);
83
84        // Test that version 1 (static) works with any variant.
85        for variant in 0..=7 {
86            let leaf = TransitionLeaf::<CurrentNetwork>::new(0, variant, id);
87            let bytes = leaf.to_bytes_le()?;
88            assert!(TransitionLeaf::<CurrentNetwork>::read_le(&bytes[..]).is_ok());
89        }
90
91        // Test that version 2 (dynamic) only works with Record (3) and ExternalRecord (4).
92        let leaf_record = TransitionLeaf::<CurrentNetwork>::new_record_with_dynamic_id(0, id);
93        let bytes = leaf_record.to_bytes_le()?;
94        assert!(TransitionLeaf::<CurrentNetwork>::read_le(&bytes[..]).is_ok());
95
96        let leaf_external = TransitionLeaf::<CurrentNetwork>::new_external_record_with_dynamic_id(0, id);
97        let bytes = leaf_external.to_bytes_le()?;
98        assert!(TransitionLeaf::<CurrentNetwork>::read_le(&bytes[..]).is_ok());
99
100        // Test that version 2 (dynamic) returns an error for invalid variants.
101        for variant in [0u8, 1, 2, 5, 6, 7] {
102            assert!(
103                TransitionLeaf::<CurrentNetwork>::from(LeafVersion::Dynamic as u8, 0, variant, id).is_err(),
104                "Expected error for dynamic variant {variant}"
105            );
106        }
107
108        Ok(())
109    }
110}