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
use super::{AckedIndexer, VoteResult};
use crate::util::Union;
use crate::HashSet;
use crate::MajorityConfig;
use std::cmp;
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Configuration {
pub(crate) incoming: MajorityConfig,
pub(crate) outgoing: MajorityConfig,
}
impl Configuration {
pub fn new(voters: HashSet<u64>) -> Configuration {
Configuration {
incoming: MajorityConfig::new(voters),
outgoing: MajorityConfig::default(),
}
}
#[cfg(test)]
pub(crate) fn new_joint_from_majorities(
incoming: MajorityConfig,
outgoing: MajorityConfig,
) -> Self {
Self { incoming, outgoing }
}
pub fn with_capacity(cap: usize) -> Configuration {
Configuration {
incoming: MajorityConfig::with_capacity(cap),
outgoing: MajorityConfig::default(),
}
}
pub fn committed_index(&self, use_group_commit: bool, l: &impl AckedIndexer) -> (u64, bool) {
let (i_idx, i_use_gc) = self.incoming.committed_index(use_group_commit, l);
let (o_idx, o_use_gc) = self.outgoing.committed_index(use_group_commit, l);
(cmp::min(i_idx, o_idx), i_use_gc && o_use_gc)
}
pub fn vote_result(&self, check: impl Fn(u64) -> Option<bool>) -> VoteResult {
let i = self.incoming.vote_result(&check);
let o = self.outgoing.vote_result(check);
match (i, o) {
(VoteResult::Won, VoteResult::Won) => VoteResult::Won,
(VoteResult::Lost, _) | (_, VoteResult::Lost) => VoteResult::Lost,
_ => VoteResult::Pending,
}
}
pub fn clear(&mut self) {
self.incoming.clear();
self.outgoing.clear();
}
pub fn is_singleton(&self) -> bool {
self.outgoing.is_empty() && self.incoming.len() == 1
}
pub fn ids(&self) -> Union<'_> {
Union::new(&self.incoming, &self.outgoing)
}
#[inline]
pub fn contains(&self, id: u64) -> bool {
self.incoming.contains(&id) || self.outgoing.contains(&id)
}
#[cfg(test)]
pub(crate) fn describe(&self, l: &impl AckedIndexer) -> String {
MajorityConfig::new(self.ids().iter().collect()).describe(l)
}
}