Skip to content

Commit c769c4a

Browse files
committed
Avoid counting failed connect attempts when probably offline.
If a node is offline failed outbound connection attempts will crank up the addrman counter and effectively blow away our state. This change reduces the problem by only counting attempts made while the node believes it has outbound connections to at least two netgroups. Connect and addnode connections are also not counted, as there is no reason to unequally penalize them for their more frequent connections -- though there should be no real effect from this unless their addnode configureation is later removed. Wasteful repeated connection attempts while only a few connections are up are avoided via nLastTry. This is still somewhat incomplete protection because our outbound peers could be down but not timed out or might all be on 'local' networks (although the requirement for multiple netgroups helps).
1 parent f6b7df3 commit c769c4a

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

src/addrman.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
311311
return fNew;
312312
}
313313

314-
void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
314+
void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
315315
{
316316
CAddrInfo* pinfo = Find(addr);
317317

@@ -327,7 +327,7 @@ void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
327327

328328
// update info
329329
info.nLastTry = nTime;
330-
info.nAttempts++;
330+
if (fCountFailure) info.nAttempts++;
331331
}
332332

333333
CAddrInfo CAddrMan::Select_(bool newOnly)

src/addrman.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class CAddrMan
230230
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
231231

232232
//! Mark an entry as attempted to connect.
233-
void Attempt_(const CService &addr, int64_t nTime);
233+
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime);
234234

235235
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
236236
CAddrInfo Select_(bool newOnly);
@@ -532,12 +532,12 @@ class CAddrMan
532532
}
533533

534534
//! Mark an entry as connection attempted to.
535-
void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
535+
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
536536
{
537537
{
538538
LOCK(cs);
539539
Check();
540-
Attempt_(addr, nTime);
540+
Attempt_(addr, fCountFailure, nTime);
541541
Check();
542542
}
543543
}

src/net.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ CNode* FindNode(const CService& addr)
367367
return NULL;
368368
}
369369

370-
CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
370+
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
371371
{
372372
if (pszDest == NULL) {
373373
if (IsLocal(addrConnect))
@@ -399,7 +399,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
399399
return NULL;
400400
}
401401

402-
addrman.Attempt(addrConnect);
402+
addrman.Attempt(addrConnect, fCountFailure);
403403

404404
// Add node
405405
CNode* pnode = new CNode(hSocket, addrConnect, pszDest ? pszDest : "", false);
@@ -416,7 +416,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
416416
} else if (!proxyConnectionFailed) {
417417
// If connecting to the node failed, and failure is not caused by a problem connecting to
418418
// the proxy, mark this as an attempt.
419-
addrman.Attempt(addrConnect);
419+
addrman.Attempt(addrConnect, fCountFailure);
420420
}
421421

422422
return NULL;
@@ -1533,7 +1533,7 @@ void static ProcessOneShot()
15331533
CAddress addr;
15341534
CSemaphoreGrant grant(*semOutbound, true);
15351535
if (grant) {
1536-
if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
1536+
if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
15371537
AddOneShot(strDest);
15381538
}
15391539
}
@@ -1549,7 +1549,7 @@ void ThreadOpenConnections()
15491549
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
15501550
{
15511551
CAddress addr;
1552-
OpenNetworkConnection(addr, NULL, strAddr.c_str());
1552+
OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
15531553
for (int i = 0; i < 10 && i < nLoop; i++)
15541554
{
15551555
MilliSleep(500);
@@ -1633,7 +1633,7 @@ void ThreadOpenConnections()
16331633
}
16341634

16351635
if (addrConnect.IsValid())
1636-
OpenNetworkConnection(addrConnect, &grant);
1636+
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= min(nMaxConnections - 1, 2), &grant);
16371637
}
16381638
}
16391639

@@ -1655,7 +1655,7 @@ void ThreadOpenAddedConnections()
16551655
BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
16561656
CAddress addr;
16571657
CSemaphoreGrant grant(*semOutbound);
1658-
OpenNetworkConnection(addr, &grant, strAddNode.c_str());
1658+
OpenNetworkConnection(addr, false, &grant, strAddNode.c_str());
16591659
MilliSleep(500);
16601660
}
16611661
MilliSleep(120000); // Retry every 2 minutes
@@ -1694,15 +1694,15 @@ void ThreadOpenAddedConnections()
16941694
BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd)
16951695
{
16961696
CSemaphoreGrant grant(*semOutbound);
1697-
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
1697+
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), false, &grant);
16981698
MilliSleep(500);
16991699
}
17001700
MilliSleep(120000); // Retry every 2 minutes
17011701
}
17021702
}
17031703

17041704
// if successful, this moves the passed grant to the constructed node
1705-
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot)
1705+
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound, const char *pszDest, bool fOneShot)
17061706
{
17071707
//
17081708
// Initiate outbound network connection
@@ -1716,7 +1716,7 @@ bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOu
17161716
} else if (FindNode(std::string(pszDest)))
17171717
return false;
17181718

1719-
CNode* pnode = ConnectNode(addrConnect, pszDest);
1719+
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
17201720
boost::this_thread::interruption_point();
17211721

17221722
if (!pnode)

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ CNode* FindNode(const CNetAddr& ip);
8383
CNode* FindNode(const CSubNet& subNet);
8484
CNode* FindNode(const std::string& addrName);
8585
CNode* FindNode(const CService& ip);
86-
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
86+
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
8787
void MapPort(bool fUseUPnP);
8888
unsigned short GetListenPort();
8989
bool BindListenPort(const CService &bindAddr, std::string& strError, bool fWhitelisted = false);

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ UniValue addnode(const UniValue& params, bool fHelp)
219219
if (strCommand == "onetry")
220220
{
221221
CAddress addr;
222-
OpenNetworkConnection(addr, NULL, strNode.c_str());
222+
OpenNetworkConnection(addr, false, NULL, strNode.c_str());
223223
return NullUniValue;
224224
}
225225

0 commit comments

Comments
 (0)