snarkvm_ledger_block/ratifications/
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.
15mod bytes;
16mod merkle;
17mod serialize;
18mod string;
19
20use crate::Ratify;
21use console::{
22    network::prelude::*,
23    program::{RATIFICATIONS_DEPTH, RatificationsPath, RatificationsTree},
24    types::Field,
25};
26
27use indexmap::IndexMap;
28
29#[cfg(not(feature = "serial"))]
30use rayon::prelude::*;
31
32#[derive(Clone, PartialEq, Eq)]
33pub struct Ratifications<N: Network> {
34    /// The ratifications included in a block.
35    ratifications: IndexMap<N::RatificationID, Ratify<N>>,
36}
37
38impl<N: Network> Ratifications<N> {
39    /// Initializes from an iterator of ratifications.
40    pub fn try_from_iter<T: IntoIterator<Item = Ratify<N>>>(iter: T) -> Result<Self> {
41        Ok(Self {
42            ratifications: iter
43                .into_iter()
44                .map(|ratification| Ok::<_, Error>((ratification.to_id()?, ratification)))
45                .collect::<Result<IndexMap<_, _>, _>>()?,
46        })
47    }
48}
49
50impl<N: Network> TryFrom<Vec<Ratify<N>>> for Ratifications<N> {
51    type Error = Error;
52
53    /// Initializes from a given ratifications list.
54    fn try_from(ratifications: Vec<Ratify<N>>) -> Result<Self> {
55        Self::try_from_iter(ratifications)
56    }
57}
58
59impl<N: Network> TryFrom<&Vec<Ratify<N>>> for Ratifications<N> {
60    type Error = Error;
61
62    /// Initializes from a given ratifications list.
63    fn try_from(ratifications: &Vec<Ratify<N>>) -> Result<Self> {
64        Self::try_from(ratifications.as_slice())
65    }
66}
67
68impl<N: Network> TryFrom<&[Ratify<N>]> for Ratifications<N> {
69    type Error = Error;
70
71    /// Initializes from a given ratifications list.
72    fn try_from(ratifications: &[Ratify<N>]) -> Result<Self> {
73        Self::try_from(ratifications.to_vec())
74    }
75}
76
77impl<N: Network> Ratifications<N> {
78    /// Returns `true` if the ratifications contains the given commitment.
79    pub fn contains(&self, ratification_id: &N::RatificationID) -> bool {
80        self.ratifications.contains_key(ratification_id)
81    }
82
83    /// Returns the ratification for the given ratification ID.
84    pub fn get(&self, ratification_id: &N::RatificationID) -> Option<&Ratify<N>> {
85        self.ratifications.get(ratification_id)
86    }
87
88    /// Returns 'true' if there are no ratifications.
89    pub fn is_empty(&self) -> bool {
90        self.ratifications.is_empty()
91    }
92
93    /// Returns the number of ratifications.
94    pub fn len(&self) -> usize {
95        self.ratifications.len()
96    }
97}
98
99impl<N: Network> Ratifications<N> {
100    /// The maximum number of ratifications allowed in a block.
101    pub const MAX_RATIFICATIONS: usize = usize::pow(2, RATIFICATIONS_DEPTH as u32);
102
103    /// Returns an iterator over all ratifications, for all ratifications in `self`.
104    pub fn iter(&self) -> impl '_ + ExactSizeIterator<Item = &Ratify<N>> {
105        self.ratifications.values()
106    }
107
108    /// Returns a parallel iterator over all ratifications, for all ratifications in `self`.
109    #[cfg(not(feature = "serial"))]
110    pub fn par_iter(&self) -> impl '_ + ParallelIterator<Item = &Ratify<N>> {
111        self.ratifications.par_values()
112    }
113
114    /// Returns an iterator over the ratification IDs, for all ratifications in `self`.
115    pub fn ratification_ids(&self) -> impl '_ + ExactSizeIterator<Item = &N::RatificationID> {
116        self.ratifications.keys()
117    }
118}
119
120impl<N: Network> IntoIterator for Ratifications<N> {
121    type IntoIter = indexmap::map::IntoValues<N::RatificationID, Self::Item>;
122    type Item = Ratify<N>;
123
124    /// Returns a consuming iterator over all ratifications, for all ratifications in `self`.
125    fn into_iter(self) -> Self::IntoIter {
126        self.ratifications.into_values()
127    }
128}
129
130impl<N: Network> Ratifications<N> {
131    /// Returns a consuming iterator over the ratification IDs, for all ratifications in `self`.
132    pub fn into_ratification_ids(self) -> impl ExactSizeIterator<Item = N::RatificationID> {
133        self.ratifications.into_keys()
134    }
135}
136
137#[cfg(test)]
138pub mod test_helpers {
139    use super::*;
140
141    type CurrentNetwork = console::network::MainnetV0;
142
143    /// Samples a block ratifications.
144    pub(crate) fn sample_block_ratifications(rng: &mut TestRng) -> Ratifications<CurrentNetwork> {
145        Ratifications::try_from(crate::ratify::test_helpers::sample_ratifications(rng)).unwrap()
146    }
147}