snarkvm_ledger_block/transaction/fee/
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 Fee<N> {
19    /// Reads the fee 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 fee version"));
26        }
27        // Read the transition.
28        let transition = Transition::read_le(&mut reader)?;
29        // Read the global state root.
30        let global_state_root = N::StateRoot::read_le(&mut reader)?;
31        // Read the proof variant.
32        let proof_variant = u8::read_le(&mut reader)?;
33        // Read the proof.
34        let proof = match proof_variant {
35            0 => None,
36            1 => Some(Proof::read_le(&mut reader)?),
37            _ => return Err(error(format!("Invalid proof variant '{proof_variant}'"))),
38        };
39        // Return the new `Fee` instance.
40        Self::from(transition, global_state_root, proof).map_err(|e| error(e.to_string()))
41    }
42}
43
44impl<N: Network> ToBytes for Fee<N> {
45    /// Writes the fee to a buffer.
46    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
47        // Write the version.
48        1u8.write_le(&mut writer)?;
49        // Write the transition.
50        self.transition.write_le(&mut writer)?;
51        // Write the global state root.
52        self.global_state_root.write_le(&mut writer)?;
53        // Write the proof.
54        match self.proof {
55            None => 0u8.write_le(&mut writer)?,
56            Some(ref proof) => {
57                1u8.write_le(&mut writer)?;
58                proof.write_le(&mut writer)?;
59            }
60        }
61        Ok(())
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_bytes() -> Result<()> {
71        let rng = &mut TestRng::default();
72
73        // Construct a new private fee.
74        let expected = crate::transaction::fee::test_helpers::sample_fee_private_hardcoded(rng);
75
76        // Check the byte representation.
77        let expected_bytes = expected.to_bytes_le()?;
78        assert_eq!(expected, Fee::read_le(&expected_bytes[..])?);
79
80        // Construct a new public fee.
81        let expected = crate::transaction::fee::test_helpers::sample_fee_public_hardcoded(rng);
82
83        // Check the byte representation.
84        let expected_bytes = expected.to_bytes_le()?;
85        assert_eq!(expected, Fee::read_le(&expected_bytes[..])?);
86
87        Ok(())
88    }
89}