Bitcoin ABC 0.32.6
P2P Digital Currency
peermanager.cpp
Go to the documentation of this file.
1// Copyright (c) 2020 The Bitcoin developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6
12#include <cashaddrenc.h>
13#include <common/args.h>
15#include <logging.h>
16#include <random.h>
17#include <scheduler.h>
18#include <threadsafety.h>
19#include <uint256.h>
20#include <util/fastrange.h>
21#include <util/fs_helpers.h>
22#include <util/strencodings.h>
23#include <util/time.h>
24#include <validation.h> // For ChainstateManager
25
26#include <algorithm>
27#include <cassert>
28#include <limits>
29
30namespace avalanche {
31static constexpr uint64_t PEERS_DUMP_VERSION{1};
32
33bool PeerManager::addNode(NodeId nodeid, const ProofId &proofid) {
34 auto &pview = peers.get<by_proofid>();
35 auto it = pview.find(proofid);
36 if (it == pview.end()) {
37 // If the node exists, it is actually updating its proof to an unknown
38 // one. In this case we need to remove it so it is not both active and
39 // pending at the same time.
40 removeNode(nodeid);
41 pendingNodes.emplace(proofid, nodeid);
42 return false;
43 }
44
45 return addOrUpdateNode(peers.project<0>(it), nodeid);
46}
47
48bool PeerManager::addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid) {
49 assert(it != peers.end());
50
51 const PeerId peerid = it->peerid;
52
53 auto nit = nodes.find(nodeid);
54 if (nit == nodes.end()) {
55 if (!nodes.emplace(nodeid, peerid).second) {
56 return false;
57 }
58 } else {
59 const PeerId oldpeerid = nit->peerid;
60 if (!nodes.modify(nit, [&](Node &n) { n.peerid = peerid; })) {
61 return false;
62 }
63
64 // We actually have this node already, we need to update it.
65 bool success = removeNodeFromPeer(peers.find(oldpeerid));
66 assert(success);
67 }
68
69 // Then increase the node counter, and create the slot if needed
70 bool success = addNodeToPeer(it);
71 assert(success);
72
73 // If the added node was in the pending set, remove it
74 pendingNodes.get<by_nodeid>().erase(nodeid);
75
76 // If the proof was in the dangling pool, remove it
77 const ProofId &proofid = it->getProofId();
78 if (danglingProofPool.getProof(proofid)) {
80 }
81
82 // We know for sure there is at least 1 node. Note that this can fail if
83 // there is more than 1, in this case it's a no-op.
84 shareableProofs.insert(it->proof);
85
86 return true;
87}
88
89bool PeerManager::addNodeToPeer(const PeerSet::iterator &it) {
90 assert(it != peers.end());
91 return peers.modify(it, [&](Peer &p) {
92 if (p.node_count++ > 0) {
93 // We are done.
94 return;
95 }
96
97 // We need to allocate this peer.
98 p.index = uint32_t(slots.size());
99 const uint32_t score = p.getScore();
100 const uint64_t start = slotCount;
101 slots.emplace_back(start, score, it->peerid);
102 slotCount = start + score;
103
104 // Add to our allocated score when we allocate a new peer in the slots
105 connectedPeersScore += score;
106 });
107}
108
110 // Remove all the remote proofs from this node
111 auto &remoteProofsView = remoteProofs.get<by_nodeid>();
112 auto [begin, end] = remoteProofsView.equal_range(nodeid);
113 remoteProofsView.erase(begin, end);
114
115 if (pendingNodes.get<by_nodeid>().erase(nodeid) > 0) {
116 // If this was a pending node, there is nothing else to do.
117 return true;
118 }
119
120 auto it = nodes.find(nodeid);
121 if (it == nodes.end()) {
122 return false;
123 }
124
125 const PeerId peerid = it->peerid;
126 nodes.erase(it);
127
128 // Keep the track of the reference count.
129 bool success = removeNodeFromPeer(peers.find(peerid));
130 assert(success);
131
132 return true;
133}
134
135bool PeerManager::removeNodeFromPeer(const PeerSet::iterator &it,
136 uint32_t count) {
137 // It is possible for nodes to be dangling. If there was an inflight query
138 // when the peer gets removed, the node was not erased. In this case there
139 // is nothing to do.
140 if (it == peers.end()) {
141 return true;
142 }
143
144 assert(count <= it->node_count);
145 if (count == 0) {
146 // This is a NOOP.
147 return false;
148 }
149
150 const uint32_t new_count = it->node_count - count;
151 if (!peers.modify(it, [&](Peer &p) { p.node_count = new_count; })) {
152 return false;
153 }
154
155 if (new_count > 0) {
156 // We are done.
157 return true;
158 }
159
160 // There are no more nodes left, we need to clean up. Remove from the radix
161 // tree (unless it's our local proof), subtract allocated score and remove
162 // from slots.
163 if (!localProof || it->getProofId() != localProof->getId()) {
164 const auto removed = shareableProofs.remove(it->getProofId());
165 assert(removed);
166 }
167
168 const size_t i = it->index;
169 assert(i < slots.size());
170 assert(connectedPeersScore >= slots[i].getScore());
171 connectedPeersScore -= slots[i].getScore();
172
173 if (i + 1 == slots.size()) {
174 slots.pop_back();
175 slotCount = slots.empty() ? 0 : slots.back().getStop();
176 } else {
177 fragmentation += slots[i].getScore();
178 slots[i] = slots[i].withPeerId(NO_PEER);
179 }
180
181 return true;
182}
183
185 SteadyMilliseconds timeout) {
186 auto it = nodes.find(nodeid);
187 if (it == nodes.end()) {
188 return false;
189 }
190
191 return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; });
192}
193
195 auto it = nodes.find(nodeid);
196 if (it == nodes.end()) {
197 return false;
198 }
199
200 return !it->avaproofsSent &&
201 nodes.modify(it, [&](Node &n) { n.avaproofsSent = true; });
202}
203
204static bool isImmatureState(const ProofValidationState &state) {
206}
207
209 PeerId peerid, const std::chrono::seconds &nextTime) {
210 auto it = peers.find(peerid);
211 if (it == peers.end()) {
212 // No such peer
213 return false;
214 }
215
216 // Make sure we don't move the time in the past.
217 peers.modify(it, [&](Peer &p) {
219 std::max(p.nextPossibleConflictTime, nextTime);
220 });
221
222 return it->nextPossibleConflictTime == nextTime;
223}
224
226 auto it = peers.find(peerid);
227 if (it == peers.end()) {
228 // No such peer
229 return false;
230 }
231
232 peers.modify(it, [&](Peer &p) { p.hasFinalized = true; });
233
234 return true;
235}
236
237template <typename ProofContainer>
238void PeerManager::moveToConflictingPool(const ProofContainer &proofs) {
239 auto &peersView = peers.get<by_proofid>();
240 for (const ProofRef &proof : proofs) {
241 auto it = peersView.find(proof->getId());
242 if (it != peersView.end()) {
243 removePeer(it->peerid);
244 }
245
247 }
248}
249
251 ProofRegistrationState &registrationState,
252 RegistrationMode mode) {
253 assert(proof);
254
255 const ProofId &proofid = proof->getId();
256
257 auto invalidate = [&](ProofRegistrationResult result,
258 const std::string &message) {
259 return registrationState.Invalid(
260 result, message, strprintf("proofid: %s", proofid.ToString()));
261 };
262
263 if ((mode != RegistrationMode::FORCE_ACCEPT ||
264 !isInConflictingPool(proofid)) &&
265 exists(proofid)) {
266 // In default mode, we expect the proof to be unknown, i.e. in none of
267 // the pools.
268 // In forced accept mode, the proof can be in the conflicting pool.
270 "proof-already-registered");
271 }
272
273 if (danglingProofPool.getProof(proofid) &&
274 pendingNodes.count(proofid) == 0) {
275 // Don't attempt to register a proof that we already evicted because it
276 // was dangling, but rather attempt to retrieve an associated node.
277 needMoreNodes = true;
278 return invalidate(ProofRegistrationResult::DANGLING, "dangling-proof");
279 }
280
281 // Check the proof's validity.
282 ProofValidationState validationState;
283 if (!WITH_LOCK(cs_main, return proof->verify(stakeUtxoDustThreshold,
284 chainman, validationState))) {
285 if (isImmatureState(validationState)) {
289 // Adding this proof exceeds the immature pool limit, so evict
290 // the lowest scoring proof.
293 }
294
295 return invalidate(ProofRegistrationResult::IMMATURE,
296 "immature-proof");
297 }
298
299 if (validationState.GetResult() ==
302 "utxo-missing-or-spent");
303 }
304
305 // Reject invalid proof.
306 return invalidate(ProofRegistrationResult::INVALID, "invalid-proof");
307 }
308
309 auto now = GetTime<std::chrono::seconds>();
310 auto nextCooldownTimePoint =
311 now + std::chrono::seconds(gArgs.GetIntArg(
312 "-avalancheconflictingproofcooldown",
314
315 ProofPool::ConflictingProofSet conflictingProofs;
316 switch (validProofPool.addProofIfNoConflict(proof, conflictingProofs)) {
317 case ProofPool::AddProofStatus::REJECTED: {
318 if (mode != RegistrationMode::FORCE_ACCEPT) {
319 auto bestPossibleConflictTime = std::chrono::seconds(0);
320 auto &pview = peers.get<by_proofid>();
321 for (auto &conflictingProof : conflictingProofs) {
322 auto it = pview.find(conflictingProof->getId());
323 assert(it != pview.end());
324
325 // Search the most recent time over the peers
326 bestPossibleConflictTime = std::max(
327 bestPossibleConflictTime, it->nextPossibleConflictTime);
328
330 nextCooldownTimePoint);
331 }
332
333 if (bestPossibleConflictTime > now) {
334 // Cooldown not elapsed, reject the proof.
335 return invalidate(
337 "cooldown-not-elapsed");
338 }
339
340 // Give the proof a chance to replace the conflicting ones.
342 // If we have overridden other proofs due to conflict,
343 // remove the peers and attempt to move them to the
344 // conflicting pool.
345 moveToConflictingPool(conflictingProofs);
346
347 // Replacement is successful, continue to peer creation
348 break;
349 }
350
351 // Not the preferred proof, or replacement is not enabled
353 ProofPool::AddProofStatus::REJECTED
355 "rejected-proof")
357 "conflicting-utxos");
358 }
359
361
362 // Move the conflicting proofs from the valid pool to the
363 // conflicting pool
364 moveToConflictingPool(conflictingProofs);
365
366 auto status = validProofPool.addProofIfNoConflict(proof);
367 assert(status == ProofPool::AddProofStatus::SUCCEED);
368
369 break;
370 }
371 case ProofPool::AddProofStatus::DUPLICATED:
372 // If the proof was already in the pool, don't duplicate the peer.
374 "proof-already-registered");
375 case ProofPool::AddProofStatus::SUCCEED:
376 break;
377
378 // No default case, so the compiler can warn about missing cases
379 }
380
381 // At this stage we are going to create a peer so the proof should never
382 // exist in the conflicting pool, but use belt and suspenders.
384
385 // New peer means new peerid!
386 const PeerId peerid = nextPeerId++;
387
388 // We have no peer for this proof, time to create it.
389 auto inserted = peers.emplace(peerid, proof, nextCooldownTimePoint);
390 assert(inserted.second);
391
392 if (localProof && proof->getId() == localProof->getId()) {
393 // Add it to the shareable proofs even if there is no node, we are the
394 // node. Otherwise it will be inserted after a node is attached to the
395 // proof.
396 shareableProofs.insert(proof);
397 }
398
399 // Add to our registered score when adding to the peer list
400 totalPeersScore += proof->getScore();
401
402 // If there are nodes waiting for this proof, add them
403 auto &pendingNodesView = pendingNodes.get<by_proofid>();
404 auto range = pendingNodesView.equal_range(proofid);
405
406 // We want to update the nodes then remove them from the pending set. That
407 // will invalidate the range iterators, so we need to save the node ids
408 // first before we can loop over them.
409 std::vector<NodeId> nodeids;
410 nodeids.reserve(std::distance(range.first, range.second));
411 std::transform(range.first, range.second, std::back_inserter(nodeids),
412 [](const PendingNode &n) { return n.nodeid; });
413
414 for (const NodeId &nodeid : nodeids) {
415 addOrUpdateNode(inserted.first, nodeid);
416 }
417
419 addStakeContender(proof);
420 }
421
422 return true;
423}
424
426 if (isDangling(proofid) && mode == RejectionMode::INVALIDATE) {
428 return true;
429 }
430
431 if (!exists(proofid)) {
432 return false;
433 }
434
435 if (immatureProofPool.removeProof(proofid)) {
436 return true;
437 }
438
439 if (mode == RejectionMode::DEFAULT &&
441 // In default mode we keep the proof in the conflicting pool
442 return true;
443 }
444
445 if (mode == RejectionMode::INVALIDATE &&
447 // In invalidate mode we remove the proof completely
448 return true;
449 }
450
451 auto &pview = peers.get<by_proofid>();
452 auto it = pview.find(proofid);
453 assert(it != pview.end());
454
455 const ProofRef proof = it->proof;
456
457 if (!removePeer(it->peerid)) {
458 return false;
459 }
460
461 // If there was conflicting proofs, attempt to pull them back
462 for (const SignedStake &ss : proof->getStakes()) {
463 const ProofRef conflictingProof =
464 conflictingProofPool.getProof(ss.getStake().getUTXO());
465 if (!conflictingProof) {
466 continue;
467 }
468
469 conflictingProofPool.removeProof(conflictingProof->getId());
470 registerProof(conflictingProof);
471 }
472
473 if (mode == RejectionMode::DEFAULT) {
475 }
476
477 return true;
478}
479
481 std::unordered_set<ProofRef, SaltedProofHasher> &registeredProofs) {
482 registeredProofs.clear();
483 const auto now = GetTime<std::chrono::seconds>();
484
485 std::vector<ProofRef> newlyDanglingProofs;
486 for (const Peer &peer : peers) {
487 // If the peer is not our local proof, has been registered for some
488 // time and has no node attached, discard it.
489 if ((!localProof || peer.getProofId() != localProof->getId()) &&
490 peer.node_count == 0 &&
491 (peer.registration_time + Peer::DANGLING_TIMEOUT) <= now) {
492 // Check the remotes status to determine if we should set the proof
493 // as dangling. This prevents from dropping a proof on our own due
494 // to a network issue. If the remote presence status is inconclusive
495 // we assume our own position (missing = false).
496 if (!getRemotePresenceStatus(peer.getProofId()).value_or(false)) {
497 newlyDanglingProofs.push_back(peer.proof);
498 }
499 }
500 }
501
502 // Similarly, check if we have dangling proofs that could be pulled back
503 // because the network says so.
504 std::vector<ProofRef> previouslyDanglingProofs;
505 danglingProofPool.forEachProof([&](const ProofRef &proof) {
506 if (getRemotePresenceStatus(proof->getId()).value_or(false)) {
507 previouslyDanglingProofs.push_back(proof);
508 }
509 });
510 for (const ProofRef &proof : previouslyDanglingProofs) {
511 danglingProofPool.removeProof(proof->getId());
512 if (registerProof(proof)) {
513 registeredProofs.insert(proof);
514 }
515 }
516
517 for (const ProofRef &proof : newlyDanglingProofs) {
518 rejectProof(proof->getId(), RejectionMode::INVALIDATE);
520 // If the proof is added, it means there is no better conflicting
521 // dangling proof and this is not a duplicated, so it's worth
522 // printing a message to the log.
524 "Proof dangling for too long (no connected node): %s\n",
525 proof->getId().GetHex());
526 }
527 }
528
529 // If we have dangling proof, this is a good indicator that we need to
530 // request more nodes from our peers.
531 needMoreNodes = !newlyDanglingProofs.empty();
532}
533
535 for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) {
536 const PeerId p = selectPeer();
537
538 // If we cannot find a peer, it may be due to the fact that it is
539 // unlikely due to high fragmentation, so compact and retry.
540 if (p == NO_PEER) {
541 compact();
542 continue;
543 }
544
545 // See if that peer has an available node.
546 auto &nview = nodes.get<next_request_time>();
547 auto it = nview.lower_bound(boost::make_tuple(p, SteadyMilliseconds()));
548 if (it != nview.end() && it->peerid == p &&
549 it->nextRequestTime <= Now<SteadyMilliseconds>()) {
550 return it->nodeid;
551 }
552 }
553
554 // We failed to find a node to query, flag this so we can request more
555 needMoreNodes = true;
556
557 return NO_NODE;
558}
559
560std::unordered_set<ProofRef, SaltedProofHasher> PeerManager::updatedBlockTip() {
561 std::vector<ProofId> invalidProofIds;
562 std::vector<ProofRef> newImmatures;
563
564 {
565 LOCK(cs_main);
566
567 for (const auto &p : peers) {
569 if (!p.proof->verify(stakeUtxoDustThreshold, chainman, state)) {
570 if (isImmatureState(state)) {
571 newImmatures.push_back(p.proof);
572 }
573 invalidProofIds.push_back(p.getProofId());
574
576 "Invalidating proof %s: verification failed (%s)\n",
577 p.proof->getId().GetHex(), state.ToString());
578 }
579 }
580
581 // Disable thread safety analysis here because it does not play nicely
582 // with the lambda
584 [&](const ProofRef &proof) NO_THREAD_SAFETY_ANALYSIS {
587 if (!proof->verify(stakeUtxoDustThreshold, chainman, state)) {
588 invalidProofIds.push_back(proof->getId());
589
590 LogPrint(
592 "Invalidating dangling proof %s: verification failed "
593 "(%s)\n",
594 proof->getId().GetHex(), state.ToString());
595 }
596 });
597 }
598
599 // Remove the invalid proofs before the immature rescan. This makes it
600 // possible to pull back proofs with utxos that conflicted with these
601 // invalid proofs.
602 for (const ProofId &invalidProofId : invalidProofIds) {
603 rejectProof(invalidProofId, RejectionMode::INVALIDATE);
604 }
605
606 auto registeredProofs = immatureProofPool.rescan(*this);
607
608 for (auto &p : newImmatures) {
610 }
611
612 return registeredProofs;
613}
614
616 ProofRef proof;
617
618 forPeer(proofid, [&](const Peer &p) {
619 proof = p.proof;
620 return true;
621 });
622
623 if (!proof) {
624 proof = conflictingProofPool.getProof(proofid);
625 }
626
627 if (!proof) {
628 proof = immatureProofPool.getProof(proofid);
629 }
630
631 return proof;
632}
633
634bool PeerManager::isBoundToPeer(const ProofId &proofid) const {
635 auto &pview = peers.get<by_proofid>();
636 return pview.find(proofid) != pview.end();
637}
638
639bool PeerManager::isImmature(const ProofId &proofid) const {
640 return immatureProofPool.getProof(proofid) != nullptr;
641}
642
643bool PeerManager::isInConflictingPool(const ProofId &proofid) const {
644 return conflictingProofPool.getProof(proofid) != nullptr;
645}
646
647bool PeerManager::isDangling(const ProofId &proofid) const {
648 return danglingProofPool.getProof(proofid) != nullptr;
649}
650
651void PeerManager::setInvalid(const ProofId &proofid) {
652 invalidProofs.insert(proofid);
653}
654
655bool PeerManager::isInvalid(const ProofId &proofid) const {
656 return invalidProofs.contains(proofid);
657}
658
661}
662
663bool PeerManager::saveRemoteProof(const ProofId &proofid, const NodeId nodeid,
664 const bool present) {
665 if (present && isStakingPreconsensusActivated() && isBoundToPeer(proofid) &&
666 !isRemotelyPresentProof(proofid)) {
667 // If this is the first time this peer's proof becomes a remote proof of
668 // any node, ensure it is included in the contender cache. There is a
669 // special case where the contender cache can lose track of a proof if
670 // it is not saved as a remote proof before the next finalized block
671 // (triggering promotion, where non-remote cache entries are dropped).
672 // This does not happen in the hot path since receiving a proof
673 // immediately saves it as a remote, however it becomes more likely if
674 // the proof was loaded from a file (-persistavapeers) or added via RPC.
675 addStakeContender(getProof(proofid));
676 }
677
678 // Get how many proofs this node has announced
679 auto &remoteProofsByLastUpdate = remoteProofs.get<by_lastUpdate>();
680 auto [begin, end] = remoteProofsByLastUpdate.equal_range(nodeid);
681
682 // Limit the number of proofs a single node can save:
683 // - At least MAX_REMOTE_PROOFS
684 // - Up to 2x as much as we have
685 // The MAX_REMOTE_PROOFS minimum is there to ensure we don't overlimit at
686 // startup when we don't have proofs yet.
687 while (size_t(std::distance(begin, end)) >=
688 std::max(MAX_REMOTE_PROOFS, 2 * peers.size())) {
689 // Remove the proof with the oldest update time
690 begin = remoteProofsByLastUpdate.erase(begin);
691 }
692
693 auto it = remoteProofs.find(boost::make_tuple(proofid, nodeid));
694 if (it != remoteProofs.end()) {
695 remoteProofs.erase(it);
696 }
697
698 return remoteProofs
699 .emplace(RemoteProof{proofid, nodeid, GetTime<std::chrono::seconds>(),
700 present})
701 .second;
702}
703
704std::vector<RemoteProof>
706 std::vector<RemoteProof> nodeRemoteProofs;
707
708 auto &remoteProofsByLastUpdate = remoteProofs.get<by_lastUpdate>();
709 auto [begin, end] = remoteProofsByLastUpdate.equal_range(nodeid);
710
711 for (auto &it = begin; it != end; it++) {
712 nodeRemoteProofs.emplace_back(*it);
713 }
714
715 return nodeRemoteProofs;
716}
717
718bool PeerManager::hasRemoteProofStatus(const ProofId &proofid) const {
719 auto &view = remoteProofs.get<by_proofid>();
720 return view.count(proofid) > 0;
721}
722
724 auto &view = remoteProofs.get<by_proofid>();
725 auto [begin, end] = view.equal_range(proofid);
726 return std::any_of(begin, end, [](const auto &remoteProof) {
727 return remoteProof.present;
728 });
729}
730
731bool PeerManager::removePeer(const PeerId peerid) {
732 auto it = peers.find(peerid);
733 if (it == peers.end()) {
734 return false;
735 }
736
737 // Remove all nodes from this peer.
738 removeNodeFromPeer(it, it->node_count);
739
740 auto &nview = nodes.get<next_request_time>();
741
742 // Add the nodes to the pending set
743 auto range = nview.equal_range(peerid);
744 for (auto &nit = range.first; nit != range.second; ++nit) {
745 pendingNodes.emplace(it->getProofId(), nit->nodeid);
746 };
747
748 // Remove nodes associated with this peer, unless their timeout is still
749 // active. This ensure that we don't overquery them in case they are
750 // subsequently added to another peer.
751 nview.erase(
752 nview.lower_bound(boost::make_tuple(peerid, SteadyMilliseconds())),
753 nview.upper_bound(
754 boost::make_tuple(peerid, Now<SteadyMilliseconds>())));
755
756 // Release UTXOs attached to this proof.
757 validProofPool.removeProof(it->getProofId());
758
759 // If there were nodes attached, remove from the radix tree as well
760 auto removed = shareableProofs.remove(Uint256RadixKey(it->getProofId()));
761
762 m_unbroadcast_proofids.erase(it->getProofId());
763
764 // Remove the peer from the PeerSet and remove its score from the registered
765 // score total.
766 assert(totalPeersScore >= it->getScore());
767 totalPeersScore -= it->getScore();
768 peers.erase(it);
769 return true;
770}
771
773 if (slots.empty() || slotCount == 0) {
774 return NO_PEER;
775 }
776
777 const uint64_t max = slotCount;
778 for (int retry = 0; retry < SELECT_PEER_MAX_RETRY; retry++) {
779 size_t i =
780 selectPeerImpl(slots, FastRandomContext().randrange(max), max);
781 if (i != NO_PEER) {
782 return i;
783 }
784 }
785
786 return NO_PEER;
787}
788
790 // There is nothing to compact.
791 if (fragmentation == 0) {
792 return 0;
793 }
794
795 std::vector<Slot> newslots;
796 newslots.reserve(peers.size());
797
798 uint64_t prevStop = 0;
799 uint32_t i = 0;
800 for (auto it = peers.begin(); it != peers.end(); it++) {
801 if (it->node_count == 0) {
802 continue;
803 }
804
805 newslots.emplace_back(prevStop, it->getScore(), it->peerid);
806 prevStop = slots[i].getStop();
807 if (!peers.modify(it, [&](Peer &p) { p.index = i++; })) {
808 return 0;
809 }
810 }
811
812 slots = std::move(newslots);
813
814 const uint64_t saved = slotCount - prevStop;
815 slotCount = prevStop;
816 fragmentation = 0;
817
818 return saved;
819}
820
822 uint64_t prevStop = 0;
823 uint32_t scoreFromSlots = 0;
824 for (size_t i = 0; i < slots.size(); i++) {
825 const Slot &s = slots[i];
826
827 // Slots must be in correct order.
828 if (s.getStart() < prevStop) {
829 return false;
830 }
831
832 prevStop = s.getStop();
833
834 // If this is a dead slot, then nothing more needs to be checked.
835 if (s.getPeerId() == NO_PEER) {
836 continue;
837 }
838
839 // We have a live slot, verify index.
840 auto it = peers.find(s.getPeerId());
841 if (it == peers.end() || it->index != i) {
842 return false;
843 }
844
845 // Accumulate score across slots
846 scoreFromSlots += slots[i].getScore();
847 }
848
849 // Score across slots must be the same as our allocated score
850 if (scoreFromSlots != connectedPeersScore) {
851 return false;
852 }
853
854 uint32_t scoreFromAllPeers = 0;
855 uint32_t scoreFromPeersWithNodes = 0;
856
857 std::unordered_set<COutPoint, SaltedOutpointHasher> peersUtxos;
858 for (const auto &p : peers) {
859 // Accumulate the score across peers to compare with total known score
860 scoreFromAllPeers += p.getScore();
861
862 // A peer should have a proof attached
863 if (!p.proof) {
864 return false;
865 }
866
867 // Check proof pool consistency
868 for (const auto &ss : p.proof->getStakes()) {
869 const COutPoint &outpoint = ss.getStake().getUTXO();
870 auto proof = validProofPool.getProof(outpoint);
871
872 if (!proof) {
873 // Missing utxo
874 return false;
875 }
876 if (proof != p.proof) {
877 // Wrong proof
878 return false;
879 }
880
881 if (!peersUtxos.emplace(outpoint).second) {
882 // Duplicated utxo
883 return false;
884 }
885 }
886
887 // Count node attached to this peer.
888 const auto count_nodes = [&]() {
889 size_t count = 0;
890 auto &nview = nodes.get<next_request_time>();
891 auto begin = nview.lower_bound(
892 boost::make_tuple(p.peerid, SteadyMilliseconds()));
893 auto end = nview.upper_bound(
894 boost::make_tuple(p.peerid + 1, SteadyMilliseconds()));
895
896 for (auto it = begin; it != end; ++it) {
897 count++;
898 }
899
900 return count;
901 };
902
903 if (p.node_count != count_nodes()) {
904 return false;
905 }
906
907 // If there are no nodes attached to this peer, then we are done.
908 if (p.node_count == 0) {
909 continue;
910 }
911
912 scoreFromPeersWithNodes += p.getScore();
913 // The index must point to a slot refering to this peer.
914 if (p.index >= slots.size() || slots[p.index].getPeerId() != p.peerid) {
915 return false;
916 }
917
918 // If the score do not match, same thing.
919 if (slots[p.index].getScore() != p.getScore()) {
920 return false;
921 }
922
923 // Check the proof is in the radix tree only if there are nodes attached
924 if (((localProof && p.getProofId() == localProof->getId()) ||
925 p.node_count > 0) &&
926 shareableProofs.get(p.getProofId()) == nullptr) {
927 return false;
928 }
929 if (p.node_count == 0 &&
930 shareableProofs.get(p.getProofId()) != nullptr) {
931 return false;
932 }
933 }
934
935 // Check our accumulated scores against our registred and allocated scores
936 if (scoreFromAllPeers != totalPeersScore) {
937 return false;
938 }
939 if (scoreFromPeersWithNodes != connectedPeersScore) {
940 return false;
941 }
942
943 // We checked the utxo consistency for all our peers utxos already, so if
944 // the pool size differs from the expected one there are dangling utxos.
945 if (validProofPool.size() != peersUtxos.size()) {
946 return false;
947 }
948
949 // Check there is no dangling proof in the radix tree
951 return isBoundToPeer(pLeaf->getId());
952 });
953}
954
955PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot,
956 const uint64_t max) {
957 assert(slot <= max);
958
959 size_t begin = 0, end = slots.size();
960 uint64_t bottom = 0, top = max;
961
962 // Try to find the slot using dichotomic search.
963 while ((end - begin) > 8) {
964 // The slot we picked in not allocated.
965 if (slot < bottom || slot >= top) {
966 return NO_PEER;
967 }
968
969 // Guesstimate the position of the slot.
970 size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom));
971 assert(begin <= i && i < end);
972
973 // We have a match.
974 if (slots[i].contains(slot)) {
975 return slots[i].getPeerId();
976 }
977
978 // We undershooted.
979 if (slots[i].precedes(slot)) {
980 begin = i + 1;
981 if (begin >= end) {
982 return NO_PEER;
983 }
984
985 bottom = slots[begin].getStart();
986 continue;
987 }
988
989 // We overshooted.
990 if (slots[i].follows(slot)) {
991 end = i;
992 top = slots[end].getStart();
993 continue;
994 }
995
996 // We have an unalocated slot.
997 return NO_PEER;
998 }
999
1000 // Enough of that nonsense, let fallback to linear search.
1001 for (size_t i = begin; i < end; i++) {
1002 // We have a match.
1003 if (slots[i].contains(slot)) {
1004 return slots[i].getPeerId();
1005 }
1006 }
1007
1008 // We failed to find a slot, retry.
1009 return NO_PEER;
1010}
1011
1013 // The proof should be bound to a peer
1014 if (isBoundToPeer(proofid)) {
1015 m_unbroadcast_proofids.insert(proofid);
1016 }
1017}
1018
1020 m_unbroadcast_proofids.erase(proofid);
1021}
1022
1024 const CBlockIndex *pprev,
1025 std::vector<std::pair<ProofId, CScript>> &winners) {
1026 if (!pprev) {
1027 return false;
1028 }
1029
1030 // Don't select proofs that have not been known for long enough, i.e. at
1031 // least since twice the dangling proof cleanup timeout before the last
1032 // block time, so we're sure to not account for proofs more recent than the
1033 // previous block or lacking node connected.
1034 // The previous block time is capped to now for the unlikely event the
1035 // previous block time is in the future.
1036 auto registrationDelay = std::chrono::duration_cast<std::chrono::seconds>(
1038 auto maxRegistrationDelay =
1039 std::chrono::duration_cast<std::chrono::seconds>(
1041 auto minRegistrationDelay =
1042 std::chrono::duration_cast<std::chrono::seconds>(
1044
1045 const int64_t refTime = std::min(pprev->GetBlockTime(), GetTime());
1046
1047 const int64_t targetRegistrationTime = refTime - registrationDelay.count();
1048 const int64_t maxRegistrationTime = refTime - minRegistrationDelay.count();
1049 const int64_t minRegistrationTime = refTime - maxRegistrationDelay.count();
1050
1051 const BlockHash prevblockhash = pprev->GetBlockHash();
1052
1053 std::vector<ProofRef> selectedProofs;
1054 ProofRef firstCompliantProof = ProofRef();
1055 while (selectedProofs.size() < peers.size()) {
1056 double bestRewardRank = std::numeric_limits<double>::max();
1057 ProofRef selectedProof = ProofRef();
1058 int64_t selectedProofRegistrationTime{0};
1059 StakeContenderId bestRewardHash;
1060
1061 for (const Peer &peer : peers) {
1062 if (!peer.proof) {
1063 // Should never happen, continue
1064 continue;
1065 }
1066
1067 if (!peer.hasFinalized ||
1068 peer.registration_time.count() >= maxRegistrationTime) {
1069 continue;
1070 }
1071
1072 if (std::find_if(selectedProofs.begin(), selectedProofs.end(),
1073 [&peer](const ProofRef &proof) {
1074 return peer.getProofId() == proof->getId();
1075 }) != selectedProofs.end()) {
1076 continue;
1077 }
1078
1079 StakeContenderId proofRewardHash(prevblockhash, peer.getProofId());
1080 if (proofRewardHash == uint256::ZERO) {
1081 // This either the result of an incredibly unlikely lucky hash,
1082 // or a the hash is getting abused. In this case, skip the
1083 // proof.
1084 LogPrintf(
1085 "Staking reward hash has a suspicious value of zero for "
1086 "proof %s and blockhash %s, skipping\n",
1087 peer.getProofId().ToString(), prevblockhash.ToString());
1088 continue;
1089 }
1090
1091 double proofRewardRank =
1092 proofRewardHash.ComputeProofRewardRank(peer.getScore());
1093 // If selectedProof is nullptr, this means that bestRewardRank is
1094 // MAX_DOUBLE so the comparison will always select this proof as the
1095 // preferred one. As a consequence it is safe to use 0 as a proofid.
1097 proofRewardHash, proofRewardRank, peer.getProofId(),
1098 bestRewardHash, bestRewardRank,
1099 selectedProof ? selectedProof->getId()
1100 : ProofId(uint256::ZERO))) {
1101 bestRewardRank = proofRewardRank;
1102 selectedProof = peer.proof;
1103 selectedProofRegistrationTime = peer.registration_time.count();
1104 bestRewardHash = proofRewardHash;
1105 }
1106 }
1107
1108 if (!selectedProof) {
1109 // No winner
1110 break;
1111 }
1112
1113 if (!firstCompliantProof &&
1114 selectedProofRegistrationTime < targetRegistrationTime) {
1115 firstCompliantProof = selectedProof;
1116 }
1117
1118 selectedProofs.push_back(selectedProof);
1119
1120 if (selectedProofRegistrationTime < minRegistrationTime &&
1121 !isFlaky(selectedProof->getId())) {
1122 break;
1123 }
1124 }
1125
1126 winners.clear();
1127
1128 if (!firstCompliantProof) {
1129 return false;
1130 }
1131
1132 winners.reserve(selectedProofs.size());
1133
1134 // Find the winner
1135 for (const ProofRef &proof : selectedProofs) {
1136 if (proof->getId() == firstCompliantProof->getId()) {
1137 winners.push_back({proof->getId(), proof->getPayoutScript()});
1138 }
1139 }
1140 // Add the others (if any) after the winner
1141 for (const ProofRef &proof : selectedProofs) {
1142 if (proof->getId() != firstCompliantProof->getId()) {
1143 winners.push_back({proof->getId(), proof->getPayoutScript()});
1144 }
1145 }
1146
1147 return true;
1148}
1149
1150bool PeerManager::setFlaky(const ProofId &proofid) {
1151 return manualFlakyProofids.insert(proofid).second;
1152}
1153
1154bool PeerManager::unsetFlaky(const ProofId &proofid) {
1155 return manualFlakyProofids.erase(proofid) > 0;
1156}
1157
1158bool PeerManager::isFlaky(const ProofId &proofid) const {
1159 if (localProof && proofid == localProof->getId()) {
1160 return false;
1161 }
1162
1163 if (manualFlakyProofids.count(proofid) > 0) {
1164 return true;
1165 }
1166
1167 // If we are missing connection to this proof, consider flaky
1168 if (forPeer(proofid,
1169 [](const Peer &peer) { return peer.node_count == 0; })) {
1170 return true;
1171 }
1172
1173 auto &remoteProofsByNodeId = remoteProofs.get<by_nodeid>();
1174 auto &nview = nodes.get<next_request_time>();
1175
1176 std::unordered_map<PeerId, std::unordered_set<ProofId, SaltedProofIdHasher>>
1177 missing_per_peer;
1178
1179 // Construct a set of missing proof ids per peer
1180 double total_score{0};
1181 for (const Peer &peer : peers) {
1182 const PeerId peerid = peer.peerid;
1183
1184 total_score += peer.getScore();
1185
1186 auto nodes_range = nview.equal_range(peerid);
1187 for (auto &nit = nodes_range.first; nit != nodes_range.second; ++nit) {
1188 auto proofs_range = remoteProofsByNodeId.equal_range(nit->nodeid);
1189 for (auto &proofit = proofs_range.first;
1190 proofit != proofs_range.second; ++proofit) {
1191 if (!proofit->present) {
1192 missing_per_peer[peerid].insert(proofit->proofid);
1193 }
1194 }
1195 };
1196 }
1197
1198 double missing_score{0};
1199
1200 // Now compute a score for the missing proof
1201 for (const auto &[peerid, missingProofs] : missing_per_peer) {
1202 if (missingProofs.size() > 3) {
1203 // Ignore peers with too many missing proofs
1204 continue;
1205 }
1206
1207 auto pit = peers.find(peerid);
1208 if (pit == peers.end()) {
1209 // Peer not found
1210 continue;
1211 }
1212
1213 if (missingProofs.count(proofid) > 0) {
1214 missing_score += pit->getScore();
1215 }
1216 }
1217
1218 return (missing_score / total_score) > 0.3;
1219}
1220
1221std::optional<bool>
1223 auto &remoteProofsView = remoteProofs.get<by_proofid>();
1224 auto [begin, end] = remoteProofsView.equal_range(proofid);
1225
1226 if (begin == end) {
1227 // No remote registered anything yet, we are on our own
1228 return std::nullopt;
1229 }
1230
1231 double total_score{0};
1232 double present_score{0};
1233 double missing_score{0};
1234
1235 for (auto it = begin; it != end; it++) {
1236 auto nit = nodes.find(it->nodeid);
1237 if (nit == nodes.end()) {
1238 // No such node
1239 continue;
1240 }
1241
1242 const PeerId peerid = nit->peerid;
1243
1244 auto pit = peers.find(peerid);
1245 if (pit == peers.end()) {
1246 // Peer not found
1247 continue;
1248 }
1249
1250 uint32_t node_count = pit->node_count;
1251 if (localProof && pit->getProofId() == localProof->getId()) {
1252 // If that's our local proof, account for ourself
1253 ++node_count;
1254 }
1255
1256 if (node_count == 0) {
1257 // should never happen
1258 continue;
1259 }
1260
1261 const double score = double(pit->getScore()) / node_count;
1262
1263 total_score += score;
1264 if (it->present) {
1265 present_score += score;
1266 } else {
1267 missing_score += score;
1268 }
1269 }
1270
1271 if (localProof) {
1272 auto &peersByProofid = peers.get<by_proofid>();
1273
1274 // Do we have a node connected for that proof ?
1275 bool present = false;
1276 auto pit = peersByProofid.find(proofid);
1277 if (pit != peersByProofid.end()) {
1278 present = pit->node_count > 0;
1279 }
1280
1281 pit = peersByProofid.find(localProof->getId());
1282 if (pit != peersByProofid.end()) {
1283 // Also divide by node_count, we can have several nodes even for our
1284 // local proof.
1285 const double score =
1286 double(pit->getScore()) / (1 + pit->node_count);
1287
1288 total_score += score;
1289 if (present) {
1290 present_score += score;
1291 } else {
1292 missing_score += score;
1293 }
1294 }
1295 }
1296
1297 if (present_score / total_score > 0.55) {
1298 return std::make_optional(true);
1299 }
1300
1301 if (missing_score / total_score > 0.55) {
1302 return std::make_optional(false);
1303 }
1304
1305 return std::nullopt;
1306}
1307
1308bool PeerManager::dumpPeersToFile(const fs::path &dumpPath) const {
1309 try {
1310 const fs::path dumpPathTmp = dumpPath + ".new";
1311 FILE *filestr = fsbridge::fopen(dumpPathTmp, "wb");
1312 if (!filestr) {
1313 return false;
1314 }
1315
1316 AutoFile file{filestr};
1317 file << PEERS_DUMP_VERSION;
1318 file << uint64_t(peers.size());
1319 for (const Peer &peer : peers) {
1320 file << peer.proof;
1321 file << peer.hasFinalized;
1322 file << int64_t(peer.registration_time.count());
1323 file << int64_t(peer.nextPossibleConflictTime.count());
1324 }
1325
1326 if (!FileCommit(file.Get())) {
1327 throw std::runtime_error(strprintf("Failed to commit to file %s",
1328 PathToString(dumpPathTmp)));
1329 }
1330 file.fclose();
1331
1332 if (!RenameOver(dumpPathTmp, dumpPath)) {
1333 throw std::runtime_error(strprintf("Rename failed from %s to %s",
1334 PathToString(dumpPathTmp),
1335 PathToString(dumpPath)));
1336 }
1337 } catch (const std::exception &e) {
1338 LogPrint(BCLog::AVALANCHE, "Failed to dump the avalanche peers: %s.\n",
1339 e.what());
1340 return false;
1341 }
1342
1343 LogPrint(BCLog::AVALANCHE, "Successfully dumped %d peers to %s.\n",
1344 peers.size(), PathToString(dumpPath));
1345
1346 return true;
1347}
1348
1350 const fs::path &dumpPath,
1351 std::unordered_set<ProofRef, SaltedProofHasher> &registeredProofs) {
1352 registeredProofs.clear();
1353
1354 FILE *filestr = fsbridge::fopen(dumpPath, "rb");
1355 AutoFile file{filestr};
1356 if (file.IsNull()) {
1358 "Failed to open avalanche peers file from disk.\n");
1359 return false;
1360 }
1361
1362 try {
1363 uint64_t version;
1364 file >> version;
1365
1366 if (version != PEERS_DUMP_VERSION) {
1368 "Unsupported avalanche peers file version.\n");
1369 return false;
1370 }
1371
1372 uint64_t numPeers;
1373 file >> numPeers;
1374
1375 auto &peersByProofId = peers.get<by_proofid>();
1376
1377 for (uint64_t i = 0; i < numPeers; i++) {
1378 ProofRef proof;
1379 bool hasFinalized;
1380 int64_t registrationTime;
1381 int64_t nextPossibleConflictTime;
1382
1383 file >> proof;
1384 file >> hasFinalized;
1385 file >> registrationTime;
1386 file >> nextPossibleConflictTime;
1387
1388 if (registerProof(proof)) {
1389 auto it = peersByProofId.find(proof->getId());
1390 if (it == peersByProofId.end()) {
1391 // Should never happen
1392 continue;
1393 }
1394
1395 // We don't modify any key so we don't need to rehash.
1396 // If the modify fails, it means we don't get the full benefit
1397 // from the file but we still added our peer to the set. The
1398 // non-overridden fields will be set the normal way.
1399 peersByProofId.modify(it, [&](Peer &p) {
1400 p.hasFinalized = hasFinalized;
1402 std::chrono::seconds{registrationTime};
1404 std::chrono::seconds{nextPossibleConflictTime};
1405 });
1406
1407 registeredProofs.insert(proof);
1408 }
1409 }
1410 } catch (const std::exception &e) {
1412 "Failed to read the avalanche peers file data on disk: %s.\n",
1413 e.what());
1414 return false;
1415 }
1416
1417 return true;
1418}
1419
1420void PeerManager::cleanupStakeContenders(const int requestedMinHeight) {
1421 stakeContenderCache.cleanup(requestedMinHeight);
1422}
1423
1425 const CBlockIndex *tip = WITH_LOCK(cs_main, return chainman.ActiveTip());
1426 stakeContenderCache.add(tip, proof);
1427
1428 const BlockHash blockhash = tip->GetBlockHash();
1429 const ProofId &proofid = proof->getId();
1431 "Cached stake contender with proofid %s, payout %s at block "
1432 "%s (height %d) with id %s\n",
1433 proofid.ToString(), HexStr(proof->getPayoutScript()),
1434 blockhash.ToString(), tip->nHeight,
1435 StakeContenderId(blockhash, proofid).ToString());
1436}
1437
1439 BlockHash &prevblockhashout) const {
1440 return stakeContenderCache.getVoteStatus(contenderId, prevblockhashout);
1441}
1442
1444 stakeContenderCache.accept(contenderId);
1445}
1446
1448 const StakeContenderId &contenderId, BlockHash &prevblockhash,
1449 std::vector<std::pair<ProofId, CScript>> &newWinners) {
1450 stakeContenderCache.finalize(contenderId);
1451
1452 // Get block hash related to this contender. We should not assume the
1453 // current chain tip is the block this contender is a winner for.
1454 getStakeContenderStatus(contenderId, prevblockhash);
1455
1456 // Calculate the new winners for this block
1457 stakeContenderCache.getWinners(prevblockhash, newWinners);
1458}
1459
1461 stakeContenderCache.reject(contenderId);
1462}
1463
1465 stakeContenderCache.promoteToBlock(pindex, [&](const ProofId &proofid) {
1466 return isBoundToPeer(proofid) ||
1467 // isDangling check appears redundant, but remote proofs are not
1468 // guaranteed to be cleaned up when one of our peers is removed
1469 // for dangling too long. Whether or not a proof is dangling is
1470 // gated by remote presence status, so only proofs that are very
1471 // poorly connected to the network will stop being promoted.
1472 (isRemotelyPresentProof(proofid) && isDangling(proofid));
1473 });
1474}
1475
1477 const CBlockIndex *prevblock,
1478 const std::vector<std::pair<ProofId, CScript>> winners, size_t maxPollable,
1479 std::vector<StakeContenderId> &pollableContenders) {
1480 const BlockHash prevblockhash = prevblock->GetBlockHash();
1481 // Set status for local winners
1482 for (const auto &winner : winners) {
1483 const StakeContenderId contenderId(prevblockhash, winner.first);
1484 stakeContenderCache.finalize(contenderId);
1486 "Stake contender set as local winner: proofid %s, payout "
1487 "%s at block %s (height %d) with id %s\n",
1488 winner.first.ToString(), HexStr(winner.second),
1489 prevblockhash.ToString(), prevblock->nHeight,
1490 contenderId.ToString());
1491 }
1492
1493 // Treat the highest ranking contender similarly to local winners except
1494 // that it is not automatically included in the winner set (unless it
1495 // happens to be selected as a local winner).
1496 if (stakeContenderCache.getPollableContenders(prevblockhash, maxPollable,
1497 pollableContenders) > 0) {
1498 // Accept the highest ranking contender. This is a no-op if the highest
1499 // ranking contender is already the local winner.
1500 stakeContenderCache.accept(pollableContenders[0]);
1502 "Stake contender set as best contender: id %s at block "
1503 "%s (height %d)\n",
1504 pollableContenders[0].ToString(), prevblockhash.ToString(),
1505 prevblock->nHeight);
1506 return true;
1507 }
1508
1509 return false;
1510}
1511
1513 const CBlockIndex *pindex, const std::vector<CScript> &payoutScripts) {
1514 return stakeContenderCache.setWinners(pindex, payoutScripts);
1515}
1516
1517} // namespace avalanche
ArgsManager gArgs
Definition: args.cpp:40
static constexpr PeerId NO_PEER
Definition: node.h:16
uint32_t PeerId
Definition: node.h:15
static constexpr size_t AVALANCHE_DEFAULT_CONFLICTING_PROOF_COOLDOWN
Conflicting proofs cooldown time default value in seconds.
Definition: avalanche.h:28
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
Definition: args.cpp:495
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:430
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
int64_t GetBlockTime() const
Definition: blockindex.h:160
BlockHash GetBlockHash() const
Definition: blockindex.h:130
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:38
void insert(Span< const uint8_t > vKey)
Definition: bloom.cpp:215
bool contains(Span< const uint8_t > vKey) const
Definition: bloom.cpp:249
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
Definition: validation.h:1443
Fast randomness source.
Definition: random.h:411
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Definition: validation.h:101
Result GetResult() const
Definition: validation.h:122
std::string ToString() const
Definition: validation.h:125
bool selectStakingRewardWinner(const CBlockIndex *pprev, std::vector< std::pair< ProofId, CScript > > &winners)
Deterministically select a list of payout scripts based on the proof set and the previous block hash.
uint32_t connectedPeersScore
Definition: peermanager.h:239
std::vector< RemoteProof > getRemoteProofs(const NodeId nodeid) const
bool removeNode(NodeId nodeid)
bool setFinalized(PeerId peerid)
Latch on that this peer has a finalized proof.
bool dumpPeersToFile(const fs::path &dumpPath) const
RemoteProofSet remoteProofs
Remember which node sent which proof so we have an image of the proof set of our peers.
Definition: peermanager.h:283
bool isDangling(const ProofId &proofid) const
bool updateNextRequestTime(NodeId nodeid, SteadyMilliseconds timeout)
bool unsetFlaky(const ProofId &proofid)
std::optional< bool > getRemotePresenceStatus(const ProofId &proofid) const
Get the presence remote status of a proof.
bool addNodeToPeer(const PeerSet::iterator &it)
Definition: peermanager.cpp:89
bool exists(const ProofId &proofid) const
Return true if the (valid) proof exists, but only for non-dangling proofs.
Definition: peermanager.h:411
PendingNodeSet pendingNodes
Definition: peermanager.h:225
bool verify() const
Perform consistency check on internal data structures.
bool hasRemoteProofStatus(const ProofId &proofid) const
bool forPeer(const ProofId &proofid, Callable &&func) const
Definition: peermanager.h:419
void finalizeStakeContender(const StakeContenderId &contenderId, BlockHash &prevblockhash, std::vector< std::pair< ProofId, CScript > > &newWinners)
bool latchAvaproofsSent(NodeId nodeid)
Flag that a node did send its compact proofs.
void cleanupStakeContenders(const int requestedMinHeight)
Make some of the contender cache API available.
bool addNode(NodeId nodeid, const ProofId &proofid)
Node API.
Definition: peermanager.cpp:33
static constexpr int SELECT_PEER_MAX_RETRY
Definition: peermanager.h:227
ProofIdSet m_unbroadcast_proofids
Track proof ids to broadcast.
Definition: peermanager.h:233
bool loadPeersFromFile(const fs::path &dumpPath, std::unordered_set< ProofRef, SaltedProofHasher > &registeredProofs)
RejectionMode
Rejection mode.
Definition: peermanager.h:399
void addUnbroadcastProof(const ProofId &proofid)
Proof broadcast API.
std::unordered_set< ProofRef, SaltedProofHasher > updatedBlockTip()
Update the peer set when a new block is connected.
void removeUnbroadcastProof(const ProofId &proofid)
void promoteStakeContendersToBlock(const CBlockIndex *pindex)
bool isBoundToPeer(const ProofId &proofid) const
bool setContenderStatusForLocalWinners(const CBlockIndex *prevblock, const std::vector< std::pair< ProofId, CScript > > winners, size_t maxPollable, std::vector< StakeContenderId > &pollableContenders)
ProofRadixTree shareableProofs
Definition: peermanager.h:191
bool saveRemoteProof(const ProofId &proofid, const NodeId nodeid, const bool present)
CRollingBloomFilter invalidProofs
Filter for proofs that are consensus-invalid or were recently invalidated by avalanche (finalized rej...
Definition: peermanager.h:297
uint64_t compact()
Trigger maintenance of internal data structures.
std::vector< Slot > slots
Definition: peermanager.h:163
uint32_t totalPeersScore
Quorum management.
Definition: peermanager.h:238
ProofPool danglingProofPool
Definition: peermanager.h:188
StakeContenderCache stakeContenderCache
Definition: peermanager.h:301
void setInvalid(const ProofId &proofid)
int getStakeContenderStatus(const StakeContenderId &contenderId, BlockHash &prevblockhashout) const
bool isFlaky(const ProofId &proofid) const
ChainstateManager & chainman
Definition: peermanager.h:243
bool isInvalid(const ProofId &proofid) const
std::unordered_set< ProofId, SaltedProofIdHasher > manualFlakyProofids
Definition: peermanager.h:299
bool removePeer(const PeerId peerid)
Remove an existing peer.
bool isImmature(const ProofId &proofid) const
bool addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid)
Definition: peermanager.cpp:48
bool rejectProof(const ProofId &proofid, RejectionMode mode=RejectionMode::DEFAULT)
ProofPool immatureProofPool
Definition: peermanager.h:187
RegistrationMode
Registration mode.
Definition: peermanager.h:376
ProofPool conflictingProofPool
Definition: peermanager.h:186
bool isStakingPreconsensusActivated() const
Definition: peermanager.h:566
static constexpr size_t MAX_REMOTE_PROOFS
Definition: peermanager.h:304
bool setFlaky(const ProofId &proofid)
void addStakeContender(const ProofRef &proof)
std::atomic< bool > needMoreNodes
Flag indicating that we failed to select a node and need to expand our node set.
Definition: peermanager.h:211
PeerId selectPeer() const
Randomly select a peer to poll.
bool isInConflictingPool(const ProofId &proofid) const
bool isRemotelyPresentProof(const ProofId &proofid) const
static constexpr int SELECT_NODE_MAX_RETRY
Definition: peermanager.h:228
void cleanupDanglingProofs(std::unordered_set< ProofRef, SaltedProofHasher > &registeredProofs)
void acceptStakeContender(const StakeContenderId &contenderId)
ProofRef getProof(const ProofId &proofid) const
bool registerProof(const ProofRef &proof, ProofRegistrationState &registrationState, RegistrationMode mode=RegistrationMode::DEFAULT)
void rejectStakeContender(const StakeContenderId &contenderId)
bool removeNodeFromPeer(const PeerSet::iterator &it, uint32_t count=1)
bool updateNextPossibleConflictTime(PeerId peerid, const std::chrono::seconds &nextTime)
Proof and Peer related API.
void moveToConflictingPool(const ProofContainer &proofs)
bool setStakeContenderWinners(const CBlockIndex *pindex, const std::vector< CScript > &payoutScripts)
AddProofStatus addProofIfPreferred(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool.
Definition: proofpool.cpp:54
size_t size() const
Definition: proofpool.h:135
AddProofStatus addProofIfNoConflict(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool, and fail if there is a conflict on any UTXO.
Definition: proofpool.cpp:13
size_t countProofs() const
Definition: proofpool.cpp:129
bool removeProof(ProofId proofid)
Definition: proofpool.cpp:79
void forEachProof(Callable &&func) const
Definition: proofpool.h:118
ProofRef getProof(const ProofId &proofid) const
Definition: proofpool.cpp:112
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
Definition: proofpool.h:88
ProofRef getLowestScoreProof() const
Definition: proofpool.cpp:123
std::unordered_set< ProofRef, SaltedProofHasher > rescan(PeerManager &peerManager)
Definition: proofpool.cpp:86
bool getWinners(const BlockHash &prevblockhash, std::vector< std::pair< ProofId, CScript > > &winners) const
bool accept(const StakeContenderId &contenderId)
Helpers to set avalanche state of a contender.
void cleanup(const int requestedMinHeight)
size_t getPollableContenders(const BlockHash &prevblockhash, size_t maxPollable, std::vector< StakeContenderId > &pollableContenders) const
Get the best ranking contenders, accepted contenders ranking first.
bool reject(const StakeContenderId &contenderId)
bool setWinners(const CBlockIndex *pindex, const std::vector< CScript > &payoutScripts)
Set proof(s) that should be treated as winners (already finalized).
bool add(const CBlockIndex *pindex, const ProofRef &proof, uint8_t status=StakeContenderStatus::UNKNOWN)
Add a proof to consider in staking rewards pre-consensus.
void promoteToBlock(const CBlockIndex *activeTip, std::function< bool(const ProofId &proofid)> const &shouldPromote)
Promote cache entries to a the active chain tip.
int getVoteStatus(const StakeContenderId &contenderId, BlockHash &prevblockhashout) const
Get contender acceptance state for avalanche voting.
bool finalize(const StakeContenderId &contenderId)
std::string ToString() const
Definition: uint256.h:80
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
static const uint256 ZERO
Definition: uint256.h:134
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
int64_t NodeId
Definition: eviction.h:16
bool RenameOver(fs::path src, fs::path dest)
Definition: fs_helpers.cpp:273
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
Definition: fs_helpers.cpp:126
#define LogPrint(category,...)
Definition: logging.h:452
#define LogTrace(category,...)
Definition: logging.h:448
#define LogPrintf(...)
Definition: logging.h:424
@ AVALANCHE
Definition: logging.h:91
ProofRegistrationResult
Definition: peermanager.h:145
static constexpr uint32_t AVALANCHE_MAX_IMMATURE_PROOFS
Maximum number of immature proofs the peer manager will accept from the network.
Definition: peermanager.h:46
static bool isImmatureState(const ProofValidationState &state)
static constexpr uint64_t PEERS_DUMP_VERSION
Definition: peermanager.cpp:31
PeerId selectPeerImpl(const std::vector< Slot > &slots, const uint64_t slot, const uint64_t max)
Internal methods that are exposed for testing purposes.
RCUPtr< const Proof > ProofRef
Definition: proof.h:186
static std::string PathToString(const path &path)
Convert path object to byte string.
Definition: fs.h:147
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:30
static constexpr NodeId NO_NODE
Special NodeId that represent no node.
Definition: nodeid.h:15
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:108
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
RCUPtr< T > remove(const KeyType &key)
Remove an element from the tree.
Definition: radix.h:181
RCUPtr< T > get(const KeyType &key)
Get the value corresponding to a key.
Definition: radix.h:118
bool forEachLeaf(Callable &&func) const
Definition: radix.h:144
bool insert(const RCUPtr< T > &value)
Insert a value into the tree.
Definition: radix.h:112
Facility for using an uint256 as a radix tree key.
SteadyMilliseconds nextRequestTime
Definition: node.h:23
bool avaproofsSent
Definition: node.h:24
std::chrono::seconds registration_time
Definition: peermanager.h:95
std::chrono::seconds nextPossibleConflictTime
Definition: peermanager.h:96
uint32_t node_count
Definition: peermanager.h:89
static constexpr auto DANGLING_TIMEOUT
Consider dropping the peer if no node is attached after this timeout expired.
Definition: peermanager.h:102
uint32_t index
Definition: peermanager.h:88
uint32_t getScore() const
Definition: peermanager.h:111
ProofRef proof
Definition: peermanager.h:91
uint64_t getStop() const
Definition: peermanager.h:75
uint64_t getStart() const
Definition: peermanager.h:74
PeerId getPeerId() const
Definition: peermanager.h:77
StakeContenderIds are unique for each block to ensure that the peer polling for their acceptance has ...
double ComputeProofRewardRank(uint32_t proofScore) const
To make sure the selection is properly weighted according to the proof score, we normalize the conten...
#define LOCK(cs)
Definition: sync.h:306
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:357
static int count
Definition: tests.c:31
#define NO_THREAD_SAFETY_ANALYSIS
Definition: threadsafety.h:58
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:105
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
Definition: time.h:31
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())