From f171c24a291f26d4aee63d4579b1117b6e6ad247 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Thu, 21 Mar 2024 11:57:38 +0000 Subject: [PATCH] refactor: add CActiveMasternodeManager NodeContext alias, use in RPC --- src/init.cpp | 6 ++++-- src/node/context.h | 2 ++ src/rpc/governance.cpp | 16 ++++++++++------ src/rpc/masternode.cpp | 13 +++++++------ src/rpc/quorums.cpp | 13 ++++++++----- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index b1ea2097ce..42de4103b3 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -369,7 +369,8 @@ void PrepareShutdown(NodeContext& node) pdsNotificationInterface = nullptr; } if (fMasternodeMode) { - UnregisterValidationInterface(::activeMasternodeManager.get()); + UnregisterValidationInterface(node.mn_activeman); + node.mn_activeman = nullptr; ::activeMasternodeManager.reset(); } @@ -1854,7 +1855,8 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc { // Create and register activeMasternodeManager, will init later in ThreadImport ::activeMasternodeManager = std::make_unique(keyOperator, *node.connman, ::deterministicMNManager); - RegisterValidationInterface(::activeMasternodeManager.get()); + node.mn_activeman = ::activeMasternodeManager.get(); + RegisterValidationInterface(node.mn_activeman); } } diff --git a/src/node/context.h b/src/node/context.h index 0f1c6e5d4a..bd03c9c8f6 100644 --- a/src/node/context.h +++ b/src/node/context.h @@ -12,6 +12,7 @@ class ArgsManager; class BanMan; +class CActiveMasternodeManager; class CAddrMan; class CBlockPolicyEstimator; class CConnman; @@ -79,6 +80,7 @@ struct NodeContext { std::unique_ptr netfulfilledman; std::unique_ptr sporkman; std::unique_ptr llmq_ctx; + CActiveMasternodeManager* mn_activeman{nullptr}; CDeterministicMNManager* dmnman{nullptr}; CMasternodeMetaMan* mn_metaman{nullptr}; CMasternodeSync* mn_sync{nullptr}; diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index 09d43fbee3..fede610ed2 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -316,17 +316,21 @@ static UniValue gobject_submit(const JSONRPCRequest& request) auto mnList = node.dmnman->GetListAtChainTip(); - bool fMnFound{false}; if (fMasternodeMode) { - LOCK(::activeMasternodeManager->cs); - fMnFound = mnList.HasValidMNByCollateral(::activeMasternodeManager->GetOutPoint()); + CHECK_NONFATAL(node.mn_activeman); + + LOCK(node.mn_activeman->cs); + const bool fMnFound = mnList.HasValidMNByCollateral(node.mn_activeman->GetOutPoint()); LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = %s, outpoint = %s, params.size() = %lld, fMnFound = %d\n", - ::activeMasternodeManager->GetPubKey().ToString(::activeMasternodeManager->IsLegacy()), - ::activeMasternodeManager->GetOutPoint().ToStringShort(), request.params.size(), fMnFound); + node.mn_activeman->GetPubKey().ToString(node.mn_activeman->IsLegacy()), + node.mn_activeman->GetOutPoint().ToStringShort(), + request.params.size(), + fMnFound); } else { LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = N/A, outpoint = N/A, params.size() = %lld, fMnFound = %d\n", - request.params.size(), fMnFound); + request.params.size(), + false); } // ASSEMBLE NEW GOVERNANCE OBJECT FROM USER PARAMETERS diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 0355a75bb7..9ca837c5a1 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -262,16 +262,17 @@ static UniValue masternode_status(const JSONRPCRequest& request) } const NodeContext& node = EnsureAnyNodeContext(request.context); + CHECK_NONFATAL(node.mn_activeman); UniValue mnObj(UniValue::VOBJ); CDeterministicMNCPtr dmn; { - LOCK(::activeMasternodeManager->cs); + LOCK(node.mn_activeman->cs); // keep compatibility with legacy status for now (might get deprecated/removed later) - mnObj.pushKV("outpoint", ::activeMasternodeManager->GetOutPoint().ToStringShort()); - mnObj.pushKV("service", ::activeMasternodeManager->GetService().ToString()); - dmn = node.dmnman->GetListAtChainTip().GetMN(::activeMasternodeManager->GetProTxHash()); + mnObj.pushKV("outpoint", node.mn_activeman->GetOutPoint().ToStringShort()); + mnObj.pushKV("service", node.mn_activeman->GetService().ToString()); + dmn = node.dmnman->GetListAtChainTip().GetMN(node.mn_activeman->GetProTxHash()); } if (dmn) { mnObj.pushKV("proTxHash", dmn->proTxHash.ToString()); @@ -280,8 +281,8 @@ static UniValue masternode_status(const JSONRPCRequest& request) mnObj.pushKV("collateralIndex", (int)dmn->collateralOutpoint.n); mnObj.pushKV("dmnState", dmn->pdmnState->ToJson(dmn->nType)); } - mnObj.pushKV("state", activeMasternodeManager->GetStateString()); - mnObj.pushKV("status", activeMasternodeManager->GetStatus()); + mnObj.pushKV("state", node.mn_activeman->GetStateString()); + mnObj.pushKV("status", node.mn_activeman->GetStatus()); return mnObj; } diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index 12c0e3e33e..8a3f17391b 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -275,7 +275,8 @@ static void quorum_dkgstatus_help(const JSONRPCRequest& request) }.Check(request); } -static UniValue quorum_dkgstatus(const JSONRPCRequest& request, CDeterministicMNManager& dmnman, const ChainstateManager& chainman, const CSporkManager& sporkman, const LLMQContext& llmq_ctx) +static UniValue quorum_dkgstatus(const JSONRPCRequest& request, CDeterministicMNManager& dmnman, const CActiveMasternodeManager* mn_activeman, + const ChainstateManager& chainman, const CSporkManager& sporkman, const LLMQContext& llmq_ctx) { quorum_dkgstatus_help(request); @@ -295,9 +296,11 @@ static UniValue quorum_dkgstatus(const JSONRPCRequest& request, CDeterministicMN CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()); int tipHeight = pindexTip->nHeight; - const uint256 proTxHash = fMasternodeMode ? - WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->GetProTxHash()) : - uint256(); + const uint256 proTxHash = [&mn_activeman]() { + if (!fMasternodeMode) return uint256(); + CHECK_NONFATAL(mn_activeman); + return WITH_LOCK(mn_activeman->cs, return mn_activeman->GetProTxHash()); + }(); UniValue minableCommitments(UniValue::VARR); UniValue quorumArrConnections(UniValue::VARR); @@ -881,7 +884,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, *node.dmnman, chainman, *node.sporkman, llmq_ctx); + return quorum_dkgstatus(new_request, *node.dmnman, node.mn_activeman, 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") {