diff --git a/src/avalanche/processor.h b/src/avalanche/processor.h --- a/src/avalanche/processor.h +++ b/src/avalanche/processor.h @@ -288,7 +288,6 @@ CPubKey getSessionPubKey() const; bool sendHello(CNode *pfrom) const; - bool addProof(const std::shared_ptr &proof); std::shared_ptr getProof(const ProofId &proofid) const; std::shared_ptr getLocalProof() const; std::shared_ptr getOrphan(const ProofId &proofid) const; diff --git a/src/avalanche/processor.cpp b/src/avalanche/processor.cpp --- a/src/avalanche/processor.cpp +++ b/src/avalanche/processor.cpp @@ -558,11 +558,6 @@ return true; } -bool Processor::addProof(const std::shared_ptr &proof) { - LOCK(cs_peerManager); - return peerManager->registerProof(proof); -} - std::shared_ptr Processor::getProof(const ProofId &proofid) const { LOCK(cs_peerManager); return peerManager->getProof(proofid); diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -133,13 +133,18 @@ bool addNode(NodeId nodeid) { auto proof = GetProof(); - BOOST_CHECK(m_processor->addProof(proof)); - return addNode(nodeid, proof->getId()); + return m_processor->withPeerManager([&](avalanche::PeerManager &pm) { + return pm.registerProof(proof) && + pm.addNode(nodeid, proof->getId()); + }); } std::array ConnectNodes() { auto proof = GetProof(); - BOOST_CHECK(m_processor->addProof(proof)); + BOOST_CHECK( + m_processor->withPeerManager([&](avalanche::PeerManager &pm) { + return pm.registerProof(proof); + })); const ProofId &proofid = proof->getId(); std::array nodes; @@ -744,7 +749,8 @@ BOOST_AUTO_TEST_CASE(poll_inflight_count) { // Create enough nodes so that we run into the inflight request limit. auto proof = GetProof(); - BOOST_CHECK(m_processor->addProof(proof)); + BOOST_CHECK(m_processor->withPeerManager( + [&](avalanche::PeerManager &pm) { return pm.registerProof(proof); })); std::array nodes; for (auto &n : nodes) { diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -4372,22 +4372,25 @@ // addProof should not be called while cs_proofrequest because it holds // cs_main and that creates a potential deadlock during shutdown - if (g_avalanche->addProof(proof)) { - WITH_LOCK(cs_proofrequest, m_proofrequest.ForgetInvId(proofid)); - RelayProof(proofid, m_connman); + g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { + if (pm.registerProof(proof)) { + WITH_LOCK(cs_proofrequest, m_proofrequest.ForgetInvId(proofid)); + RelayProof(proofid, m_connman); - LogPrint(BCLog::NET, "New avalanche proof: peer=%d, proofid %s\n", - nodeid, proofid.ToString()); - } else { - // If the proof couldn't be added, it can be either orphan or - // invalid. In the latter case we should increase the ban score. - // TODO improve the ban reason by printing the validation state - if (!g_avalanche->getOrphan(proofid)) { - WITH_LOCK(cs_rejectedProofs, rejectedProofs->insert(proofid)); - Misbehaving(nodeid, 100, "invalid-avaproof"); + LogPrint(BCLog::NET, + "New avalanche proof: peer=%d, proofid %s\n", nodeid, + proofid.ToString()); + } else { + // If the proof couldn't be added, it can be either orphan or + // invalid. In the latter case we should increase the ban score. + // TODO improve the ban reason by printing the validation state + if (!pm.getOrphan(proofid)) { + WITH_LOCK(cs_rejectedProofs, + rejectedProofs->insert(proofid)); + Misbehaving(nodeid, 100, "invalid-avaproof"); + } } - } - + }); return; } diff --git a/src/rpc/avalanche.cpp b/src/rpc/avalanche.cpp --- a/src/rpc/avalanche.cpp +++ b/src/rpc/avalanche.cpp @@ -52,6 +52,13 @@ return HexToPubKey(keyHex); } +static bool registerProofIfNeeded(std::shared_ptr proof) { + return g_avalanche->withPeerManager([&](avalanche::PeerManager &pm) { + return pm.getProof(proof->getId()) || + pm.registerProof(std::move(proof)); + }); +} + static UniValue addavalanchenode(const Config &config, const JSONRPCRequest &request) { RPCHelpMan{ @@ -95,7 +102,7 @@ } const avalanche::ProofId &proofid = proof->getId(); - if (!g_avalanche->getProof(proofid) && !g_avalanche->addProof(proof)) { + if (!registerProofIfNeeded(proof)) { return false; } @@ -576,7 +583,7 @@ // verification has already been done, a failure likely indicates that there // already is a proof with conflicting utxos. const avalanche::ProofId &proofid = proof->getId(); - if (!g_avalanche->getProof(proofid) && !g_avalanche->addProof(proof)) { + if (!registerProofIfNeeded(proof)) { throw JSONRPCError( RPC_INVALID_PARAMETER, "The proof has conflicting utxo with an existing proof");