Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | bool PeerManager::updateNextRequestTime(NodeId nodeid, TimePoint timeout) { | ||||
auto it = nodes.find(nodeid); | auto it = nodes.find(nodeid); | ||||
if (it == nodes.end()) { | if (it == nodes.end()) { | ||||
return false; | return false; | ||||
} | } | ||||
return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); | return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); | ||||
} | } | ||||
bool PeerManager::registerProof(const std::shared_ptr<Proof> &proof) { | |||||
return !getProof(proof->getId()) && getPeerId(proof) != NO_PEER; | |||||
} | |||||
NodeId PeerManager::selectNode() { | NodeId PeerManager::selectNode() { | ||||
for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | ||||
const PeerId p = selectPeer(); | const PeerId p = selectPeer(); | ||||
// If we cannot find a peer, it may be due to the fact that it is | // 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. | // unlikely due to high fragmentation, so compact and retry. | ||||
if (p == NO_PEER) { | if (p == NO_PEER) { | ||||
compact(); | compact(); | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
PeerId PeerManager::getPeerId(const std::shared_ptr<Proof> &proof) { | PeerId PeerManager::getPeerId(const std::shared_ptr<Proof> &proof) { | ||||
auto it = fetchOrCreatePeer(proof); | auto it = fetchOrCreatePeer(proof); | ||||
return it == peers.end() ? NO_PEER : it->peerid; | return it == peers.end() ? NO_PEER : it->peerid; | ||||
} | } | ||||
std::shared_ptr<Proof> PeerManager::getProof(const ProofId &proofid) const { | std::shared_ptr<Proof> PeerManager::getProof(const ProofId &proofid) const { | ||||
auto &pview = peers.get<proof_index>(); | std::shared_ptr<Proof> proof = nullptr; | ||||
auto it = pview.find(proofid); | |||||
return it == pview.end() ? nullptr : it->proof; | |||||
} | |||||
std::chrono::seconds | forPeer(proofid, [&](const Peer &p) { | ||||
PeerManager::getProofRegistrationTime(const ProofId &proofid) const { | proof = p.proof; | ||||
auto &pview = peers.get<proof_index>(); | return true; | ||||
auto it = pview.find(proofid); | }); | ||||
return it == pview.end() ? std::chrono::seconds::max() | |||||
: it->registration_time; | return proof; | ||||
} | } | ||||
PeerManager::PeerSet::iterator | PeerManager::PeerSet::iterator | ||||
PeerManager::fetchOrCreatePeer(const std::shared_ptr<Proof> &proof) { | PeerManager::fetchOrCreatePeer(const std::shared_ptr<Proof> &proof) { | ||||
{ | { | ||||
// Check if we already know of that peer. | // Check if we already know of that peer. | ||||
auto &pview = peers.get<proof_index>(); | auto &pview = peers.get<proof_index>(); | ||||
auto it = pview.find(proof->getId()); | auto it = pview.find(proof->getId()); | ||||
▲ Show 20 Lines • Show All 277 Lines • Show Last 20 Lines |