Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 169 Lines • ▼ Show 20 Lines | bool PeerManager::updateNextPossibleConflictTime( | ||||
peers.modify(it, [&](Peer &p) { | peers.modify(it, [&](Peer &p) { | ||||
p.nextPossibleConflictTime = | p.nextPossibleConflictTime = | ||||
std::max(p.nextPossibleConflictTime, nextTime); | std::max(p.nextPossibleConflictTime, nextTime); | ||||
}); | }); | ||||
return it->nextPossibleConflictTime == nextTime; | return it->nextPossibleConflictTime == nextTime; | ||||
} | } | ||||
void PeerManager::moveToConflictingPool(const ProofRef &proof) { | template <typename ProofContainer> | ||||
void PeerManager::moveToConflictingPool(const ProofContainer &proofs) { | |||||
auto &peersView = peers.get<by_proofid>(); | auto &peersView = peers.get<by_proofid>(); | ||||
for (const ProofRef &proof : proofs) { | |||||
auto it = peersView.find(proof->getId()); | auto it = peersView.find(proof->getId()); | ||||
if (it != peersView.end()) { | if (it != peersView.end()) { | ||||
removePeer(it->peerid); | removePeer(it->peerid); | ||||
} | } | ||||
conflictingProofPool.addProofIfPreferred(proof); | conflictingProofPool.addProofIfPreferred(proof); | ||||
} | } | ||||
} | |||||
bool PeerManager::registerProof(const ProofRef &proof, | bool PeerManager::registerProof(const ProofRef &proof, | ||||
ProofRegistrationState ®istrationState, | ProofRegistrationState ®istrationState, | ||||
RegistrationMode mode) { | RegistrationMode mode) { | ||||
assert(proof); | assert(proof); | ||||
const ProofId &proofid = proof->getId(); | const ProofId &proofid = proof->getId(); | ||||
▲ Show 20 Lines • Show All 62 Lines • ▼ Show 20 Lines | switch (validProofPool.addProofIfNoConflict(proof, conflictingProofs)) { | ||||
// replace the conflicting ones. | // replace the conflicting ones. | ||||
if (gArgs.GetBoolArg( | if (gArgs.GetBoolArg( | ||||
"-enableavalancheproofreplacement", | "-enableavalancheproofreplacement", | ||||
AVALANCHE_DEFAULT_PROOF_REPLACEMENT_ENABLED)) { | AVALANCHE_DEFAULT_PROOF_REPLACEMENT_ENABLED)) { | ||||
if (validProofPool.addProofIfPreferred(proof)) { | if (validProofPool.addProofIfPreferred(proof)) { | ||||
// If we have overridden other proofs due to conflict, | // If we have overridden other proofs due to conflict, | ||||
// remove the peers and attempt to move them to the | // remove the peers and attempt to move them to the | ||||
// conflicting pool. | // conflicting pool. | ||||
for (const ProofRef &conflictingProof : | moveToConflictingPool(conflictingProofs); | ||||
conflictingProofs) { | |||||
moveToConflictingPool(conflictingProof); | |||||
} | |||||
// Replacement is successful, continue to peer creation | // Replacement is successful, continue to peer creation | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
// Not the preferred proof, or replacement is not enabled | // Not the preferred proof, or replacement is not enabled | ||||
return conflictingProofPool.addProofIfPreferred(proof) == | return conflictingProofPool.addProofIfPreferred(proof) == | ||||
ProofPool::AddProofStatus::REJECTED | ProofPool::AddProofStatus::REJECTED | ||||
? invalidate(ProofRegistrationResult::REJECTED, | ? invalidate(ProofRegistrationResult::REJECTED, | ||||
"rejected-proof") | "rejected-proof") | ||||
: invalidate(ProofRegistrationResult::CONFLICTING, | : invalidate(ProofRegistrationResult::CONFLICTING, | ||||
"conflicting-utxos"); | "conflicting-utxos"); | ||||
} | } | ||||
conflictingProofPool.removeProof(proofid); | conflictingProofPool.removeProof(proofid); | ||||
// Move the conflicting proofs from the valid pool to the | // Move the conflicting proofs from the valid pool to the | ||||
// conflicting pool | // conflicting pool | ||||
auto &peersView = peers.get<by_proofid>(); | moveToConflictingPool(conflictingProofs); | ||||
for (const ProofRef &conflictingProof : conflictingProofs) { | |||||
auto it = peersView.find(conflictingProof->getId()); | |||||
if (it != peersView.end()) { | |||||
removePeer(it->peerid); | |||||
} | |||||
conflictingProofPool.addProofIfPreferred(conflictingProof); | |||||
} | |||||
auto status = validProofPool.addProofIfNoConflict(proof); | auto status = validProofPool.addProofIfNoConflict(proof); | ||||
assert(status == ProofPool::AddProofStatus::SUCCEED); | assert(status == ProofPool::AddProofStatus::SUCCEED); | ||||
break; | break; | ||||
} | } | ||||
case ProofPool::AddProofStatus::DUPLICATED: | case ProofPool::AddProofStatus::DUPLICATED: | ||||
// If the proof was already in the pool, don't duplicate the peer. | // If the proof was already in the pool, don't duplicate the peer. | ||||
▲ Show 20 Lines • Show All 456 Lines • Show Last 20 Lines |