This commit is contained in:
PastaPastaPasta 2024-12-18 13:41:35 -06:00 committed by GitHub
commit bf114de44c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 36 additions and 33 deletions

View File

@ -13,8 +13,10 @@
#include <bls/bls_worker.h> #include <bls/bls_worker.h>
#include <sync.h> #include <sync.h>
#include <util/underlying.h> #include <util/underlying.h>
#include <saltedhasher.h>
#include <optional> #include <optional>
#include <unordered_set>
class CActiveMasternodeManager; class CActiveMasternodeManager;
class CInv; class CInv;
@ -293,7 +295,7 @@ private:
private: private:
std::vector<std::unique_ptr<CDKGMember>> members; std::vector<std::unique_ptr<CDKGMember>> members;
std::map<uint256, size_t> membersMap; std::map<uint256, size_t> membersMap;
std::set<uint256> relayMembers; std::unordered_set<uint256, StaticSaltedHasher> relayMembers;
BLSVerificationVectorPtr vvecContribution; BLSVerificationVectorPtr vvecContribution;
std::vector<CBLSSecretKey> m_sk_contributions; std::vector<CBLSSecretKey> m_sk_contributions;
@ -382,7 +384,7 @@ public:
public: public:
[[nodiscard]] CDKGMember* GetMember(const uint256& proTxHash) const; [[nodiscard]] CDKGMember* GetMember(const uint256& proTxHash) const;
[[nodiscard]] const std::set<uint256>& RelayMembers() const { return relayMembers; } [[nodiscard]] const std::unordered_set<uint256, StaticSaltedHasher>& RelayMembers() const { return relayMembers; }
[[nodiscard]] const CBlockIndex* BlockIndex() const { return m_quorum_base_block_index; } [[nodiscard]] const CBlockIndex* BlockIndex() const { return m_quorum_base_block_index; }
[[nodiscard]] const uint256& ProTx() const { return myProTxHash; } [[nodiscard]] const uint256& ProTx() const { return myProTxHash; }
[[nodiscard]] const Consensus::LLMQParams GetParams() const { return params; } [[nodiscard]] const Consensus::LLMQParams GetParams() const { return params; }

View File

@ -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()); 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)) { } else if (watchOtherISQuorums && !quorum->IsMember(myProTxHash)) {
std::set<uint256> connections; std::unordered_set<uint256, StaticSaltedHasher> connections;
const auto& cindexes = utils::CalcDeterministicWatchConnections(llmqParams.type, quorum->m_quorum_base_block_index, quorum->members.size(), 1); const auto& cindexes = utils::CalcDeterministicWatchConnections(llmqParams.type, quorum->m_quorum_base_block_index, quorum->members.size(), 1);
for (auto idx : cindexes) { for (auto idx : cindexes) {
connections.emplace(quorum->members[idx]->proTxHash); connections.emplace(quorum->members[idx]->proTxHash);

View File

@ -717,7 +717,7 @@ void CSigSharesManager::ProcessSigShare(PeerManager& peerman, const CSigShare& s
bool canTryRecovery = false; bool canTryRecovery = false;
// prepare node set for direct-push in case this is our sig share // prepare node set for direct-push in case this is our sig share
std::set<NodeId> quorumNodes; std::unordered_set<NodeId> quorumNodes;
if (!IsAllMembersConnectedEnabled(llmqType, m_sporkman) && sigShare.getQuorumMember() == quorum->GetMemberIndex(m_mn_activeman->GetProTxHash())) { if (!IsAllMembersConnectedEnabled(llmqType, m_sporkman) && sigShare.getQuorumMember() == quorum->GetMemberIndex(m_mn_activeman->GetProTxHash())) {
quorumNodes = connman.GetMasternodeQuorumNodes(sigShare.getLlmqType(), sigShare.getQuorumHash()); quorumNodes = connman.GetMasternodeQuorumNodes(sigShare.getLlmqType(), sigShare.getQuorumHash());
} }

View File

@ -653,12 +653,12 @@ uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256
return proTxHash2; return proTxHash2;
} }
std::set<uint256> GetQuorumConnections(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, std::unordered_set<uint256, StaticSaltedHasher> GetQuorumConnections(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman,
gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound) gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound)
{ {
if (IsAllMembersConnectedEnabled(llmqParams.type, sporkman)) { if (IsAllMembersConnectedEnabled(llmqParams.type, sporkman)) {
auto mns = GetAllQuorumMembers(llmqParams.type, dmnman, pQuorumBaseBlockIndex); auto mns = GetAllQuorumMembers(llmqParams.type, dmnman, pQuorumBaseBlockIndex);
std::set<uint256> result; std::unordered_set<uint256, StaticSaltedHasher> result;
for (const auto& dmn : mns) { for (const auto& dmn : mns) {
if (dmn->proTxHash == forMember) { if (dmn->proTxHash == forMember) {
@ -677,23 +677,23 @@ std::set<uint256> GetQuorumConnections(const Consensus::LLMQParams& llmqParams,
return GetQuorumRelayMembers(llmqParams, dmnman, pQuorumBaseBlockIndex, forMember, onlyOutbound); return GetQuorumRelayMembers(llmqParams, dmnman, pQuorumBaseBlockIndex, forMember, onlyOutbound);
} }
std::set<uint256> GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, std::unordered_set<uint256, StaticSaltedHasher> GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex,
const uint256& forMember, bool onlyOutbound) const uint256& forMember, bool onlyOutbound)
{ {
auto mns = GetAllQuorumMembers(llmqParams.type, dmnman, pQuorumBaseBlockIndex); auto mns = GetAllQuorumMembers(llmqParams.type, dmnman, pQuorumBaseBlockIndex);
std::set<uint256> result; std::unordered_set<uint256, StaticSaltedHasher> result;
auto calcOutbound = [&](size_t i, const uint256& proTxHash) { 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<uint256, StaticSaltedHasher> r{};
if (mns.size() == 1) { if (mns.size() == 1) {
// No outbound connections are needed when there is one MN only. // No outbound connections are needed when there is one MN only.
// Also note that trying to calculate results via the algorithm below // Also note that trying to calculate results via the algorithm below
// would result in an endless loop. // would result in an endless loop.
return std::set<uint256>(); 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<uint256> r;
int gap = 1; int gap = 1;
int gap_max = (int)mns.size() - 1; int gap_max = (int)mns.size() - 1;
int k = 0; int k = 0;
@ -775,8 +775,8 @@ bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, CConnman&
LogPrint(BCLog::NET_NETCONN, "%s -- isMember=%d for quorum %s:\n", LogPrint(BCLog::NET_NETCONN, "%s -- isMember=%d for quorum %s:\n",
__func__, isMember, pQuorumBaseBlockIndex->GetBlockHash().ToString()); __func__, isMember, pQuorumBaseBlockIndex->GetBlockHash().ToString());
std::set<uint256> connections; std::unordered_set<uint256, StaticSaltedHasher> connections;
std::set<uint256> relayMembers; std::unordered_set<uint256, StaticSaltedHasher> relayMembers;
if (isMember) { if (isMember) {
connections = GetQuorumConnections(llmqParams, dmnman, sporkman, pQuorumBaseBlockIndex, myProTxHash, true); connections = GetQuorumConnections(llmqParams, dmnman, sporkman, pQuorumBaseBlockIndex, myProTxHash, true);
relayMembers = GetQuorumRelayMembers(llmqParams, dmnman, pQuorumBaseBlockIndex, myProTxHash, true); relayMembers = GetQuorumRelayMembers(llmqParams, dmnman, pQuorumBaseBlockIndex, myProTxHash, true);

View File

@ -9,10 +9,12 @@
#include <sync.h> #include <sync.h>
#include <gsl/pointers.h> #include <gsl/pointers.h>
#include <uint256.h> #include <uint256.h>
#include <saltedhasher.h>
#include <map> #include <map>
#include <set> #include <set>
#include <vector> #include <vector>
#include <unordered_set>
class CConnman; class CConnman;
class CBlockIndex; class CBlockIndex;
@ -34,8 +36,8 @@ namespace utils
std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqType, CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool reset_cache = false); std::vector<CDeterministicMNCPtr> GetAllQuorumMembers(Consensus::LLMQType llmqType, CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, bool reset_cache = false);
uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256& proTxHash2); uint256 DeterministicOutboundConnection(const uint256& proTxHash1, const uint256& proTxHash2);
std::set<uint256> GetQuorumConnections(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); std::unordered_set<uint256, StaticSaltedHasher> GetQuorumConnections(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound);
std::set<uint256> GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound); std::unordered_set<uint256, StaticSaltedHasher> GetQuorumRelayMembers(const Consensus::LLMQParams& llmqParams, CDeterministicMNManager& dmnman, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, const uint256& forMember, bool onlyOutbound);
std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, size_t memberCount, size_t connectionCount); std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, gsl::not_null<const CBlockIndex*> pQuorumBaseBlockIndex, size_t memberCount, size_t connectionCount);
bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, CConnman& connman, CDeterministicMNManager& dmnman, const CSporkManager& sporkman, bool EnsureQuorumConnections(const Consensus::LLMQParams& llmqParams, CConnman& connman, CDeterministicMNManager& dmnman, const CSporkManager& sporkman,

View File

@ -3718,12 +3718,11 @@ void CConnman::ThreadOpenMasternodeConnections(CDeterministicMNManager& dmnman,
if (!fNetworkActive || !m_masternode_thread_active || !mn_sync.IsBlockchainSynced()) continue; if (!fNetworkActive || !m_masternode_thread_active || !mn_sync.IsBlockchainSynced()) continue;
std::set<CService> connectedNodes; std::unordered_set<CService, CServiceHash> connectedNodes;
std::map<uint256 /*proTxHash*/, bool /*fInbound*/> connectedProRegTxHashes; std::unordered_map<uint256 /*proTxHash*/, bool /*fInbound*/, StaticSaltedHasher> connectedProRegTxHashes;
ForEachNode([&](const CNode* pnode) { ForEachNode([&](const CNode* pnode) {
auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash();
connectedNodes.emplace(pnode->addr); connectedNodes.emplace(pnode->addr);
if (!verifiedProRegTxHash.IsNull()) { if (auto verifiedProRegTxHash = pnode->GetVerifiedProRegTxHash(); !verifiedProRegTxHash.IsNull()) {
connectedProRegTxHashes.emplace(verifiedProRegTxHash, pnode->IsInboundConn()); connectedProRegTxHashes.emplace(verifiedProRegTxHash, pnode->IsInboundConn());
} }
}); });
@ -4617,7 +4616,7 @@ bool CConnman::AddPendingMasternode(const uint256& proTxHash)
return true; return true;
} }
void CConnman::SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set<uint256>& proTxHashes) void CConnman::SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set<uint256, StaticSaltedHasher>& proTxHashes)
{ {
LOCK(cs_vPendingMasternodes); LOCK(cs_vPendingMasternodes);
auto it = masternodeQuorumNodes.emplace(std::make_pair(llmqType, quorumHash), proTxHashes); 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<uint256>& proTxHashes) void CConnman::SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set<uint256, StaticSaltedHasher>& proTxHashes)
{ {
{ {
LOCK(cs_vPendingMasternodes); LOCK(cs_vPendingMasternodes);
@ -4657,10 +4656,10 @@ bool CConnman::HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint
return masternodeQuorumNodes.count(std::make_pair(llmqType, quorumHash)); return masternodeQuorumNodes.count(std::make_pair(llmqType, quorumHash));
} }
std::set<uint256> CConnman::GetMasternodeQuorums(Consensus::LLMQType llmqType) const std::unordered_set<uint256, StaticSaltedHasher> CConnman::GetMasternodeQuorums(Consensus::LLMQType llmqType) const
{ {
LOCK(cs_vPendingMasternodes); LOCK(cs_vPendingMasternodes);
std::set<uint256> result; std::unordered_set<uint256, StaticSaltedHasher> result;
for (const auto& p : masternodeQuorumNodes) { for (const auto& p : masternodeQuorumNodes) {
if (p.first.first != llmqType) { if (p.first.first != llmqType) {
continue; continue;
@ -4670,7 +4669,7 @@ std::set<uint256> CConnman::GetMasternodeQuorums(Consensus::LLMQType llmqType) c
return result; return result;
} }
std::set<NodeId> CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const std::unordered_set<NodeId> CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const
{ {
LOCK2(m_nodes_mutex, cs_vPendingMasternodes); LOCK2(m_nodes_mutex, cs_vPendingMasternodes);
auto it = masternodeQuorumNodes.find(std::make_pair(llmqType, quorumHash)); auto it = masternodeQuorumNodes.find(std::make_pair(llmqType, quorumHash));
@ -4679,7 +4678,7 @@ std::set<NodeId> CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType
} }
const auto& proRegTxHashes = it->second; const auto& proRegTxHashes = it->second;
std::set<NodeId> nodes; std::unordered_set<NodeId> nodes;
for (const auto pnode : m_nodes) { for (const auto pnode : m_nodes) {
if (pnode->fDisconnect) { if (pnode->fDisconnect) {
continue; continue;

View File

@ -1501,12 +1501,12 @@ public:
EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex, !mutexMsgProc); EXCLUSIVE_LOCKS_REQUIRED(!m_unused_i2p_sessions_mutex, !mutexMsgProc);
bool AddPendingMasternode(const uint256& proTxHash); bool AddPendingMasternode(const uint256& proTxHash);
void SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set<uint256>& proTxHashes); void SetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set<uint256, StaticSaltedHasher>& proTxHashes);
void SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set<uint256>& proTxHashes); void SetMasternodeQuorumRelayMembers(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::unordered_set<uint256, StaticSaltedHasher>& proTxHashes);
bool HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const; bool HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const;
std::set<uint256> GetMasternodeQuorums(Consensus::LLMQType llmqType) const; std::unordered_set<uint256, StaticSaltedHasher> GetMasternodeQuorums(Consensus::LLMQType llmqType) const;
// also returns QWATCH nodes // also returns QWATCH nodes
std::set<NodeId> GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const; std::unordered_set<NodeId> GetMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash) const;
void RemoveMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash); void RemoveMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash);
bool IsMasternodeQuorumNode(const CNode* pnode, const CDeterministicMNList& tip_mn_list) const; bool IsMasternodeQuorumNode(const CNode* pnode, const CDeterministicMNList& tip_mn_list) const;
bool IsMasternodeQuorumRelayMember(const uint256& protxHash); bool IsMasternodeQuorumRelayMember(const uint256& protxHash);
@ -1815,8 +1815,8 @@ private:
std::vector<uint256> vPendingMasternodes; std::vector<uint256> vPendingMasternodes;
mutable RecursiveMutex cs_vPendingMasternodes; mutable RecursiveMutex cs_vPendingMasternodes;
std::map<std::pair<Consensus::LLMQType, uint256>, std::set<uint256>> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes); std::map<std::pair<Consensus::LLMQType, uint256>, std::unordered_set<uint256, StaticSaltedHasher>> masternodeQuorumNodes GUARDED_BY(cs_vPendingMasternodes);
std::map<std::pair<Consensus::LLMQType, uint256>, std::set<uint256>> masternodeQuorumRelayMembers GUARDED_BY(cs_vPendingMasternodes); std::map<std::pair<Consensus::LLMQType, uint256>, std::unordered_set<uint256, StaticSaltedHasher>> masternodeQuorumRelayMembers GUARDED_BY(cs_vPendingMasternodes);
std::set<uint256> masternodePendingProbes GUARDED_BY(cs_vPendingMasternodes); std::set<uint256> masternodePendingProbes GUARDED_BY(cs_vPendingMasternodes);
mutable Mutex cs_mapSocketToNode; mutable Mutex cs_mapSocketToNode;