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
use crate::{
error::Error,
id::{Proof, PublicId},
network_event::NetworkEvent,
observation::Observation,
vote::Vote,
DkgResult, DkgResultWrapper,
};
use std::{
collections::{vec_deque, BTreeMap, BTreeSet, VecDeque},
ops::{Deref, DerefMut},
};
#[serde(bound = "")]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)]
pub struct Block<T: NetworkEvent, P: PublicId> {
payload: Observation<T, P>,
proofs: BTreeSet<Proof<P>>,
}
impl<T: NetworkEvent, P: PublicId> Block<T, P> {
pub fn new_dkg_block((participants, dkg_result): (BTreeSet<P>, DkgResult)) -> Self {
Self {
payload: Observation::DkgResult {
participants,
dkg_result: DkgResultWrapper(dkg_result),
},
proofs: BTreeSet::new(),
}
}
pub fn new(votes: &BTreeMap<P, Vote<T, P>>) -> Result<Self, Error> {
let payload = if let Some(vote) = votes.values().next() {
vote.payload().clone()
} else {
return Err(Error::MissingVotes);
};
let proofs: Result<BTreeSet<_>, _> = votes
.iter()
.map(|(public_id, vote)| {
if *vote.payload() == payload {
vote.create_proof(public_id)
} else {
Err(Error::MismatchedPayload)
}
})
.collect();
let proofs = proofs?;
Ok(Self { payload, proofs })
}
pub fn payload(&self) -> &Observation<T, P> {
&self.payload
}
pub fn proofs(&self) -> &BTreeSet<Proof<P>> {
&self.proofs
}
pub fn is_signed_by(&self, peer_id: &P) -> bool {
self.proofs.iter().any(|proof| proof.public_id() == peer_id)
}
pub fn add_vote(&mut self, peer_id: &P, vote: &Vote<T, P>) -> Result<bool, Error> {
if &self.payload != vote.payload() {
return Err(Error::MismatchedPayload);
}
let proof = vote.create_proof(peer_id)?;
Ok(self.proofs.insert(proof))
}
}
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub(crate) struct BlockGroup<T: NetworkEvent, P: PublicId>(pub VecDeque<Block<T, P>>);
impl<T: NetworkEvent, P: PublicId> BlockGroup<T, P> {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl<T: NetworkEvent, P: PublicId> IntoIterator for BlockGroup<T, P> {
type Item = Block<T, P>;
type IntoIter = vec_deque::IntoIter<Block<T, P>>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}
impl<'a, T: NetworkEvent, P: PublicId> IntoIterator for &'a BlockGroup<T, P> {
type Item = &'a Block<T, P>;
type IntoIter = vec_deque::Iter<'a, Block<T, P>>;
fn into_iter(self) -> Self::IntoIter {
self.0.iter()
}
}
impl<T: NetworkEvent, P: PublicId> Deref for BlockGroup<T, P> {
type Target = VecDeque<Block<T, P>>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T: NetworkEvent, P: PublicId> DerefMut for BlockGroup<T, P> {
fn deref_mut(&mut self) -> &mut VecDeque<Block<T, P>> {
&mut self.0
}
}