diff --git a/src/avalanche/peermanager.h b/src/avalanche/peermanager.h --- a/src/avalanche/peermanager.h +++ b/src/avalanche/peermanager.h @@ -357,7 +357,9 @@ bool isInConflictingPool(const ProofId &proofid) const; private: - void moveToConflictingPool(const ProofRef &proof); + template + void moveToConflictingPool(const ProofContainer &proofs); + bool addOrUpdateNode(const PeerSet::iterator &it, NodeId nodeid); bool addNodeToPeer(const PeerSet::iterator &it); bool removeNodeFromPeer(const PeerSet::iterator &it, uint32_t count = 1); diff --git a/src/avalanche/peermanager.cpp b/src/avalanche/peermanager.cpp --- a/src/avalanche/peermanager.cpp +++ b/src/avalanche/peermanager.cpp @@ -175,14 +175,17 @@ return it->nextPossibleConflictTime == nextTime; } -void PeerManager::moveToConflictingPool(const ProofRef &proof) { +template +void PeerManager::moveToConflictingPool(const ProofContainer &proofs) { auto &peersView = peers.get(); - auto it = peersView.find(proof->getId()); - if (it != peersView.end()) { - removePeer(it->peerid); - } + for (const ProofRef &proof : proofs) { + auto it = peersView.find(proof->getId()); + if (it != peersView.end()) { + removePeer(it->peerid); + } - conflictingProofPool.addProofIfPreferred(proof); + conflictingProofPool.addProofIfPreferred(proof); + } } bool PeerManager::registerProof(const ProofRef &proof, @@ -262,10 +265,7 @@ // If we have overridden other proofs due to conflict, // remove the peers and attempt to move them to the // conflicting pool. - for (const ProofRef &conflictingProof : - conflictingProofs) { - moveToConflictingPool(conflictingProof); - } + moveToConflictingPool(conflictingProofs); // Replacement is successful, continue to peer creation break; @@ -285,15 +285,7 @@ // Move the conflicting proofs from the valid pool to the // conflicting pool - auto &peersView = peers.get(); - for (const ProofRef &conflictingProof : conflictingProofs) { - auto it = peersView.find(conflictingProof->getId()); - if (it != peersView.end()) { - removePeer(it->peerid); - } - - conflictingProofPool.addProofIfPreferred(conflictingProof); - } + moveToConflictingPool(conflictingProofs); auto status = validProofPool.addProofIfNoConflict(proof); assert(status == ProofPool::AddProofStatus::SUCCEED);