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> {
ratifications: IndexMap<N::RatificationID, Ratify<N>>,
}
impl<N: Network> Ratifications<N> {
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;
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;
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;
fn try_from(ratifications: &[Ratify<N>]) -> Result<Self> {
Self::try_from(ratifications.to_vec())
}
}
impl<N: Network> Ratifications<N> {
pub fn contains(&self, ratification_id: &N::RatificationID) -> bool {
self.ratifications.contains_key(ratification_id)
}
pub fn get(&self, ratification_id: &N::RatificationID) -> Option<&Ratify<N>> {
self.ratifications.get(ratification_id)
}
pub fn is_empty(&self) -> bool {
self.ratifications.is_empty()
}
pub fn len(&self) -> usize {
self.ratifications.len()
}
}
impl<N: Network> Ratifications<N> {
pub const MAX_RATIFICATIONS: usize = usize::pow(2, RATIFICATIONS_DEPTH as u32);
pub fn iter(&self) -> impl '_ + ExactSizeIterator<Item = &Ratify<N>> {
self.ratifications.values()
}
#[cfg(not(feature = "serial"))]
pub fn par_iter(&self) -> impl '_ + ParallelIterator<Item = &Ratify<N>> {
self.ratifications.par_values()
}
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>;
fn into_iter(self) -> Self::IntoIter {
self.ratifications.into_values()
}
}
impl<N: Network> Ratifications<N> {
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;
pub(crate) fn sample_block_ratifications(rng: &mut TestRng) -> Ratifications<CurrentNetwork> {
Ratifications::try_from(crate::ratify::test_helpers::sample_ratifications(rng)).unwrap()
}
}