Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
// 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. | ||||
#include <avalanche/peermanager.h> | #include <avalanche/peermanager.h> | ||||
#include <random.h> | #include <random.h> | ||||
#include <cassert> | #include <cassert> | ||||
namespace avalanche { | |||||
PeerId PeerManager::addPeer(PeerId p, uint32_t score) { | PeerId PeerManager::addPeer(PeerId p, uint32_t score) { | ||||
auto inserted = peers.emplace(p, Peer(score, uint32_t(slots.size()))); | auto inserted = peers.emplace(p, Peer(score, uint32_t(slots.size()))); | ||||
assert(inserted.second); | assert(inserted.second); | ||||
const uint64_t start = slotCount; | const uint64_t start = slotCount; | ||||
slots.emplace_back(start, score, p); | slots.emplace_back(start, score, p); | ||||
slotCount = start + score; | slotCount = start + score; | ||||
return p; | return p; | ||||
▲ Show 20 Lines • Show All 78 Lines • ▼ Show 20 Lines | bool PeerManager::addNodeToPeer(PeerId peerid, NodeId nodeid, CPubKey pubkey) { | ||||
} | } | ||||
auto nit = nodes.find(nodeid); | auto nit = nodes.find(nodeid); | ||||
if (nit == nodes.end()) { | if (nit == nodes.end()) { | ||||
return nodes.emplace(nodeid, peerid, std::move(pubkey)).second; | return nodes.emplace(nodeid, peerid, std::move(pubkey)).second; | ||||
} | } | ||||
// We actually have this node already, we need to update it. | // We actually have this node already, we need to update it. | ||||
return nodes.modify(nit, [&](AvalancheNode &n) { | return nodes.modify(nit, [&](Node &n) { | ||||
n.peerid = peerid; | n.peerid = peerid; | ||||
n.pubkey = std::move(pubkey); | n.pubkey = std::move(pubkey); | ||||
}); | }); | ||||
} | } | ||||
bool PeerManager::removeNode(NodeId nodeid) { | bool PeerManager::removeNode(NodeId nodeid) { | ||||
return nodes.erase(nodeid) > 0; | return nodes.erase(nodeid) > 0; | ||||
} | } | ||||
bool PeerManager::forNode( | bool PeerManager::forNode(NodeId nodeid, | ||||
NodeId nodeid, std::function<bool(const AvalancheNode &n)> func) const { | std::function<bool(const Node &n)> func) const { | ||||
auto it = nodes.find(nodeid); | auto it = nodes.find(nodeid); | ||||
return it != nodes.end() && func(*it); | return it != nodes.end() && func(*it); | ||||
} | } | ||||
bool PeerManager::updateNextRequestTime(NodeId nodeid, TimePoint timeout) { | 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, | return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); | ||||
[&](AvalancheNode &n) { n.nextRequestTime = timeout; }); | |||||
} | } | ||||
NodeId PeerManager::getSuitableNodeToQuery() { | NodeId PeerManager::getSuitableNodeToQuery() { | ||||
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. | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | for (size_t i = begin; i < end; i++) { | ||||
if (slots[i].contains(slot)) { | if (slots[i].contains(slot)) { | ||||
return slots[i].getPeerId(); | return slots[i].getPeerId(); | ||||
} | } | ||||
} | } | ||||
// We failed to find a slot, retry. | // We failed to find a slot, retry. | ||||
return NO_PEER; | return NO_PEER; | ||||
} | } | ||||
} // namespace avalanche |