snarkvm_ledger_block/transition/
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 Transition<N> {
19    /// Reads the output 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 transition version"));
26        }
27
28        // Read the transition ID.
29        let transition_id = N::TransitionID::read_le(&mut reader)?;
30        // Read the program ID.
31        let program_id = FromBytes::read_le(&mut reader)?;
32        // Read the function name.
33        let function_name = FromBytes::read_le(&mut reader)?;
34
35        // Read the number of inputs.
36        let num_inputs: u8 = FromBytes::read_le(&mut reader)?;
37        // Read the inputs.
38        let mut inputs = Vec::with_capacity(num_inputs as usize);
39        for _ in 0..num_inputs {
40            // Read the input.
41            inputs.push(FromBytes::read_le(&mut reader)?);
42        }
43
44        // Read the number of outputs.
45        let num_outputs: u8 = FromBytes::read_le(&mut reader)?;
46        // Read the outputs.
47        let mut outputs = Vec::with_capacity(num_outputs as usize);
48        for _ in 0..num_outputs {
49            // Read the output.
50            outputs.push(FromBytes::read_le(&mut reader)?);
51        }
52
53        // Read the transition public key.
54        let tpk = FromBytes::read_le(&mut reader)?;
55        // Read the transition commitment.
56        let tcm = FromBytes::read_le(&mut reader)?;
57        // Read the signer commitment.
58        let scm = FromBytes::read_le(&mut reader)?;
59
60        // Construct the candidate transition.
61        let transition =
62            Self::new(program_id, function_name, inputs, outputs, tpk, tcm, scm).map_err(|e| error(e.to_string()))?;
63        // Ensure the transition ID matches the expected ID.
64        match transition_id == *transition.id() {
65            true => Ok(transition),
66            false => Err(error("Transition ID is incorrect, possible data corruption")),
67        }
68    }
69}
70
71impl<N: Network> ToBytes for Transition<N> {
72    /// Writes the literal to a buffer.
73    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
74        // Write the version.
75        1u8.write_le(&mut writer)?;
76
77        // Write the transition ID.
78        self.id.write_le(&mut writer)?;
79        // Write the program ID.
80        self.program_id.write_le(&mut writer)?;
81        // Write the function name.
82        self.function_name.write_le(&mut writer)?;
83
84        // Write the number of inputs.
85        (u8::try_from(self.inputs.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
86        // Write the inputs.
87        self.inputs.write_le(&mut writer)?;
88
89        // Write the number of outputs.
90        (u8::try_from(self.outputs.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
91        // Write the outputs.
92        self.outputs.write_le(&mut writer)?;
93
94        // Write the transition public key.
95        self.tpk.write_le(&mut writer)?;
96        // Write the transition commitment.
97        self.tcm.write_le(&mut writer)?;
98        // Write the signer commitment.
99        self.scm.write_le(&mut writer)
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn test_bytes() -> Result<()> {
109        let rng = &mut TestRng::default();
110
111        // Sample the transition.
112        let expected = crate::transition::test_helpers::sample_transition(rng);
113
114        // Check the byte representation.
115        let expected_bytes = expected.to_bytes_le()?;
116        assert_eq!(expected, Transition::read_le(&expected_bytes[..])?);
117
118        Ok(())
119    }
120}