Skip to main content

snarkvm_ledger_block/ratify/
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 Ratify<N> {
19    /// Reads the ratify object 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 ratify version"));
26        }
27
28        let variant = Variant::read_le(&mut reader)?;
29        let ratify = match variant {
30            0 => {
31                // Read the committee.
32                let committee: Committee<N> = FromBytes::read_le(&mut reader)?;
33                // Read the number of public balances.
34                let num_public_balances: u16 = FromBytes::read_le(&mut reader)?;
35                // Read the public balances.
36                let mut public_balances = PublicBalances::with_capacity(num_public_balances as usize);
37                for _ in 0..num_public_balances {
38                    // Read the address.
39                    let address: Address<N> = FromBytes::read_le(&mut reader)?;
40                    // Read the amount.
41                    let amount: u64 = FromBytes::read_le(&mut reader)?;
42                    // Insert the public balance.
43                    public_balances.insert(address, amount);
44                }
45                // Read the number of bonded balances.
46                let num_bonded_balances: u16 = FromBytes::read_le(&mut reader)?;
47                // Read the bonded balances.
48                let mut bonded_balances = BondedBalances::with_capacity(num_bonded_balances as usize);
49                for _ in 0..num_bonded_balances {
50                    // Read the address.
51                    let address: Address<N> = FromBytes::read_le(&mut reader)?;
52                    // Read the validator address.
53                    let validator_address: Address<N> = FromBytes::read_le(&mut reader)?;
54                    // Read the withdrawal address.
55                    let withdrawal_address: Address<N> = FromBytes::read_le(&mut reader)?;
56                    // Read the amount.
57                    let amount: u64 = FromBytes::read_le(&mut reader)?;
58                    // Insert the bonded balance.
59                    bonded_balances.insert(address, (validator_address, withdrawal_address, amount));
60                }
61                // Return the ratify object.
62                Self::Genesis(Box::new(committee), Box::new(public_balances), Box::new(bonded_balances))
63            }
64            1 => {
65                // Read the amount.
66                let amount: u64 = FromBytes::read_le(&mut reader)?;
67                // Return the ratify object.
68                Self::BlockReward(amount)
69            }
70            2 => {
71                // Read the amount.
72                let amount: u64 = FromBytes::read_le(&mut reader)?;
73                // Return the ratify object.
74                Self::PuzzleReward(amount)
75            }
76            3.. => return Err(error(format!("Failed to decode ratify object variant {variant}"))),
77        };
78        Ok(ratify)
79    }
80}
81
82impl<N: Network> ToBytes for Ratify<N> {
83    /// Writes the ratify object to a buffer.
84    fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
85        // Write the version.
86        1u8.write_le(&mut writer)?;
87
88        match self {
89            Self::Genesis(committee, public_balances, bonded_balances) => {
90                (0 as Variant).write_le(&mut writer)?;
91                committee.write_le(&mut writer)?;
92                u16::try_from(public_balances.len()).map_err(|e| error(e.to_string()))?.write_le(&mut writer)?;
93                for (address, amount) in public_balances.iter() {
94                    address.write_le(&mut writer)?;
95                    amount.write_le(&mut writer)?;
96                }
97                u16::try_from(bonded_balances.len()).map_err(|e| error(e.to_string()))?.write_le(&mut writer)?;
98                for (address, (validator_address, withdrawal_address, amount)) in bonded_balances.iter() {
99                    address.write_le(&mut writer)?;
100                    validator_address.write_le(&mut writer)?;
101                    withdrawal_address.write_le(&mut writer)?;
102                    amount.write_le(&mut writer)?;
103                }
104                Ok(())
105            }
106            Self::BlockReward(amount) => {
107                (1 as Variant).write_le(&mut writer)?;
108                amount.write_le(&mut writer)
109            }
110            Self::PuzzleReward(amount) => {
111                (2 as Variant).write_le(&mut writer)?;
112                amount.write_le(&mut writer)
113            }
114        }
115    }
116}
117
118#[cfg(test)]
119mod tests {
120    use super::*;
121
122    #[test]
123    fn test_bytes() {
124        let rng = &mut TestRng::default();
125
126        for expected in crate::ratify::test_helpers::sample_ratifications(rng) {
127            // Check the byte representation.
128            let expected_bytes = expected.to_bytes_le().unwrap();
129            assert_eq!(expected, Ratify::read_le(&expected_bytes[..]).unwrap());
130        }
131    }
132}