snarkvm_ledger_block/transaction/fee/
serialize.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> Serialize for Fee<N> {
19    /// Serializes the fee into string or bytes.
20    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
21        match serializer.is_human_readable() {
22            true => {
23                let mut fee = serializer.serialize_struct("Fee", 2 + self.proof.is_some() as usize)?;
24                fee.serialize_field("transition", &self.transition)?;
25                fee.serialize_field("global_state_root", &self.global_state_root)?;
26                if let Some(proof) = &self.proof {
27                    fee.serialize_field("proof", proof)?;
28                }
29                fee.end()
30            }
31            false => ToBytesSerializer::serialize_with_size_encoding(self, serializer),
32        }
33    }
34}
35
36impl<'de, N: Network> Deserialize<'de> for Fee<N> {
37    /// Deserializes the fee from a string or bytes.
38    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
39        match deserializer.is_human_readable() {
40            true => {
41                // Parse the fee from a string into a value.
42                let mut fee = serde_json::Value::deserialize(deserializer)?;
43                // Retrieve the transitions.
44                let transition = DeserializeExt::take_from_value::<D>(&mut fee, "transition")?;
45                // Retrieve the global state root.
46                let global_state_root = DeserializeExt::take_from_value::<D>(&mut fee, "global_state_root")?;
47                // Retrieve the proof.
48                let proof = DeserializeExt::take_from_value::<D>(&mut fee, "proof")?;
49                // Recover the fee.
50                Self::from(transition, global_state_root, proof).map_err(de::Error::custom)
51            }
52            false => FromBytesDeserializer::<Self>::deserialize_with_size_encoding(deserializer, "fee"),
53        }
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn test_serde_json() -> Result<()> {
63        let rng = &mut TestRng::default();
64
65        // Sample the private fee.
66        let expected = crate::transaction::fee::test_helpers::sample_fee_private_hardcoded(rng);
67
68        // Serialize
69        let expected_string = &expected.to_string();
70        let candidate_string = serde_json::to_string(&expected)?;
71        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
72
73        // Deserialize
74        assert_eq!(expected, Fee::from_str(expected_string)?);
75        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
76
77        // Sample the public fee.
78        let expected = crate::transaction::fee::test_helpers::sample_fee_public_hardcoded(rng);
79
80        // Serialize
81        let expected_string = &expected.to_string();
82        let candidate_string = serde_json::to_string(&expected)?;
83        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
84
85        // Deserialize
86        assert_eq!(expected, Fee::from_str(expected_string)?);
87        assert_eq!(expected, serde_json::from_str(&candidate_string)?);
88
89        Ok(())
90    }
91
92    #[test]
93    fn test_bincode() -> Result<()> {
94        let rng = &mut TestRng::default();
95
96        // Sample the private fee.
97        let expected = crate::transaction::fee::test_helpers::sample_fee_private_hardcoded(rng);
98
99        // Serialize
100        let expected_bytes = expected.to_bytes_le()?;
101        let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
102        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
103
104        // Deserialize
105        assert_eq!(expected, Fee::read_le(&expected_bytes[..])?);
106        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
107
108        // Sample the public fee.
109        let expected = crate::transaction::fee::test_helpers::sample_fee_public_hardcoded(rng);
110
111        // Serialize
112        let expected_bytes = expected.to_bytes_le()?;
113        let expected_bytes_with_size_encoding = bincode::serialize(&expected)?;
114        assert_eq!(&expected_bytes[..], &expected_bytes_with_size_encoding[8..]);
115
116        // Deserialize
117        assert_eq!(expected, Fee::read_le(&expected_bytes[..])?);
118        assert_eq!(expected, bincode::deserialize(&expected_bytes_with_size_encoding[..])?);
119
120        Ok(())
121    }
122}