snarkvm_ledger_block/transaction/deployment/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 Deployment<N> {
19 /// Reads the deployment 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 deployment version"));
26 }
27
28 // Read the edition.
29 let edition = u16::read_le(&mut reader)?;
30 // Read the program.
31 let program = Program::read_le(&mut reader)?;
32
33 // Read the number of entries in the bundle.
34 let num_entries = u16::read_le(&mut reader)?;
35 // Read the verifying keys.
36 let mut verifying_keys = Vec::with_capacity(num_entries as usize);
37 for _ in 0..num_entries {
38 // Read the identifier.
39 let identifier = Identifier::<N>::read_le(&mut reader)?;
40 // Read the verifying key.
41 let verifying_key = VerifyingKey::<N>::read_le(&mut reader)?;
42 // Read the certificate.
43 let certificate = Certificate::<N>::read_le(&mut reader)?;
44 // Add the entry.
45 verifying_keys.push((identifier, (verifying_key, certificate)));
46 }
47
48 // Return the deployment.
49 Self::new(edition, program, verifying_keys).map_err(|err| error(format!("{err}")))
50 }
51}
52
53impl<N: Network> ToBytes for Deployment<N> {
54 /// Writes the deployment to a buffer.
55 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
56 // Write the version.
57 1u8.write_le(&mut writer)?;
58 // Write the edition.
59 self.edition.write_le(&mut writer)?;
60 // Write the program.
61 self.program.write_le(&mut writer)?;
62 // Write the number of entries in the bundle.
63 (u16::try_from(self.verifying_keys.len()).map_err(|e| error(e.to_string()))?).write_le(&mut writer)?;
64 // Write each entry.
65 for (function_name, (verifying_key, certificate)) in &self.verifying_keys {
66 // Write the function name.
67 function_name.write_le(&mut writer)?;
68 // Write the verifying key.
69 verifying_key.write_le(&mut writer)?;
70 // Write the certificate.
71 certificate.write_le(&mut writer)?;
72 }
73 Ok(())
74 }
75}
76
77#[cfg(test)]
78mod tests {
79 use super::*;
80
81 #[test]
82 fn test_bytes() -> Result<()> {
83 let rng = &mut TestRng::default();
84
85 // Construct a new deployment.
86 let expected = test_helpers::sample_deployment(rng);
87
88 // Check the byte representation.
89 let expected_bytes = expected.to_bytes_le()?;
90 assert_eq!(expected, Deployment::read_le(&expected_bytes[..])?);
91 Ok(())
92 }
93}