fedimint_hbbft/dynamic_honey_badger/
batch.rs1use std::collections::BTreeMap;
2use std::sync::Arc;
3
4use super::{ChangeState, JoinPlan, Params};
5use crate::{NetworkInfo, NodeIdT, PubKeyMap};
6
7#[derive(Clone, Debug)]
9pub struct Batch<C, N: Ord> {
10 pub(super) epoch: u64,
12 pub(super) era: u64,
14 pub(super) contributions: BTreeMap<N, C>,
16 pub(super) change: ChangeState<N>,
19 pub(super) pub_keys: PubKeyMap<N>,
21 pub(super) netinfo: Arc<NetworkInfo<N>>,
23 pub(super) params: Params,
25}
26
27impl<C, N: NodeIdT> Batch<C, N> {
28 pub fn epoch(&self) -> u64 {
30 self.epoch
31 }
32
33 pub fn era(&self) -> u64 {
35 self.era
36 }
37
38 pub fn change(&self) -> &ChangeState<N> {
41 &self.change
42 }
43
44 pub fn public_keys(&self) -> &PubKeyMap<N> {
46 &self.pub_keys
47 }
48
49 pub fn network_info(&self) -> &Arc<NetworkInfo<N>> {
52 &self.netinfo
53 }
54
55 pub fn contributions(&self) -> impl Iterator<Item = (&N, &C)> {
57 self.contributions.iter()
58 }
59
60 pub fn iter<'a>(&'a self) -> impl Iterator<Item = <&'a C as IntoIterator>::Item>
62 where
63 &'a C: IntoIterator,
64 {
65 self.contributions.values().flatten()
66 }
67
68 pub fn into_tx_iter(self) -> impl Iterator<Item = <C as IntoIterator>::Item>
70 where
71 C: IntoIterator,
72 {
73 self.contributions.into_iter().flat_map(|(_, vec)| vec)
74 }
75
76 pub fn len<T>(&self) -> usize
78 where
79 C: AsRef<[T]>,
80 {
81 self.contributions
82 .values()
83 .map(C::as_ref)
84 .map(<[T]>::len)
85 .sum()
86 }
87
88 pub fn is_empty<T>(&self) -> bool
90 where
91 C: AsRef<[T]>,
92 {
93 self.contributions
94 .values()
95 .map(C::as_ref)
96 .all(<[T]>::is_empty)
97 }
98
99 pub fn join_plan(&self) -> Option<JoinPlan<N>> {
102 if self.change == ChangeState::None {
103 return None;
104 }
105 Some(JoinPlan {
106 era: self.epoch + 1,
107 change: self.change.clone(),
108 pub_keys: self.pub_keys.clone(),
109 pub_key_set: self.netinfo.public_key_set().clone(),
110 params: self.params.clone(),
111 })
112 }
113
114 pub fn public_eq(&self, other: &Self) -> bool
117 where
118 C: PartialEq,
119 {
120 self.epoch == other.epoch
121 && self.era == other.era
122 && self.contributions == other.contributions
123 && self.change == other.change
124 && self.pub_keys == other.pub_keys
125 && self.netinfo.public_key_set() == other.netinfo.public_key_set()
126 && self.params == other.params
127 }
128}