Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/peermanager.cpp
Show First 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
bool PeerManager::registerProof(const ProofRef &proof) { | bool PeerManager::registerProof(const ProofRef &proof) { | ||||
if (exists(proof->getId())) { | if (exists(proof->getId())) { | ||||
// The proof is already registered, or orphaned. | // The proof is already registered, or orphaned. | ||||
return false; | return false; | ||||
} | } | ||||
return createPeer(proof); | // Set a cooldown time to limit the proof replacement rate | ||||
std::chrono::seconds cooldown{ | |||||
gArgs.GetArg("-avalancheproofreplacementcooldown", | |||||
AVALANCHE_DEFAULT_PROOF_REPLACEMENT_COOLDOWN)}; | |||||
return createPeer(proof, cooldown); | |||||
} | } | ||||
NodeId PeerManager::selectNode() { | NodeId PeerManager::selectNode() { | ||||
for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | for (int retry = 0; retry < SELECT_NODE_MAX_RETRY; retry++) { | ||||
const PeerId p = selectPeer(); | const PeerId p = selectPeer(); | ||||
// If we cannot find a peer, it may be due to the fact that it is | // If we cannot find a peer, it may be due to the fact that it is | ||||
// unlikely due to high fragmentation, so compact and retry. | // unlikely due to high fragmentation, so compact and retry. | ||||
▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | void PeerManager::moveToOrphan(const ProofRef &proof) { | ||||
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); | ||||
} | } | ||||
orphanProofPool.addProofIfPreferred(proof); | orphanProofPool.addProofIfPreferred(proof); | ||||
} | } | ||||
bool PeerManager::createPeer(const ProofRef &proof) { | bool PeerManager::createPeer(const ProofRef &proof, | ||||
std::chrono::seconds cooldown) { | |||||
assert(proof); | assert(proof); | ||||
// Check the proof's validity. | // Check the proof's validity. | ||||
ProofValidationState state; | ProofValidationState state; | ||||
// Using WITH_LOCK directly inside the if statement will trigger a cppcheck | // Using WITH_LOCK directly inside the if statement will trigger a cppcheck | ||||
// false positive syntax error | // false positive syntax error | ||||
const bool valid = WITH_LOCK( | const bool valid = WITH_LOCK( | ||||
cs_main, return proof->verify(state, ::ChainstateActive().CoinsTip())); | cs_main, return proof->verify(state, ::ChainstateActive().CoinsTip())); | ||||
if (!valid) { | if (!valid) { | ||||
if (isOrphanState(state)) { | if (isOrphanState(state)) { | ||||
orphanProofPool.addProofIfPreferred(proof); | orphanProofPool.addProofIfPreferred(proof); | ||||
} | } | ||||
// Reject invalid proof. | // Reject invalid proof. | ||||
return false; | return false; | ||||
} | } | ||||
ProofPool::ConflictingProofSet conflictingProofs; | ProofPool::ConflictingProofSet conflictingProofs; | ||||
// Set a cooldown time to limit the proof replacement rate | |||||
std::chrono::seconds cooldown{ | |||||
gArgs.GetArg("-avalancheproofreplacementcooldown", | |||||
AVALANCHE_DEFAULT_PROOF_REPLACEMENT_COOLDOWN)}; | |||||
auto added = | auto added = | ||||
gArgs.GetBoolArg("-enableavalancheproofreplacement", | gArgs.GetBoolArg("-enableavalancheproofreplacement", | ||||
AVALANCHE_DEFAULT_PROOF_REPLACEMENT_ENABLED) | AVALANCHE_DEFAULT_PROOF_REPLACEMENT_ENABLED) | ||||
? validProofPool.addProofIfPreferred(proof, conflictingProofs, | ? validProofPool.addProofIfPreferred(proof, conflictingProofs, | ||||
cooldown) | cooldown) | ||||
: validProofPool.addProofIfNoConflict(proof, conflictingProofs); | : validProofPool.addProofIfNoConflict(proof, conflictingProofs); | ||||
switch (added) { | switch (added) { | ||||
▲ Show 20 Lines • Show All 294 Lines • Show Last 20 Lines |