diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -143,32 +143,12 @@ return nodes.modify(it, [&](Node &n) { n.nextRequestTime = timeout; }); } -static bool isOrphanState(const ProofValidationState &state) { - return state.GetResult() == ProofValidationResult::MISSING_UTXO || - state.GetResult() == ProofValidationResult::HEIGHT_MISMATCH; -} - bool PeerManager::registerProof(const ProofRef &proof) { if (exists(proof->getId())) { // The proof is already registered, or orphaned. return false; } - // Check the proof's validity. - ProofValidationState state; - // Using WITH_LOCK directly inside the if statement will trigger a cppcheck - // false positive syntax error - const bool valid = WITH_LOCK( - cs_main, return proof->verify(state, ::ChainstateActive().CoinsTip())); - if (!valid) { - if (isOrphanState(state)) { - orphanProofPool.addProofIfPreferred(proof); - } - - // Reject invalid proof. - return false; - } - return createPeer(proof); } @@ -195,6 +175,11 @@ return NO_NODE; } +static bool isOrphanState(const ProofValidationState &state) { + return state.GetResult() == ProofValidationResult::MISSING_UTXO || + state.GetResult() == ProofValidationResult::HEIGHT_MISMATCH; +} + void PeerManager::updatedBlockTip() { std::vector invalidPeers; std::vector newOrphans; @@ -265,6 +250,21 @@ bool PeerManager::createPeer(const ProofRef &proof) { assert(proof); + // Check the proof's validity. + ProofValidationState state; + // Using WITH_LOCK directly inside the if statement will trigger a cppcheck + // false positive syntax error + const bool valid = WITH_LOCK( + cs_main, return proof->verify(state, ::ChainstateActive().CoinsTip())); + if (!valid) { + if (isOrphanState(state)) { + orphanProofPool.addProofIfPreferred(proof); + } + + // Reject invalid proof. + return false; + } + ProofPool::ConflictingProofSet conflictingProofs; // Set a cooldown time to limit the proof replacement rate std::chrono::seconds cooldown{ @@ -298,6 +298,8 @@ moveToOrphan(conflictingProof); } + orphanProofPool.removeProof(proof); + // New peer means new peerid! const PeerId peerid = nextPeerId++;