snarkvm_ledger_block/transactions/confirmed/bytes.rs
1// Copyright 2024 Aleo Network Foundation
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 ConfirmedTransaction<N> {
19 /// Reads the confirmed transaction from a buffer.
20 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
21 let variant = u8::read_le(&mut reader)?;
22 match variant {
23 0 => {
24 // Read the index.
25 let index = u32::read_le(&mut reader)?;
26 // Read the transaction.
27 let transaction = Transaction::<N>::read_le(&mut reader)?;
28 // Read the number of finalize operations.
29 let num_finalize = NumFinalizeSize::read_le(&mut reader)?;
30 // Read the finalize operations.
31 let finalize =
32 (0..num_finalize).map(|_| FromBytes::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
33 // Return the confirmed transaction.
34 Self::accepted_deploy(index, transaction, finalize).map_err(error)
35 }
36 1 => {
37 // Read the index.
38 let index = u32::read_le(&mut reader)?;
39 // Read the transaction.
40 let transaction = Transaction::<N>::read_le(&mut reader)?;
41 // Read the number of finalize operations.
42 let num_finalize = NumFinalizeSize::read_le(&mut reader)?;
43 // Read the finalize operations.
44 let finalize =
45 (0..num_finalize).map(|_| FromBytes::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
46 // Return the confirmed transaction.
47 Self::accepted_execute(index, transaction, finalize).map_err(error)
48 }
49 2 => {
50 // Read the index.
51 let index = u32::read_le(&mut reader)?;
52 // Read the transaction.
53 let transaction = Transaction::<N>::read_le(&mut reader)?;
54 // Read the rejected deployment.
55 let rejected = Rejected::<N>::read_le(&mut reader)?;
56 // Read the number of finalize operations.
57 let num_finalize = NumFinalizeSize::read_le(&mut reader)?;
58 // Read the finalize operations.
59 let finalize =
60 (0..num_finalize).map(|_| FromBytes::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
61 // Return the confirmed transaction.
62 Self::rejected_deploy(index, transaction, rejected, finalize).map_err(error)
63 }
64 3 => {
65 // Read the index.
66 let index = u32::read_le(&mut reader)?;
67 // Read the transaction.
68 let transaction = Transaction::<N>::read_le(&mut reader)?;
69 // Read the rejected execution.
70 let rejected = Rejected::<N>::read_le(&mut reader)?;
71 // Read the number of finalize operations.
72 let num_finalize = NumFinalizeSize::read_le(&mut reader)?;
73 // Read the finalize operations.
74 let finalize =
75 (0..num_finalize).map(|_| FromBytes::read_le(&mut reader)).collect::<Result<Vec<_>, _>>()?;
76 // Return the confirmed transaction.
77 Self::rejected_execute(index, transaction, rejected, finalize).map_err(error)
78 }
79 4.. => Err(error(format!("Failed to decode confirmed transaction variant {variant}"))),
80 }
81 }
82}
83
84impl<N: Network> ToBytes for ConfirmedTransaction<N> {
85 /// Writes the confirmed transaction to a buffer.
86 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
87 match self {
88 Self::AcceptedDeploy(index, transaction, finalize) => {
89 // Write the variant.
90 0u8.write_le(&mut writer)?;
91 // Write the index.
92 index.write_le(&mut writer)?;
93 // Write the transaction.
94 transaction.write_le(&mut writer)?;
95 // Write the number of finalize operations.
96 NumFinalizeSize::try_from(finalize.len()).map_err(error)?.write_le(&mut writer)?;
97 // Write the finalize operations.
98 finalize.iter().try_for_each(|finalize| finalize.write_le(&mut writer))
99 }
100 Self::AcceptedExecute(index, transaction, finalize) => {
101 // Write the variant.
102 1u8.write_le(&mut writer)?;
103 // Write the index.
104 index.write_le(&mut writer)?;
105 // Write the transaction.
106 transaction.write_le(&mut writer)?;
107 // Write the number of finalize operations.
108 NumFinalizeSize::try_from(finalize.len()).map_err(error)?.write_le(&mut writer)?;
109 // Write the finalize operations.
110 finalize.iter().try_for_each(|finalize| finalize.write_le(&mut writer))
111 }
112 Self::RejectedDeploy(index, transaction, rejected, finalize) => {
113 // Write the variant.
114 2u8.write_le(&mut writer)?;
115 // Write the index.
116 index.write_le(&mut writer)?;
117 // Write the transaction.
118 transaction.write_le(&mut writer)?;
119 // Write the rejected deployment.
120 rejected.write_le(&mut writer)?;
121 // Write the number of finalize operations.
122 NumFinalizeSize::try_from(finalize.len()).map_err(error)?.write_le(&mut writer)?;
123 // Write the finalize operations.
124 finalize.iter().try_for_each(|finalize| finalize.write_le(&mut writer))
125 }
126 Self::RejectedExecute(index, transaction, rejected, finalize) => {
127 // Write the variant.
128 3u8.write_le(&mut writer)?;
129 // Write the index.
130 index.write_le(&mut writer)?;
131 // Write the transaction.
132 transaction.write_le(&mut writer)?;
133 // Write the rejected execution.
134 rejected.write_le(&mut writer)?;
135 // Write the number of finalize operations.
136 NumFinalizeSize::try_from(finalize.len()).map_err(error)?.write_le(&mut writer)?;
137 // Write the finalize operations.
138 finalize.iter().try_for_each(|finalize| finalize.write_le(&mut writer))
139 }
140 }
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn test_bytes() {
150 for expected in crate::transactions::confirmed::test_helpers::sample_confirmed_transactions() {
151 // Check the byte representation.
152 let expected_bytes = expected.to_bytes_le().unwrap();
153 assert_eq!(expected, ConfirmedTransaction::read_le(&expected_bytes[..]).unwrap());
154 }
155 }
156}