Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show All 11 Lines | |||||
PeerId PeerManager::getPeer(const Proof &proof) { | PeerId PeerManager::getPeer(const Proof &proof) { | ||||
auto &pview = peers.get<proof_index>(); | auto &pview = peers.get<proof_index>(); | ||||
auto it = pview.find(proof.getId()); | auto it = pview.find(proof.getId()); | ||||
if (it != pview.end()) { | if (it != pview.end()) { | ||||
return it->peerid; | return it->peerid; | ||||
} | } | ||||
// Reject invalid proof. | |||||
if (!proof.verify()) { | |||||
return NO_PEER; | |||||
} | |||||
// We have no peer for this proof, time to create it. | // We have no peer for this proof, time to create it. | ||||
const PeerId peerid = nextPeerId++; | const PeerId peerid = nextPeerId++; | ||||
auto inserted = peers.emplace(peerid, uint32_t(slots.size()), proof); | auto inserted = peers.emplace(peerid, uint32_t(slots.size()), proof); | ||||
assert(inserted.second); | assert(inserted.second); | ||||
const uint32_t score = proof.getScore(); | const uint32_t score = proof.getScore(); | ||||
const uint64_t start = slotCount; | const uint64_t start = slotCount; | ||||
slots.emplace_back(start, score, peerid); | slots.emplace_back(start, score, peerid); | ||||
Show All 28 Lines | bool PeerManager::removePeer(const PeerId peerid) { | ||||
peers.erase(it); | peers.erase(it); | ||||
return true; | return true; | ||||
} | } | ||||
bool PeerManager::addNode(NodeId nodeid, const Proof &proof, | bool PeerManager::addNode(NodeId nodeid, const Proof &proof, | ||||
const CPubKey &pubkey) { | const CPubKey &pubkey) { | ||||
const PeerId peerid = getPeer(proof); | const PeerId peerid = getPeer(proof); | ||||
if (peerid == NO_PEER) { | |||||
auto pit = peers.find(peerid); | |||||
if (pit == peers.end()) { | |||||
return false; | return false; | ||||
} | } | ||||
auto nit = nodes.find(nodeid); | auto nit = nodes.find(nodeid); | ||||
if (nit == nodes.end()) { | if (nit == nodes.end()) { | ||||
return nodes.emplace(nodeid, peerid, pubkey).second; | return nodes.emplace(nodeid, peerid, pubkey).second; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 189 Lines • Show Last 20 Lines |