Skip to content

Commit cb1716a

Browse files
committed
Merge #10441: net: only enforce expected services for half of outgoing connections
b6fbfc2 net: only enforce the services required to connect (Cory Fields) Tree-SHA512: 88943bff63213a734f3c96c45760cadaeb9ba18287c8a20c279851ebaf058a334c969028fb2180f155508e3eea4b838147382e4f2b655e7a9aa098eadc81d53e
2 parents b6b150b + b6fbfc2 commit cb1716a

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/net.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,11 +1721,17 @@ void CConnman::ThreadOpenConnections()
17211721
// Only connect out to one peer per network group (/16 for IPv4).
17221722
// Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
17231723
int nOutbound = 0;
1724+
int nOutboundRelevant = 0;
17241725
std::set<std::vector<unsigned char> > setConnected;
17251726
{
17261727
LOCK(cs_vNodes);
17271728
BOOST_FOREACH(CNode* pnode, vNodes) {
17281729
if (!pnode->fInbound && !pnode->fAddnode) {
1730+
1731+
// Count the peers that have all relevant services
1732+
if (pnode->fSuccessfullyConnected && !pnode->fFeeler && ((pnode->nServices & nRelevantServices) == nRelevantServices)) {
1733+
nOutboundRelevant++;
1734+
}
17291735
// Netgroups for inbound and addnode peers are not excluded because our goal here
17301736
// is to not use multiple of our limited outbound slots on a single netgroup
17311737
// but inbound and addnode peers do not use our outbound slots. Inbound peers
@@ -1789,14 +1795,27 @@ void CConnman::ThreadOpenConnections()
17891795
continue;
17901796

17911797
// only consider nodes missing relevant services after 40 failed attempts and only if less than half the outbound are up.
1792-
if ((addr.nServices & nRelevantServices) != nRelevantServices && (nTries < 40 || nOutbound >= (nMaxOutbound >> 1)))
1798+
ServiceFlags nRequiredServices = nRelevantServices;
1799+
if (nTries >= 40 && nOutbound < (nMaxOutbound >> 1)) {
1800+
nRequiredServices = REQUIRED_SERVICES;
1801+
}
1802+
1803+
if ((addr.nServices & nRequiredServices) != nRequiredServices) {
17931804
continue;
1805+
}
17941806

17951807
// do not allow non-default ports, unless after 50 invalid addresses selected already
17961808
if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
17971809
continue;
17981810

17991811
addrConnect = addr;
1812+
1813+
// regardless of the services assumed to be available, only require the minimum if half or more outbound have relevant services
1814+
if (nOutboundRelevant >= (nMaxOutbound >> 1)) {
1815+
addrConnect.nServices = REQUIRED_SERVICES;
1816+
} else {
1817+
addrConnect.nServices = nRequiredServices;
1818+
}
18001819
break;
18011820
}
18021821

0 commit comments

Comments
 (0)