From 9a3c5a3c4898345034e99976775223ea299ac3d2 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 12 Mar 2024 01:24:39 +0000 Subject: [PATCH 01/14] trivial: access activeMasternodeInfo when lock is in scope --- src/init.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index ea49bb0aa6..5e6d9411d2 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1606,8 +1606,11 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc StartScriptCheckWorkerThreads(script_threads); } - assert(activeMasternodeInfo.blsKeyOperator == nullptr); - assert(activeMasternodeInfo.blsPubKeyOperator == nullptr); + { + LOCK(activeMasternodeInfoCs); + assert(activeMasternodeInfo.blsKeyOperator == nullptr); + assert(activeMasternodeInfo.blsPubKeyOperator == nullptr); + } fMasternodeMode = false; std::string strMasterNodeBLSPrivKey = args.GetArg("-masternodeblsprivkey", ""); if (!strMasterNodeBLSPrivKey.empty()) { @@ -1620,11 +1623,11 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc LOCK(activeMasternodeInfoCs); activeMasternodeInfo.blsKeyOperator = std::make_unique(keyOperator); activeMasternodeInfo.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); + // We don't know the actual scheme at this point, print both + LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", + activeMasternodeInfo.blsPubKeyOperator->ToString(true), + activeMasternodeInfo.blsPubKeyOperator->ToString(false)); } - // We don't know the actual scheme at this point, print both - LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", - activeMasternodeInfo.blsPubKeyOperator->ToString(true), - activeMasternodeInfo.blsPubKeyOperator->ToString(false)); } else { LOCK(activeMasternodeInfoCs); activeMasternodeInfo.blsKeyOperator = std::make_unique(); From b8c1f010e70c739c82895d2ca486a55603814e65 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 24 Mar 2024 07:20:41 +0000 Subject: [PATCH 02/14] refactor: avoid accessing active masternode info if not in masternode mode A later commit will be moving activeMasternodeInfo into activeMasternodeManager and that is only conditionally initialized if the node is in masternode mode, which will render access attempts outside of masternode mode invalid. We need to adjust behaviour to account for that. --- src/evo/mnauth.cpp | 10 +++--- src/governance/governance.cpp | 3 +- src/init.cpp | 61 +++++++++++++++-------------------- src/llmq/context.cpp | 8 +++-- src/llmq/quorums.cpp | 10 ++++-- src/llmq/signing.cpp | 5 ++- src/llmq/signing_shares.cpp | 5 ++- src/rpc/coinjoin.cpp | 3 +- src/rpc/governance.cpp | 16 ++++++--- src/rpc/masternode.cpp | 5 +-- src/rpc/quorums.cpp | 4 ++- 11 files changed, 72 insertions(+), 58 deletions(-) diff --git a/src/evo/mnauth.cpp b/src/evo/mnauth.cpp index 4ec90de570..b3fd16a87b 100644 --- a/src/evo/mnauth.cpp +++ b/src/evo/mnauth.cpp @@ -21,10 +21,10 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip) { + if (!fMasternodeMode) return; + LOCK(activeMasternodeInfoCs); - if (!fMasternodeMode || activeMasternodeInfo.proTxHash.IsNull()) { - return; - } + if (activeMasternodeInfo.proTxHash.IsNull()) return; uint256 signHash; const auto receivedMNAuthChallenge = peer.GetReceivedMNAuthChallenge(); @@ -127,7 +127,9 @@ PeerMsgRet CMNAuth::ProcessMessage(CNode& peer, CConnman& connman, const CDeterm } } - const uint256 myProTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash); + const uint256 myProTxHash = fMasternodeMode ? + WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + uint256(); connman.ForEachNode([&](CNode* pnode2) { if (peer.fDisconnect) { diff --git a/src/governance/governance.cpp b/src/governance/governance.cpp index 7aad8debff..04cae5a900 100644 --- a/src/governance/governance.cpp +++ b/src/governance/governance.cpp @@ -719,7 +719,8 @@ std::optional CGovernanceManager::CreateGovernanceTrigg void CGovernanceManager::VoteGovernanceTriggers(const std::optional& trigger_opt, CConnman& connman) { // only active masternodes can vote on triggers - if (!fMasternodeMode || WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return; + if (!fMasternodeMode) return; + if (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return; LOCK2(cs_main, cs); diff --git a/src/init.cpp b/src/init.cpp index 5e6d9411d2..81781a84b5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -370,14 +370,13 @@ void PrepareShutdown(NodeContext& node) } if (fMasternodeMode) { UnregisterValidationInterface(activeMasternodeManager.get()); - activeMasternodeManager.reset(); - } - { LOCK(activeMasternodeInfoCs); // make sure to clean up BLS keys before global destructors are called (they have allocated from the secure memory pool) activeMasternodeInfo.blsKeyOperator.reset(); activeMasternodeInfo.blsPubKeyOperator.reset(); + + activeMasternodeManager.reset(); } node.chain_clients.clear(); @@ -1606,34 +1605,6 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc StartScriptCheckWorkerThreads(script_threads); } - { - LOCK(activeMasternodeInfoCs); - assert(activeMasternodeInfo.blsKeyOperator == nullptr); - assert(activeMasternodeInfo.blsPubKeyOperator == nullptr); - } - fMasternodeMode = false; - std::string strMasterNodeBLSPrivKey = args.GetArg("-masternodeblsprivkey", ""); - if (!strMasterNodeBLSPrivKey.empty()) { - CBLSSecretKey keyOperator(ParseHex(strMasterNodeBLSPrivKey)); - if (!keyOperator.IsValid()) { - return InitError(_("Invalid masternodeblsprivkey. Please see documentation.")); - } - fMasternodeMode = true; - { - LOCK(activeMasternodeInfoCs); - activeMasternodeInfo.blsKeyOperator = std::make_unique(keyOperator); - activeMasternodeInfo.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); - // We don't know the actual scheme at this point, print both - LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", - activeMasternodeInfo.blsPubKeyOperator->ToString(true), - activeMasternodeInfo.blsPubKeyOperator->ToString(false)); - } - } else { - LOCK(activeMasternodeInfoCs); - activeMasternodeInfo.blsKeyOperator = std::make_unique(); - activeMasternodeInfo.blsPubKeyOperator = std::make_unique(); - } - assert(!node.scheduler); node.scheduler = std::make_unique(); @@ -1878,10 +1849,30 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc ); RegisterValidationInterface(pdsNotificationInterface); - if (fMasternodeMode) { - // Create and register activeMasternodeManager, will init later in ThreadImport - activeMasternodeManager = std::make_unique(*node.connman, ::deterministicMNManager); - RegisterValidationInterface(activeMasternodeManager.get()); + fMasternodeMode = false; + std::string strMasterNodeBLSPrivKey = args.GetArg("-masternodeblsprivkey", ""); + if (!strMasterNodeBLSPrivKey.empty()) { + CBLSSecretKey keyOperator(ParseHex(strMasterNodeBLSPrivKey)); + if (!keyOperator.IsValid()) { + return InitError(_("Invalid masternodeblsprivkey. Please see documentation.")); + } + fMasternodeMode = true; + { + // Create and register activeMasternodeManager, will init later in ThreadImport + activeMasternodeManager = std::make_unique(*node.connman, ::deterministicMNManager); + + LOCK(activeMasternodeInfoCs); + assert(activeMasternodeInfo.blsKeyOperator == nullptr); + assert(activeMasternodeInfo.blsPubKeyOperator == nullptr); + activeMasternodeInfo.blsKeyOperator = std::make_unique(keyOperator); + activeMasternodeInfo.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); + // We don't know the actual scheme at this point, print both + LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", + activeMasternodeInfo.blsPubKeyOperator->ToString(true), + activeMasternodeInfo.blsPubKeyOperator->ToString(false)); + + RegisterValidationInterface(activeMasternodeManager.get()); + } } // ********************************************************* Step 7a: Load sporks diff --git a/src/llmq/context.cpp b/src/llmq/context.cpp index 933cd58776..c37415343b 100644 --- a/src/llmq/context.cpp +++ b/src/llmq/context.cpp @@ -77,7 +77,9 @@ void LLMQContext::Start() { assert(isman == llmq::quorumInstantSendManager.get()); bls_worker->Start(); - qdkgsman->StartThreads(); + if (fMasternodeMode) { + qdkgsman->StartThreads(); + } qman->Start(); shareman->RegisterAsRecoveredSigsListener(); shareman->StartWorkerThread(); @@ -100,6 +102,8 @@ void LLMQContext::Stop() { shareman->UnregisterAsRecoveredSigsListener(); sigman->StopWorkerThread(); qman->Stop(); - qdkgsman->StopThreads(); + if (fMasternodeMode) { + qdkgsman->StopThreads(); + } bls_worker->Stop(); } diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 103067aadc..9e5cfe1abd 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -250,7 +250,10 @@ void CQuorumManager::TriggerQuorumDataRecoveryThreads(const CBlockIndex* pIndex) const auto vecQuorums = ScanQuorums(params.type, pIndex, params.keepOldConnections); // First check if we are member of any quorum of this type - auto proTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash); + const uint256 proTxHash = fMasternodeMode ? + WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + uint256(); + bool fWeAreQuorumTypeMember = ranges::any_of(vecQuorums, [&proTxHash](const auto& pQuorum) { return pQuorum->IsValidMember(proTxHash); }); @@ -341,7 +344,10 @@ void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqPar LogPrint(BCLog::LLMQ, "CQuorumManager::%s -- llmqType[%d] h[%d] keeping mn quorum connections for quorum: [%d:%s]\n", __func__, ToUnderlying(llmqParams.type), pindexNew->nHeight, curDkgHeight, curDkgBlock.ToString()); } - const auto myProTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash); + const uint256 myProTxHash = fMasternodeMode ? + WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + uint256(); + bool isISType = llmqParams.type == Params().GetConsensus().llmqTypeDIP0024InstantSend; bool watchOtherISQuorums = isISType && !myProTxHash.IsNull() && diff --git a/src/llmq/signing.cpp b/src/llmq/signing.cpp index f6f721fee1..2f032f69b9 100644 --- a/src/llmq/signing.cpp +++ b/src/llmq/signing.cpp @@ -895,9 +895,8 @@ void CSigningManager::UnregisterRecoveredSigsListener(CRecoveredSigsListener* l) bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, CSigSharesManager& shareman, const uint256& id, const uint256& msgHash, const uint256& quorumHash, bool allowReSign) { - if (!fMasternodeMode || WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) { - return false; - } + if (!fMasternodeMode) return false; + if (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return false; const CQuorumCPtr quorum = [&]() { if (quorumHash.IsNull()) { diff --git a/src/llmq/signing_shares.cpp b/src/llmq/signing_shares.cpp index 4032d38758..4430c7dba9 100644 --- a/src/llmq/signing_shares.cpp +++ b/src/llmq/signing_shares.cpp @@ -218,9 +218,8 @@ void CSigSharesManager::InterruptWorkerThread() void CSigSharesManager::ProcessMessage(const CNode& pfrom, const CSporkManager& sporkman, const std::string& msg_type, CDataStream& vRecv) { // non-masternodes are not interested in sigshares - if (!fMasternodeMode || WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) { - return; - } + if (!fMasternodeMode) return; + if (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return; if (sporkman.IsSporkActive(SPORK_21_QUORUM_ALL_CONNECTED) && msg_type == NetMsgType::QSIGSHARE) { std::vector receivedSigShares; diff --git a/src/rpc/coinjoin.cpp b/src/rpc/coinjoin.cpp index b4b615ff97..889395ad70 100644 --- a/src/rpc/coinjoin.cpp +++ b/src/rpc/coinjoin.cpp @@ -38,8 +38,9 @@ static RPCHelpMan coinjoin() std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); if (!wallet) return NullUniValue; - if (fMasternodeMode) + if (fMasternodeMode) { throw JSONRPCError(RPC_INTERNAL_ERROR, "Client-side mixing is not supported on masternodes"); + } if (!CCoinJoinClientOptions::IsEnabled()) { if (!gArgs.GetBoolArg("-enablecoinjoin", true)) { diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index 6f547b2a1a..673e42b76c 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -315,11 +315,19 @@ static UniValue gobject_submit(const JSONRPCRequest& request) } auto mnList = node.dmnman->GetListAtChainTip(); - bool fMnFound = WITH_LOCK(activeMasternodeInfoCs, return mnList.HasValidMNByCollateral(activeMasternodeInfo.outpoint)); - LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = %s, outpoint = %s, params.size() = %lld, fMnFound = %d\n", - (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsPubKeyOperator ? activeMasternodeInfo.blsPubKeyOperator->ToString(activeMasternodeInfo.legacy) : "N/A")), - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint.ToStringShort()), request.params.size(), fMnFound); + bool fMnFound{false}; + if (fMasternodeMode) { + LOCK(activeMasternodeInfoCs); + fMnFound = mnList.HasValidMNByCollateral(activeMasternodeInfo.outpoint); + + LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = %s, outpoint = %s, params.size() = %lld, fMnFound = %d\n", + (activeMasternodeInfo.blsPubKeyOperator ? activeMasternodeInfo.blsPubKeyOperator->ToString(activeMasternodeInfo.legacy) : "N/A"), + activeMasternodeInfo.outpoint.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); + } // ASSEMBLE NEW GOVERNANCE OBJECT FROM USER PARAMETERS diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 3f47366d00..0ab332f210 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -257,8 +257,9 @@ static UniValue masternode_status(const JSONRPCRequest& request) { masternode_status_help(request); - if (!fMasternodeMode) - throw JSONRPCError(RPC_INTERNAL_ERROR, "This is not a masternode"); + if (!fMasternodeMode) { + throw JSONRPCError(RPC_INTERNAL_ERROR, "This node does not run an active masternode."); + } const NodeContext& node = EnsureAnyNodeContext(request.context); diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index 8b0b039c52..d2c63fc514 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -295,7 +295,9 @@ static UniValue quorum_dkgstatus(const JSONRPCRequest& request, CDeterministicMN CBlockIndex* pindexTip = WITH_LOCK(cs_main, return chainman.ActiveChain().Tip()); int tipHeight = pindexTip->nHeight; - auto proTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash); + const uint256 proTxHash = fMasternodeMode ? + WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + uint256(); UniValue minableCommitments(UniValue::VARR); UniValue quorumArrConnections(UniValue::VARR); From e5295dec1f8bc462c9cfa190ec9a5397cf5ed250 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sat, 16 Mar 2024 19:19:24 +0000 Subject: [PATCH 03/14] refactor: move activeMasternodeInfo{Cs} into CActiveMasternodeManager --- src/coinjoin/coinjoin.cpp | 4 +-- src/coinjoin/server.cpp | 16 ++++++------ src/evo/mnauth.cpp | 12 ++++----- src/governance/governance.cpp | 16 ++++++------ src/init.cpp | 28 ++++++++++---------- src/llmq/dkgsession.cpp | 10 +++---- src/llmq/dkgsessionhandler.cpp | 2 +- src/llmq/quorums.cpp | 16 ++++++------ src/llmq/signing.cpp | 4 +-- src/llmq/signing_shares.cpp | 10 +++---- src/masternode/node.cpp | 48 ++++++++++++++++------------------ src/masternode/node.h | 14 +++++----- src/rpc/governance.cpp | 8 +++--- src/rpc/masternode.cpp | 8 +++--- src/rpc/quorums.cpp | 2 +- 15 files changed, 97 insertions(+), 101 deletions(-) diff --git a/src/coinjoin/coinjoin.cpp b/src/coinjoin/coinjoin.cpp index 3d92462986..94d43ea478 100644 --- a/src/coinjoin/coinjoin.cpp +++ b/src/coinjoin/coinjoin.cpp @@ -52,7 +52,7 @@ bool CCoinJoinQueue::Sign() if (!fMasternodeMode) return false; uint256 hash = GetSignatureHash(); - CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash, false)); + CBLSSignature sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(hash, false)); if (!sig.IsValid()) { return false; } @@ -104,7 +104,7 @@ bool CCoinJoinBroadcastTx::Sign() if (!fMasternodeMode) return false; uint256 hash = GetSignatureHash(); - CBLSSignature sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(hash, false)); + CBLSSignature sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(hash, false)); if (!sig.IsValid()) { return false; } diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 1f40b1be88..032de37264 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -56,7 +56,7 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) LogPrint(BCLog::COINJOIN, "DSACCEPT -- nDenom %d (%s) txCollateral %s", dsa.nDenom, CoinJoin::DenominationToString(dsa.nDenom), dsa.txCollateral.ToString()); /* Continued */ auto mnList = m_dmnman.GetListAtChainTip(); - auto dmn = WITH_LOCK(activeMasternodeInfoCs, return mnList.GetValidMNByCollateral(activeMasternodeInfo.outpoint)); + auto dmn = WITH_LOCK(::activeMasternodeManager->cs, return mnList.GetValidMNByCollateral(::activeMasternodeManager->m_info.outpoint)); if (!dmn) { PushStatus(peer, STATUS_REJECTED, ERR_MN_LIST); return; @@ -67,7 +67,7 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) TRY_LOCK(cs_vecqueue, lockRecv); if (!lockRecv) return; - auto mnOutpoint = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint); + auto mnOutpoint = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint); if (ranges::any_of(vecCoinJoinQueue, [&mnOutpoint](const auto& q){return q.masternodeOutpoint == mnOutpoint;})) { @@ -331,8 +331,8 @@ void CCoinJoinServer::CommitFinalTransaction() // create and sign masternode dstx transaction if (!m_dstxman.GetDSTX(hashTx)) { CCoinJoinBroadcastTx dstxNew(finalTransaction, - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint), - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash), + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint), + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash), GetAdjustedTime()); dstxNew.Sign(); m_dstxman.AddDSTX(dstxNew); @@ -499,8 +499,8 @@ void CCoinJoinServer::CheckForCompleteQueue() SetState(POOL_STATE_ACCEPTING_ENTRIES); CCoinJoinQueue dsq(nSessionDenom, - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint), - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash), + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint), + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash), GetAdjustedTime(), true); LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckForCompleteQueue -- queue is ready, signing and relaying (%s) " /* Continued */ "with %d participants\n", dsq.ToString(), vecSessionCollaterals.size()); @@ -713,8 +713,8 @@ bool CCoinJoinServer::CreateNewSession(const CCoinJoinAccept& dsa, PoolMessage& if (!fUnitTest) { //broadcast that I'm accepting entries, only if it's the first entry through CCoinJoinQueue dsq(nSessionDenom, - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.outpoint), - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash), + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.outpoint), + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash), GetAdjustedTime(), false); LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateNewSession -- signing and relaying new queue: %s\n", dsq.ToString()); dsq.Sign(); diff --git a/src/evo/mnauth.cpp b/src/evo/mnauth.cpp index b3fd16a87b..9df175b52d 100644 --- a/src/evo/mnauth.cpp +++ b/src/evo/mnauth.cpp @@ -23,8 +23,8 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip) { if (!fMasternodeMode) return; - LOCK(activeMasternodeInfoCs); - if (activeMasternodeInfo.proTxHash.IsNull()) return; + LOCK(::activeMasternodeManager->cs); + if (::activeMasternodeManager->m_info.proTxHash.IsNull()) return; uint256 signHash; const auto receivedMNAuthChallenge = peer.GetReceivedMNAuthChallenge(); @@ -42,7 +42,7 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip) nOurNodeVersion = gArgs.GetArg("-pushversion", PROTOCOL_VERSION); } const bool is_basic_scheme_active{DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)}; - const CBLSPublicKeyVersionWrapper pubKey(*activeMasternodeInfo.blsPubKeyOperator, !is_basic_scheme_active); + const CBLSPublicKeyVersionWrapper pubKey(*::activeMasternodeManager->m_info.blsPubKeyOperator, !is_basic_scheme_active); if (peer.nVersion < MNAUTH_NODE_VER_VERSION || nOurNodeVersion < MNAUTH_NODE_VER_VERSION) { signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn())); } else { @@ -50,8 +50,8 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip) } CMNAuth mnauth; - mnauth.proRegTxHash = activeMasternodeInfo.proTxHash; - mnauth.sig = activeMasternodeInfo.blsKeyOperator->Sign(signHash); + mnauth.proRegTxHash = ::activeMasternodeManager->m_info.proTxHash; + mnauth.sig = ::activeMasternodeManager->m_info.blsKeyOperator->Sign(signHash); LogPrint(BCLog::NET_NETCONN, "CMNAuth::%s -- Sending MNAUTH, peer=%d\n", __func__, peer.GetId()); @@ -128,7 +128,7 @@ PeerMsgRet CMNAuth::ProcessMessage(CNode& peer, CConnman& connman, const CDeterm } const uint256 myProTxHash = fMasternodeMode ? - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) : uint256(); connman.ForEachNode([&](CNode* pnode2) { diff --git a/src/governance/governance.cpp b/src/governance/governance.cpp index 04cae5a900..7fde83dcf3 100644 --- a/src/governance/governance.cpp +++ b/src/governance/governance.cpp @@ -692,14 +692,14 @@ std::optional CGovernanceManager::CreateGovernanceTrigg } { - LOCK(activeMasternodeInfoCs); - if (mn_payees.front()->proTxHash != activeMasternodeInfo.proTxHash) { + LOCK(::activeMasternodeManager->cs); + if (mn_payees.front()->proTxHash != ::activeMasternodeManager->m_info.proTxHash) { LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s we are not the payee, skipping\n", __func__); return std::nullopt; } - gov_sb.SetMasternodeOutpoint(activeMasternodeInfo.outpoint); - gov_sb.Sign( *activeMasternodeInfo.blsKeyOperator); - } // activeMasternodeInfoCs + gov_sb.SetMasternodeOutpoint(::activeMasternodeManager->m_info.outpoint); + gov_sb.Sign( *::activeMasternodeManager->m_info.blsKeyOperator); + } // ::activeMasternodeManager->cs if (std::string strError; !gov_sb.IsValidLocally(m_dmnman->GetListAtChainTip(), strError, true)) { LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Created trigger is invalid:%s\n", __func__, strError); @@ -720,7 +720,7 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optionalcs, return ::activeMasternodeManager->m_info.proTxHash.IsNull())) return; LOCK2(cs_main, cs); @@ -763,9 +763,9 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optionalcs, return ::activeMasternodeManager->m_info.outpoint), nHash, VOTE_SIGNAL_FUNDING, outcome); vote.SetTime(GetAdjustedTime()); - vote.Sign(WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator)); + vote.Sign(WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator)); CGovernanceException exception; if (!ProcessVoteAndRelay(vote, exception, connman)) { diff --git a/src/init.cpp b/src/init.cpp index 81781a84b5..78f6a56dfa 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -369,14 +369,14 @@ void PrepareShutdown(NodeContext& node) pdsNotificationInterface = nullptr; } if (fMasternodeMode) { - UnregisterValidationInterface(activeMasternodeManager.get()); + UnregisterValidationInterface(::activeMasternodeManager.get()); - LOCK(activeMasternodeInfoCs); + LOCK(::activeMasternodeManager->cs); // make sure to clean up BLS keys before global destructors are called (they have allocated from the secure memory pool) - activeMasternodeInfo.blsKeyOperator.reset(); - activeMasternodeInfo.blsPubKeyOperator.reset(); + ::activeMasternodeManager->m_info.blsKeyOperator.reset(); + ::activeMasternodeManager->m_info.blsPubKeyOperator.reset(); - activeMasternodeManager.reset(); + ::activeMasternodeManager.reset(); } node.chain_clients.clear(); @@ -1859,19 +1859,19 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc fMasternodeMode = true; { // Create and register activeMasternodeManager, will init later in ThreadImport - activeMasternodeManager = std::make_unique(*node.connman, ::deterministicMNManager); + ::activeMasternodeManager = std::make_unique(*node.connman, ::deterministicMNManager); - LOCK(activeMasternodeInfoCs); - assert(activeMasternodeInfo.blsKeyOperator == nullptr); - assert(activeMasternodeInfo.blsPubKeyOperator == nullptr); - activeMasternodeInfo.blsKeyOperator = std::make_unique(keyOperator); - activeMasternodeInfo.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); + LOCK(::activeMasternodeManager->cs); + assert(::activeMasternodeManager->m_info.blsKeyOperator == nullptr); + assert(::activeMasternodeManager->m_info.blsPubKeyOperator == nullptr); + ::activeMasternodeManager->m_info.blsKeyOperator = std::make_unique(keyOperator); + ::activeMasternodeManager->m_info.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); // We don't know the actual scheme at this point, print both LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", - activeMasternodeInfo.blsPubKeyOperator->ToString(true), - activeMasternodeInfo.blsPubKeyOperator->ToString(false)); + ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(true), + ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(false)); - RegisterValidationInterface(activeMasternodeManager.get()); + RegisterValidationInterface(::activeMasternodeManager.get()); } } diff --git a/src/llmq/dkgsession.cpp b/src/llmq/dkgsession.cpp index 13a5a1df2e..0f00e103cb 100644 --- a/src/llmq/dkgsession.cpp +++ b/src/llmq/dkgsession.cpp @@ -199,7 +199,7 @@ void CDKGSession::SendContributions(CDKGPendingMessages& pendingMessages) logger.Batch("encrypted contributions. time=%d", t1.count()); - qc.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(qc.GetSignHash())); + qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qc.GetSignHash())); logger.Flush(); @@ -316,7 +316,7 @@ void CDKGSession::ReceiveMessage(const CDKGContribution& qc, bool& retBan) bool complain = false; CBLSSecretKey skContribution; - if (!qc.contributions->Decrypt(*myIdx, WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator), skContribution, PROTOCOL_VERSION)) { + if (!qc.contributions->Decrypt(*myIdx, WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator), skContribution, PROTOCOL_VERSION)) { logger.Batch("contribution from %s could not be decrypted", member->dmn->proTxHash.ToString()); complain = true; } else if (member->idx != myIdx && ShouldSimulateError(DKGError::type::COMPLAIN_LIE)) { @@ -517,7 +517,7 @@ void CDKGSession::SendComplaint(CDKGPendingMessages& pendingMessages) logger.Batch("sending complaint. badCount=%d, complaintCount=%d", badCount, complaintCount); - qc.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(qc.GetSignHash())); + qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qc.GetSignHash())); logger.Flush(); @@ -711,7 +711,7 @@ void CDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, const return; } - qj.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(qj.GetSignHash())); + qj.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qj.GetSignHash())); logger.Flush(); @@ -1003,7 +1003,7 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages) (*commitmentHash.begin())++; } - qc.sig = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.blsKeyOperator->Sign(commitmentHash)); + qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(commitmentHash)); qc.quorumSig = skShare.Sign(commitmentHash); if (lieType == 3) { diff --git a/src/llmq/dkgsessionhandler.cpp b/src/llmq/dkgsessionhandler.cpp index 699239b916..f386c66e22 100644 --- a/src/llmq/dkgsessionhandler.cpp +++ b/src/llmq/dkgsessionhandler.cpp @@ -192,7 +192,7 @@ bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pQuorumBaseBlockIndex) } auto mns = utils::GetAllQuorumMembers(params.type, m_dmnman, pQuorumBaseBlockIndex); - if (!curSession->Init(pQuorumBaseBlockIndex, mns, WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash), quorumIndex)) { + if (!curSession->Init(pQuorumBaseBlockIndex, mns, WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash), quorumIndex)) { LogPrintf("CDKGSessionManager::%s -- height[%d] quorum initialization failed for %s qi[%d] mns[%d]\n", __func__, pQuorumBaseBlockIndex->nHeight, curSession->params.name, quorumIndex, mns.size()); return false; } diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 9e5cfe1abd..9456973b3c 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -103,7 +103,7 @@ bool CQuorum::SetVerificationVector(const std::vector& quorumVecI bool CQuorum::SetSecretKeyShare(const CBLSSecretKey& secretKeyShare) { - if (!secretKeyShare.IsValid() || (secretKeyShare.GetPublicKey() != GetPubKeyShare(WITH_LOCK(activeMasternodeInfoCs, return GetMemberIndex(activeMasternodeInfo.proTxHash))))) { + if (!secretKeyShare.IsValid() || (secretKeyShare.GetPublicKey() != GetPubKeyShare(WITH_LOCK(::activeMasternodeManager->cs, return GetMemberIndex(::activeMasternodeManager->m_info.proTxHash))))) { return false; } LOCK(cs); @@ -251,7 +251,7 @@ void CQuorumManager::TriggerQuorumDataRecoveryThreads(const CBlockIndex* pIndex) // First check if we are member of any quorum of this type const uint256 proTxHash = fMasternodeMode ? - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) : uint256(); bool fWeAreQuorumTypeMember = ranges::any_of(vecQuorums, [&proTxHash](const auto& pQuorum) { @@ -345,7 +345,7 @@ void CQuorumManager::CheckQuorumConnections(const Consensus::LLMQParams& llmqPar } const uint256 myProTxHash = fMasternodeMode ? - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) : uint256(); bool isISType = llmqParams.type == Params().GetConsensus().llmqTypeDIP0024InstantSend; @@ -655,10 +655,10 @@ size_t CQuorumManager::GetQuorumRecoveryStartOffset(const CQuorumCPtr pQuorum, c std::sort(vecProTxHashes.begin(), vecProTxHashes.end()); size_t nIndex{0}; { - LOCK(activeMasternodeInfoCs); + LOCK(::activeMasternodeManager->cs); for (const auto i : irange::range(vecProTxHashes.size())) { // cppcheck-suppress useStlAlgorithm - if (activeMasternodeInfo.proTxHash == vecProTxHashes[i]) { + if (::activeMasternodeManager->m_info.proTxHash == vecProTxHashes[i]) { nIndex = i; break; } @@ -834,7 +834,7 @@ PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_t std::vector vecSecretKeys; vecSecretKeys.resize(vecEncrypted.size()); - auto secret = WITH_LOCK(activeMasternodeInfoCs, return *activeMasternodeInfo.blsKeyOperator); + auto secret = WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator); for (const auto i : irange::range(vecEncrypted.size())) { if (!vecEncrypted[i].Decrypt(memberIdx, secret, vecSecretKeys[i], PROTOCOL_VERSION)) { return errorHandler("Failed to decrypt"); @@ -917,7 +917,7 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co vecMemberHashes.reserve(pQuorum->qc->validMembers.size()); for (auto& member : pQuorum->members) { - if (pQuorum->IsValidMember(member->proTxHash) && member->proTxHash != WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash)) { + if (pQuorum->IsValidMember(member->proTxHash) && member->proTxHash != WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash)) { vecMemberHashes.push_back(member->proTxHash); } } @@ -966,7 +966,7 @@ void CQuorumManager::StartQuorumDataRecoveryThread(const CQuorumCPtr pQuorum, co printLog("Connect"); } - auto proTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash); + auto proTxHash = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash); connman.ForEachNode([&](CNode* pNode) { auto verifiedProRegTxHash = pNode->GetVerifiedProRegTxHash(); if (pCurrentMemberHash == nullptr || verifiedProRegTxHash != *pCurrentMemberHash) { diff --git a/src/llmq/signing.cpp b/src/llmq/signing.cpp index 2f032f69b9..037e07d42c 100644 --- a/src/llmq/signing.cpp +++ b/src/llmq/signing.cpp @@ -896,7 +896,7 @@ void CSigningManager::UnregisterRecoveredSigsListener(CRecoveredSigsListener* l) bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, CSigSharesManager& shareman, const uint256& id, const uint256& msgHash, const uint256& quorumHash, bool allowReSign) { if (!fMasternodeMode) return false; - if (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return false; + if (WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash.IsNull())) return false; const CQuorumCPtr quorum = [&]() { if (quorumHash.IsNull()) { @@ -918,7 +918,7 @@ bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, CSigShares return false; } - if (!WITH_LOCK(activeMasternodeInfoCs, return quorum->IsValidMember(activeMasternodeInfo.proTxHash))) { + if (!WITH_LOCK(::activeMasternodeManager->cs, return quorum->IsValidMember(::activeMasternodeManager->m_info.proTxHash))) { return false; } diff --git a/src/llmq/signing_shares.cpp b/src/llmq/signing_shares.cpp index 4430c7dba9..49264e9462 100644 --- a/src/llmq/signing_shares.cpp +++ b/src/llmq/signing_shares.cpp @@ -219,7 +219,7 @@ void CSigSharesManager::ProcessMessage(const CNode& pfrom, const CSporkManager& { // non-masternodes are not interested in sigshares if (!fMasternodeMode) return; - if (WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash.IsNull())) return; + if (WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash.IsNull())) return; if (sporkman.IsSporkActive(SPORK_21_QUORUM_ALL_CONNECTED) && msg_type == NetMsgType::QSIGSHARE) { std::vector receivedSigShares; @@ -464,7 +464,7 @@ void CSigSharesManager::ProcessMessageSigShare(NodeId fromId, const CSigShare& s // quorum is too old return; } - if (!quorum->IsMember(WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) { + if (!quorum->IsMember(WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash))) { // we're not a member so we can't verify it (we actually shouldn't have received it) return; } @@ -513,7 +513,7 @@ bool CSigSharesManager::PreVerifyBatchedSigShares(const CQuorumManager& quorum_m // quorum is too old return false; } - if (!session.quorum->IsMember(WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) { + if (!session.quorum->IsMember(WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash))) { // we're not a member so we can't verify it (we actually shouldn't have received it) return false; } @@ -697,7 +697,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, m_sporkman) && sigShare.getQuorumMember() == quorum->GetMemberIndex(WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash))) { + if (!IsAllMembersConnectedEnabled(llmqType, m_sporkman) && sigShare.getQuorumMember() == quorum->GetMemberIndex(WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash))) { quorumNodes = connman.GetMasternodeQuorumNodes(sigShare.getLlmqType(), sigShare.getQuorumHash()); } @@ -1488,7 +1488,7 @@ void CSigSharesManager::SignPendingSigShares() std::optional CSigSharesManager::CreateSigShare(const CQuorumCPtr& quorum, const uint256& id, const uint256& msgHash) const { cxxtimer::Timer t(true); - auto activeMasterNodeProTxHash = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash); + auto activeMasterNodeProTxHash = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash); if (!quorum->IsValidMember(activeMasterNodeProTxHash)) { return std::nullopt; diff --git a/src/masternode/node.cpp b/src/masternode/node.cpp index 5c246c2cb1..047559e8f3 100644 --- a/src/masternode/node.cpp +++ b/src/masternode/node.cpp @@ -15,8 +15,6 @@ #include // Keep track of the active Masternode -RecursiveMutex activeMasternodeInfoCs; -CActiveMasternodeInfo activeMasternodeInfo GUARDED_BY(activeMasternodeInfoCs); std::unique_ptr activeMasternodeManager; std::string CActiveMasternodeManager::GetStateString() const @@ -65,7 +63,7 @@ std::string CActiveMasternodeManager::GetStatus() const void CActiveMasternodeManager::Init(const CBlockIndex* pindex) { - LOCK2(cs_main, activeMasternodeInfoCs); + LOCK2(::cs_main, cs); if (!fMasternodeMode) return; @@ -80,14 +78,14 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex) return; } - if (!GetLocalAddress(activeMasternodeInfo.service)) { + if (!GetLocalAddress(m_info.service)) { state = MASTERNODE_ERROR; return; } CDeterministicMNList mnList = Assert(m_dmnman)->GetListForBlock(pindex); - CDeterministicMNCPtr dmn = mnList.GetMNByOperatorKey(*activeMasternodeInfo.blsPubKeyOperator); + CDeterministicMNCPtr dmn = mnList.GetMNByOperatorKey(*m_info.blsPubKeyOperator); if (!dmn) { // MN not appeared on the chain yet return; @@ -104,7 +102,7 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex) LogPrintf("CActiveMasternodeManager::Init -- proTxHash=%s, proTx=%s\n", dmn->proTxHash.ToString(), dmn->ToString()); - if (activeMasternodeInfo.service != dmn->pdmnState->addr) { + if (m_info.service != dmn->pdmnState->addr) { state = MASTERNODE_ERROR; strError = "Local address does not match the address from ProTx"; LogPrintf("CActiveMasternodeManager::Init -- ERROR: %s\n", strError); @@ -112,33 +110,33 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex) } // Check socket connectivity - LogPrintf("CActiveMasternodeManager::Init -- Checking inbound connection to '%s'\n", activeMasternodeInfo.service.ToString()); - std::unique_ptr sock = CreateSock(activeMasternodeInfo.service); + LogPrintf("CActiveMasternodeManager::Init -- Checking inbound connection to '%s'\n", m_info.service.ToString()); + std::unique_ptr sock = CreateSock(m_info.service); if (!sock) { state = MASTERNODE_ERROR; - strError = "Could not create socket to connect to " + activeMasternodeInfo.service.ToString(); + strError = "Could not create socket to connect to " + m_info.service.ToString(); LogPrintf("CActiveMasternodeManager::Init -- ERROR: %s\n", strError); return; } - bool fConnected = ConnectSocketDirectly(activeMasternodeInfo.service, *sock, nConnectTimeout, true) && IsSelectableSocket(sock->Get()); + bool fConnected = ConnectSocketDirectly(m_info.service, *sock, nConnectTimeout, true) && IsSelectableSocket(sock->Get()); sock->Reset(); if (!fConnected && Params().RequireRoutableExternalIP()) { state = MASTERNODE_ERROR; - strError = "Could not connect to " + activeMasternodeInfo.service.ToString(); + strError = "Could not connect to " + m_info.service.ToString(); LogPrintf("CActiveMasternodeManager::Init -- ERROR: %s\n", strError); return; } - activeMasternodeInfo.proTxHash = dmn->proTxHash; - activeMasternodeInfo.outpoint = dmn->collateralOutpoint; - activeMasternodeInfo.legacy = dmn->pdmnState->nVersion == CProRegTx::LEGACY_BLS_VERSION; + m_info.proTxHash = dmn->proTxHash; + m_info.outpoint = dmn->collateralOutpoint; + m_info.legacy = dmn->pdmnState->nVersion == CProRegTx::LEGACY_BLS_VERSION; state = MASTERNODE_READY; } void CActiveMasternodeManager::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) { - LOCK2(cs_main, activeMasternodeInfoCs); + LOCK2(::cs_main, cs); if (!fMasternodeMode) return; @@ -147,23 +145,23 @@ void CActiveMasternodeManager::UpdatedBlockTip(const CBlockIndex* pindexNew, con if (state == MASTERNODE_READY) { auto oldMNList = Assert(m_dmnman)->GetListForBlock(pindexNew->pprev); auto newMNList = m_dmnman->GetListForBlock(pindexNew); - if (!newMNList.IsMNValid(activeMasternodeInfo.proTxHash)) { + if (!newMNList.IsMNValid(m_info.proTxHash)) { // MN disappeared from MN list state = MASTERNODE_REMOVED; - activeMasternodeInfo.proTxHash = uint256(); - activeMasternodeInfo.outpoint.SetNull(); + m_info.proTxHash = uint256(); + m_info.outpoint.SetNull(); // MN might have reappeared in same block with a new ProTx Init(pindexNew); return; } - auto oldDmn = oldMNList.GetMN(activeMasternodeInfo.proTxHash); - auto newDmn = newMNList.GetMN(activeMasternodeInfo.proTxHash); + auto oldDmn = oldMNList.GetMN(m_info.proTxHash); + auto newDmn = newMNList.GetMN(m_info.proTxHash); if (newDmn->pdmnState->pubKeyOperator != oldDmn->pdmnState->pubKeyOperator) { // MN operator key changed or revoked state = MASTERNODE_OPERATOR_KEY_CHANGED; - activeMasternodeInfo.proTxHash = uint256(); - activeMasternodeInfo.outpoint.SetNull(); + m_info.proTxHash = uint256(); + m_info.outpoint.SetNull(); // MN might have reappeared in same block with a new ProTx Init(pindexNew); return; @@ -172,8 +170,8 @@ void CActiveMasternodeManager::UpdatedBlockTip(const CBlockIndex* pindexNew, con if (newDmn->pdmnState->addr != oldDmn->pdmnState->addr) { // MN IP changed state = MASTERNODE_PROTX_IP_CHANGED; - activeMasternodeInfo.proTxHash = uint256(); - activeMasternodeInfo.outpoint.SetNull(); + m_info.proTxHash = uint256(); + m_info.outpoint.SetNull(); Init(pindexNew); return; } @@ -203,7 +201,7 @@ bool CActiveMasternodeManager::GetLocalAddress(CService& addrRet) if (!fFoundLocal) { bool empty = true; // If we have some peers, let's try to find our local address from one of them - auto service = WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.service); + auto service = WITH_LOCK(cs, return m_info.service); connman.ForEachNodeContinueIf(CConnman::AllNodes, [&](CNode* pnode) { empty = false; if (pnode->addr.IsIPv4()) diff --git a/src/masternode/node.h b/src/masternode/node.h index d842b555b8..e3e25c66ce 100644 --- a/src/masternode/node.h +++ b/src/masternode/node.h @@ -7,19 +7,13 @@ #include #include +#include #include -class CActiveMasternodeManager; class CBLSPublicKey; class CBLSSecretKey; class CDeterministicMNManager; -struct CActiveMasternodeInfo; - -extern CActiveMasternodeInfo activeMasternodeInfo; -extern RecursiveMutex activeMasternodeInfoCs; -extern std::unique_ptr activeMasternodeManager; - struct CActiveMasternodeInfo { // Keys for the active Masternode std::unique_ptr blsPubKeyOperator; @@ -32,7 +26,6 @@ struct CActiveMasternodeInfo { bool legacy{true}; }; - class CActiveMasternodeManager final : public CValidationInterface { public: @@ -46,6 +39,9 @@ public: MASTERNODE_ERROR, }; + mutable RecursiveMutex cs; + CActiveMasternodeInfo m_info GUARDED_BY(cs); + private: masternode_state_t state{MASTERNODE_WAITING_FOR_PROTX}; std::string strError; @@ -70,4 +66,6 @@ private: bool GetLocalAddress(CService& addrRet); }; +extern std::unique_ptr activeMasternodeManager; + #endif // BITCOIN_MASTERNODE_NODE_H diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index 673e42b76c..a688ac3215 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -318,12 +318,12 @@ static UniValue gobject_submit(const JSONRPCRequest& request) bool fMnFound{false}; if (fMasternodeMode) { - LOCK(activeMasternodeInfoCs); - fMnFound = mnList.HasValidMNByCollateral(activeMasternodeInfo.outpoint); + LOCK(::activeMasternodeManager->cs); + fMnFound = mnList.HasValidMNByCollateral(::activeMasternodeManager->m_info.outpoint); LogPrint(BCLog::GOBJECT, "gobject_submit -- pubKeyOperator = %s, outpoint = %s, params.size() = %lld, fMnFound = %d\n", - (activeMasternodeInfo.blsPubKeyOperator ? activeMasternodeInfo.blsPubKeyOperator->ToString(activeMasternodeInfo.legacy) : "N/A"), - activeMasternodeInfo.outpoint.ToStringShort(), request.params.size(), fMnFound); + (::activeMasternodeManager->m_info.blsPubKeyOperator ? ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(::activeMasternodeManager->m_info.legacy) : "N/A"), + ::activeMasternodeManager->m_info.outpoint.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); diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 0ab332f210..7a403de02c 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -266,12 +266,12 @@ static UniValue masternode_status(const JSONRPCRequest& request) UniValue mnObj(UniValue::VOBJ); CDeterministicMNCPtr dmn; { - LOCK(activeMasternodeInfoCs); + LOCK(::activeMasternodeManager->cs); // keep compatibility with legacy status for now (might get deprecated/removed later) - mnObj.pushKV("outpoint", activeMasternodeInfo.outpoint.ToStringShort()); - mnObj.pushKV("service", activeMasternodeInfo.service.ToString()); - dmn = node.dmnman->GetListAtChainTip().GetMN(activeMasternodeInfo.proTxHash); + mnObj.pushKV("outpoint", ::activeMasternodeManager->m_info.outpoint.ToStringShort()); + mnObj.pushKV("service", ::activeMasternodeManager->m_info.service.ToString()); + dmn = node.dmnman->GetListAtChainTip().GetMN(::activeMasternodeManager->m_info.proTxHash); } if (dmn) { mnObj.pushKV("proTxHash", dmn->proTxHash.ToString()); diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index d2c63fc514..ecedbe94f9 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -296,7 +296,7 @@ static UniValue quorum_dkgstatus(const JSONRPCRequest& request, CDeterministicMN int tipHeight = pindexTip->nHeight; const uint256 proTxHash = fMasternodeMode ? - WITH_LOCK(activeMasternodeInfoCs, return activeMasternodeInfo.proTxHash) : + WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.proTxHash) : uint256(); UniValue minableCommitments(UniValue::VARR); From 3827355cce44fbb7fdbc4171077d71b9bc332194 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Sun, 17 Mar 2024 14:22:36 +0000 Subject: [PATCH 04/14] refactor: move key initialization to InitKeys, define destructor --- src/init.cpp | 17 +---------------- src/masternode/node.cpp | 26 ++++++++++++++++++++++++++ src/masternode/node.h | 3 ++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 78f6a56dfa..a0cecbd5e8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -370,12 +370,6 @@ void PrepareShutdown(NodeContext& node) } if (fMasternodeMode) { UnregisterValidationInterface(::activeMasternodeManager.get()); - - LOCK(::activeMasternodeManager->cs); - // make sure to clean up BLS keys before global destructors are called (they have allocated from the secure memory pool) - ::activeMasternodeManager->m_info.blsKeyOperator.reset(); - ::activeMasternodeManager->m_info.blsPubKeyOperator.reset(); - ::activeMasternodeManager.reset(); } @@ -1860,16 +1854,7 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc { // Create and register activeMasternodeManager, will init later in ThreadImport ::activeMasternodeManager = std::make_unique(*node.connman, ::deterministicMNManager); - - LOCK(::activeMasternodeManager->cs); - assert(::activeMasternodeManager->m_info.blsKeyOperator == nullptr); - assert(::activeMasternodeManager->m_info.blsPubKeyOperator == nullptr); - ::activeMasternodeManager->m_info.blsKeyOperator = std::make_unique(keyOperator); - ::activeMasternodeManager->m_info.blsPubKeyOperator = std::make_unique(keyOperator.GetPublicKey()); - // We don't know the actual scheme at this point, print both - LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", - ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(true), - ::activeMasternodeManager->m_info.blsPubKeyOperator->ToString(false)); + ::activeMasternodeManager->InitKeys(keyOperator); RegisterValidationInterface(::activeMasternodeManager.get()); } diff --git a/src/masternode/node.cpp b/src/masternode/node.cpp index 047559e8f3..de2aaf1685 100644 --- a/src/masternode/node.cpp +++ b/src/masternode/node.cpp @@ -17,6 +17,17 @@ // Keep track of the active Masternode std::unique_ptr activeMasternodeManager; +CActiveMasternodeManager::~CActiveMasternodeManager() +{ + // Make sure to clean up BLS keys before global destructors are called + // (they have been allocated from the secure memory pool) + { + LOCK(cs); + m_info.blsKeyOperator.reset(); + m_info.blsPubKeyOperator.reset(); + } +} + std::string CActiveMasternodeManager::GetStateString() const { switch (state) { @@ -134,6 +145,21 @@ void CActiveMasternodeManager::Init(const CBlockIndex* pindex) state = MASTERNODE_READY; } +void CActiveMasternodeManager::InitKeys(const CBLSSecretKey& sk) +{ + AssertLockNotHeld(cs); + + LOCK(cs); + assert(m_info.blsKeyOperator == nullptr); + assert(m_info.blsPubKeyOperator == nullptr); + m_info.blsKeyOperator = std::make_unique(sk); + m_info.blsPubKeyOperator = std::make_unique(sk.GetPublicKey()); + // We don't know the actual scheme at this point, print both + LogPrintf("MASTERNODE:\n blsPubKeyOperator legacy: %s\n blsPubKeyOperator basic: %s\n", + m_info.blsPubKeyOperator->ToString(/*specificLegacyScheme=*/ true), + m_info.blsPubKeyOperator->ToString(/*specificLegacyScheme=*/ false)); +} + void CActiveMasternodeManager::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) { LOCK2(::cs_main, cs); diff --git a/src/masternode/node.h b/src/masternode/node.h index e3e25c66ce..eba9eb3698 100644 --- a/src/masternode/node.h +++ b/src/masternode/node.h @@ -51,11 +51,12 @@ private: public: explicit CActiveMasternodeManager(CConnman& _connman, const std::unique_ptr& dmnman) : connman(_connman), m_dmnman(dmnman) {}; - ~CActiveMasternodeManager() = default; + ~CActiveMasternodeManager(); void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override; void Init(const CBlockIndex* pindex); + void InitKeys(const CBLSSecretKey& sk) LOCKS_EXCLUDED(cs); std::string GetStateString() const; std::string GetStatus() const; From 3eb931b596cdc0f180efd3185ad04aa79812362b Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 12 Mar 2024 02:52:08 +0000 Subject: [PATCH 05/14] refactor: add helper function to sign messages with blsKeyOperator Avoid passing around the operator secret key if we can help it. Ask CActiveMasternodeManager to perform the operation for you instead. --- src/coinjoin/coinjoin.cpp | 4 +-- src/evo/mnauth.cpp | 62 +++++++++++++++++++++------------------ src/llmq/dkgsession.cpp | 8 ++--- src/masternode/node.cpp | 12 ++++++++ src/masternode/node.h | 4 +++ 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/coinjoin/coinjoin.cpp b/src/coinjoin/coinjoin.cpp index 94d43ea478..b9832322b0 100644 --- a/src/coinjoin/coinjoin.cpp +++ b/src/coinjoin/coinjoin.cpp @@ -52,7 +52,7 @@ bool CCoinJoinQueue::Sign() if (!fMasternodeMode) return false; uint256 hash = GetSignatureHash(); - CBLSSignature sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(hash, false)); + CBLSSignature sig = ::activeMasternodeManager->Sign(hash, /*is_legacy=*/ false); if (!sig.IsValid()) { return false; } @@ -104,7 +104,7 @@ bool CCoinJoinBroadcastTx::Sign() if (!fMasternodeMode) return false; uint256 hash = GetSignatureHash(); - CBLSSignature sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(hash, false)); + CBLSSignature sig = ::activeMasternodeManager->Sign(hash, /*is_legacy=*/ false); if (!sig.IsValid()) { return false; } diff --git a/src/evo/mnauth.cpp b/src/evo/mnauth.cpp index 9df175b52d..c4f10e8d87 100644 --- a/src/evo/mnauth.cpp +++ b/src/evo/mnauth.cpp @@ -23,38 +23,42 @@ void CMNAuth::PushMNAUTH(CNode& peer, CConnman& connman, const CBlockIndex* tip) { if (!fMasternodeMode) return; - LOCK(::activeMasternodeManager->cs); - if (::activeMasternodeManager->m_info.proTxHash.IsNull()) return; - - uint256 signHash; - const auto receivedMNAuthChallenge = peer.GetReceivedMNAuthChallenge(); - if (receivedMNAuthChallenge.IsNull()) { - return; - } - // We include fInbound in signHash to forbid interchanging of challenges by a man in the middle (MITM). This way - // we protect ourselves against MITM in this form: - // node1 <- Eve -> node2 - // It does not protect against: - // node1 -> Eve -> node2 - // This is ok as we only use MNAUTH as a DoS protection and not for sensitive stuff - int nOurNodeVersion{PROTOCOL_VERSION}; - if (Params().NetworkIDString() != CBaseChainParams::MAIN && gArgs.IsArgSet("-pushversion")) { - nOurNodeVersion = gArgs.GetArg("-pushversion", PROTOCOL_VERSION); - } - const bool is_basic_scheme_active{DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)}; - const CBLSPublicKeyVersionWrapper pubKey(*::activeMasternodeManager->m_info.blsPubKeyOperator, !is_basic_scheme_active); - if (peer.nVersion < MNAUTH_NODE_VER_VERSION || nOurNodeVersion < MNAUTH_NODE_VER_VERSION) { - signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn())); - } else { - signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn(), nOurNodeVersion)); - } - CMNAuth mnauth; - mnauth.proRegTxHash = ::activeMasternodeManager->m_info.proTxHash; - mnauth.sig = ::activeMasternodeManager->m_info.blsKeyOperator->Sign(signHash); + uint256 signHash; + { + LOCK(::activeMasternodeManager->cs); + if (::activeMasternodeManager->m_info.proTxHash.IsNull()) { + return; + } + + const auto receivedMNAuthChallenge = peer.GetReceivedMNAuthChallenge(); + if (receivedMNAuthChallenge.IsNull()) { + return; + } + // We include fInbound in signHash to forbid interchanging of challenges by a man in the middle (MITM). This way + // we protect ourselves against MITM in this form: + // node1 <- Eve -> node2 + // It does not protect against: + // node1 -> Eve -> node2 + // This is ok as we only use MNAUTH as a DoS protection and not for sensitive stuff + int nOurNodeVersion{PROTOCOL_VERSION}; + if (Params().NetworkIDString() != CBaseChainParams::MAIN && gArgs.IsArgSet("-pushversion")) { + nOurNodeVersion = gArgs.GetArg("-pushversion", PROTOCOL_VERSION); + } + const bool is_basic_scheme_active{DeploymentActiveAfter(tip, Params().GetConsensus(), Consensus::DEPLOYMENT_V19)}; + const CBLSPublicKeyVersionWrapper pubKey(*::activeMasternodeManager->m_info.blsPubKeyOperator, !is_basic_scheme_active); + if (peer.nVersion < MNAUTH_NODE_VER_VERSION || nOurNodeVersion < MNAUTH_NODE_VER_VERSION) { + signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn())); + } else { + signHash = ::SerializeHash(std::make_tuple(pubKey, receivedMNAuthChallenge, peer.IsInboundConn(), nOurNodeVersion)); + } + + mnauth.proRegTxHash = ::activeMasternodeManager->m_info.proTxHash; + } // ::activeMasternodeManager->cs + + mnauth.sig = ::activeMasternodeManager->Sign(signHash); LogPrint(BCLog::NET_NETCONN, "CMNAuth::%s -- Sending MNAUTH, peer=%d\n", __func__, peer.GetId()); - connman.PushMessage(&peer, CNetMsgMaker(peer.GetCommonVersion()).Make(NetMsgType::MNAUTH, mnauth)); } diff --git a/src/llmq/dkgsession.cpp b/src/llmq/dkgsession.cpp index 0f00e103cb..3ad7d7c0ac 100644 --- a/src/llmq/dkgsession.cpp +++ b/src/llmq/dkgsession.cpp @@ -199,7 +199,7 @@ void CDKGSession::SendContributions(CDKGPendingMessages& pendingMessages) logger.Batch("encrypted contributions. time=%d", t1.count()); - qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qc.GetSignHash())); + qc.sig = ::activeMasternodeManager->Sign(qc.GetSignHash()); logger.Flush(); @@ -517,7 +517,7 @@ void CDKGSession::SendComplaint(CDKGPendingMessages& pendingMessages) logger.Batch("sending complaint. badCount=%d, complaintCount=%d", badCount, complaintCount); - qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qc.GetSignHash())); + qc.sig = ::activeMasternodeManager->Sign(qc.GetSignHash()); logger.Flush(); @@ -711,7 +711,7 @@ void CDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, const return; } - qj.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(qj.GetSignHash())); + qj.sig = ::activeMasternodeManager->Sign(qj.GetSignHash()); logger.Flush(); @@ -1003,7 +1003,7 @@ void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages) (*commitmentHash.begin())++; } - qc.sig = WITH_LOCK(::activeMasternodeManager->cs, return ::activeMasternodeManager->m_info.blsKeyOperator->Sign(commitmentHash)); + qc.sig = ::activeMasternodeManager->Sign(commitmentHash); qc.quorumSig = skShare.Sign(commitmentHash); if (lieType == 3) { diff --git a/src/masternode/node.cpp b/src/masternode/node.cpp index de2aaf1685..1198dcf336 100644 --- a/src/masternode/node.cpp +++ b/src/masternode/node.cpp @@ -251,3 +251,15 @@ bool CActiveMasternodeManager::IsValidNetAddr(const CService& addrIn) return !Params().RequireRoutableExternalIP() || (addrIn.IsIPv4() && IsReachable(addrIn) && addrIn.IsRoutable()); } + +[[nodiscard]] CBLSSignature CActiveMasternodeManager::Sign(const uint256& hash) const +{ + AssertLockNotHeld(cs); + return WITH_LOCK(cs, return Assert(m_info.blsKeyOperator)->Sign(hash)); +} + +[[nodiscard]] CBLSSignature CActiveMasternodeManager::Sign(const uint256& hash, const bool is_legacy) const +{ + AssertLockNotHeld(cs); + return WITH_LOCK(cs, return Assert(m_info.blsKeyOperator)->Sign(hash, is_legacy)); +} diff --git a/src/masternode/node.h b/src/masternode/node.h index eba9eb3698..af61a0a349 100644 --- a/src/masternode/node.h +++ b/src/masternode/node.h @@ -12,6 +12,7 @@ class CBLSPublicKey; class CBLSSecretKey; +class CBLSSignature; class CDeterministicMNManager; struct CActiveMasternodeInfo { @@ -63,6 +64,9 @@ public: static bool IsValidNetAddr(const CService& addrIn); + [[nodiscard]] CBLSSignature Sign(const uint256& hash) const LOCKS_EXCLUDED(cs); + [[nodiscard]] CBLSSignature Sign(const uint256& hash, const bool is_legacy) const LOCKS_EXCLUDED(cs); + private: bool GetLocalAddress(CService& addrRet); }; From 33702aca394e2e321f3869f7b33db552ad3a98cc Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Tue, 12 Mar 2024 03:05:51 +0000 Subject: [PATCH 06/14] refactor: add helper function to decrypt messages with blsKeyOperator --- src/llmq/dkgsession.cpp | 2 +- src/llmq/quorums.cpp | 3 +-- src/masternode/node.cpp | 16 ++++++++++++++-- src/masternode/node.h | 3 +++ 4 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/llmq/dkgsession.cpp b/src/llmq/dkgsession.cpp index 3ad7d7c0ac..47495ccbd5 100644 --- a/src/llmq/dkgsession.cpp +++ b/src/llmq/dkgsession.cpp @@ -316,7 +316,7 @@ void CDKGSession::ReceiveMessage(const CDKGContribution& qc, bool& retBan) bool complain = false; CBLSSecretKey skContribution; - if (!qc.contributions->Decrypt(*myIdx, WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator), skContribution, PROTOCOL_VERSION)) { + if (!::activeMasternodeManager->Decrypt(*qc.contributions, *myIdx, skContribution, PROTOCOL_VERSION)) { logger.Batch("contribution from %s could not be decrypted", member->dmn->proTxHash.ToString()); complain = true; } else if (member->idx != myIdx && ShouldSimulateError(DKGError::type::COMPLAIN_LIE)) { diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 9456973b3c..c35814ac15 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -834,9 +834,8 @@ PeerMsgRet CQuorumManager::ProcessMessage(CNode& pfrom, const std::string& msg_t std::vector vecSecretKeys; vecSecretKeys.resize(vecEncrypted.size()); - auto secret = WITH_LOCK(::activeMasternodeManager->cs, return *::activeMasternodeManager->m_info.blsKeyOperator); for (const auto i : irange::range(vecEncrypted.size())) { - if (!vecEncrypted[i].Decrypt(memberIdx, secret, vecSecretKeys[i], PROTOCOL_VERSION)) { + if (!::activeMasternodeManager->Decrypt(vecEncrypted[i], memberIdx, vecSecretKeys[i], PROTOCOL_VERSION)) { return errorHandler("Failed to decrypt"); } } diff --git a/src/masternode/node.cpp b/src/masternode/node.cpp index 1198dcf336..d8cdadb1c3 100644 --- a/src/masternode/node.cpp +++ b/src/masternode/node.cpp @@ -4,10 +4,10 @@ #include -#include - +#include #include #include +#include #include #include #include @@ -252,6 +252,18 @@ bool CActiveMasternodeManager::IsValidNetAddr(const CService& addrIn) (addrIn.IsIPv4() && IsReachable(addrIn) && addrIn.IsRoutable()); } +template