From 364d6c37f7193650c2cf96c44687abcfc82cf3c9 Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Mon, 16 Mar 2020 10:54:59 +0100 Subject: [PATCH] Move and unify logic for quorum connection establishment into CLLMQUtils --- src/llmq/quorums.cpp | 29 +++------------------ src/llmq/quorums_dkgsessionhandler.cpp | 28 +------------------- src/llmq/quorums_utils.cpp | 36 ++++++++++++++++++++++++++ src/llmq/quorums_utils.h | 2 ++ 4 files changed, 42 insertions(+), 53 deletions(-) diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 8b2caff58e..4bb0b7303f 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -187,37 +187,14 @@ void CQuorumManager::EnsureQuorumConnections(Consensus::LLMQType llmqType, const auto curDkgBlock = pindexNew->GetAncestor(curDkgHeight)->GetBlockHash(); connmanQuorumsToDelete.erase(curDkgBlock); + bool allowWatch = gArgs.GetBoolArg("-watchquorums", DEFAULT_WATCH_QUORUMS); for (auto& quorum : lastQuorums) { - if (!quorum->IsMember(myProTxHash) && !gArgs.GetBoolArg("-watchquorums", DEFAULT_WATCH_QUORUMS)) { + if (!quorum->IsMember(myProTxHash) && !allowWatch) { continue; } if (!g_connman->HasMasternodeQuorumNodes(llmqType, quorum->qc.quorumHash)) { - std::set connections; - if (quorum->IsMember(myProTxHash)) { - connections = CLLMQUtils::GetQuorumConnections(llmqType, quorum->pindexQuorum, myProTxHash); - } else { - auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(llmqType, quorum->pindexQuorum, quorum->members.size(), 1); - for (auto idx : cindexes) { - connections.emplace(quorum->members[idx]->proTxHash); - } - } - if (!connections.empty()) { - if (LogAcceptCategory(BCLog::LLMQ)) { - auto mnList = deterministicMNManager->GetListAtChainTip(); - std::string debugMsg = strprintf("CQuorumManager::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, quorum->qc.quorumHash.ToString()); - for (auto& c : connections) { - auto dmn = mnList.GetValidMN(c); - if (!dmn) { - debugMsg += strprintf(" %s (not in valid MN set anymore)\n", c.ToString()); - } else { - debugMsg += strprintf(" %s (%s)\n", c.ToString(), dmn->pdmnState->addr.ToString(false)); - } - } - LogPrint(BCLog::LLMQ, debugMsg.c_str()); - } - g_connman->AddMasternodeQuorumNodes(llmqType, quorum->qc.quorumHash, connections); - } + CLLMQUtils::EnsureQuorumConnections(llmqType, quorum->pindexQuorum, myProTxHash, allowWatch); } connmanQuorumsToDelete.erase(quorum->qc.quorumHash); } diff --git a/src/llmq/quorums_dkgsessionhandler.cpp b/src/llmq/quorums_dkgsessionhandler.cpp index 332e53330c..97778cb95f 100644 --- a/src/llmq/quorums_dkgsessionhandler.cpp +++ b/src/llmq/quorums_dkgsessionhandler.cpp @@ -500,33 +500,7 @@ void CDKGSessionHandler::HandleDKGRound() return changed; }); - if (curSession->AreWeMember() || gArgs.GetBoolArg("-watchquorums", DEFAULT_WATCH_QUORUMS)) { - std::set connections; - if (curSession->AreWeMember()) { - connections = CLLMQUtils::GetQuorumConnections(params.type, pindexQuorum, curSession->myProTxHash); - } else { - auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(params.type, pindexQuorum, curSession->members.size(), 1); - for (auto idx : cindexes) { - connections.emplace(curSession->members[idx]->dmn->proTxHash); - } - } - if (!connections.empty()) { - if (LogAcceptCategory(BCLog::LLMQ_DKG)) { - std::string debugMsg = strprintf("CDKGSessionManager::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, curSession->pindexQuorum->GetBlockHash().ToString()); - auto mnList = deterministicMNManager->GetListAtChainTip(); - for (const auto& c : connections) { - auto dmn = mnList.GetValidMN(c); - if (!dmn) { - debugMsg += strprintf(" %s (not in valid MN set anymore)\n", c.ToString()); - } else { - debugMsg += strprintf(" %s (%s)\n", c.ToString(), dmn->pdmnState->addr.ToString(false)); - } - } - LogPrint(BCLog::LLMQ_DKG, debugMsg.c_str()); - } - g_connman->AddMasternodeQuorumNodes(params.type, curQuorumHash, connections); - } - } + CLLMQUtils::EnsureQuorumConnections(params.type, pindexQuorum, curSession->myProTxHash, gArgs.GetBoolArg("-watchquorums", DEFAULT_WATCH_QUORUMS)); WaitForNextPhase(QuorumPhase_Initialized, QuorumPhase_Contribute, curQuorumHash, []{return false;}); diff --git a/src/llmq/quorums_utils.cpp b/src/llmq/quorums_utils.cpp index 417668b8bf..0f66118d9f 100644 --- a/src/llmq/quorums_utils.cpp +++ b/src/llmq/quorums_utils.cpp @@ -95,6 +95,42 @@ std::set CLLMQUtils::CalcDeterministicWatchConnections(Consensus::LLMQTy return result; } +void CLLMQUtils::EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexQuorum, const uint256& myProTxHash, bool allowWatch) +{ + auto members = GetAllQuorumMembers(llmqType, pindexQuorum); + bool isMember = std::find_if(members.begin(), members.end(), [&](const CDeterministicMNCPtr& dmn) { return dmn->proTxHash == myProTxHash; }) != members.end(); + + if (!isMember && !allowWatch) { + return; + } + + std::set connections; + if (isMember) { + connections = CLLMQUtils::GetQuorumConnections(llmqType, pindexQuorum, myProTxHash); + } else { + auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(llmqType, pindexQuorum, members.size(), 1); + for (auto idx : cindexes) { + connections.emplace(members[idx]->proTxHash); + } + } + if (!connections.empty()) { + if (LogAcceptCategory(BCLog::LLMQ)) { + auto mnList = deterministicMNManager->GetListAtChainTip(); + std::string debugMsg = strprintf("CLLMQUtils::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, pindexQuorum->GetBlockHash().ToString()); + for (auto& c : connections) { + auto dmn = mnList.GetValidMN(c); + if (!dmn) { + debugMsg += strprintf(" %s (not in valid MN set anymore)\n", c.ToString()); + } else { + debugMsg += strprintf(" %s (%s)\n", c.ToString(), dmn->pdmnState->addr.ToString(false)); + } + } + LogPrint(BCLog::LLMQ, debugMsg.c_str()); + } + g_connman->AddMasternodeQuorumNodes(llmqType, pindexQuorum->GetBlockHash(), connections); + } +} + bool CLLMQUtils::IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash) { auto& params = Params().GetConsensus().llmqs.at(llmqType); diff --git a/src/llmq/quorums_utils.h b/src/llmq/quorums_utils.h index 14ceafcfe1..4a047f0c0f 100644 --- a/src/llmq/quorums_utils.h +++ b/src/llmq/quorums_utils.h @@ -34,6 +34,8 @@ public: static std::set GetQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& forMember); static std::set CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, size_t memberCount, size_t connectionCount); + static void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& myProTxHash, bool allowWatch); + static bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash); template