Skip to content

Commit

Permalink
merge bitcoin#27467: skip netgroup diversity follow-up
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Sep 11, 2024
1 parent 9c847b1 commit 2d280a7
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2460,7 +2460,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
int nOutboundBlockRelay = 0;
int nOutboundOnionRelay = 0;
int outbound_privacy_network_peers = 0;
std::set<std::vector<unsigned char>> setConnected; // netgroups of our ipv4/ipv6 outbound peers
std::set<std::vector<unsigned char>> outbound_ipv46_peer_netgroups;

if (!Params().AllowMultipleAddressesFromGroup()) {
LOCK(m_nodes_mutex);
Expand All @@ -2483,7 +2483,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
case ConnectionType::MANUAL:
case ConnectionType::OUTBOUND_FULL_RELAY:
case ConnectionType::BLOCK_RELAY:
CAddress address{pnode->addr};
const CAddress address{pnode->addr};
if (address.IsTor() || address.IsI2P() || address.IsCJDNS()) {
// Since our addrman-groups for these networks are
// random, without relation to the route we
Expand All @@ -2494,7 +2494,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
// these networks.
++outbound_privacy_network_peers;
} else {
setConnected.insert(m_netgroupman.GetGroup(address));
outbound_ipv46_peer_netgroups.insert(m_netgroupman.GetGroup(address));
}
} // no default case, so the compiler can warn about missing cases
}
Expand Down Expand Up @@ -2585,7 +2585,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
m_anchors.pop_back();
if (!addr.IsValid() || IsLocal(addr) || !IsReachable(addr) ||
!HasAllDesirableServiceFlags(addr.nServices) ||
setConnected.count(m_netgroupman.GetGroup(addr))) continue;
outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr))) continue;
addrConnect = addr;
LogPrint(BCLog::NET, "Trying to make an anchor connection to %s\n", addrConnect.ToStringAddrPort());
break;
Expand Down Expand Up @@ -2628,13 +2628,13 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
auto dmn = mnList.GetMNByService(addr);
bool isMasternode = dmn != nullptr;

// Require outbound connections, other than feelers, to be to distinct network groups
if (!fFeeler && setConnected.count(m_netgroupman.GetGroup(addr))) {
// Require outbound IPv4/IPv6 connections, other than feelers, to be to distinct network groups
if (!fFeeler && outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr))) {
break;
}

// if we selected an invalid address, restart
if (!addr.IsValid() || setConnected.count(m_netgroupman.GetGroup(addr)))
if (!addr.IsValid() || outbound_ipv46_peer_netgroups.count(m_netgroupman.GetGroup(addr)))
break;

// don't try to connect to masternodes that we already have a connection to (most likely inbound)
Expand Down Expand Up @@ -2692,8 +2692,9 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect, CDe
// Record addrman failure attempts when node has at least 2 persistent outbound connections to peers with
// different netgroups in ipv4/ipv6 networks + all peers in Tor/I2P/CJDNS networks.
// Don't record addrman failure attempts when node is offline. This can be identified since all local
// network connections(if any) belong in the same netgroup and size of setConnected would only be 1.
OpenNetworkConnection(addrConnect, (int)setConnected.size() + outbound_privacy_network_peers >= std::min(nMaxConnections - 1, 2), &grant, nullptr, conn_type);
// network connections (if any) belong in the same netgroup, and the size of `outbound_ipv46_peer_netgroups` would only be 1.
const bool count_failures{((int)outbound_ipv46_peer_netgroups.size() + outbound_privacy_network_peers) >= std::min(nMaxConnections - 1, 2)};
OpenNetworkConnection(addrConnect, count_failures, &grant, /*strDest=*/nullptr, conn_type);
}
}
}
Expand Down

0 comments on commit 2d280a7

Please sign in to comment.