32 auto &pview =
peers.get<by_proofid>();
33 auto it = pview.find(proofid);
34 if (it == pview.end()) {
49 const PeerId peerid = it->peerid;
51 auto nit =
nodes.find(nodeid);
52 if (nit ==
nodes.end()) {
53 if (!
nodes.emplace(nodeid, peerid).second) {
57 const PeerId oldpeerid = nit->peerid;
58 if (!
nodes.modify(nit, [&](
Node &n) { n.peerid = peerid; })) {
75 const ProofId &proofid = it->getProofId();
99 slots.emplace_back(start, score, it->peerid);
110 auto [begin, end] = remoteProofsView.equal_range(nodeid);
111 remoteProofsView.erase(begin, end);
118 auto it =
nodes.find(nodeid);
119 if (it ==
nodes.end()) {
123 const PeerId peerid = it->peerid;
138 if (it ==
peers.end()) {
142 assert(count <= it->node_count);
148 const uint32_t new_count = it->node_count -
count;
149 if (!
peers.modify(it, [&](
Peer &p) { p.node_count = new_count; })) {
166 const size_t i = it->index;
171 if (i + 1 ==
slots.size()) {
184 auto it =
nodes.find(nodeid);
185 if (it ==
nodes.end()) {
193 auto it =
nodes.find(nodeid);
194 if (it ==
nodes.end()) {
198 return !it->avaproofsSent &&
207 PeerId peerid,
const std::chrono::seconds &nextTime) {
208 auto it =
peers.find(peerid);
209 if (it ==
peers.end()) {
220 return it->nextPossibleConflictTime == nextTime;
224 auto it =
peers.find(peerid);
225 if (it ==
peers.end()) {
235 template <
typename ProofContainer>
237 auto &peersView =
peers.get<by_proofid>();
238 for (
const ProofRef &proof : proofs) {
239 auto it = peersView.find(proof->getId());
240 if (it != peersView.end()) {
253 const ProofId &proofid = proof->getId();
256 const std::string &message) {
257 return registrationState.
Invalid(
268 "proof-already-registered");
300 "utxo-missing-or-spent");
307 auto now = GetTime<std::chrono::seconds>();
308 auto nextCooldownTimePoint =
310 "-avalancheconflictingproofcooldown",
315 case ProofPool::AddProofStatus::REJECTED: {
317 auto bestPossibleConflictTime = std::chrono::seconds(0);
318 auto &pview =
peers.get<by_proofid>();
319 for (
auto &conflictingProof : conflictingProofs) {
320 auto it = pview.find(conflictingProof->getId());
321 assert(it != pview.end());
324 bestPossibleConflictTime = std::max(
325 bestPossibleConflictTime, it->nextPossibleConflictTime);
328 nextCooldownTimePoint);
331 if (bestPossibleConflictTime > now) {
335 "cooldown-not-elapsed");
351 ProofPool::AddProofStatus::REJECTED
355 "conflicting-utxos");
365 assert(status == ProofPool::AddProofStatus::SUCCEED);
369 case ProofPool::AddProofStatus::DUPLICATED:
372 "proof-already-registered");
373 case ProofPool::AddProofStatus::SUCCEED:
387 auto inserted =
peers.emplace(peerid, proof, nextCooldownTimePoint);
401 auto &pendingNodesView =
pendingNodes.get<by_proofid>();
402 auto range = pendingNodesView.equal_range(proofid);
407 std::vector<NodeId> nodeids;
408 nodeids.reserve(std::distance(range.first, range.second));
409 std::transform(range.first, range.second, std::back_inserter(nodeids),
412 for (
const NodeId &nodeid : nodeids) {
445 auto &pview =
peers.get<by_proofid>();
446 auto it = pview.find(proofid);
447 assert(it != pview.end());
459 if (!conflictingProof) {
475 std::unordered_set<ProofRef, SaltedProofHasher> ®isteredProofs) {
476 registeredProofs.clear();
477 const auto now = GetTime<std::chrono::seconds>();
479 std::vector<ProofRef> newlyDanglingProofs;
484 peer.node_count == 0 &&
491 newlyDanglingProofs.push_back(peer.proof);
498 std::vector<ProofRef> previouslyDanglingProofs;
501 previouslyDanglingProofs.push_back(proof);
504 for (
const ProofRef &proof : previouslyDanglingProofs) {
507 registeredProofs.insert(proof);
511 for (
const ProofRef &proof : newlyDanglingProofs) {
518 "Proof dangling for too long (no connected node): %s\n",
519 proof->getId().GetHex());
542 if (it != nview.end() && it->peerid == p &&
543 it->nextRequestTime <= Now<SteadyMilliseconds>()) {
555 std::vector<ProofId> invalidProofIds;
556 std::vector<ProofRef> newImmatures;
561 for (
const auto &p :
peers) {
565 newImmatures.push_back(p.proof);
567 invalidProofIds.push_back(p.getProofId());
570 "Invalidating proof %s: verification failed (%s)\n",
571 p.proof->getId().GetHex(), state.
ToString());
579 for (
const ProofId &invalidProofId : invalidProofIds) {
585 for (
auto &p : newImmatures) {
589 return registeredProofs;
612 auto &pview =
peers.get<by_proofid>();
613 return pview.find(proofid) != pview.end();
641 const bool present) {
643 auto &remoteProofsByLastUpdate =
remoteProofs.get<by_lastUpdate>();
644 auto [begin, end] = remoteProofsByLastUpdate.equal_range(nodeid);
651 while (
size_t(std::distance(begin, end)) >=
654 begin = remoteProofsByLastUpdate.erase(begin);
657 auto it =
remoteProofs.find(boost::make_tuple(proofid, nodeid));
663 .emplace(
RemoteProof{proofid, nodeid, GetTime<std::chrono::seconds>(),
668 std::vector<RemoteProof>
670 std::vector<RemoteProof> nodeRemoteProofs;
672 auto &remoteProofsByLastUpdate =
remoteProofs.get<by_lastUpdate>();
673 auto [begin, end] = remoteProofsByLastUpdate.equal_range(nodeid);
675 for (
auto &it = begin; it != end; it++) {
676 nodeRemoteProofs.emplace_back(*it);
679 return nodeRemoteProofs;
683 auto it =
peers.find(peerid);
684 if (it ==
peers.end()) {
694 auto range = nview.equal_range(peerid);
695 for (
auto &nit = range.first; nit != range.second; ++nit) {
705 boost::make_tuple(peerid, Now<SteadyMilliseconds>())));
745 std::vector<Slot> newslots;
746 newslots.reserve(
peers.size());
748 uint64_t prevStop = 0;
750 for (
auto it =
peers.begin(); it !=
peers.end(); it++) {
751 if (it->node_count == 0) {
755 newslots.emplace_back(prevStop, it->getScore(), it->peerid);
756 prevStop =
slots[i].getStop();
757 if (!
peers.modify(it, [&](
Peer &p) { p.index = i++; })) {
762 slots = std::move(newslots);
764 const uint64_t saved =
slotCount - prevStop;
772 uint64_t prevStop = 0;
773 uint32_t scoreFromSlots = 0;
774 for (
size_t i = 0; i <
slots.size(); i++) {
791 if (it ==
peers.end() || it->index != i) {
796 scoreFromSlots +=
slots[i].getScore();
804 uint32_t scoreFromAllPeers = 0;
805 uint32_t scoreFromPeersWithNodes = 0;
807 std::unordered_set<COutPoint, SaltedOutpointHasher> peersUtxos;
808 for (
const auto &p :
peers) {
810 scoreFromAllPeers += p.getScore();
818 for (
const auto &ss : p.proof->getStakes()) {
819 const COutPoint &outpoint = ss.getStake().getUTXO();
826 if (proof != p.proof) {
831 if (!peersUtxos.emplace(outpoint).second) {
838 const auto count_nodes = [&]() {
841 auto begin = nview.lower_bound(
843 auto end = nview.upper_bound(
846 for (
auto it = begin; it != end; ++it) {
853 if (p.node_count != count_nodes()) {
858 if (p.node_count == 0) {
862 scoreFromPeersWithNodes += p.getScore();
864 if (p.index >=
slots.size() ||
slots[p.index].getPeerId() != p.peerid) {
869 if (
slots[p.index].getScore() != p.getScore()) {
879 if (p.node_count == 0 &&
906 const uint64_t max) {
909 size_t begin = 0, end = slots.size();
910 uint64_t bottom = 0, top = max;
913 while ((end - begin) > 8) {
915 if (slot < bottom || slot >= top) {
920 size_t i = begin + ((slot - bottom) * (end - begin) / (top - bottom));
921 assert(begin <= i && i < end);
924 if (slots[i].contains(slot)) {
925 return slots[i].getPeerId();
929 if (slots[i].precedes(slot)) {
935 bottom = slots[begin].getStart();
940 if (slots[i].follows(slot)) {
942 top = slots[end].getStart();
951 for (
size_t i = begin; i < end; i++) {
953 if (slots[i].contains(slot)) {
954 return slots[i].getPeerId();
975 std::vector<std::pair<ProofId, CScript>> &winners) {
986 auto registrationDelay = std::chrono::duration_cast<std::chrono::seconds>(
988 auto maxRegistrationDelay =
989 std::chrono::duration_cast<std::chrono::seconds>(
991 auto minRegistrationDelay =
992 std::chrono::duration_cast<std::chrono::seconds>(
997 const int64_t targetRegistrationTime = refTime - registrationDelay.count();
998 const int64_t maxRegistrationTime = refTime - minRegistrationDelay.count();
999 const int64_t minRegistrationTime = refTime - maxRegistrationDelay.count();
1003 std::vector<ProofRef> selectedProofs;
1005 while (selectedProofs.size() <
peers.size()) {
1006 double bestRewardRank = std::numeric_limits<double>::max();
1008 int64_t selectedProofRegistrationTime{0};
1017 if (!peer.hasFinalized ||
1018 peer.registration_time.count() >= maxRegistrationTime) {
1022 if (std::find_if(selectedProofs.begin(), selectedProofs.end(),
1024 return peer.getProofId() == proof->getId();
1025 }) != selectedProofs.end()) {
1031 .
Write(prevblockhash)
1032 .
Write(peer.getProofId())
1040 "Staking reward hash has a suspicious value of zero for "
1041 "proof %s and blockhash %s, skipping\n",
1042 peer.getProofId().ToString(), prevblockhash.
ToString());
1052 double proofRewardRank =
1058 if (proofRewardRank < bestRewardRank) {
1059 bestRewardRank = proofRewardRank;
1060 selectedProof = peer.proof;
1061 selectedProofRegistrationTime = peer.registration_time.count();
1062 bestRewardHash = proofRewardHash;
1067 if (proofRewardRank == bestRewardRank &&
1068 (proofRewardHash < bestRewardHash ||
1069 (proofRewardHash == bestRewardHash &&
1070 peer.getProofId() < selectedProof->getId()))) {
1071 selectedProof = peer.proof;
1072 selectedProofRegistrationTime = peer.registration_time.count();
1073 bestRewardHash = proofRewardHash;
1077 if (!selectedProof) {
1082 if (!firstCompliantProof &&
1083 selectedProofRegistrationTime < targetRegistrationTime) {
1084 firstCompliantProof = selectedProof;
1087 selectedProofs.push_back(selectedProof);
1089 if (selectedProofRegistrationTime < minRegistrationTime &&
1090 !
isFlaky(selectedProof->getId())) {
1097 if (!firstCompliantProof) {
1101 winners.reserve(selectedProofs.size());
1104 for (
const ProofRef &proof : selectedProofs) {
1105 if (proof->getId() == firstCompliantProof->getId()) {
1106 winners.push_back({proof->getId(), proof->getPayoutScript()});
1110 for (
const ProofRef &proof : selectedProofs) {
1111 if (proof->getId() != firstCompliantProof->getId()) {
1112 winners.push_back({proof->getId(), proof->getPayoutScript()});
1142 auto &remoteProofsByNodeId =
remoteProofs.get<by_nodeid>();
1145 std::unordered_map<PeerId, std::unordered_set<ProofId, SaltedProofIdHasher>>
1149 double total_score{0};
1155 auto nodes_range = nview.equal_range(peerid);
1156 for (
auto &nit = nodes_range.first; nit != nodes_range.second; ++nit) {
1157 auto proofs_range = remoteProofsByNodeId.equal_range(nit->nodeid);
1158 for (
auto &proofit = proofs_range.first;
1159 proofit != proofs_range.second; ++proofit) {
1160 if (!proofit->present) {
1161 missing_per_peer[peerid].insert(proofit->proofid);
1167 double missing_score{0};
1170 for (
const auto &[peerid, missingProofs] : missing_per_peer) {
1171 if (missingProofs.size() > 3) {
1176 auto pit =
peers.find(peerid);
1177 if (pit ==
peers.end()) {
1182 if (missingProofs.count(proofid) > 0) {
1183 missing_score += pit->getScore();
1187 return (missing_score / total_score) > 0.3;
1192 auto &remoteProofsView =
remoteProofs.get<by_proofid>();
1193 auto [begin, end] = remoteProofsView.equal_range(proofid);
1197 return std::nullopt;
1200 double total_score{0};
1201 double present_score{0};
1202 double missing_score{0};
1204 for (
auto it = begin; it != end; it++) {
1205 auto nit =
nodes.find(it->nodeid);
1206 if (nit ==
nodes.end()) {
1211 const PeerId peerid = nit->peerid;
1213 auto pit =
peers.find(peerid);
1214 if (pit ==
peers.end()) {
1219 uint32_t node_count = pit->node_count;
1225 if (node_count == 0) {
1230 const double score = double(pit->getScore()) / node_count;
1232 total_score += score;
1234 present_score += score;
1236 missing_score += score;
1241 auto &peersByProofid =
peers.get<by_proofid>();
1244 bool present =
false;
1245 auto pit = peersByProofid.find(proofid);
1246 if (pit != peersByProofid.end()) {
1247 present = pit->node_count > 0;
1250 pit = peersByProofid.find(
localProof->getId());
1251 if (pit != peersByProofid.end()) {
1254 const double score =
1255 double(pit->getScore()) / (1 + pit->node_count);
1257 total_score += score;
1259 present_score += score;
1261 missing_score += score;
1266 if (present_score / total_score > 0.55) {
1267 return std::make_optional(
true);
1270 if (missing_score / total_score > 0.55) {
1271 return std::make_optional(
false);
1274 return std::nullopt;
1279 const fs::path dumpPathTmp = dumpPath +
".new";
1287 file << uint64_t(
peers.size());
1290 file << peer.hasFinalized;
1291 file << int64_t(peer.registration_time.count());
1292 file << int64_t(peer.nextPossibleConflictTime.count());
1296 throw std::runtime_error(
strprintf(
"Failed to commit to file %s",
1302 throw std::runtime_error(
strprintf(
"Rename failed from %s to %s",
1306 }
catch (
const std::exception &e) {
1320 std::unordered_set<ProofRef, SaltedProofHasher> ®isteredProofs) {
1321 registeredProofs.clear();
1327 "Failed to open avalanche peers file from disk.\n");
1337 "Unsupported avalanche peers file version.\n");
1344 auto &peersByProofId =
peers.get<by_proofid>();
1346 for (uint64_t i = 0; i < numPeers; i++) {
1349 int64_t registrationTime;
1350 int64_t nextPossibleConflictTime;
1353 file >> hasFinalized;
1354 file >> registrationTime;
1355 file >> nextPossibleConflictTime;
1358 auto it = peersByProofId.find(proof->getId());
1359 if (it == peersByProofId.end()) {
1368 peersByProofId.modify(it, [&](
Peer &p) {
1371 std::chrono::seconds{registrationTime};
1373 std::chrono::seconds{nextPossibleConflictTime};
1376 registeredProofs.insert(proof);
1379 }
catch (
const std::exception &e) {
1381 "Failed to read the avalanche peers file data on disk: %s.\n",
arith_uint256 UintToArith256(const uint256 &a)
static constexpr PeerId NO_PEER
static constexpr size_t AVALANCHE_DEFAULT_CONFLICTING_PROOF_COOLDOWN
Conflicting proofs cooldown time default value in seconds.
int64_t GetIntArg(const std::string &strArg, int64_t nDefault) const
Return integer argument or default value.
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
The block chain is a tree shaped structure starting with the genesis block at the root,...
int64_t GetBlockTime() const
BlockHash GetBlockHash() const
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
void Finalize(Span< uint8_t > output)
CHash256 & Write(Span< const uint8_t > input)
An outpoint - a combination of a transaction hash and an index n into its vout.
void insert(Span< const uint8_t > vKey)
bool contains(Span< const uint8_t > vKey) const
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
std::string ToString() const
uint32_t connectedPeersScore
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.
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)
bool exists(const ProofId &proofid) const
PendingNodeSet pendingNodes
bool verify() const
Perform consistency check on internal data structures.
bool forPeer(const ProofId &proofid, Callable &&func) const
bool latchAvaproofsSent(NodeId nodeid)
Flag that a node did send its compact proofs.
bool addNode(NodeId nodeid, const ProofId &proofid)
Node API.
static constexpr int SELECT_PEER_MAX_RETRY
ProofIdSet m_unbroadcast_proofids
Track proof ids to broadcast.
bool loadPeersFromFile(const fs::path &dumpPath, std::unordered_set< ProofRef, SaltedProofHasher > ®isteredProofs)
RejectionMode
Rejection mode.
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)
bool isBoundToPeer(const ProofId &proofid) const
ProofRadixTree shareableProofs
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...
uint64_t compact()
Trigger maintenance of internal data structures.
std::vector< Slot > slots
uint32_t totalPeersScore
Quorum management.
ProofPool danglingProofPool
void setInvalid(const ProofId &proofid)
bool isFlaky(const ProofId &proofid) const
ChainstateManager & chainman
bool isInvalid(const ProofId &proofid) const
std::unordered_set< ProofId, SaltedProofIdHasher > manualFlakyProofids
bool removePeer(const PeerId peerid)
Remove an existing peer.
bool isImmature(const ProofId &proofid) const
bool addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid)
bool rejectProof(const ProofId &proofid, RejectionMode mode=RejectionMode::DEFAULT)
ProofPool immatureProofPool
Amount stakeUtxoDustThreshold
RegistrationMode
Registration mode.
ProofPool conflictingProofPool
static constexpr size_t MAX_REMOTE_PROOFS
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.
bool setFlaky(const ProofId &proofid)
std::atomic< bool > needMoreNodes
Flag indicating that we failed to select a node and need to expand our node set.
PeerId selectPeer() const
Randomly select a peer to poll.
bool isInConflictingPool(const ProofId &proofid) const
static constexpr int SELECT_NODE_MAX_RETRY
void cleanupDanglingProofs(std::unordered_set< ProofRef, SaltedProofHasher > ®isteredProofs)
ProofRef getProof(const ProofId &proofid) const
bool registerProof(const ProofRef &proof, ProofRegistrationState ®istrationState, RegistrationMode mode=RegistrationMode::DEFAULT)
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)
AddProofStatus addProofIfPreferred(const ProofRef &proof, ConflictingProofSet &conflictingProofs)
Attempt to add a proof to the pool.
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.
size_t countProofs() const
bool removeProof(ProofId proofid)
void forEachProof(Callable &&func) const
ProofRef getProof(const ProofId &proofid) const
std::set< ProofRef, ConflictingProofComparator > ConflictingProofSet
ProofRef getLowestScoreProof() const
std::unordered_set< ProofRef, SaltedProofHasher > rescan(PeerManager &peerManager)
std::string ToString() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const uint256 ZERO
static constexpr int CLIENT_VERSION
bitcoind-res.rc includes this file, but it cannot cope with real c++ code.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool RenameOver(fs::path src, fs::path dest)
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
#define LogPrint(category,...)
static constexpr uint32_t AVALANCHE_MAX_IMMATURE_PROOFS
Maximum number of immature proofs the peer manager will accept from the network.
static bool isImmatureState(const ProofValidationState &state)
static constexpr uint64_t PEERS_DUMP_VERSION
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
static std::string PathToString(const path &path)
Convert path object to byte string.
FILE * fopen(const fs::path &p, const char *mode)
static constexpr NodeId NO_NODE
Special NodeId that represent no node.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
A BlockHash is a unqiue identifier for a block.
RCUPtr< T > get(const KeyType &key)
Get the value corresponding to a key.
RCUPtr< T > remove(const KeyType &key)
Remove an element from the tree.
bool forEachLeaf(Callable &&func) const
bool insert(const RCUPtr< T > &value)
Insert a value into the tree.
Facility for using an uint256 as a radix tree key.
SteadyMilliseconds nextRequestTime
std::chrono::seconds registration_time
std::chrono::seconds nextPossibleConflictTime
static constexpr auto DANGLING_TIMEOUT
Consider dropping the peer if no node is attached after this timeout expired.
uint32_t getScore() const
uint64_t getStart() const
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds