forked from Bitcoin-ABC/bitcoin-abc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeermanager.cpp
441 lines (356 loc) · 11.7 KB
/
peermanager.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright (c) 2020 The Bitcoin developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <avalanche/peermanager.h>
#include <avalanche/delegation.h>
#include <avalanche/validation.h>
#include <random.h>
#include <validation.h> // For ChainstateActive()
#include <cassert>
namespace avalanche {
bool PeerManager::addNode(NodeId nodeid, const Proof &proof,
const Delegation &delegation) {
auto it = fetchOrCreatePeer(proof);
if (it == peers.end()) {
return false;
}
const PeerId peerid = it->peerid;
DelegationState state;
CPubKey pubkey;
if (!delegation.verify(state, proof, pubkey)) {
return false;
}
auto nit = nodes.find(nodeid);
if (nit == nodes.end()) {
if (!nodes.emplace(nodeid, peerid, std::move(pubkey)).second) {
return false;
}
} else {
const PeerId oldpeerid = nit->peerid;
if (!nodes.modify(nit, [&](Node &n) {
n.peerid = peerid;
n.pubkey = std::move(pubkey);
})) {
return false;
}
// We actually have this node already, we need to update it.
bool success = removeNodeFromPeer(peers.find(oldpeerid));
assert(success);
// Make sure it is not invalidated.
it = peers.find(peerid);
}
bool success = addNodeToPeer(it);
assert(success);
return true;
}
bool PeerManager::addNodeToPeer(const PeerSet::iterator &it) {
assert(it != peers.end());
return peers.modify(it, [&](Peer &p) {
if (p.node_count++ > 0) {
// We are done.
return;
}
// We ned to allocate this peer.
p.index = uint32_t(slots.size());
const uint32_t score = p.getScore();
const uint64_t start = slotCount;
slots.emplace_back(start, score, it->peerid);
slotCount = start + score;
});
}
bool PeerManager::removeNode(NodeId nodeid) {
auto it = nodes.find(nodeid);
if (it == nodes.end()) {
return false;
}
const PeerId peerid = it->peerid;
nodes.erase(it);
// Keep the track of the reference count.
bool success = removeNodeFromPeer(peers.find(peerid));
assert(success);
return true;
}
bool PeerManager::removeNodeFromPeer(const PeerSet::iterator &it,
uint32_t count) {
assert(it != peers.end());
assert(count <= it->node_count);
if (count == 0) {
// This is a NOOP.
return false;
}
const uint32_t new_count = it->node_count - count;
if (!peers.modify(it, [&](Peer &p) { p.node_count = new_count; })) {
return false;
}
if (new_count > 0) {
// We are done.
return true;
}
// There are no more node left, we need to cleanup.
const size_t i = it->index;
assert(i < slots.size());
if (i + 1 == slots.size()) {
slots.pop_back();
slotCount = slots.empty() ? 0 : slots.back().getStop();
} else {
fragmentation += slots[i].getScore();
slots[i] = slots[i].withPeerId(NO_PEER);
}
return true;
}
bool PeerManager::forNode(NodeId nodeid,
std::function<bool(const Node &n)> func) const {
auto it = nodes.find(nodeid);
return it != nodes.end() && func(*it);
}
bool PeerManager::updateNextRequestTime(NodeId nodeid, TimePoint timeout) {
auto it = nodes.find(nodeid);
if (it == nodes.end()) {
return false;
}
return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; });
}
NodeId PeerManager::selectNode() {
for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) {
const PeerId p = selectPeer();
// If we cannot find a peer, it may be due to the fact that it is
// unlikely due to high fragmentation, so compact and retry.
if (p == NO_PEER) {
compact();
continue;
}
// See if that peer has an available node.
auto &nview = nodes.get<next_request_time>();
auto it = nview.lower_bound(boost::make_tuple(p, TimePoint()));
if (it != nview.end() && it->peerid == p &&
it->nextRequestTime <= std::chrono::steady_clock::now()) {
return it->nodeid;
}
}
return NO_NODE;
}
void PeerManager::updatedBlockTip() {
std::vector<PeerId> invalidPeers;
{
LOCK(cs_main);
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip();
for (const auto &p : peers) {
ProofValidationState state;
if (!p.proof.verify(state, coins)) {
invalidPeers.push_back(p.peerid);
}
}
}
for (const auto &pid : invalidPeers) {
removePeer(pid);
}
}
PeerId PeerManager::getPeerId(const Proof &proof) {
auto it = fetchOrCreatePeer(proof);
return it == peers.end() ? NO_PEER : it->peerid;
}
PeerManager::PeerSet::iterator
PeerManager::fetchOrCreatePeer(const Proof &proof) {
{
// Check if we already know of that peer.
auto &pview = peers.get<proof_index>();
auto it = pview.find(proof.getId());
if (it != pview.end()) {
return peers.project<0>(it);
}
}
{
// Reject invalid proof.
LOCK(cs_main);
const CCoinsViewCache &coins = ::ChainstateActive().CoinsTip();
ProofValidationState state;
if (!proof.verify(state, coins)) {
return peers.end();
}
}
// New peer means new peerid!
const PeerId peerid = nextPeerId++;
// Attach UTXOs to this proof.
std::unordered_set<PeerId> conflicting_peerids;
for (const auto &s : proof.getStakes()) {
auto p = utxos.emplace(s.getStake().getUTXO(), peerid);
if (!p.second) {
// We have a collision with an existing proof.
conflicting_peerids.insert(p.first->second);
}
}
// For now, if there is a conflict, just ceanup the mess.
if (conflicting_peerids.size() > 0) {
for (const auto &s : proof.getStakes()) {
auto it = utxos.find(s.getStake().getUTXO());
assert(it != utxos.end());
// We need to delete that one.
if (it->second == peerid) {
utxos.erase(it);
}
}
return peers.end();
}
// We have no peer for this proof, time to create it.
auto inserted = peers.emplace(peerid, proof);
assert(inserted.second);
return inserted.first;
}
bool PeerManager::removePeer(const PeerId peerid) {
auto it = peers.find(peerid);
if (it == peers.end()) {
return false;
}
// Remove all nodes from this peer.
removeNodeFromPeer(it, it->node_count);
// Remove nodes associated with this peer, unless their timeout is still
// active. This ensure that we don't overquery them in case they are
// subsequently added to another peer.
auto &nview = nodes.get<next_request_time>();
nview.erase(nview.lower_bound(boost::make_tuple(peerid, TimePoint())),
nview.upper_bound(boost::make_tuple(
peerid, std::chrono::steady_clock::now())));
// Release UTXOs attached to this proof.
for (const auto &s : it->proof.getStakes()) {
bool deleted = utxos.erase(s.getStake().getUTXO()) > 0;
assert(deleted);
}
peers.erase(it);
return true;
}
PeerId PeerManager::selectPeer() const {
if (slots.empty() || slotCount == 0) {
return NO_PEER;
}
const uint64_t max = slotCount;
for (int retry = 0; retry < SELECT_PEER_MAX_RETRY; retry++) {
size_t i = selectPeerImpl(slots, GetRand(max), max);
if (i != NO_PEER) {
return i;
}
}
return NO_PEER;
}
uint64_t PeerManager::compact() {
// There is nothing to compact.
if (fragmentation == 0) {
return 0;
}
std::vector<Slot> newslots;
newslots.reserve(peers.size());
uint64_t prevStop = 0;
uint32_t i = 0;
for (auto it = peers.begin(); it != peers.end(); it++) {
if (it->node_count == 0) {
continue;
}
newslots.emplace_back(prevStop, it->getScore(), it->peerid);
prevStop = slots[i].getStop();
if (!peers.modify(it, [&](Peer &p) { p.index = i++; })) {
return 0;
}
}
slots = std::move(newslots);
const uint64_t saved = slotCount - prevStop;
slotCount = prevStop;
fragmentation = 0;
return saved;
}
bool PeerManager::verify() const {
uint64_t prevStop = 0;
for (size_t i = 0; i < slots.size(); i++) {
const Slot &s = slots[i];
// Slots must be in correct order.
if (s.getStart() < prevStop) {
return false;
}
prevStop = s.getStop();
// If this is a dead slot, then nothing more needs to be checked.
if (s.getPeerId() == NO_PEER) {
continue;
}
// We have a live slot, verify index.
auto it = peers.find(s.getPeerId());
if (it == peers.end() || it->index != i) {
return false;
}
}
for (const auto &p : peers) {
// Count node attached to this peer.
const auto count_nodes = [&]() {
size_t count = 0;
auto &nview = nodes.get<next_request_time>();
auto begin =
nview.lower_bound(boost::make_tuple(p.peerid, TimePoint()));
auto end =
nview.upper_bound(boost::make_tuple(p.peerid + 1, TimePoint()));
for (auto it = begin; it != end; ++it) {
count++;
}
return count;
};
if (p.node_count != count_nodes()) {
return false;
}
// If there are no nodes attached to this peer, then we are done.
if (p.node_count == 0) {
continue;
}
// The index must point to a slot refering to this peer.
if (p.index >= slots.size() || slots[p.index].getPeerId() != p.peerid) {
return false;
}
// If the score do not match, same thing.
if (slots[p.index].getScore() != p.getScore()) {
return false;
}
}
return true;
}
PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot,
const uint64_t max) {
assert(slot <= max);
size_t begin = 0, end = slots.size();
uint64_t bottom = 0, top = max;
// Try to find the slot using dichotomic search.
while ((end - begin) > 8) {
// The slot we picked in not allocated.
if (slot < bottom || slot >= top) {
return NO_PEER;
}
// Guesstimate the position of the slot.
size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom));
assert(begin <= i && i < end);
// We have a match.
if (slots[i].contains(slot)) {
return slots[i].getPeerId();
}
// We undershooted.
if (slots[i].precedes(slot)) {
begin = i + 1;
if (begin >= end) {
return NO_PEER;
}
bottom = slots[begin].getStart();
continue;
}
// We overshooted.
if (slots[i].follows(slot)) {
end = i;
top = slots[end].getStart();
continue;
}
// We have an unalocated slot.
return NO_PEER;
}
// Enough of that nonsense, let fallback to linear search.
for (size_t i = begin; i < end; i++) {
// We have a match.
if (slots[i].contains(slot)) {
return slots[i].getPeerId();
}
}
// We failed to find a slot, retry.
return NO_PEER;
}
} // namespace avalanche