Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.h
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_AVALANCHE_PEERMANAGER_H | #ifndef BITCOIN_AVALANCHE_PEERMANAGER_H | ||||
#define BITCOIN_AVALANCHE_PEERMANAGER_H | #define BITCOIN_AVALANCHE_PEERMANAGER_H | ||||
#include <avalanche/node.h> | #include <avalanche/node.h> | ||||
#include <avalanche/proof.h> | #include <avalanche/proof.h> | ||||
#include <avalanche/proofpool.h> | #include <avalanche/proofpool.h> | ||||
#include <coins.h> | #include <coins.h> | ||||
#include <pubkey.h> | #include <pubkey.h> | ||||
#include <salteduint256hasher.h> | #include <salteduint256hasher.h> | ||||
#include <util/time.h> | |||||
#include <boost/multi_index/composite_key.hpp> | #include <boost/multi_index/composite_key.hpp> | ||||
#include <boost/multi_index/hashed_index.hpp> | #include <boost/multi_index/hashed_index.hpp> | ||||
#include <boost/multi_index/mem_fun.hpp> | #include <boost/multi_index/mem_fun.hpp> | ||||
#include <boost/multi_index/member.hpp> | #include <boost/multi_index/member.hpp> | ||||
#include <boost/multi_index/ordered_index.hpp> | #include <boost/multi_index/ordered_index.hpp> | ||||
#include <boost/multi_index_container.hpp> | #include <boost/multi_index_container.hpp> | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
struct Peer { | struct Peer { | ||||
PeerId peerid; | PeerId peerid; | ||||
uint32_t index = -1; | uint32_t index = -1; | ||||
uint32_t node_count = 0; | uint32_t node_count = 0; | ||||
ProofRef proof; | ProofRef proof; | ||||
// The network stack uses timestamp in seconds, so we oblige. | |||||
std::chrono::seconds registration_time; | |||||
Peer(PeerId peerid_, ProofRef proof_) | Peer(PeerId peerid_, ProofRef proof_) | ||||
: peerid(peerid_), proof(std::move(proof_)), | : peerid(peerid_), proof(std::move(proof_)) {} | ||||
registration_time(GetTime<std::chrono::seconds>()) {} | |||||
const ProofId &getProofId() const { return proof->getId(); } | const ProofId &getProofId() const { return proof->getId(); } | ||||
uint32_t getScore() const { return proof->getScore(); } | uint32_t getScore() const { return proof->getScore(); } | ||||
}; | }; | ||||
struct proof_index { | struct proof_index { | ||||
using result_type = ProofId; | using result_type = ProofId; | ||||
result_type operator()(const Peer &p) const { return p.proof->getId(); } | result_type operator()(const Peer &p) const { return p.proof->getId(); } | ||||
▲ Show 20 Lines • Show All 103 Lines • ▼ Show 20 Lines | public: | ||||
/** | /** | ||||
* Proof and Peer related API. | * Proof and Peer related API. | ||||
*/ | */ | ||||
bool registerProof(const ProofRef &proof); | bool registerProof(const ProofRef &proof); | ||||
bool exists(const ProofId &proofid) const { | bool exists(const ProofId &proofid) const { | ||||
return getProof(proofid) != nullptr; | return getProof(proofid) != nullptr; | ||||
} | } | ||||
std::chrono::seconds getRegistrationTime(const ProofId &proofid) const { | |||||
return validProofPool.getRegistrationTime(proofid); | |||||
} | |||||
template <typename Callable> | template <typename Callable> | ||||
bool forPeer(const ProofId &proofid, Callable &&func) const { | bool forPeer(const ProofId &proofid, Callable &&func) const { | ||||
auto &pview = peers.get<by_proofid>(); | auto &pview = peers.get<by_proofid>(); | ||||
auto it = pview.find(proofid); | auto it = pview.find(proofid); | ||||
return it != pview.end() && func(*it); | return it != pview.end() && func(*it); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |