From 0adef2cf7a2ba0ec64924e84bf2126cf20fa89e6 Mon Sep 17 00:00:00 2001 From: Alexander Block Date: Tue, 17 Mar 2020 08:36:45 +0100 Subject: [PATCH] Fix ThreadOpenMasternodeConnections to not drop pending MN connections The way it was implemented caused vPendingMasternodes entries to be popped but not necessarily connected to when at the same time quorum connections were pending. --- src/net.cpp | 54 +++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/net.cpp b/src/net.cpp index 09a729c0ca..59565ba5ca 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -2130,40 +2130,42 @@ void CConnman::ThreadOpenMasternodeConnections() { // don't hold lock while calling OpenMasternodeConnection as cs_main is locked deep inside LOCK2(cs_vNodes, cs_vPendingMasternodes); - std::vector pending; - for (const auto& group : masternodeQuorumNodes) { - for (const auto& proRegTxHash : group.second) { - auto dmn = mnList.GetMN(proRegTxHash); - if (!dmn) { - continue; - } - const auto& addr2 = dmn->pdmnState->addr; - if (!connectedNodes.count(addr2) && !IsMasternodeOrDisconnectRequested(addr2) && !connectedProRegTxHashes.count(proRegTxHash)) { - auto addrInfo = addrman.GetAddressInfo(addr2); - // back off trying connecting to an address if we already tried recently - if (addrInfo.IsValid() && nANow - addrInfo.nLastTry < 60) { - continue; - } - pending.emplace_back(addr2); - } - } - } - if (!vPendingMasternodes.empty()) { auto addr2 = vPendingMasternodes.front(); vPendingMasternodes.erase(vPendingMasternodes.begin()); if (!connectedNodes.count(addr2) && !IsMasternodeOrDisconnectRequested(addr2)) { - pending.emplace_back(addr2); + addr = addr2; } } - if (pending.empty()) { - // nothing to do, keep waiting - continue; - } + if (addr == CService()) { + std::vector pending; + for (const auto& group : masternodeQuorumNodes) { + for (const auto& proRegTxHash : group.second) { + auto dmn = mnList.GetMN(proRegTxHash); + if (!dmn) { + continue; + } + const auto& addr2 = dmn->pdmnState->addr; + if (!connectedNodes.count(addr2) && !IsMasternodeOrDisconnectRequested(addr2) && !connectedProRegTxHashes.count(proRegTxHash)) { + auto addrInfo = addrman.GetAddressInfo(addr2); + // back off trying connecting to an address if we already tried recently + if (addrInfo.IsValid() && nANow - addrInfo.nLastTry < 60) { + continue; + } + pending.emplace_back(addr2); + } + } + } - std::random_shuffle(pending.begin(), pending.end()); - addr = pending.front(); + if (!pending.empty()) { + addr = pending[GetRandInt(pending.size())]; + } + } + } + + if (addr == CService()) { + continue; } OpenMasternodeConnection(CAddress(addr, NODE_NETWORK));