Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 262 Lines • ▼ Show 20 Lines | bool PeerManager::createPeer(const ProofRef &proof) { | ||||
} | } | ||||
// New peer means new peerid! | // New peer means new peerid! | ||||
const PeerId peerid = nextPeerId++; | const PeerId peerid = nextPeerId++; | ||||
// Attach UTXOs to this proof. | // Attach UTXOs to this proof. | ||||
std::unordered_set<ProofRef> conflicting_proofs; | std::unordered_set<ProofRef> conflicting_proofs; | ||||
for (const auto &s : proof->getStakes()) { | for (const auto &s : proof->getStakes()) { | ||||
auto p = utxos.emplace(s.getStake().getUTXO(), proof); | auto p = validProofPool.emplace(s.getStake().getUTXO(), proof); | ||||
if (!p.second) { | if (!p.second) { | ||||
// We have a collision with an existing proof. | // We have a collision with an existing proof. | ||||
conflicting_proofs.insert(p.first->second); | conflicting_proofs.insert(p.first->proof); | ||||
} | } | ||||
} | } | ||||
// For now, if there is a conflict, just cleanup the mess. | // For now, if there is a conflict, just cleanup the mess. | ||||
if (conflicting_proofs.size() > 0) { | if (conflicting_proofs.size() > 0) { | ||||
for (const auto &s : proof->getStakes()) { | for (const auto &s : proof->getStakes()) { | ||||
auto it = utxos.find(s.getStake().getUTXO()); | auto it = validProofPool.find(s.getStake().getUTXO()); | ||||
assert(it != utxos.end()); | assert(it != validProofPool.end()); | ||||
// We need to delete that one. | // We need to delete that one. | ||||
if (it->second->getId() == proofid) { | if (it->proof->getId() == proofid) { | ||||
utxos.erase(it); | validProofPool.erase(it); | ||||
} | } | ||||
} | } | ||||
// Orphan the proof so it can be pulled back if the conflicting ones are | // Orphan the proof so it can be pulled back if the conflicting ones are | ||||
// invalidated. | // invalidated. | ||||
orphanProofs.addProof(proof); | orphanProofs.addProof(proof); | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | bool PeerManager::removePeer(const PeerId peerid) { | ||||
// active. This ensure that we don't overquery them in case they are | // active. This ensure that we don't overquery them in case they are | ||||
// 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. | ||||
for (const auto &s : it->proof->getStakes()) { | for (const auto &s : it->proof->getStakes()) { | ||||
bool deleted = utxos.erase(s.getStake().getUTXO()) > 0; | bool deleted = validProofPool.erase(s.getStake().getUTXO()) > 0; | ||||
assert(deleted); | assert(deleted); | ||||
} | } | ||||
m_unbroadcast_proofids.erase(it->proof->getId()); | m_unbroadcast_proofids.erase(it->proof->getId()); | ||||
peers.erase(it); | peers.erase(it); | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 72 Lines • ▼ Show 20 Lines | bool PeerManager::verify() const { | ||||
std::unordered_set<COutPoint, SaltedOutpointHasher> peersUtxos; | std::unordered_set<COutPoint, SaltedOutpointHasher> peersUtxos; | ||||
for (const auto &p : peers) { | for (const auto &p : peers) { | ||||
// A peer should have a proof attached | // A peer should have a proof attached | ||||
if (!p.proof) { | if (!p.proof) { | ||||
return false; | return false; | ||||
} | } | ||||
// Check utxos consistency | // Check proof pool consistency | ||||
for (const auto &ss : p.proof->getStakes()) { | for (const auto &ss : p.proof->getStakes()) { | ||||
auto it = utxos.find(ss.getStake().getUTXO()); | auto it = validProofPool.find(ss.getStake().getUTXO()); | ||||
if (it == utxos.end()) { | if (it == validProofPool.end()) { | ||||
return false; | return false; | ||||
} | } | ||||
if (it->second->getId() != p.getProofId()) { | if (it->proof->getId() != p.getProofId()) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!peersUtxos.emplace(it->first).second) { | if (!peersUtxos.emplace(it->utxo).second) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
// Count node attached to this peer. | // Count node attached to this peer. | ||||
const auto count_nodes = [&]() { | const auto count_nodes = [&]() { | ||||
size_t count = 0; | size_t count = 0; | ||||
auto &nview = nodes.get<next_request_time>(); | auto &nview = nodes.get<next_request_time>(); | ||||
Show All 25 Lines | for (const auto &p : peers) { | ||||
// 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 there is no dangling utxo | // Check there is no dangling utxo | ||||
for (const auto &[outpoint, proof] : utxos) { | for (const auto &[outpoint, proof] : validProofPool) { | ||||
if (!peersUtxos.count(outpoint)) { | if (!peersUtxos.count(outpoint)) { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 69 Lines • Show Last 20 Lines |