forked from stellar/stellar-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInferredQuorum.cpp
201 lines (188 loc) · 4.9 KB
/
InferredQuorum.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
#include "history/InferredQuorum.h"
#include "crypto/SHA.h"
#include "util/Logging.h"
#include "xdrpp/marshal.h"
#include <fstream>
#include <sstream>
namespace stellar
{
InferredQuorum::InferredQuorum()
{
}
InferredQuorum::InferredQuorum(QuorumTracker::QuorumMap const& qmap)
{
for (auto const& pair : qmap)
{
notePubKey(pair.first);
if (pair.second)
{
noteQset(*pair.second);
Hash qSetHash = sha256(xdr::xdr_to_opaque(*pair.second));
noteQsetHash(pair.first, qSetHash);
}
}
}
void
InferredQuorum::noteSCPHistory(SCPHistoryEntry const& hist)
{
for (auto const& qset : hist.v0().quorumSets)
{
noteQset(qset);
}
for (auto const& msg : hist.v0().ledgerMessages.messages)
{
auto pk = msg.statement.nodeID;
notePubKey(pk);
auto const& pledges = msg.statement.pledges;
switch (pledges.type())
{
case SCP_ST_PREPARE:
noteQsetHash(pk, pledges.prepare().quorumSetHash);
break;
case SCP_ST_CONFIRM:
noteQsetHash(pk, pledges.confirm().quorumSetHash);
break;
case SCP_ST_EXTERNALIZE:
noteQsetHash(pk, pledges.externalize().commitQuorumSetHash);
break;
case SCP_ST_NOMINATE:
noteQsetHash(pk, pledges.nominate().quorumSetHash);
break;
}
}
}
void
InferredQuorum::noteQsetHash(PublicKey const& pk, Hash const& qsetHash)
{
auto& v = mQsetHashes[pk];
for (auto const& h : v)
{
if (h == qsetHash)
{
// Already noted, quit now.
return;
}
}
v.emplace_back(qsetHash);
}
void
InferredQuorum::noteQset(SCPQuorumSet const& qset)
{
Hash qSetHash = sha256(xdr::xdr_to_opaque(qset));
if (mQsets.find(qSetHash) == mQsets.end())
{
mQsets.insert(std::make_pair(qSetHash, qset));
}
for (auto const& pk : qset.validators)
{
notePubKey(pk);
}
for (auto const& inner : qset.innerSets)
{
noteQset(inner);
}
}
void
InferredQuorum::notePubKey(PublicKey const& pk)
{
mPubKeys[pk]++;
}
std::string
InferredQuorum::toString(Config const& cfg) const
{
std::ostringstream out;
// By default we will emit only those keys involved in half or
// more of the quorums we've observed them in. This could be made
// more clever.
size_t thresh = 0;
for (auto const& pair : mPubKeys)
{
thresh = pair.second > thresh ? pair.second : thresh;
}
thresh >>= 2;
for (auto const& pair : mPubKeys)
{
auto isAlias = false;
auto name = cfg.toStrKey(pair.first, isAlias);
if (pair.second < thresh)
{
out << "# skipping unreliable "
<< "(" << pair.second << "/" << thresh << ") node: " << '"'
<< (isAlias ? "$" : "") << name << '"' << std::endl;
}
}
out << "[QUORUM_SET]" << std::endl;
out << "[" << std::endl;
auto first = true;
for (auto const& pair : mPubKeys)
{
if (pair.second < thresh)
{
continue;
}
auto isAlias = false;
auto name = cfg.toStrKey(pair.first, isAlias);
if (first)
{
first = false;
}
else
{
out << "," << std::endl;
}
out << '"' << (isAlias ? "$" : "") << name << '"';
}
out << std::endl << "]" << std::endl;
return out.str();
}
void
InferredQuorum::writeQuorumGraph(Config const& cfg, std::ostream& out) const
{
out << "digraph {" << std::endl;
for (auto const& pkq : mQsetHashes)
{
for (auto pkqv : pkq.second)
{
auto qp = mQsets.find(pkqv);
if (qp != mQsets.end())
{
auto src = cfg.toShortString(pkq.first);
for (auto const& dst : qp->second.validators)
{
out << src << " -> " << cfg.toShortString(dst) << ";"
<< std::endl;
}
for (auto const& iqs : qp->second.innerSets)
{
for (auto const& dst : iqs.validators)
{
out << src << " -> " << cfg.toShortString(dst) << ";"
<< std::endl;
}
}
}
}
}
out << "}" << std::endl;
}
QuorumTracker::QuorumMap
InferredQuorum::getQuorumMap() const
{
QuorumTracker::QuorumMap qm;
for (auto const& pair : mQsetHashes)
{
qm[pair.first] = nullptr;
for (auto i = pair.second.rbegin(); i != pair.second.rend(); ++i)
{
auto qi = mQsets.find(*i);
if (qi != mQsets.end())
{
SCPQuorumSetPtr p = std::make_shared<SCPQuorumSet>(qi->second);
qm[pair.first] = p;
break;
}
}
}
return qm;
}
}