Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 307 Lines • ▼ Show 20 Lines | bool PeerManager::registerProof(const ProofRef &proof, | ||||
// New peer means new peerid! | // New peer means new peerid! | ||||
const PeerId peerid = nextPeerId++; | const PeerId peerid = nextPeerId++; | ||||
// We have no peer for this proof, time to create it. | // We have no peer for this proof, time to create it. | ||||
auto inserted = peers.emplace(peerid, proof, nextCooldownTimePoint); | auto inserted = peers.emplace(peerid, proof, nextCooldownTimePoint); | ||||
assert(inserted.second); | assert(inserted.second); | ||||
auto insertedRadixTree = shareableProofs.insert(proof); | |||||
assert(insertedRadixTree); | |||||
// Add to our registered score when adding to the peer list | // Add to our registered score when adding to the peer list | ||||
totalPeersScore += proof->getScore(); | totalPeersScore += proof->getScore(); | ||||
// If there are nodes waiting for this proof, add them | // If there are nodes waiting for this proof, add them | ||||
auto &pendingNodesView = pendingNodes.get<by_proofid>(); | auto &pendingNodesView = pendingNodes.get<by_proofid>(); | ||||
auto range = pendingNodesView.equal_range(proofid); | auto range = pendingNodesView.equal_range(proofid); | ||||
// We want to update the nodes then remove them from the pending set. That | // We want to update the nodes then remove them from the pending set. That | ||||
▲ Show 20 Lines • Show All 174 Lines • ▼ Show 20 Lines | bool PeerManager::removePeer(const PeerId peerid) { | ||||
// subsequently added to another peer. | // subsequently added to another peer. | ||||
nview.erase(nview.lower_bound(boost::make_tuple(peerid, TimePoint())), | nview.erase(nview.lower_bound(boost::make_tuple(peerid, TimePoint())), | ||||
nview.upper_bound(boost::make_tuple( | nview.upper_bound(boost::make_tuple( | ||||
peerid, std::chrono::steady_clock::now()))); | peerid, std::chrono::steady_clock::now()))); | ||||
// Release UTXOs attached to this proof. | // Release UTXOs attached to this proof. | ||||
validProofPool.removeProof(it->getProofId()); | validProofPool.removeProof(it->getProofId()); | ||||
auto removed = shareableProofs.remove(Uint256RadixKey(it->getProofId())); | |||||
assert(removed != nullptr); | |||||
m_unbroadcast_proofids.erase(it->getProofId()); | m_unbroadcast_proofids.erase(it->getProofId()); | ||||
// Remove the peer from the PeerSet and remove its score from the registered | // Remove the peer from the PeerSet and remove its score from the registered | ||||
// score total. | // score total. | ||||
assert(totalPeersScore >= it->getScore()); | assert(totalPeersScore >= it->getScore()); | ||||
totalPeersScore -= it->getScore(); | totalPeersScore -= it->getScore(); | ||||
peers.erase(it); | peers.erase(it); | ||||
return true; | return true; | ||||
▲ Show 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | for (const auto &p : peers) { | ||||
if (p.index >= slots.size() || slots[p.index].getPeerId() != p.peerid) { | if (p.index >= slots.size() || slots[p.index].getPeerId() != p.peerid) { | ||||
return false; | return false; | ||||
} | } | ||||
// If the score do not match, same thing. | // If the score do not match, same thing. | ||||
if (slots[p.index].getScore() != p.getScore()) { | if (slots[p.index].getScore() != p.getScore()) { | ||||
return false; | return false; | ||||
} | } | ||||
// Check the proof is in the radix tree | |||||
if (shareableProofs.get(p.getProofId()) == nullptr) { | |||||
return false; | |||||
} | |||||
} | } | ||||
// Check our accumulated scores against our registred and allocated scores | // Check our accumulated scores against our registred and allocated scores | ||||
if (scoreFromAllPeers != totalPeersScore) { | if (scoreFromAllPeers != totalPeersScore) { | ||||
return false; | return false; | ||||
} | } | ||||
if (scoreFromPeersWithNodes != connectedPeersScore) { | if (scoreFromPeersWithNodes != connectedPeersScore) { | ||||
return false; | return false; | ||||
} | } | ||||
// We checked the utxo consistency for all our peers utxos already, so if | // We checked the utxo consistency for all our peers utxos already, so if | ||||
// the pool size differs from the expected one there are dangling utxos. | // the pool size differs from the expected one there are dangling utxos. | ||||
if (validProofPool.size() != peersUtxos.size()) { | if (validProofPool.size() != peersUtxos.size()) { | ||||
return false; | return false; | ||||
} | } | ||||
return true; | // Check there is no dangling proof in the radix tree | ||||
return shareableProofs.forEachLeaf([&](RCUPtr<const Proof> pLeaf) { | |||||
return isBoundToPeer(pLeaf->getId()); | |||||
}); | |||||
} | } | ||||
PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | PeerId selectPeerImpl(const std::vector<Slot> &slots, const uint64_t slot, | ||||
const uint64_t max) { | const uint64_t max) { | ||||
assert(slot <= max); | assert(slot <= max); | ||||
size_t begin = 0, end = slots.size(); | size_t begin = 0, end = slots.size(); | ||||
uint64_t bottom = 0, top = max; | uint64_t bottom = 0, top = max; | ||||
▲ Show 20 Lines • Show All 63 Lines • Show Last 20 Lines |