diff --git a/src/init.cpp b/src/init.cpp index 99efaf808c..d0c1050330 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -313,8 +313,7 @@ void PrepareShutdown(NodeContext& node) ::dstxManager.reset(); node.mn_sync = nullptr; ::masternodeSync.reset(); - node.sporkman = nullptr; - ::sporkManager.reset(); + node.sporkman.reset(); node.govman = nullptr; ::governance.reset(); @@ -1717,15 +1716,8 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc ::governance = std::make_unique(); node.govman = ::governance.get(); - assert(!node.peerman); - node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(), - *node.scheduler, chainman, *node.mempool, *node.govman, - node.cj_ctx, node.llmq_ctx, ignores_incoming_txs); - RegisterValidationInterface(node.peerman.get()); - - assert(!::sporkManager); - ::sporkManager = std::make_unique(); - node.sporkman = ::sporkManager.get(); + assert(!node.sporkman); + node.sporkman = std::make_unique(); std::vector vSporkAddresses; if (args.IsArgSet("-sporkaddr")) { @@ -1755,6 +1747,12 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc ::masternodeSync = std::make_unique(*node.connman, *node.govman); node.mn_sync = ::masternodeSync.get(); + assert(!node.peerman); + node.peerman = PeerManager::make(chainparams, *node.connman, *node.addrman, node.banman.get(), + *node.scheduler, chainman, *node.mempool, *node.govman, *node.sporkman, + node.cj_ctx, node.llmq_ctx, ignores_incoming_txs); + RegisterValidationInterface(node.peerman.get()); + // sanitize comments per BIP-0014, format user agent and check total size std::vector uacomments; diff --git a/src/llmq/context.cpp b/src/llmq/context.cpp index 11617d35e7..6e4e5dfcc3 100644 --- a/src/llmq/context.cpp +++ b/src/llmq/context.cpp @@ -31,11 +31,11 @@ LLMQContext::LLMQContext(CChainState& chainstate, CConnman& connman, CEvoDB& evo qdkgsman{std::make_unique(*bls_worker, chainstate, connman, *dkg_debugman, *quorum_block_processor, sporkman, unit_tests, wipe)}, qman{[&]() -> llmq::CQuorumManager* const { assert(llmq::quorumManager == nullptr); - llmq::quorumManager = std::make_unique(*bls_worker, chainstate, connman, *qdkgsman, evo_db, *quorum_block_processor, ::masternodeSync); + llmq::quorumManager = std::make_unique(*bls_worker, chainstate, connman, *qdkgsman, evo_db, *quorum_block_processor, sporkman, ::masternodeSync); return llmq::quorumManager.get(); }()}, sigman{std::make_unique(connman, *llmq::quorumManager, unit_tests, wipe)}, - shareman{std::make_unique(connman, *llmq::quorumManager, *sigman, peerman)}, + shareman{std::make_unique(connman, *sigman, *llmq::quorumManager, sporkman, peerman)}, clhandler{[&]() -> llmq::CChainLocksHandler* const { assert(llmq::chainLocksHandler == nullptr); llmq::chainLocksHandler = std::make_unique(chainstate, connman, *::masternodeSync, *llmq::quorumManager, *sigman, *shareman, sporkman, mempool); diff --git a/src/llmq/dkgsession.cpp b/src/llmq/dkgsession.cpp index 53896d2f86..8ce8069ff2 100644 --- a/src/llmq/dkgsession.cpp +++ b/src/llmq/dkgsession.cpp @@ -450,7 +450,7 @@ void CDKGSession::VerifyAndComplain(CDKGPendingMessages& pendingMessages) void CDKGSession::VerifyConnectionAndMinProtoVersions() const { - if (!IsQuorumPoseEnabled(params.type)) { + if (!IsQuorumPoseEnabled(params.type, m_sporkman)) { return; } @@ -465,7 +465,7 @@ void CDKGSession::VerifyConnectionAndMinProtoVersions() const protoMap.emplace(verifiedProRegTxHash, pnode->nVersion); }); - bool fShouldAllMembersBeConnected = IsAllMembersConnectedEnabled(params.type); + bool fShouldAllMembersBeConnected = IsAllMembersConnectedEnabled(params.type, m_sporkman); for (const auto& m : members) { if (m->dmn->proTxHash == myProTxHash) { continue; diff --git a/src/llmq/dkgsession.h b/src/llmq/dkgsession.h index 601274ed50..02b31ac585 100644 --- a/src/llmq/dkgsession.h +++ b/src/llmq/dkgsession.h @@ -19,6 +19,7 @@ class UniValue; class CInv; class CConnman; class CDeterministicMN; +class CSporkManager; using CDeterministicMNCPtr = std::shared_ptr; namespace llmq @@ -271,6 +272,7 @@ private: CBLSWorkerCache cache; CDKGSessionManager& dkgManager; CDKGDebugManager& dkgDebugManager; + const CSporkManager& m_sporkman; const CBlockIndex* m_quorum_base_block_index{nullptr}; int quorumIndex{0}; @@ -311,8 +313,8 @@ private: std::set validCommitments GUARDED_BY(invCs); public: - CDKGSession(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager, CDKGDebugManager& _dkgDebugManager, CConnman& _connman) : - params(_params), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager), dkgDebugManager(_dkgDebugManager), connman(_connman) {} + CDKGSession(const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker, CDKGSessionManager& _dkgManager, CDKGDebugManager& _dkgDebugManager, CConnman& _connman, const CSporkManager& sporkman) : + params(_params), blsWorker(_blsWorker), cache(_blsWorker), dkgManager(_dkgManager), dkgDebugManager(_dkgDebugManager), m_sporkman(sporkman), connman(_connman) {} bool Init(gsl::not_null pQuorumBaseBlockIndex, Span mns, const uint256& _myProTxHash, int _quorumIndex); diff --git a/src/llmq/dkgsessionhandler.cpp b/src/llmq/dkgsessionhandler.cpp index 2248de9a4f..c145652da1 100644 --- a/src/llmq/dkgsessionhandler.cpp +++ b/src/llmq/dkgsessionhandler.cpp @@ -25,7 +25,7 @@ namespace llmq { CDKGSessionHandler::CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGDebugManager& _dkgDebugManager, - CDKGSessionManager& _dkgManager, CQuorumBlockProcessor& _quorumBlockProcessor, + CDKGSessionManager& _dkgManager, CQuorumBlockProcessor& _quorumBlockProcessor, const CSporkManager& sporkman, const Consensus::LLMQParams& _params, int _quorumIndex) : blsWorker(_blsWorker), m_chainstate(chainstate), @@ -33,9 +33,10 @@ CDKGSessionHandler::CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chai dkgDebugManager(_dkgDebugManager), dkgManager(_dkgManager), quorumBlockProcessor(_quorumBlockProcessor), + m_sporkman(sporkman), params(_params), quorumIndex(_quorumIndex), - curSession(std::make_unique(_params, _blsWorker, _dkgManager, _dkgDebugManager, _connman)), + curSession(std::make_unique(_params, _blsWorker, _dkgManager, _dkgDebugManager, _connman, sporkman)), pendingContributions((size_t)_params.size * 2, MSG_QUORUM_CONTRIB), // we allow size*2 messages as we need to make sure we see bad behavior (double messages) pendingComplaints((size_t)_params.size * 2, MSG_QUORUM_COMPLAINT), pendingJustifications((size_t)_params.size * 2, MSG_QUORUM_JUSTIFICATION), @@ -183,7 +184,7 @@ void CDKGSessionHandler::StopThread() bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pQuorumBaseBlockIndex) { - curSession = std::make_unique(params, blsWorker, dkgManager, dkgDebugManager, connman); + curSession = std::make_unique(params, blsWorker, dkgManager, dkgDebugManager, connman, m_sporkman); if (!DeploymentDIP0003Enforced(pQuorumBaseBlockIndex->nHeight, Params().GetConsensus())) { return false; @@ -526,9 +527,9 @@ void CDKGSessionHandler::HandleDKGRound() return changed; }); - utils::EnsureQuorumConnections(params, pQuorumBaseBlockIndex, connman, curSession->myProTxHash); + utils::EnsureQuorumConnections(params, connman, m_sporkman, pQuorumBaseBlockIndex, curSession->myProTxHash); if (curSession->AreWeMember()) { - utils::AddQuorumProbeConnections(params, pQuorumBaseBlockIndex, connman, curSession->myProTxHash); + utils::AddQuorumProbeConnections(params, connman, m_sporkman, pQuorumBaseBlockIndex, curSession->myProTxHash); } WaitForNextPhase(QuorumPhase::Initialized, QuorumPhase::Contribute, curQuorumHash); diff --git a/src/llmq/dkgsessionhandler.h b/src/llmq/dkgsessionhandler.h index 7990786f62..d00d5b173c 100644 --- a/src/llmq/dkgsessionhandler.h +++ b/src/llmq/dkgsessionhandler.h @@ -17,6 +17,7 @@ class CBlockIndex; class CBLSWorker; class CChainState; +class CSporkManager; class PeerManager; namespace llmq @@ -122,6 +123,7 @@ private: CDKGDebugManager& dkgDebugManager; CDKGSessionManager& dkgManager; CQuorumBlockProcessor& quorumBlockProcessor; + const CSporkManager& m_sporkman; const Consensus::LLMQParams params; const int quorumIndex; @@ -141,7 +143,7 @@ private: public: CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGDebugManager& _dkgDebugManager, - CDKGSessionManager& _dkgManager, CQuorumBlockProcessor& _quorumBlockProcessor, + CDKGSessionManager& _dkgManager, CQuorumBlockProcessor& _quorumBlockProcessor, const CSporkManager& sporkman, const Consensus::LLMQParams& _params, int _quorumIndex); ~CDKGSessionHandler() = default; diff --git a/src/llmq/dkgsessionmgr.cpp b/src/llmq/dkgsessionmgr.cpp index 7b81f8c092..3b4ad78e79 100644 --- a/src/llmq/dkgsessionmgr.cpp +++ b/src/llmq/dkgsessionmgr.cpp @@ -25,7 +25,7 @@ static const std::string DB_SKCONTRIB = "qdkg_S"; static const std::string DB_ENC_CONTRIB = "qdkg_E"; CDKGSessionManager::CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGDebugManager& _dkgDebugManager, - CQuorumBlockProcessor& _quorumBlockProcessor, CSporkManager& sporkManager, + CQuorumBlockProcessor& _quorumBlockProcessor, const CSporkManager& sporkManager, bool unitTests, bool fWipe) : db(std::make_unique(unitTests ? "" : (GetDataDir() / "llmq/dkgdb"), 1 << 20, unitTests, fWipe)), blsWorker(_blsWorker), @@ -48,7 +48,7 @@ CDKGSessionManager::CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chai for (const auto i : irange::range(session_count)) { dkgSessionHandlers.emplace(std::piecewise_construct, std::forward_as_tuple(params.type, i), - std::forward_as_tuple(blsWorker, m_chainstate, connman, dkgDebugManager, *this, quorumBlockProcessor, params, i)); + std::forward_as_tuple(blsWorker, m_chainstate, connman, dkgDebugManager, *this, quorumBlockProcessor, spork_manager, params, i)); } } } diff --git a/src/llmq/dkgsessionmgr.h b/src/llmq/dkgsessionmgr.h index d1f9ea0456..7d2f12fbf0 100644 --- a/src/llmq/dkgsessionmgr.h +++ b/src/llmq/dkgsessionmgr.h @@ -38,7 +38,7 @@ private: CConnman& connman; CDKGDebugManager& dkgDebugManager; CQuorumBlockProcessor& quorumBlockProcessor; - CSporkManager& spork_manager; + const CSporkManager& spork_manager; //TODO name struct instead of std::pair std::map, CDKGSessionHandler> dkgSessionHandlers; @@ -64,7 +64,7 @@ private: public: CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGDebugManager& _dkgDebugManager, - CQuorumBlockProcessor& _quorumBlockProcessor, CSporkManager& sporkManager, + CQuorumBlockProcessor& _quorumBlockProcessor, const CSporkManager& sporkManager, bool unitTests, bool fWipe); ~CDKGSessionManager() = default; diff --git a/src/llmq/options.cpp b/src/llmq/options.cpp index 2baba72605..d75df2134a 100644 --- a/src/llmq/options.cpp +++ b/src/llmq/options.cpp @@ -20,7 +20,7 @@ static constexpr int TESTNET_LLMQ_25_67_ACTIVATION_HEIGHT = 847000; namespace llmq { -static bool EvalSpork(Consensus::LLMQType llmqType, int64_t spork_value) +static bool EvalSpork(const Consensus::LLMQType llmqType, const int64_t spork_value) { if (spork_value == 0) { return true; @@ -31,14 +31,14 @@ static bool EvalSpork(Consensus::LLMQType llmqType, int64_t spork_value) return false; } -bool IsAllMembersConnectedEnabled(Consensus::LLMQType llmqType) +bool IsAllMembersConnectedEnabled(const Consensus::LLMQType llmqType, const CSporkManager& sporkman) { - return EvalSpork(llmqType, sporkManager->GetSporkValue(SPORK_21_QUORUM_ALL_CONNECTED)); + return EvalSpork(llmqType, sporkman.GetSporkValue(SPORK_21_QUORUM_ALL_CONNECTED)); } -bool IsQuorumPoseEnabled(Consensus::LLMQType llmqType) +bool IsQuorumPoseEnabled(const Consensus::LLMQType llmqType, const CSporkManager& sporkman) { - return EvalSpork(llmqType, sporkManager->GetSporkValue(SPORK_23_QUORUM_POSE)); + return EvalSpork(llmqType, sporkman.GetSporkValue(SPORK_23_QUORUM_POSE)); } diff --git a/src/llmq/options.h b/src/llmq/options.h index 4c230f94d5..f78f2362fd 100644 --- a/src/llmq/options.h +++ b/src/llmq/options.h @@ -13,6 +13,7 @@ #include class CBlockIndex; +class CSporkManager; namespace llmq { @@ -28,8 +29,8 @@ static constexpr bool DEFAULT_ENABLE_QUORUM_DATA_RECOVERY{true}; // If true, we will connect to all new quorums and watch their communication static constexpr bool DEFAULT_WATCH_QUORUMS{false}; -bool IsAllMembersConnectedEnabled(Consensus::LLMQType llmqType); -bool IsQuorumPoseEnabled(Consensus::LLMQType llmqType); +bool IsAllMembersConnectedEnabled(const Consensus::LLMQType llmqType, const CSporkManager& sporkman); +bool IsQuorumPoseEnabled(const Consensus::LLMQType llmqType, const CSporkManager& sporkman); bool IsQuorumRotationEnabled(const Consensus::LLMQParams& llmqParams, gsl::not_null pindex); diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index d66d58d4a7..4d904b94d6 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -206,13 +206,14 @@ bool CQuorum::ReadContributions(CEvoDB& evoDb) } CQuorumManager::CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGSessionManager& _dkgManager, - CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const std::unique_ptr& mn_sync) : + CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const CSporkManager& sporkman, const std::unique_ptr& mn_sync) : blsWorker(_blsWorker), m_chainstate(chainstate), connman(_connman), dkgManager(_dkgManager), m_evoDb(_evoDb), quorumBlockProcessor(_quorumBlockProcessor), + m_sporkman(sporkman), m_mn_sync(mn_sync) { utils::InitQuorumsCache(mapQuorumsCache, false); @@ -348,7 +349,7 @@ void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqPar }); for (const auto& quorum : lastQuorums) { - if (utils::EnsureQuorumConnections(llmqParams, quorum->m_quorum_base_block_index, connman, myProTxHash)) { + if (utils::EnsureQuorumConnections(llmqParams, connman, m_sporkman, quorum->m_quorum_base_block_index, myProTxHash)) { if (connmanQuorumsToDelete.erase(quorum->qc->quorumHash) > 0) { LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, quorum->m_quorum_base_block_index->nHeight, quorum->m_quorum_base_block_index->GetBlockHash().ToString()); } diff --git a/src/llmq/quorums.h b/src/llmq/quorums.h index a4b37ceb46..54c0489729 100644 --- a/src/llmq/quorums.h +++ b/src/llmq/quorums.h @@ -27,6 +27,7 @@ class CConnman; class CDeterministicMN; class CMasternodeSync; class CNode; +class CSporkManager; using CDeterministicMNCPtr = std::shared_ptr; @@ -220,6 +221,7 @@ private: CDKGSessionManager& dkgManager; CEvoDB& m_evoDb; CQuorumBlockProcessor& quorumBlockProcessor; + const CSporkManager& m_sporkman; const std::unique_ptr& m_mn_sync; mutable RecursiveMutex cs_map_quorums; @@ -234,7 +236,7 @@ private: public: CQuorumManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman, CDKGSessionManager& _dkgManager, - CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const std::unique_ptr& mn_sync); + CEvoDB& _evoDb, CQuorumBlockProcessor& _quorumBlockProcessor, const CSporkManager& sporkman, const std::unique_ptr& mn_sync); ~CQuorumManager() { Stop(); }; void Start(); diff --git a/src/llmq/signing_shares.cpp b/src/llmq/signing_shares.cpp index 258df3ee88..89b8b5a526 100644 --- a/src/llmq/signing_shares.cpp +++ b/src/llmq/signing_shares.cpp @@ -698,7 +698,7 @@ void CSigSharesManager::ProcessSigShare(const CSigShare& sigShare, const CConnma // prepare node set for direct-push in case this is our sig share std::set quorumNodes; - if (!IsAllMembersConnectedEnabled(llmqType) && sigShare.getQuorumMember() == quorum->GetMemberIndex(WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) { + if (!IsAllMembersConnectedEnabled(llmqType, m_sporkman) && sigShare.getQuorumMember() == quorum->GetMemberIndex(WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) { quorumNodes = connman.GetMasternodeQuorumNodes(sigShare.getLlmqType(), sigShare.getQuorumHash()); } @@ -712,7 +712,7 @@ void CSigSharesManager::ProcessSigShare(const CSigShare& sigShare, const CConnma if (!sigShares.Add(sigShare.GetKey(), sigShare)) { return; } - if (!IsAllMembersConnectedEnabled(llmqType)) { + if (!IsAllMembersConnectedEnabled(llmqType, m_sporkman)) { sigSharesQueuedToAnnounce.Add(sigShare.GetKey(), true); } @@ -857,7 +857,7 @@ void CSigSharesManager::CollectSigSharesToRequest(std::unordered_mapsecond)* invMap = nullptr; for (auto& [signHash, session] : nodeState.sessions) { - if (IsAllMembersConnectedEnabled(session.llmqType)) { + if (IsAllMembersConnectedEnabled(session.llmqType, m_sporkman)) { continue; } @@ -928,7 +928,7 @@ void CSigSharesManager::CollectSigSharesToSend(std::unordered_mapsecond)* sigSharesToSend2 = nullptr; for (auto& [signHash, session] : nodeState.sessions) { - if (IsAllMembersConnectedEnabled(session.llmqType)) { + if (IsAllMembersConnectedEnabled(session.llmqType, m_sporkman)) { continue; } @@ -982,7 +982,7 @@ void CSigSharesManager::CollectSigSharesToSendConcentrated(std::unordered_map().count(); for (auto& [_, signedSession] : signedSessions) { - if (!IsAllMembersConnectedEnabled(signedSession.quorum->params.type)) { + if (!IsAllMembersConnectedEnabled(signedSession.quorum->params.type, m_sporkman)) { continue; } @@ -1474,7 +1474,7 @@ void CSigSharesManager::SignPendingSigShares() auto sigShare = *opt_sigShare; ProcessSigShare(sigShare, connman, pQuorum); - if (IsAllMembersConnectedEnabled(pQuorum->params.type)) { + if (IsAllMembersConnectedEnabled(pQuorum->params.type, m_sporkman)) { LOCK(cs); auto& session = signedSessions[sigShare.GetSignHash()]; session.sigShare = sigShare; @@ -1528,7 +1528,7 @@ std::optional CSigSharesManager::CreateSigShare(const CQuorumCPtr& qu // causes all known sigShares to be re-announced void CSigSharesManager::ForceReAnnouncement(const CQuorumCPtr& quorum, Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) { - if (IsAllMembersConnectedEnabled(llmqType)) { + if (IsAllMembersConnectedEnabled(llmqType, m_sporkman)) { return; } diff --git a/src/llmq/signing_shares.h b/src/llmq/signing_shares.h index 2c97ffd25b..b438a22fef 100644 --- a/src/llmq/signing_shares.h +++ b/src/llmq/signing_shares.h @@ -400,8 +400,9 @@ private: FastRandomContext rnd GUARDED_BY(cs); CConnman& connman; - const CQuorumManager& qman; CSigningManager& sigman; + const CQuorumManager& qman; + const CSporkManager& m_sporkman; const std::unique_ptr& m_peerman; @@ -409,8 +410,8 @@ private: std::atomic recoveredSigsCounter{0}; public: - explicit CSigSharesManager(CConnman& _connman, CQuorumManager& _qman, CSigningManager& _sigman, const std::unique_ptr& peerman) : - connman(_connman), qman(_qman), sigman(_sigman), m_peerman(peerman) + explicit CSigSharesManager(CConnman& _connman, CSigningManager& _sigman, const CQuorumManager& _qman, const CSporkManager& sporkman, const std::unique_ptr& peerman) : + connman(_connman), sigman(_sigman), qman(_qman), m_sporkman(sporkman), m_peerman(peerman) { workInterrupt.reset(); }; diff --git a/src/llmq/utils.cpp b/src/llmq/utils.cpp index 7b99884701..ea79f55f1c 100644 --- a/src/llmq/utils.cpp +++ b/src/llmq/utils.cpp @@ -649,10 +649,10 @@ uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256 return proTxHash2; } -std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, - const uint256& forMember, bool onlyOutbound) +std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, const CSporkManager& sporkman, + gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound) { - if (IsAllMembersConnectedEnabled(llmqParams.type)) { + if (IsAllMembersConnectedEnabled(llmqParams.type, sporkman)) { auto mns = GetAllQuorumMembers(llmqParams.type, pQuorumBaseBlockIndex); std::set result; @@ -749,8 +749,8 @@ std::set CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, return result; } -bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, - CConnman& connman, const uint256& myProTxHash) +bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, CConnman& connman, const CSporkManager& sporkman, + gsl::not_null pQuorumBaseBlockIndex, const uint256& myProTxHash) { if (!fMasternodeMode && !IsWatchQuorumsEnabled()) { return false; @@ -773,7 +773,7 @@ bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_n std::set connections; std::set relayMembers; if (isMember) { - connections = GetQuorumConnections(llmqParams, pQuorumBaseBlockIndex, myProTxHash, true); + connections = GetQuorumConnections(llmqParams, sporkman, pQuorumBaseBlockIndex, myProTxHash, true); relayMembers = GetQuorumRelayMembers(llmqParams, pQuorumBaseBlockIndex, myProTxHash, true); } else { auto cindexes = CalcDeterministicWatchConnections(llmqParams.type, pQuorumBaseBlockIndex, members.size(), 1); @@ -804,10 +804,10 @@ bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_n return true; } -void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, - CConnman& connman, const uint256 &myProTxHash) +void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, CConnman& connman, const CSporkManager& sporkman, + gsl::not_null pQuorumBaseBlockIndex, const uint256 &myProTxHash) { - if (!IsQuorumPoseEnabled(llmqParams.type)) { + if (!IsQuorumPoseEnabled(llmqParams.type, sporkman)) { return; } diff --git a/src/llmq/utils.h b/src/llmq/utils.h index 61afeb5c41..4e478958bd 100644 --- a/src/llmq/utils.h +++ b/src/llmq/utils.h @@ -19,6 +19,7 @@ class CBlockIndex; class CDeterministicMN; class CDeterministicMNList; using CDeterministicMNCPtr = std::shared_ptr; +class CSporkManager; namespace llmq { @@ -30,12 +31,12 @@ namespace utils std::vector GetAllQuorumMembers(Consensus::LLMQType llmqType, gsl::not_null pQuorumBaseBlockIndex, bool reset_cache = false); uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256& proTxHash2); -std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); +std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, const CSporkManager& sporkman, gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); std::set GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); std::set CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, gsl::not_null pQuorumBaseBlockIndex, size_t memberCount, size_t connectionCount); -bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, CConnman& connman, const uint256& myProTxHash); -void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, gsl::not_null pQuorumBaseBlockIndex, CConnman& connman, const uint256& myProTxHash); +bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, CConnman& connman, const CSporkManager& sporkman, gsl::not_null pQuorumBaseBlockIndex, const uint256& myProTxHash); +void AddQuorumProbeConnections(const Consensus::LLMQParams& llmqParams, CConnman& connman, const CSporkManager& sporkman, gsl::not_null pQuorumBaseBlockIndex, const uint256& myProTxHash); template void InitQuorumsCache(CacheType& cache, bool limit_by_connections = true); diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 53f79dacad..df3e2339fb 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -238,7 +238,8 @@ class PeerManagerImpl final : public PeerManager public: PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, - CTxMemPool& pool, CGovernanceManager& govman, const std::unique_ptr& cj_ctx, + CTxMemPool& pool, CGovernanceManager& govman, CSporkManager& sporkman, + const std::unique_ptr& cj_ctx, const std::unique_ptr& llmq_ctx, bool ignore_incoming_txs); /** Overridden from CValidationInterface. */ @@ -335,6 +336,7 @@ private: const std::unique_ptr& m_cj_ctx; const std::unique_ptr& m_llmq_ctx; CGovernanceManager& m_govman; + CSporkManager& m_sporkman; /** The height of the best chain */ std::atomic m_best_height{-1}; @@ -1587,15 +1589,16 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex, const Conse std::unique_ptr PeerManager::make(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool, - CGovernanceManager& govman, const std::unique_ptr& cj_ctx, + CGovernanceManager& govman, CSporkManager& sporkman, + const std::unique_ptr& cj_ctx, const std::unique_ptr& llmq_ctx, bool ignore_incoming_txs) { - return std::make_unique(chainparams, connman, addrman, banman, scheduler, chainman, pool, govman, cj_ctx, llmq_ctx, ignore_incoming_txs); + return std::make_unique(chainparams, connman, addrman, banman, scheduler, chainman, pool, govman, sporkman, cj_ctx, llmq_ctx, ignore_incoming_txs); } PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, CTxMemPool& pool, - CGovernanceManager& govman, const std::unique_ptr& cj_ctx, + CGovernanceManager& govman, CSporkManager& sporkman, const std::unique_ptr& cj_ctx, const std::unique_ptr& llmq_ctx, bool ignore_incoming_txs) : m_chainparams(chainparams), m_connman(connman), @@ -1606,6 +1609,7 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& conn m_cj_ctx(cj_ctx), m_llmq_ctx(llmq_ctx), m_govman(govman), + m_sporkman(sporkman), m_ignore_incoming_txs(ignore_incoming_txs) { assert(std::addressof(g_chainman) == std::addressof(m_chainman)); @@ -1884,7 +1888,7 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv) case MSG_SPORK: { - return sporkManager->GetSporkByHash(inv.hash).has_value(); + return m_sporkman.GetSporkByHash(inv.hash).has_value(); } case MSG_GOVERNANCE_OBJECT: @@ -2220,7 +2224,7 @@ void PeerManagerImpl::ProcessGetData(CNode& pfrom, Peer& peer, const std::atomic } if (!push && inv.type == MSG_SPORK) { - if (auto opt_spork = sporkManager->GetSporkByHash(inv.hash)) { + if (auto opt_spork = m_sporkman.GetSporkByHash(inv.hash)) { m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::SPORK, *opt_spork)); push = true; } @@ -4429,14 +4433,14 @@ void PeerManagerImpl::ProcessMessage( } #endif // ENABLE_WALLET ProcessPeerMsgRet(m_cj_ctx->server->ProcessMessage(pfrom, msg_type, vRecv), pfrom); - ProcessPeerMsgRet(sporkManager->ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom); + ProcessPeerMsgRet(m_sporkman.ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom); ::masternodeSync->ProcessMessage(pfrom, msg_type, vRecv); ProcessPeerMsgRet(m_govman.ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom); ProcessPeerMsgRet(CMNAuth::ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom); ProcessPeerMsgRet(m_llmq_ctx->quorum_block_processor->ProcessMessage(pfrom, msg_type, vRecv), pfrom); ProcessPeerMsgRet(m_llmq_ctx->qdkgsman->ProcessMessage(pfrom, this, msg_type, vRecv), pfrom); ProcessPeerMsgRet(m_llmq_ctx->qman->ProcessMessage(pfrom, msg_type, vRecv), pfrom); - m_llmq_ctx->shareman->ProcessMessage(pfrom, *sporkManager, msg_type, vRecv); + m_llmq_ctx->shareman->ProcessMessage(pfrom, m_sporkman, msg_type, vRecv); ProcessPeerMsgRet(m_llmq_ctx->sigman->ProcessMessage(pfrom, this, msg_type, vRecv), pfrom); ProcessPeerMsgRet(m_llmq_ctx->clhandler->ProcessMessage(pfrom, msg_type, vRecv), pfrom); ProcessPeerMsgRet(m_llmq_ctx->isman->ProcessMessage(pfrom, this, msg_type, vRecv), pfrom); diff --git a/src/net_processing.h b/src/net_processing.h index a0f6c80340..980c8e07fe 100644 --- a/src/net_processing.h +++ b/src/net_processing.h @@ -16,9 +16,10 @@ class CAddrMan; class CTxMemPool; class ChainstateManager; class CCoinJoinServer; +class CGovernanceManager; +class CSporkManager; struct CJContext; struct LLMQContext; -class CGovernanceManager; extern RecursiveMutex cs_main; extern RecursiveMutex g_cs_orphans; @@ -42,7 +43,8 @@ class PeerManager : public CValidationInterface, public NetEventsInterface public: static std::unique_ptr make(const CChainParams& chainparams, CConnman& connman, CAddrMan& addrman, BanMan* banman, CScheduler &scheduler, ChainstateManager& chainman, - CTxMemPool& pool, CGovernanceManager& govman, const std::unique_ptr& cj_ctx, + CTxMemPool& pool, CGovernanceManager& govman, CSporkManager& sporkman, + const std::unique_ptr& cj_ctx, const std::unique_ptr& llmq_ctx, bool ignore_incoming_txs); virtual ~PeerManager() { } diff --git a/src/node/context.cpp b/src/node/context.cpp index c6dcfde115..1269b8294a 100644 --- a/src/node/context.cpp +++ b/src/node/context.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include NodeContext::NodeContext() {} diff --git a/src/node/context.h b/src/node/context.h index 46741fa31a..e9f430ea33 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -75,6 +75,7 @@ struct NodeContext { std::unique_ptr chain_helper; std::unique_ptr cj_ctx; std::unique_ptr mnhf_manager; + std::unique_ptr sporkman; std::unique_ptr llmq_ctx; CCreditPoolManager* cpoolman{nullptr}; CDeterministicMNManager* dmnman{nullptr}; @@ -83,7 +84,6 @@ struct NodeContext { CMasternodeMetaMan* mn_metaman{nullptr}; CMasternodeSync* mn_sync{nullptr}; CNetFulfilledRequestManager* netfulfilledman{nullptr}; - CSporkManager* sporkman{nullptr}; //! Declare default constructor and destructor that are not inline, so code //! instantiating the NodeContext struct doesn't need to #include class diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index 233329e1ae..ae8f138660 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -275,7 +275,7 @@ static void quorum_dkgstatus_help(const JSONRPCRequest& request) }.Check(request); } -static UniValue quorum_dkgstatus(const JSONRPCRequest& request, const ChainstateManager& chainman, const LLMQContext& llmq_ctx) +static UniValue quorum_dkgstatus(const JSONRPCRequest& request, const ChainstateManager& chainman, const CSporkManager& sporkman, const LLMQContext& llmq_ctx) { quorum_dkgstatus_help(request); @@ -319,11 +319,8 @@ static UniValue quorum_dkgstatus(const JSONRPCRequest& request, const Chainstate obj.pushKV("quorumHash", pQuorumBaseBlockIndex->GetBlockHash().ToString()); obj.pushKV("pindexTip", pindexTip->nHeight); - auto allConnections = llmq::utils::GetQuorumConnections(llmq_params, pQuorumBaseBlockIndex, - proTxHash, false); - auto outboundConnections = llmq::utils::GetQuorumConnections(llmq_params, - pQuorumBaseBlockIndex, proTxHash, - true); + auto allConnections = llmq::utils::GetQuorumConnections(llmq_params, sporkman, pQuorumBaseBlockIndex, proTxHash, false); + auto outboundConnections = llmq::utils::GetQuorumConnections(llmq_params, sporkman, pQuorumBaseBlockIndex, proTxHash, true); std::map foundConnections; const NodeContext& node = EnsureAnyNodeContext(request.context); node.connman->ForEachNode([&](const CNode* pnode) { @@ -882,7 +879,7 @@ static UniValue _quorum(const JSONRPCRequest& request) } else if (command == "quorumdkginfo") { return quorum_dkginfo(new_request, llmq_ctx, chainman); } else if (command == "quorumdkgstatus") { - return quorum_dkgstatus(new_request, chainman, llmq_ctx); + return quorum_dkgstatus(new_request, chainman, *node.sporkman, llmq_ctx); } else if (command == "quorummemberof") { return quorum_memberof(new_request, chainman, node, llmq_ctx); } else if (command == "quorumsign" || command == "quorumverify" || command == "quorumhasrecsig" || command == "quorumgetrecsig" || command == "quorumisconflicting") { diff --git a/src/spork.cpp b/src/spork.cpp index 86bf217d43..2a27e6821f 100644 --- a/src/spork.cpp +++ b/src/spork.cpp @@ -23,8 +23,6 @@ #include -std::unique_ptr sporkManager; - const std::string SporkStore::SERIALIZATION_VERSION_STRING = "CSporkManager-Version-2"; std::optional CSporkManager::SporkValueIfActive(SporkId nSporkID) const diff --git a/src/spork.h b/src/spork.h index 6155f406c3..effc9d7530 100644 --- a/src/spork.h +++ b/src/spork.h @@ -78,7 +78,6 @@ struct CSporkDef MAKE_SPORK_DEF(SPORK_24_TEST_EHF, 4070908800ULL), // OFF }; #undef MAKE_SPORK_DEF -extern std::unique_ptr sporkManager; /** * Sporks are network parameters used primarily to prevent forking and turn diff --git a/src/test/denialofservice_tests.cpp b/src/test/denialofservice_tests.cpp index bf18e479d7..d27cde8957 100644 --- a/src/test/denialofservice_tests.cpp +++ b/src/test/denialofservice_tests.cpp @@ -65,7 +65,7 @@ BOOST_AUTO_TEST_CASE(outbound_slow_chain_eviction) const CChainParams& chainparams = Params(); auto connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr, *m_node.scheduler, - *m_node.chainman, *m_node.mempool, *m_node.govman, m_node.cj_ctx, + *m_node.chainman, *m_node.mempool, *m_node.govman, *m_node.sporkman, m_node.cj_ctx, m_node.llmq_ctx, false); // Mock an outbound peer @@ -136,7 +136,7 @@ BOOST_AUTO_TEST_CASE(stale_tip_peer_management) const CChainParams& chainparams = Params(); auto connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, nullptr, *m_node.scheduler, - *m_node.chainman, *m_node.mempool, *m_node.govman, m_node.cj_ctx, + *m_node.chainman, *m_node.mempool, *m_node.govman, *m_node.sporkman, m_node.cj_ctx, m_node.llmq_ctx, false); constexpr int max_outbound_full_relay = MAX_OUTBOUND_FULL_RELAY_CONNECTIONS; @@ -210,7 +210,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning) auto banman = std::make_unique(GetDataDir() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(), *m_node.scheduler, - *m_node.chainman, *m_node.mempool, *m_node.govman, m_node.cj_ctx, + *m_node.chainman, *m_node.mempool, *m_node.govman, *m_node.sporkman, m_node.cj_ctx, m_node.llmq_ctx, false); banman->ClearBanned(); @@ -256,7 +256,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore) auto banman = std::make_unique(GetDataDir() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(), *m_node.scheduler, - *m_node.chainman, *m_node.mempool, *m_node.govman, m_node.cj_ctx, + *m_node.chainman, *m_node.mempool, *m_node.govman, *m_node.sporkman, m_node.cj_ctx, m_node.llmq_ctx, false); banman->ClearBanned(); @@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime) auto banman = std::make_unique(GetDataDir() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME); auto connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); auto peerLogic = PeerManager::make(chainparams, *connman, *m_node.addrman, banman.get(), *m_node.scheduler, - *m_node.chainman, *m_node.mempool, *m_node.govman, m_node.cj_ctx, + *m_node.chainman, *m_node.mempool, *m_node.govman, *m_node.sporkman, m_node.cj_ctx, m_node.llmq_ctx, false); banman->ClearBanned(); diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp index 3376687cd6..76745760de 100644 --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -222,8 +222,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve m_node.connman = std::make_unique(0x1337, 0x1337, *m_node.addrman); // Deterministic randomness for tests. - ::sporkManager = std::make_unique(); - m_node.sporkman = ::sporkManager.get(); + m_node.sporkman = std::make_unique(); ::governance = std::make_unique(); m_node.govman = ::governance.get(); ::masternodeSync = std::make_unique(*m_node.connman, *m_node.govman); @@ -257,8 +256,7 @@ ChainTestingSetup::~ChainTestingSetup() ::masternodeSync.reset(); m_node.govman = nullptr; ::governance.reset(); - m_node.sporkman = nullptr; - ::sporkManager.reset(); + m_node.sporkman.reset(); m_node.connman.reset(); m_node.addrman.reset(); m_node.args = nullptr; @@ -291,7 +289,7 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector(GetDataDir() / "banlist", nullptr, DEFAULT_MISBEHAVING_BANTIME); m_node.peerman = PeerManager::make(chainparams, *m_node.connman, *m_node.addrman, m_node.banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool, *m_node.govman, - m_node.cj_ctx, m_node.llmq_ctx, false); + *m_node.sporkman, m_node.cj_ctx, m_node.llmq_ctx, false); { CConnman::Options options; options.m_msgproc = m_node.peerman.get();