Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 164 Lines • ▼ Show 20 Lines | if (!valid) { | ||||
if (isOrphanState(state)) { | if (isOrphanState(state)) { | ||||
addToPool(orphanProofPool, proof); | addToPool(orphanProofPool, proof); | ||||
} | } | ||||
// Reject invalid proof. | // Reject invalid proof. | ||||
return false; | return false; | ||||
} | } | ||||
if (!addToPool(validProofPool, proof)) { | if (!addToPool(validProofPool, proof, false /* allowOverride */)) { | ||||
// 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. | ||||
addToPool(orphanProofPool, proof); | addToPool(orphanProofPool, proof); | ||||
return false; | return false; | ||||
} | } | ||||
return createPeer(proof); | return createPeer(proof); | ||||
▲ Show 20 Lines • Show All 83 Lines • ▼ Show 20 Lines | bool PeerManager::isValid(const ProofId &proofid) const { | ||||
return pview.find(proofid) != pview.end(); | return pview.find(proofid) != pview.end(); | ||||
} | } | ||||
bool PeerManager::isOrphan(const ProofId &proofid) const { | bool PeerManager::isOrphan(const ProofId &proofid) const { | ||||
auto &orphanView = orphanProofPool.get<by_proofid>(); | auto &orphanView = orphanProofPool.get<by_proofid>(); | ||||
return orphanView.find(proofid) != orphanView.end(); | return orphanView.find(proofid) != orphanView.end(); | ||||
} | } | ||||
bool PeerManager::addToPool(ProofPool &pool, const ProofRef &proof) { | bool PeerManager::addToPool(ProofPool &pool, const ProofRef &proof, | ||||
bool allowOverride) { | |||||
// 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 = pool.emplace(s.getStake().getUTXO(), proof); | auto p = pool.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->proof); | conflicting_proofs.insert(p.first->proof); | ||||
} | } | ||||
} | } | ||||
// For now, if there is a conflict, just cleanup the mess. | if (conflicting_proofs.size() == 0) { | ||||
if (conflicting_proofs.size() > 0) { | // No conflict | ||||
return true; | |||||
} | |||||
ConflictingProofComparator compare; | |||||
if (!allowOverride || !compare(proof, *conflicting_proofs.begin())) { | |||||
// Not the favorite, clean up the mess | |||||
for (const auto &s : proof->getStakes()) { | for (const auto &s : proof->getStakes()) { | ||||
auto it = pool.find(s.getStake().getUTXO()); | auto it = pool.find(s.getStake().getUTXO()); | ||||
assert(it != pool.end()); | assert(it != pool.end()); | ||||
// We need to delete that one. | // We need to delete that one. | ||||
if (it->proof->getId() == proof->getId()) { | if (it->proof->getId() == proof->getId()) { | ||||
pool.erase(it); | pool.erase(it); | ||||
} | } | ||||
} | } | ||||
return false; | |||||
} | |||||
// This is the favorite, erase the other proofs | |||||
for (auto &conflictingProof : conflicting_proofs) { | |||||
removeFromPool(pool, conflictingProof); | |||||
} | |||||
// Either the utxo is already mapped to this proof or it can be emplaced | |||||
for (const auto &s : proof->getStakes()) { | |||||
auto p = pool.emplace(s.getStake().getUTXO(), proof); | |||||
assert(p.first->proof->getId() == proof->getId()); | |||||
} | } | ||||
return conflicting_proofs.size() == 0; | return true; | ||||
} | } | ||||
bool PeerManager::removeFromPool(ProofPool &pool, const ProofRef &proof) { | bool PeerManager::removeFromPool(ProofPool &pool, const ProofRef &proof) { | ||||
auto &poolView = pool.get<by_proofid>(); | auto &poolView = pool.get<by_proofid>(); | ||||
return poolView.erase(proof->getId()); | return poolView.erase(proof->getId()); | ||||
} | } | ||||
bool PeerManager::createPeer(const ProofRef &proof) { | bool PeerManager::createPeer(const ProofRef &proof) { | ||||
▲ Show 20 Lines • Show All 277 Lines • Show Last 20 Lines |