Skip to main content

snarkvm_ledger_block/ratify/
mod.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
16mod bytes;
17mod serialize;
18mod string;
19
20use console::{network::prelude::*, types::Address};
21use snarkvm_ledger_committee::Committee;
22
23use indexmap::IndexMap;
24
25type Variant = u8;
26/// A helper type to represent the public balances.
27type PublicBalances<N> = IndexMap<Address<N>, u64>;
28/// A helper type to represent the bonded balances, as a
29/// mapping of `staker_address` to `(validator_address, withdrawal_address, amount)`.
30type BondedBalances<N> = IndexMap<Address<N>, (Address<N>, Address<N>, u64)>;
31
32// Note: The size of the `Ratify` object is 32 bytes.
33#[derive(Clone, PartialEq, Eq)]
34pub enum Ratify<N: Network> {
35    /// The genesis.
36    Genesis(Box<Committee<N>>, Box<PublicBalances<N>>, Box<BondedBalances<N>>),
37    /// The block reward.
38    BlockReward(u64),
39    /// The puzzle reward.
40    PuzzleReward(u64),
41}
42
43impl<N: Network> Ratify<N> {
44    /// Returns the ratification ID.
45    pub fn to_id(&self) -> Result<N::RatificationID> {
46        Ok(N::hash_bhp1024(&self.to_bytes_le()?.to_bits_le())?.into())
47    }
48}
49
50#[cfg(test)]
51pub(crate) mod test_helpers {
52    use super::*;
53    use console::network::MainnetV0;
54
55    type CurrentNetwork = MainnetV0;
56
57    pub(crate) fn sample_ratifications(rng: &mut TestRng) -> Vec<Ratify<CurrentNetwork>> {
58        let committee = snarkvm_ledger_committee::test_helpers::sample_committee(rng);
59        let mut public_balances = PublicBalances::new();
60        for (address, _) in committee.members().iter() {
61            public_balances.insert(*address, rng.r#gen());
62        }
63        let bonded_balances = committee
64            .members()
65            .iter()
66            .map(|(address, (amount, _, _))| (*address, (*address, *address, *amount)))
67            .collect();
68
69        vec![
70            Ratify::Genesis(Box::new(committee), Box::new(public_balances), Box::new(bonded_balances)),
71            Ratify::BlockReward(rng.r#gen()),
72            Ratify::PuzzleReward(rng.r#gen()),
73        ]
74    }
75}
76
77#[cfg(test)]
78mod tests {
79    use super::*;
80
81    #[test]
82    fn check_ratify_size() {
83        assert_eq!(std::mem::size_of::<Ratify<console::network::MainnetV0>>(), 32);
84    }
85}