Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show All 29 Lines | bool PeerManager::removePeer(PeerId p) { | ||||
if (i + 1 == slots.size()) { | if (i + 1 == slots.size()) { | ||||
slots.pop_back(); | slots.pop_back(); | ||||
slotCount = slots.empty() ? 0 : slots.back().getStop(); | slotCount = slots.empty() ? 0 : slots.back().getStop(); | ||||
} else { | } else { | ||||
fragmentation += slots[i].getScore(); | fragmentation += slots[i].getScore(); | ||||
slots[i] = slots[i].withPeerId(NO_PEER); | slots[i] = slots[i].withPeerId(NO_PEER); | ||||
} | } | ||||
// Remove nodes associated with this peer, unless their timeout is still | |||||
// active. This ensure that we don't overquery them in case their are | |||||
// subsequently added to another peer. | |||||
auto &nview = nodes.get<next_request_time>(); | |||||
nview.erase(nview.lower_bound(boost::make_tuple(p, TimePoint())), | |||||
nview.upper_bound( | |||||
boost::make_tuple(p, std::chrono::steady_clock::now()))); | |||||
peers.erase(it); | peers.erase(it); | ||||
return true; | return true; | ||||
} | } | ||||
bool PeerManager::rescorePeer(PeerId p, uint32_t score) { | bool PeerManager::rescorePeer(PeerId p, uint32_t score) { | ||||
auto it = peers.find(p); | auto it = peers.find(p); | ||||
if (it == peers.end()) { | if (it == peers.end()) { | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | bool PeerManager::addNodeToPeer(PeerId peerid, NodeId nodeid, CPubKey pubkey) { | ||||
// 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, [&](Node &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) { | |||||
return nodes.erase(nodeid) > 0; | |||||
} | |||||
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. | ||||
if (p == NO_PEER) { | if (p == NO_PEER) { | ||||
compact(); | compact(); | ||||
continue; | continue; | ||||
} | } | ||||
// See if that peer has an available node. | // See if that peer has an available node. | ||||
auto it = nodes.get<next_request_time>().lower_bound( | auto &nview = nodes.get<next_request_time>(); | ||||
boost::make_tuple(p, TimePoint())); | auto it = nview.lower_bound(boost::make_tuple(p, TimePoint())); | ||||
if (it != nodes.get<next_request_time>().end() && it->peerid == p && | if (it != nview.end() && it->peerid == p && | ||||
it->nextRequestTime <= std::chrono::steady_clock::now()) { | it->nextRequestTime <= std::chrono::steady_clock::now()) { | ||||
return it->nodeid; | return it->nodeid; | ||||
} | } | ||||
} | } | ||||
return NO_NODE; | return NO_NODE; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 145 Lines • Show Last 20 Lines |