Merge pull request #2848 from codablock/pr_llmq_betterintraquorum

Connect to most recently updated address in ThreadOpenMasternodeConnections
This commit is contained in:
Alexander Block 2019-04-09 13:55:04 +02:00 committed by GitHub
commit 2f8440696f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 74 additions and 53 deletions

View File

@ -168,6 +168,11 @@ CDeterministicMNCPtr CDeterministicMNList::GetValidMNByCollateral(const COutPoin
return dmn; return dmn;
} }
CDeterministicMNCPtr CDeterministicMNList::GetValidMNByService(const CService& service) const
{
return GetUniquePropertyMN(service);
}
static int CompareByLastPaid_GetHeight(const CDeterministicMN& dmn) static int CompareByLastPaid_GetHeight(const CDeterministicMN& dmn)
{ {
int height = dmn.pdmnState->nLastPaidHeight; int height = dmn.pdmnState->nLastPaidHeight;

View File

@ -306,6 +306,7 @@ public:
CDeterministicMNCPtr GetMNByOperatorKey(const CBLSPublicKey& pubKey); CDeterministicMNCPtr GetMNByOperatorKey(const CBLSPublicKey& pubKey);
CDeterministicMNCPtr GetMNByCollateral(const COutPoint& collateralOutpoint) const; CDeterministicMNCPtr GetMNByCollateral(const COutPoint& collateralOutpoint) const;
CDeterministicMNCPtr GetValidMNByCollateral(const COutPoint& collateralOutpoint) const; CDeterministicMNCPtr GetValidMNByCollateral(const COutPoint& collateralOutpoint) const;
CDeterministicMNCPtr GetValidMNByService(const CService& service) const;
CDeterministicMNCPtr GetMNPayee() const; CDeterministicMNCPtr GetMNPayee() const;
/** /**

View File

@ -190,21 +190,29 @@ void CQuorumManager::EnsureQuorumConnections(Consensus::LLMQType llmqType, const
} }
if (!g_connman->HasMasternodeQuorumNodes(llmqType, quorum->qc.quorumHash)) { if (!g_connman->HasMasternodeQuorumNodes(llmqType, quorum->qc.quorumHash)) {
std::map<CService, uint256> connections; std::set<uint256> connections;
if (quorum->IsMember(myProTxHash)) { if (quorum->IsMember(myProTxHash)) {
connections = CLLMQUtils::GetQuorumConnections(llmqType, quorum->qc.quorumHash, myProTxHash); connections = CLLMQUtils::GetQuorumConnections(llmqType, quorum->qc.quorumHash, myProTxHash);
} else { } else {
auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(llmqType, quorum->qc.quorumHash, quorum->members.size(), 1); auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(llmqType, quorum->qc.quorumHash, quorum->members.size(), 1);
for (auto idx : cindexes) { for (auto idx : cindexes) {
connections.emplace(quorum->members[idx]->pdmnState->addr, quorum->members[idx]->proTxHash); connections.emplace(quorum->members[idx]->proTxHash);
} }
} }
if (!connections.empty()) { if (!connections.empty()) {
if (LogAcceptCategory("llmq")) {
auto mnList = deterministicMNManager->GetListAtChainTip();
std::string debugMsg = strprintf("CQuorumManager::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, quorum->qc.quorumHash.ToString()); std::string debugMsg = strprintf("CQuorumManager::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, quorum->qc.quorumHash.ToString());
for (auto& c : connections) { for (auto& c : connections) {
debugMsg += strprintf(" %s\n", c.first.ToString(false)); 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("llmq", debugMsg); LogPrint("llmq", debugMsg);
}
g_connman->AddMasternodeQuorumNodes(llmqType, quorum->qc.quorumHash, connections); g_connman->AddMasternodeQuorumNodes(llmqType, quorum->qc.quorumHash, connections);
} }
} }

View File

@ -468,21 +468,29 @@ void CDKGSessionHandler::HandleDKGRound()
}); });
if (curSession->AreWeMember() || GetBoolArg("-watchquorums", DEFAULT_WATCH_QUORUMS)) { if (curSession->AreWeMember() || GetBoolArg("-watchquorums", DEFAULT_WATCH_QUORUMS)) {
std::map<CService, uint256> connections; std::set<uint256> connections;
if (curSession->AreWeMember()) { if (curSession->AreWeMember()) {
connections = CLLMQUtils::GetQuorumConnections(params.type, curQuorumHash, curSession->myProTxHash); connections = CLLMQUtils::GetQuorumConnections(params.type, curQuorumHash, curSession->myProTxHash);
} else { } else {
auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(params.type, curQuorumHash, curSession->members.size(), 1); auto cindexes = CLLMQUtils::CalcDeterministicWatchConnections(params.type, curQuorumHash, curSession->members.size(), 1);
for (auto idx : cindexes) { for (auto idx : cindexes) {
connections.emplace(curSession->members[idx]->dmn->pdmnState->addr, curSession->members[idx]->dmn->proTxHash); connections.emplace(curSession->members[idx]->dmn->proTxHash);
} }
} }
if (!connections.empty()) { if (!connections.empty()) {
if (LogAcceptCategory("llmq-dkg")) {
std::string debugMsg = strprintf("CDKGSessionManager::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, curSession->quorumHash.ToString()); std::string debugMsg = strprintf("CDKGSessionManager::%s -- adding masternodes quorum connections for quorum %s:\n", __func__, curSession->quorumHash.ToString());
auto mnList = deterministicMNManager->GetListAtChainTip();
for (const auto& c : connections) { for (const auto& c : connections) {
debugMsg += strprintf(" %s\n", c.first.ToString(false)); 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("llmq-dkg", debugMsg); LogPrint("llmq-dkg", debugMsg);
}
g_connman->AddMasternodeQuorumNodes(params.type, curQuorumHash, connections); g_connman->AddMasternodeQuorumNodes(params.type, curQuorumHash, connections);
} }
} }

View File

@ -41,12 +41,12 @@ uint256 CLLMQUtils::BuildSignHash(Consensus::LLMQType llmqType, const uint256& q
return h.GetHash(); return h.GetHash();
} }
std::map<CService, uint256> CLLMQUtils::GetQuorumConnections(Consensus::LLMQType llmqType, const uint256& blockHash, const uint256& forMember) std::set<uint256> CLLMQUtils::GetQuorumConnections(Consensus::LLMQType llmqType, const uint256& blockHash, const uint256& forMember)
{ {
auto& params = Params().GetConsensus().llmqs.at(llmqType); auto& params = Params().GetConsensus().llmqs.at(llmqType);
auto mns = GetAllQuorumMembers(llmqType, blockHash); auto mns = GetAllQuorumMembers(llmqType, blockHash);
std::map<CService, uint256> result; std::set<uint256> result;
for (size_t i = 0; i < mns.size(); i++) { for (size_t i = 0; i < mns.size(); i++) {
auto& dmn = mns[i]; auto& dmn = mns[i];
if (dmn->proTxHash == forMember) { if (dmn->proTxHash == forMember) {
@ -62,7 +62,7 @@ std::map<CService, uint256> CLLMQUtils::GetQuorumConnections(Consensus::LLMQType
if (otherDmn == dmn) { if (otherDmn == dmn) {
continue; continue;
} }
result.emplace(otherDmn->pdmnState->addr, otherDmn->proTxHash); result.emplace(otherDmn->proTxHash);
gap <<= 1; gap <<= 1;
k++; k++;
} }

View File

@ -31,7 +31,7 @@ public:
return BuildSignHash((Consensus::LLMQType)s.llmqType, s.quorumHash, s.id, s.msgHash); return BuildSignHash((Consensus::LLMQType)s.llmqType, s.quorumHash, s.id, s.msgHash);
} }
static std::map<CService, uint256> GetQuorumConnections(Consensus::LLMQType llmqType, const uint256& blockHash, const uint256& forMember); static std::set<uint256> GetQuorumConnections(Consensus::LLMQType llmqType, const uint256& blockHash, const uint256& forMember);
static std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, const uint256& blockHash, size_t memberCount, size_t connectionCount); static std::set<size_t> CalcDeterministicWatchConnections(Consensus::LLMQType llmqType, const uint256& blockHash, size_t memberCount, size_t connectionCount);
static bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash); static bool IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quorumHash);

View File

@ -2056,6 +2056,8 @@ void CConnman::ThreadOpenMasternodeConnections()
} }
}); });
auto mnList = deterministicMNManager->GetListAtChainTip();
CSemaphoreGrant grant(*semMasternodeOutbound); CSemaphoreGrant grant(*semMasternodeOutbound);
if (interruptNet) if (interruptNet)
return; return;
@ -2068,20 +2070,23 @@ void CConnman::ThreadOpenMasternodeConnections()
std::vector<CService> pending; std::vector<CService> pending;
for (const auto& group : masternodeQuorumNodes) { for (const auto& group : masternodeQuorumNodes) {
for (const auto& p : group.second) { for (const auto& proRegTxHash : group.second) {
auto& addr = p.first; auto dmn = mnList.GetValidMN(proRegTxHash);
auto& proRegTxHash = p.second; if (!dmn) {
if (!connectedNodes.count(addr) && !IsMasternodeOrDisconnectRequested(addr) && !connectedProRegTxHashes.count(proRegTxHash)) { continue;
pending.emplace_back(addr); }
const auto& addr2 = dmn->pdmnState->addr;
if (!connectedNodes.count(addr2) && !IsMasternodeOrDisconnectRequested(addr2) && !connectedProRegTxHashes.count(proRegTxHash)) {
pending.emplace_back(addr2);
} }
} }
} }
if (!vPendingMasternodes.empty()) { if (!vPendingMasternodes.empty()) {
auto addr = vPendingMasternodes.front(); auto addr2 = vPendingMasternodes.front();
vPendingMasternodes.erase(vPendingMasternodes.begin()); vPendingMasternodes.erase(vPendingMasternodes.begin());
if (!connectedNodes.count(addr) && !IsMasternodeOrDisconnectRequested(addr)) { if (!connectedNodes.count(addr2) && !IsMasternodeOrDisconnectRequested(addr2)) {
pending.emplace_back(addr); pending.emplace_back(addr2);
} }
} }
@ -2719,14 +2724,14 @@ bool CConnman::AddPendingMasternode(const CService& service)
return true; return true;
} }
bool CConnman::AddMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::map<CService, uint256>& addresses) bool CConnman::AddMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set<uint256>& proTxHashes)
{ {
LOCK(cs_vPendingMasternodes); LOCK(cs_vPendingMasternodes);
auto it = masternodeQuorumNodes.find(std::make_pair(llmqType, quorumHash)); auto it = masternodeQuorumNodes.find(std::make_pair(llmqType, quorumHash));
if (it != masternodeQuorumNodes.end()) { if (it != masternodeQuorumNodes.end()) {
return false; return false;
} }
masternodeQuorumNodes.emplace(std::make_pair(llmqType, quorumHash), addresses); masternodeQuorumNodes.emplace(std::make_pair(llmqType, quorumHash), proTxHashes);
return true; return true;
} }
@ -2756,18 +2761,14 @@ std::set<NodeId> CConnman::GetMasternodeQuorumNodes(Consensus::LLMQType llmqType
if (it == masternodeQuorumNodes.end()) { if (it == masternodeQuorumNodes.end()) {
return {}; return {};
} }
std::set<uint256> proRegTxHashes; const auto& proRegTxHashes = it->second;
for (auto& p : it->second) {
proRegTxHashes.emplace(p.second);
}
std::set<NodeId> nodes; std::set<NodeId> nodes;
for (const auto pnode : vNodes) { for (const auto pnode : vNodes) {
if (pnode->fDisconnect) { if (pnode->fDisconnect) {
continue; continue;
} }
if (!pnode->qwatch && !it->second.count(pnode->addr) && if (!pnode->qwatch && (pnode->verifiedProRegTxHash.IsNull() || !proRegTxHashes.count(pnode->verifiedProRegTxHash))) {
(pnode->verifiedProRegTxHash.IsNull() || !proRegTxHashes.count(pnode->verifiedProRegTxHash))) {
continue; continue;
} }
nodes.emplace(pnode->id); nodes.emplace(pnode->id);
@ -2783,13 +2784,27 @@ void CConnman::RemoveMasternodeQuorumNodes(Consensus::LLMQType llmqType, const u
bool CConnman::IsMasternodeQuorumNode(const CNode* pnode) bool CConnman::IsMasternodeQuorumNode(const CNode* pnode)
{ {
// Let's see if this is an outgoing connection to an address that is known to be a masternode
// We however only need to know this if the node did not authenticate itself as a MN yet
uint256 assumedProTxHash;
if (pnode->verifiedProRegTxHash.IsNull() && !pnode->fInbound) {
auto mnList = deterministicMNManager->GetListAtChainTip();
auto dmn = mnList.GetValidMNByService(pnode->addr);
if (dmn == nullptr) {
// This is definitely not a masternode
return false;
}
assumedProTxHash = dmn->proTxHash;
}
LOCK(cs_vPendingMasternodes); LOCK(cs_vPendingMasternodes);
for (const auto& p : masternodeQuorumNodes) { for (const auto& p : masternodeQuorumNodes) {
for (const auto& p2 : p.second) { if (!pnode->verifiedProRegTxHash.IsNull()) {
if (p2.first == (CService)pnode->addr) { if (p.second.count(pnode->verifiedProRegTxHash)) {
return true; return true;
} }
if (!pnode->verifiedProRegTxHash.IsNull() && p2.second == pnode->verifiedProRegTxHash) { } else if (!assumedProTxHash.IsNull()) {
if (p.second.count(assumedProTxHash)) {
return true; return true;
} }
} }
@ -3247,20 +3262,6 @@ bool CConnman::ForNode(NodeId id, std::function<bool(const CNode* pnode)> cond,
return found != nullptr && cond(found) && func(found); return found != nullptr && cond(found) && func(found);
} }
void CConnman::ForEachQuorumMember(Consensus::LLMQType llmqType, const uint256& quorumHash, std::function<bool(CNode* pnode)> func) const
{
LOCK2(cs_vNodes, cs_vPendingMasternodes);
auto it = masternodeQuorumNodes.find(std::make_pair(llmqType, quorumHash));
if (it == masternodeQuorumNodes.end()) {
return;
}
for (auto&& pnode : vNodes) {
if(it->second.count(pnode->addr)) {
func(pnode);
}
}
}
bool CConnman::IsMasternodeOrDisconnectRequested(const CService& addr) { bool CConnman::IsMasternodeOrDisconnectRequested(const CService& addr) {
return ForNode(addr, AllNodes, [](CNode* pnode){ return ForNode(addr, AllNodes, [](CNode* pnode){
return pnode->fMasternode || pnode->fDisconnect; return pnode->fMasternode || pnode->fDisconnect;

View File

@ -306,8 +306,6 @@ public:
ForEachNodeThen(FullyConnectedOnly, pre, post); ForEachNodeThen(FullyConnectedOnly, pre, post);
} }
void ForEachQuorumMember(Consensus::LLMQType llmqType, const uint256& quorumHash, std::function<bool(CNode* pnode)> func) const;
std::vector<CNode*> CopyNodeVector(std::function<bool(const CNode* pnode)> cond); std::vector<CNode*> CopyNodeVector(std::function<bool(const CNode* pnode)> cond);
std::vector<CNode*> CopyNodeVector(); std::vector<CNode*> CopyNodeVector();
void ReleaseNodeVector(const std::vector<CNode*>& vecNodes); void ReleaseNodeVector(const std::vector<CNode*>& vecNodes);
@ -358,7 +356,7 @@ public:
std::vector<AddedNodeInfo> GetAddedNodeInfo(); std::vector<AddedNodeInfo> GetAddedNodeInfo();
bool AddPendingMasternode(const CService& addr); bool AddPendingMasternode(const CService& addr);
bool AddMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::map<CService, uint256>& addresses); bool AddMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash, const std::set<uint256>& proTxHashes);
bool HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash); bool HasMasternodeQuorumNodes(Consensus::LLMQType llmqType, const uint256& quorumHash);
std::set<uint256> GetMasternodeQuorums(Consensus::LLMQType llmqType); std::set<uint256> GetMasternodeQuorums(Consensus::LLMQType llmqType);
// also returns QWATCH nodes // also returns QWATCH nodes
@ -494,7 +492,7 @@ private:
std::vector<std::string> vAddedNodes; std::vector<std::string> vAddedNodes;
CCriticalSection cs_vAddedNodes; CCriticalSection cs_vAddedNodes;
std::vector<CService> vPendingMasternodes; std::vector<CService> vPendingMasternodes;
std::map<std::pair<Consensus::LLMQType, uint256>, std::map<CService, uint256>> masternodeQuorumNodes; // protected by cs_vPendingMasternodes std::map<std::pair<Consensus::LLMQType, uint256>, std::set<uint256>> masternodeQuorumNodes; // protected by cs_vPendingMasternodes
mutable CCriticalSection cs_vPendingMasternodes; mutable CCriticalSection cs_vPendingMasternodes;
std::vector<CNode*> vNodes; std::vector<CNode*> vNodes;
std::list<CNode*> vNodesDisconnected; std::list<CNode*> vNodesDisconnected;