1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (C) 2019-2023 Aleo Systems Inc.
// This file is part of the snarkVM library.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
mod bytes;
mod merkle;
mod serialize;
mod string;

use crate::Ratify;
use console::{
    network::prelude::*,
    program::{RatificationsPath, RatificationsTree, RATIFICATIONS_DEPTH},
    types::Field,
};

use indexmap::IndexMap;

#[cfg(not(feature = "serial"))]
use rayon::prelude::*;

#[derive(Clone, PartialEq, Eq)]
pub struct Ratifications<N: Network> {
    /// The ratifications included in a block.
    ratifications: IndexMap<N::RatificationID, Ratify<N>>,
}

impl<N: Network> Ratifications<N> {
    /// Initializes from an iterator of ratifications.
    pub fn try_from_iter<T: IntoIterator<Item = Ratify<N>>>(iter: T) -> Result<Self> {
        Ok(Self {
            ratifications: iter
                .into_iter()
                .map(|ratification| Ok::<_, Error>((ratification.to_id()?, ratification)))
                .collect::<Result<IndexMap<_, _>, _>>()?,
        })
    }
}

impl<N: Network> TryFrom<Vec<Ratify<N>>> for Ratifications<N> {
    type Error = Error;

    /// Initializes from a given ratifications list.
    fn try_from(ratifications: Vec<Ratify<N>>) -> Result<Self> {
        Self::try_from_iter(ratifications.into_iter())
    }
}

impl<N: Network> TryFrom<&Vec<Ratify<N>>> for Ratifications<N> {
    type Error = Error;

    /// Initializes from a given ratifications list.
    fn try_from(ratifications: &Vec<Ratify<N>>) -> Result<Self> {
        Self::try_from(ratifications.as_slice())
    }
}

impl<N: Network> TryFrom<&[Ratify<N>]> for Ratifications<N> {
    type Error = Error;

    /// Initializes from a given ratifications list.
    fn try_from(ratifications: &[Ratify<N>]) -> Result<Self> {
        Self::try_from(ratifications.to_vec())
    }
}

impl<N: Network> Ratifications<N> {
    /// Returns `true` if the ratifications contains the given commitment.
    pub fn contains(&self, ratification_id: &N::RatificationID) -> bool {
        self.ratifications.contains_key(ratification_id)
    }

    /// Returns the ratification for the given ratification ID.
    pub fn get(&self, ratification_id: &N::RatificationID) -> Option<&Ratify<N>> {
        self.ratifications.get(ratification_id)
    }

    /// Returns 'true' if there are no ratifications.
    pub fn is_empty(&self) -> bool {
        self.ratifications.is_empty()
    }

    /// Returns the number of ratifications.
    pub fn len(&self) -> usize {
        self.ratifications.len()
    }
}

impl<N: Network> Ratifications<N> {
    /// The maximum number of ratifications allowed in a block.
    pub const MAX_RATIFICATIONS: usize = usize::pow(2, RATIFICATIONS_DEPTH as u32);

    /// Returns an iterator over all ratifications, for all ratifications in `self`.
    pub fn iter(&self) -> impl '_ + ExactSizeIterator<Item = &Ratify<N>> {
        self.ratifications.values()
    }

    /// Returns a parallel iterator over all ratifications, for all ratifications in `self`.
    #[cfg(not(feature = "serial"))]
    pub fn par_iter(&self) -> impl '_ + ParallelIterator<Item = &Ratify<N>> {
        self.ratifications.par_values()
    }

    /// Returns an iterator over the ratification IDs, for all ratifications in `self`.
    pub fn ratification_ids(&self) -> impl '_ + ExactSizeIterator<Item = &N::RatificationID> {
        self.ratifications.keys()
    }
}

impl<N: Network> IntoIterator for Ratifications<N> {
    type IntoIter = indexmap::map::IntoValues<N::RatificationID, Self::Item>;
    type Item = Ratify<N>;

    /// Returns a consuming iterator over all ratifications, for all ratifications in `self`.
    fn into_iter(self) -> Self::IntoIter {
        self.ratifications.into_values()
    }
}

impl<N: Network> Ratifications<N> {
    /// Returns a consuming iterator over the ratification IDs, for all ratifications in `self`.
    pub fn into_ratification_ids(self) -> impl ExactSizeIterator<Item = N::RatificationID> {
        self.ratifications.into_keys()
    }
}

#[cfg(test)]
pub mod test_helpers {
    use super::*;

    type CurrentNetwork = console::network::Testnet3;

    /// Samples a block ratifications.
    pub(crate) fn sample_block_ratifications(rng: &mut TestRng) -> Ratifications<CurrentNetwork> {
        Ratifications::try_from(crate::ratify::test_helpers::sample_ratifications(rng)).unwrap()
    }
}