snarkvm_ledger_block/transactions/rejected/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 Rejected<N> {
19 /// Reads the rejected 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 program owner.
25 let program_owner = ProgramOwner::read_le(&mut reader)?;
26 // Read the deployment.
27 let deployment = Deployment::read_le(&mut reader)?;
28 // Return the rejected deployment.
29 Ok(Self::new_deployment(program_owner, deployment))
30 }
31 1 => {
32 // Read the execution.
33 let execution = Execution::read_le(&mut reader)?;
34 // Return the rejected execution.
35 Ok(Self::new_execution(execution))
36 }
37 2.. => Err(error(format!("Failed to decode rejected transaction variant {variant}"))),
38 }
39 }
40}
41
42impl<N: Network> ToBytes for Rejected<N> {
43 /// Writes the rejected transaction to a buffer.
44 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
45 match self {
46 Self::Deployment(program_owner, deployment) => {
47 // Write the variant.
48 0u8.write_le(&mut writer)?;
49 // Write the program owner.
50 program_owner.write_le(&mut writer)?;
51 // Write the deployment.
52 deployment.write_le(&mut writer)
53 }
54 Self::Execution(execution) => {
55 // Write the variant.
56 1u8.write_le(&mut writer)?;
57 // Write the execution.
58 execution.write_le(&mut writer)
59 }
60 }
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn test_bytes() {
70 for expected in crate::transactions::rejected::test_helpers::sample_rejected_transactions() {
71 // Check the byte representation.
72 let expected_bytes = expected.to_bytes_le().unwrap();
73 assert_eq!(expected, Rejected::read_le(&expected_bytes[..]).unwrap());
74 }
75 }
76}