From 7ebc61e375817af19a8ba7b8ca2df0f299314924 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 1 Dec 2024 18:00:17 +0000 Subject: [PATCH] refactor: move `CConnman` out of `CQuorumManager` ctor --- src/dsnotificationinterface.cpp | 2 +- src/llmq/context.cpp | 6 +++--- src/llmq/quorums.cpp | 34 +++++++++++++++++---------------- src/llmq/quorums.h | 19 ++++++++++-------- src/net_processing.cpp | 2 +- src/rpc/quorums.cpp | 2 +- 6 files changed, 35 insertions(+), 30 deletions(-) diff --git a/src/dsnotificationinterface.cpp b/src/dsnotificationinterface.cpp index 701f96ccaa..3a0b9b9b0c 100644 --- a/src/dsnotificationinterface.cpp +++ b/src/dsnotificationinterface.cpp @@ -93,7 +93,7 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con m_llmq_ctx->isman->UpdatedBlockTip(pindexNew); m_llmq_ctx->clhandler->UpdatedBlockTip(); - m_llmq_ctx->qman->UpdatedBlockTip(pindexNew, fInitialDownload); + m_llmq_ctx->qman->UpdatedBlockTip(pindexNew, m_connman, fInitialDownload); m_llmq_ctx->qdkgsman->UpdatedBlockTip(pindexNew, fInitialDownload); m_llmq_ctx->ehfSignalsHandler->UpdatedBlockTip(pindexNew, /* is_masternode = */ m_mn_activeman != nullptr); diff --git a/src/llmq/context.cpp b/src/llmq/context.cpp index 6f857524dc..2670229e33 100644 --- a/src/llmq/context.cpp +++ b/src/llmq/context.cpp @@ -30,9 +30,9 @@ LLMQContext::LLMQContext(ChainstateManager& chainman, CConnman& connman, CDeterm qdkgsman{std::make_unique(*bls_worker, chainman.ActiveChainstate(), dmnman, *dkg_debugman, mn_metaman, *quorum_block_processor, mn_activeman, sporkman, unit_tests, wipe)}, - qman{std::make_unique(*bls_worker, chainman.ActiveChainstate(), connman, dmnman, *qdkgsman, - evo_db, *quorum_block_processor, mn_activeman, mn_sync, sporkman, - unit_tests, wipe)}, + qman{std::make_unique(*bls_worker, chainman.ActiveChainstate(), dmnman, *qdkgsman, evo_db, + *quorum_block_processor, mn_activeman, mn_sync, sporkman, unit_tests, + wipe)}, sigman{std::make_unique(mn_activeman, chainman.ActiveChainstate(), *qman, peerman, unit_tests, wipe)}, shareman{std::make_unique(connman, *sigman, mn_activeman, *qman, sporkman, peerman)}, diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 1b9902a5a8..85309f0bd3 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -210,8 +210,8 @@ bool CQuorum::ReadContributions(const CDBWrapper& db) return true; } -CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, - CDeterministicMNManager& dmnman, CDKGSessionManager& _dkgManager, CEvoDB& _evoDb, +CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman, + CDKGSessionManager& _dkgManager, CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const CActiveMasternodeManager* const mn_activeman, const CMasternodeSync& mn_sync, const CSporkManager& sporkman, bool unit_tests, bool wipe) : @@ -219,7 +219,6 @@ CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, unit_tests, wipe)), blsWorker(_blsWorker), m_chainstate(chainstate), - connman(_connman), m_dmnman(dmnman), dkgManager(_dkgManager), quorumBlockProcessor(_quorumBlockProcessor), @@ -249,7 +248,7 @@ void CQuorumManager::Stop() workerPool.stop(true); } -void CQuorumManager::TriggerQuorumDataRecoveryThreads(const CBlockIndex* pIndex) const +void CQuorumManager::TriggerQuorumDataRecoveryThreads(CConnman& connman, const CBlockIndex* pIndex) const { if ((m_mn_activeman == nullptr && !IsWatchQuorumsEnabled()) || !QuorumDataRecoveryEnabled() || pIndex == nullptr) { return; @@ -296,17 +295,17 @@ void CQuorumManager::TriggerQuorumDataRecoveryThreads(const CBlockIndex* pIndex) } // Finally start the thread which triggers the requests for this quorum - StartQuorumDataRecoveryThread(pQuorum, pIndex, nDataMask); + StartQuorumDataRecoveryThread(connman, pQuorum, pIndex, nDataMask); } } } -void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitialDownload) const +void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, CConnman& connman, bool fInitialDownload) const { if (!m_mn_sync.IsBlockchainSynced()) return; for (const auto& params : Params().GetConsensus().llmqs) { - CheckQuorumConnections(params, pindexNew); + CheckQuorumConnections(connman, params, pindexNew); } if (m_mn_activeman != nullptr || IsWatchQuorumsEnabled()) { @@ -322,11 +321,12 @@ void CQuorumManager::UpdatedBlockTip(const CBlockIndex* pindexNew, bool fInitial } } - TriggerQuorumDataRecoveryThreads(pindexNew); + TriggerQuorumDataRecoveryThreads(connman, pindexNew); StartCleanupOldQuorumDataThread(pindexNew); } -void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex* pindexNew) const +void CQuorumManager::CheckQuorumConnections(CConnman& connman, const Consensus::LLMQParams& llmqParams, + const CBlockIndex* pindexNew) const { if (m_mn_activeman == nullptr && !IsWatchQuorumsEnabled()) return; @@ -469,7 +469,9 @@ bool CQuorumManager::HasQuorum(Consensus::LLMQType llmqType, const CQuorumBlockP return quorum_block_processor.HasMinedCommitment(llmqType, quorumHash); } -bool CQuorumManager::RequestQuorumData(CNode* pfrom, Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, uint16_t nDataMask, const uint256& proTxHash) const +bool CQuorumManager::RequestQuorumData(CNode* pfrom, CConnman& connman, Consensus::LLMQType llmqType, + const CBlockIndex* pQuorumBaseBlockIndex, uint16_t nDataMask, + const uint256& proTxHash) const { if (pfrom == nullptr) { LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- Invalid pfrom: nullptr\n", __func__); @@ -697,7 +699,7 @@ size_t CQuorumManager::GetQuorumRecoveryStartOffset(const CQuorumCPtr pQuorum, c return nIndex % pQuorum->qc->validMembers.size(); } -PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) +PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, CConnman& connman, const std::string& msg_type, CDataStream& vRecv) { auto strFunc = __func__; auto errorHandler = [&](const std::string& strError, int nScore = 10) -> PeerMsgRet { @@ -709,7 +711,6 @@ PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_t }; if (msg_type == NetMsgType::QGETDATA) { - if (m_mn_activeman == nullptr || (pfrom.GetVerifiedProRegTxHash().IsNull() && !pfrom.qwatch)) { return errorHandler("Not a verified masternode or a qwatch connection"); } @@ -912,7 +913,8 @@ void CQuorumManager::StartCachePopulatorThread(const CQuorumCPtr pQuorum) const }); } -void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, const CBlockIndex* pIndex, uint16_t nDataMaskIn) const +void CQuorumManager::StartQuorumDataRecoveryThread(CConnman& connman, const CQuorumCPtr pQuorum, + const CBlockIndex* pIndex, uint16_t nDataMaskIn) const { assert(m_mn_activeman); @@ -922,7 +924,7 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co } pQuorum->fQuorumDataRecoveryThreadRunning = true; - workerPool.push([pQuorum, pIndex, nDataMaskIn, this](int threadId) { + workerPool.push([&connman, pQuorum, pIndex, nDataMaskIn, this](int threadId) { size_t nTries{0}; uint16_t nDataMask{nDataMaskIn}; int64_t nTimeLastSuccess{0}; @@ -958,7 +960,6 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co printLog("Try to request"); while (nDataMask > 0 && !quorumThreadInterrupt) { - if (nDataMask & llmq::CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR && pQuorum->HasVerificationVector()) { nDataMask &= ~llmq::CQuorumDataRequest::QUORUM_VERIFICATION_VECTOR; @@ -1005,7 +1006,8 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co return; } - if (RequestQuorumData(pNode, pQuorum->qc->llmqType, pQuorum->m_quorum_base_block_index, nDataMask, proTxHash)) { + if (RequestQuorumData(pNode, connman, pQuorum->qc->llmqType, pQuorum->m_quorum_base_block_index, + nDataMask, proTxHash)) { nTimeLastSuccess = GetTime().count(); printLog("Requested"); } else { diff --git a/src/llmq/quorums.h b/src/llmq/quorums.h index 892c0ecd27..6e8db117db 100644 --- a/src/llmq/quorums.h +++ b/src/llmq/quorums.h @@ -238,7 +238,6 @@ private: CBLSWorker& blsWorker; CChainState& m_chainstate; - CConnman& connman; CDeterministicMNManager& m_dmnman; CDKGSessionManager& dkgManager; CQuorumBlockProcessor& quorumBlockProcessor; @@ -261,7 +260,7 @@ private: mutable CThreadInterrupt quorumThreadInterrupt; public: - CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDeterministicMNManager& dmnman, + CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman, CDKGSessionManager& _dkgManager, CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const CActiveMasternodeManager* const mn_activeman, const CMasternodeSync& mn_sync, const CSporkManager& sporkman, bool unit_tests, bool wipe); @@ -270,15 +269,17 @@ public: void Start(); void Stop(); - void TriggerQuorumDataRecoveryThreads(const CBlockIndex* pIndex) const; + void TriggerQuorumDataRecoveryThreads(CConnman& connman, const CBlockIndex* pIndex) const; - void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload) const; + void UpdatedBlockTip(const CBlockIndex* pindexNew, CConnman& connman, bool fInitialDownload) const; - PeerMsgRet ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv); + PeerMsgRet ProcessMessage(CNode& pfrom, CConnman& connman, const std::string& msg_type, CDataStream& vRecv); static bool HasQuorum(Consensus::LLMQType llmqType, const CQuorumBlockProcessor& quorum_block_processor, const uint256& quorumHash); - bool RequestQuorumData(CNode* pfrom, Consensus::LLMQType llmqType, const CBlockIndex* pQuorumBaseBlockIndex, uint16_t nDataMask, const uint256& proTxHash = uint256()) const; + bool RequestQuorumData(CNode* pfrom, CConnman& connman, Consensus::LLMQType llmqType, + const CBlockIndex* pQuorumBaseBlockIndex, uint16_t nDataMask, + const uint256& proTxHash = uint256()) const; // all these methods will lock cs_main for a short period of time CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const uint256& quorumHash) const; @@ -289,7 +290,8 @@ public: private: // all private methods here are cs_main-free - void CheckQuorumConnections(const Consensus::LLMQParams& llmqParams, const CBlockIndex *pindexNew) const; + void CheckQuorumConnections(CConnman& connman, const Consensus::LLMQParams& llmqParams, + const CBlockIndex* pindexNew) const; CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, gsl::not_null pQuorumBaseBlockIndex, bool populate_cache) const; bool BuildQuorumContributions(const CFinalCommitmentPtr& fqc, const std::shared_ptr& quorum) const; @@ -301,7 +303,8 @@ private: size_t GetQuorumRecoveryStartOffset(const CQuorumCPtr pQuorum, const CBlockIndex* pIndex) const; void StartCachePopulatorThread(const CQuorumCPtr pQuorum) const; - void StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, const CBlockIndex* pIndex, uint16_t nDataMask) const; + void StartQuorumDataRecoveryThread(CConnman& connman, const CQuorumCPtr pQuorum, const CBlockIndex* pIndex, + uint16_t nDataMask) const; void StartCleanupOldQuorumDataThread(const CBlockIndex* pIndex) const; void MigrateOldQuorumDB(CEvoDB& evoDb) const; diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 5c3ceba819..15b7eddc95 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -5263,7 +5263,7 @@ void PeerManagerImpl::ProcessMessage( ProcessPeerMsgRet(CMNAuth::ProcessMessage(pfrom, peer->m_their_services, m_connman, m_mn_metaman, m_mn_activeman, m_chainman.ActiveChain(), m_mn_sync, m_dmnman->GetListAtChainTip(), msg_type, vRecv), pfrom); PostProcessMessage(m_llmq_ctx->quorum_block_processor->ProcessMessage(pfrom, msg_type, vRecv), pfrom.GetId()); ProcessPeerMsgRet(m_llmq_ctx->qdkgsman->ProcessMessage(pfrom, *this, is_masternode, msg_type, vRecv), pfrom); - ProcessPeerMsgRet(m_llmq_ctx->qman->ProcessMessage(pfrom, msg_type, vRecv), pfrom); + ProcessPeerMsgRet(m_llmq_ctx->qman->ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom); m_llmq_ctx->shareman->ProcessMessage(pfrom, m_sporkman, msg_type, vRecv); ProcessPeerMsgRet(m_llmq_ctx->sigman->ProcessMessage(pfrom, msg_type, vRecv), pfrom); diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index e4c12e8b21..5289d027a2 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -818,7 +818,7 @@ static RPCHelpMan quorum_getdata() const CBlockIndex* pQuorumBaseBlockIndex = WITH_LOCK(cs_main, return chainman.m_blockman.LookupBlockIndex(quorumHash)); return connman.ForNode(nodeId, [&](CNode* pNode) { - return llmq_ctx.qman->RequestQuorumData(pNode, llmqType, pQuorumBaseBlockIndex, nDataMask, proTxHash); + return llmq_ctx.qman->RequestQuorumData(pNode, connman, llmqType, pQuorumBaseBlockIndex, nDataMask, proTxHash); }); }, };