forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuorumSetUtils.cpp
241 lines (212 loc) · 5.71 KB
/
QuorumSetUtils.cpp
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
#include "QuorumSetUtils.h"
#include "util/XDROperators.h"
#include "xdr/Stellar-SCP.h"
#include "xdr/Stellar-types.h"
#include <algorithm>
#include <set>
namespace stellar
{
namespace
{
class QuorumSetSanityChecker
{
public:
explicit QuorumSetSanityChecker(SCPQuorumSet const& qSet, bool extraChecks);
bool
isSane() const
{
return mIsSane;
}
private:
bool mExtraChecks;
std::set<NodeID> mKnownNodes;
bool mIsSane;
size_t mCount{0};
bool checkSanity(SCPQuorumSet const& qSet, int depth);
};
QuorumSetSanityChecker::QuorumSetSanityChecker(SCPQuorumSet const& qSet,
bool extraChecks)
: mExtraChecks{extraChecks}
{
mIsSane = checkSanity(qSet, 0) && mCount >= 1 && mCount <= 1000;
}
bool
QuorumSetSanityChecker::checkSanity(SCPQuorumSet const& qSet, int depth)
{
if (depth > 2)
return false;
if (qSet.threshold < 1)
return false;
auto& v = qSet.validators;
auto& i = qSet.innerSets;
size_t totEntries = v.size() + i.size();
size_t vBlockingSize = totEntries - qSet.threshold + 1;
mCount += v.size();
if (qSet.threshold > totEntries)
return false;
// threshold is within the proper range
if (mExtraChecks && qSet.threshold < vBlockingSize)
return false;
for (auto const& n : v)
{
auto r = mKnownNodes.insert(n);
if (!r.second)
{
// n was already present
return false;
}
}
for (auto const& iSet : i)
{
if (!checkSanity(iSet, depth + 1))
{
return false;
}
}
return true;
}
}
bool
isQuorumSetSane(SCPQuorumSet const& qSet, bool extraChecks)
{
QuorumSetSanityChecker checker{qSet, extraChecks};
return checker.isSane();
}
namespace
{
// helper function that:
// * removes nodeID
// { t: n, v: { ...BEFORE... , nodeID, ...AFTER... }, ...}
// { t: n-1, v: { ...BEFORE..., ...AFTER...} , ... }
// * simplifies singleton inner set into outerset
// { t: n, v: { ... }, { t: 1, X }, ... }
// into
// { t: n, v: { ..., X }, .... }
// * simplifies singleton innersets
// { t:1, { innerSet } } into innerSet
void
normalizeQSetSimplify(SCPQuorumSet& qSet, NodeID const* idToRemove)
{
using xdr::operator==;
auto& v = qSet.validators;
if (idToRemove)
{
auto it_v = std::remove_if(v.begin(), v.end(), [&](NodeID const& n) {
return n == *idToRemove;
});
qSet.threshold -= uint32(v.end() - it_v);
v.erase(it_v, v.end());
}
auto& i = qSet.innerSets;
auto it = i.begin();
while (it != i.end())
{
normalizeQSetSimplify(*it, idToRemove);
// merge singleton inner sets into validator list
if (it->threshold == 1 && it->validators.size() == 1 &&
it->innerSets.size() == 0)
{
v.emplace_back(it->validators.front());
it = i.erase(it);
}
else
{
it++;
}
}
// simplify quorum set if needed
if (qSet.threshold == 1 && v.size() == 0 && i.size() == 1)
{
auto t = qSet.innerSets.back();
qSet = t;
}
}
template <typename InputIt1, typename InputIt2, class Compare>
int
intLexicographicalCompare(InputIt1 first1, InputIt1 last1, InputIt2 first2,
InputIt2 last2, Compare comp)
{
for (; first1 != last1 && first2 != last2; first1++, first2++)
{
auto c = comp(*first1, *first2);
if (c != 0)
{
return c;
}
}
if (first1 == last1 && first2 != last2)
{
return -1;
}
if (first1 != last1 && first2 == last2)
{
return 1;
}
return 0;
}
// returns -1 if l < r ; 0 if l == r ; 1 if l > r
// lexicographical sort
// looking at, in order: validators, innerSets, threshold
int
qSetCompareInt(SCPQuorumSet const& l, SCPQuorumSet const& r)
{
auto& lvals = l.validators;
auto& rvals = r.validators;
// compare by validators first
auto res = intLexicographicalCompare(
lvals.begin(), lvals.end(), rvals.begin(), rvals.end(),
[](PublicKey const& l, PublicKey const& r) {
if (l < r)
{
return -1;
}
if (r < l)
{
return 1;
}
return 0;
});
if (res != 0)
{
return res;
}
// then compare by inner sets
auto const& li = l.innerSets;
auto const& ri = r.innerSets;
res = intLexicographicalCompare(li.begin(), li.end(), ri.begin(), ri.end(),
qSetCompareInt);
if (res != 0)
{
return res;
}
// compare by threshold
return (l.threshold < r.threshold) ? -1
: ((l.threshold == r.threshold) ? 0 : 1);
}
// helper function that reorders validators and inner sets
// in a standard way
void
normalizeQuorumSetReorder(SCPQuorumSet& qset)
{
std::sort(qset.validators.begin(), qset.validators.end());
for (auto& qs : qset.innerSets)
{
normalizeQuorumSetReorder(qs);
}
// now, we can reorder the inner sets
std::sort(qset.innerSets.begin(), qset.innerSets.end(),
[](SCPQuorumSet const& l, SCPQuorumSet const& r) {
return qSetCompareInt(l, r) < 0;
});
}
}
void
normalizeQSet(SCPQuorumSet& qSet, NodeID const* idToRemove)
{
normalizeQSetSimplify(qSet, idToRemove);
normalizeQuorumSetReorder(qSet);
}
}