diff --git a/src/llmq/dkgsession.h b/src/llmq/dkgsession.h index 82a1263b92..b1897482fc 100644 --- a/src/llmq/dkgsession.h +++ b/src/llmq/dkgsession.h @@ -11,10 +11,12 @@ #include #include #include +#include #include #include #include +#include class CActiveMasternodeManager; class CInv; @@ -293,7 +295,7 @@ private: private: std::vector> members; std::map membersMap; - std::set relayMembers; + std::unordered_set relayMembers; BLSVerificationVectorPtr vvecContribution; std::vector m_sk_contributions; @@ -382,7 +384,7 @@ public: public: [[nodiscard]] CDKGMember* GetMember(const uint256& proTxHash) const; - [[nodiscard]] const std::set& RelayMembers() const { return relayMembers; } + [[nodiscard]] const std::unordered_set& RelayMembers() const { return relayMembers; } [[nodiscard]] const CBlockIndex* BlockIndex() const { return m_quorum_base_block_index; } [[nodiscard]] const uint256& ProTx() const { return myProTxHash; } [[nodiscard]] const Consensus::LLMQParams GetParams() const { return params; } diff --git a/src/llmq/quorums.cpp b/src/llmq/quorums.cpp index 67b2890b57..a27fa04494 100644 --- a/src/llmq/quorums.cpp +++ b/src/llmq/quorums.cpp @@ -370,7 +370,7 @@ void CQuorumManager::CheckQuorumConnections(CConnman& connman, const Consensus:: 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()); } } else if (watchOtherISQuorums && !quorum->IsMember(myProTxHash)) { - std::set connections; + std::unordered_set connections; const auto& cindexes = utils::CalcDeterministicWatchConnections(llmqParams.type, quorum->m_quorum_base_block_index, quorum->members.size(), 1); for (auto idx : cindexes) { connections.emplace(quorum->members[idx]->proTxHash); diff --git a/src/llmq/signing_shares.cpp b/src/llmq/signing_shares.cpp index 6bcc0e8996..fad33e63ee 100644 --- a/src/llmq/signing_shares.cpp +++ b/src/llmq/signing_shares.cpp @@ -717,7 +717,7 @@ void CSigSharesManager::ProcessSigShare(PeerManager& peerman, const CSigShare& s bool canTryRecovery = false; // prepare node set for direct-push in case this is our sig share - std::set quorumNodes; + std::unordered_set quorumNodes; if (!IsAllMembersConnectedEnabled(llmqType, m_sporkman) && sigShare.getQuorumMember() == quorum->GetMemberIndex(m_mn_activeman->GetProTxHash())) { quorumNodes = connman.GetMasternodeQuorumNodes(sigShare.getLlmqType(), sigShare.getQuorumHash()); } diff --git a/src/llmq/utils.cpp b/src/llmq/utils.cpp index 4f7d635958..a9bb1e3c81 100644 --- a/src/llmq/utils.cpp +++ b/src/llmq/utils.cpp @@ -653,12 +653,13 @@ uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256 return proTxHash2; } -std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, - gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound) +std::unordered_set GetQuorumConnections( + const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, + gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound) { if (IsAllMembersConnectedEnabled(llmqParams.type, sporkman)) { auto mns = GetAllQuorumMembers(llmqParams.type, dmnman, pQuorumBaseBlockIndex); - std::set result; + std::unordered_set result; for (const auto& dmn : mns) { if (dmn->proTxHash == forMember) { @@ -677,23 +678,25 @@ std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, return GetQuorumRelayMembers(llmqParams, dmnman, pQuorumBaseBlockIndex, forMember, onlyOutbound); } -std::set GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, gsl::not_null pQuorumBaseBlockIndex, - const uint256& forMember, bool onlyOutbound) +std::unordered_set GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, + CDeterministicMNManager& dmnman, + gsl::not_null pQuorumBaseBlockIndex, + const uint256& forMember, bool onlyOutbound) { auto mns = GetAllQuorumMembers(llmqParams.type, dmnman, pQuorumBaseBlockIndex); - std::set result; + std::unordered_set result; auto calcOutbound = [&](size_t i, const uint256& proTxHash) { + // Relay to nodes at indexes (i+2^k)%n, where + // k: 0..max(1, floor(log2(n-1))-1) + // n: size of the quorum/ring + std::unordered_set r{}; if (mns.size() == 1) { // No outbound connections are needed when there is one MN only. // Also note that trying to calculate results via the algorithm below // would result in an endless loop. - return std::set(); + return r; } - // Relay to nodes at indexes (i+2^k)%n, where - // k: 0..max(1, floor(log2(n-1))-1) - // n: size of the quorum/ring - std::set r; int gap = 1; int gap_max = (int)mns.size() - 1; int k = 0; @@ -775,8 +778,8 @@ bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, CConnman& LogPrint(BCLog::NET_NETCONN, "%s -- isMember=%d for quorum %s:\n", __func__, isMember, pQuorumBaseBlockIndex->GetBlockHash().ToString()); - std::set connections; - std::set relayMembers; + std::unordered_set connections; + std::unordered_set relayMembers; if (isMember) { connections = GetQuorumConnections(llmqParams, dmnman, sporkman, pQuorumBaseBlockIndex, myProTxHash, true); relayMembers = GetQuorumRelayMembers(llmqParams, dmnman, pQuorumBaseBlockIndex, myProTxHash, true); diff --git a/src/llmq/utils.h b/src/llmq/utils.h index 2b5dc3a88e..2795db58dc 100644 --- a/src/llmq/utils.h +++ b/src/llmq/utils.h @@ -5,13 +5,15 @@ #ifndef BITCOIN_LLMQ_UTILS_H #define BITCOIN_LLMQ_UTILS_H -#include -#include #include +#include +#include +#include #include #include #include +#include #include class CConnman; @@ -34,8 +36,13 @@ namespace utils std::vector GetAllQuorumMembers(Consensus::LLMQType llmqType, CDeterministicMNManager& dmnman, gsl::not_null pQuorumBaseBlockIndex, bool reset_cache = false); uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256& proTxHash2); -std::set GetQuorumConnections(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); -std::set GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); +std::unordered_set GetQuorumConnections( + const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, + gsl::not_null pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); +std::unordered_set GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, + CDeterministicMNManager& dmnman, + 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, CConnman& connman, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, diff --git a/src/net.cpp b/src/net.cpp index 9bfb813178..5b07e5f9bf 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -3718,12 +3718,11 @@ void CConnman::ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman, if (!fNetworkActive || !m_masternode_thread_active || !mn_sync.IsBlockchainSynced()) continue; - std::set connectedNodes; - std::map connectedProRegTxHashes; + std::unordered_set connectedNodes; + std::unordered_map connectedProRegTxHashes; ForEachNode([&](const CNode* pnode) { - auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash(); connectedNodes.emplace(pnode->addr); - if (!verifiedProRegTxHash.IsNull()) { + if (auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash(); !verifiedProRegTxHash.IsNull()) { connectedProRegTxHashes.emplace(verifiedProRegTxHash, pnode->IsInboundConn()); } }); @@ -4617,7 +4616,7 @@ bool CConnman::AddPendingMasternode(const uint256& proTxHash) return true; } -void CConnman::SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set& proTxHashes) +void CConnman::SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set& proTxHashes) { LOCK(cs_vPendingMasternodes); auto it = masternodeQuorumNodes.emplace(std::make_pair(llmqType, quorumHash), proTxHashes); @@ -4626,7 +4625,7 @@ void CConnman::SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint } } -void CConnman::SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set& proTxHashes) +void CConnman::SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set& proTxHashes) { { LOCK(cs_vPendingMasternodes); @@ -4657,10 +4656,10 @@ bool CConnman::HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint return masternodeQuorumNodes.count(std::make_pair(llmqType, quorumHash)); } -std::set CConnman::GetMasternodeQuorums(Consensus::LLMQType llmqType) const +std::unordered_set CConnman::GetMasternodeQuorums(Consensus::LLMQType llmqType) const { LOCK(cs_vPendingMasternodes); - std::set result; + std::unordered_set result; for (const auto& p : masternodeQuorumNodes) { if (p.first.first != llmqType) { continue; @@ -4670,7 +4669,7 @@ std::set CConnman::GetMasternodeQuorums(Consensus::LLMQType llmqType) c return result; } -std::set CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const +std::unordered_set CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const { LOCK2(m_nodes_mutex, cs_vPendingMasternodes); auto it = masternodeQuorumNodes.find(std::make_pair(llmqType, quorumHash)); @@ -4679,7 +4678,7 @@ std::set CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType } const auto& proRegTxHashes = it->second; - std::set nodes; + std::unordered_set nodes; for (const auto pnode : m_nodes) { if (pnode->fDisconnect) { continue; diff --git a/src/net.h b/src/net.h index 524f0a244b..4901507a6c 100644 --- a/src/net.h +++ b/src/net.h @@ -1501,12 +1501,12 @@ public: EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex, !mutexMsgProc); bool AddPendingMasternode(const uint256& proTxHash); - void SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set& proTxHashes); - void SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set& proTxHashes); + void SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set& proTxHashes); + void SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set& proTxHashes); bool HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const; - std::set GetMasternodeQuorums(Consensus::LLMQType llmqType) const; + std::unordered_set GetMasternodeQuorums(Consensus::LLMQType llmqType) const; // also returns QWATCH nodes - std::set GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const; + std::unordered_set GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const; void RemoveMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash); bool IsMasternodeQuorumNode(const CNode* pnode, const CDeterministicMNList& tip_mn_list) const; bool IsMasternodeQuorumRelayMember(const uint256& protxHash); @@ -1815,8 +1815,8 @@ private: std::vector vPendingMasternodes; mutable RecursiveMutex cs_vPendingMasternodes; - std::map, std::set> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes); - std::map, std::set> masternodeQuorumRelayMembers GUARDED_BY(cs_vPendingMasternodes); + std::map, std::unordered_set> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes); + std::map, std::unordered_set> masternodeQuorumRelayMembers GUARDED_BY(cs_vPendingMasternodes); std::set masternodePendingProbes GUARDED_BY(cs_vPendingMasternodes); mutable Mutex cs_mapSocketToNode;