Skip to content

Commit 67c91f8

Browse files
committed
Merge #8065: Addrman offline attempts
6182d10 Do not increment nAttempts by more than one for every Good connection. (Gregory Maxwell) c769c4a Avoid counting failed connect attempts when probably offline. (Gregory Maxwell)
2 parents 2156fa2 + 6182d10 commit 67c91f8

File tree

5 files changed

+31
-17
lines changed

5 files changed

+31
-17
lines changed

src/addrman.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ void CAddrMan::MakeTried(CAddrInfo& info, int nId)
197197
void CAddrMan::Good_(const CService& addr, int64_t nTime)
198198
{
199199
int nId;
200+
201+
nLastGood = nTime;
202+
200203
CAddrInfo* pinfo = Find(addr, &nId);
201204

202205
// if not found, bail out
@@ -311,7 +314,7 @@ bool CAddrMan::Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimeP
311314
return fNew;
312315
}
313316

314-
void CAddrMan::Attempt_(const CService& addr, int64_t nTime)
317+
void CAddrMan::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
315318
{
316319
CAddrInfo* pinfo = Find(addr);
317320

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

328331
// update info
329332
info.nLastTry = nTime;
330-
info.nAttempts++;
333+
if (fCountFailure && info.nLastCountAttempt < nLastGood) {
334+
info.nLastCountAttempt = nTime;
335+
info.nAttempts++;
336+
}
331337
}
332338

333339
CAddrInfo CAddrMan::Select_(bool newOnly)

src/addrman.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class CAddrInfo : public CAddress
2929
//! last try whatsoever by us (memory only)
3030
int64_t nLastTry;
3131

32+
//! last counted attempt (memory only)
33+
int64_t nLastCountAttempt;
34+
3235
private:
3336
//! where knowledge about this address first came from
3437
CNetAddr source;
@@ -66,6 +69,7 @@ class CAddrInfo : public CAddress
6669
{
6770
nLastSuccess = 0;
6871
nLastTry = 0;
72+
nLastCountAttempt = 0;
6973
nAttempts = 0;
7074
nRefCount = 0;
7175
fInTried = false;
@@ -200,6 +204,9 @@ class CAddrMan
200204
//! list of "new" buckets
201205
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE];
202206

207+
//! last time Good was called (memory only)
208+
int64_t nLastGood;
209+
203210
protected:
204211
//! secret key to randomize bucket select with
205212
uint256 nKey;
@@ -230,7 +237,7 @@ class CAddrMan
230237
bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty);
231238

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

235242
//! Select an address to connect to, if newOnly is set to true, only the new table is selected from.
236243
CAddrInfo Select_(bool newOnly);
@@ -458,6 +465,7 @@ class CAddrMan
458465
nIdCount = 0;
459466
nTried = 0;
460467
nNew = 0;
468+
nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
461469
}
462470

463471
CAddrMan()
@@ -532,12 +540,12 @@ class CAddrMan
532540
}
533541

534542
//! Mark an entry as connection attempted to.
535-
void Attempt(const CService &addr, int64_t nTime = GetAdjustedTime())
543+
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
536544
{
537545
{
538546
LOCK(cs);
539547
Check();
540-
Attempt_(addr, nTime);
548+
Attempt_(addr, fCountFailure, nTime);
541549
Check();
542550
}
543551
}

src/net.cpp

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

368-
CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
368+
CNode* ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
369369
{
370370
if (pszDest == NULL) {
371371
if (IsLocal(addrConnect))
@@ -397,7 +397,7 @@ CNode* ConnectNode(CAddress addrConnect, const char *pszDest)
397397
return NULL;
398398
}
399399

400-
addrman.Attempt(addrConnect);
400+
addrman.Attempt(addrConnect, fCountFailure);
401401

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

420420
return NULL;
@@ -1531,7 +1531,7 @@ void static ProcessOneShot()
15311531
CAddress addr;
15321532
CSemaphoreGrant grant(*semOutbound, true);
15331533
if (grant) {
1534-
if (!OpenNetworkConnection(addr, &grant, strDest.c_str(), true))
1534+
if (!OpenNetworkConnection(addr, false, &grant, strDest.c_str(), true))
15351535
AddOneShot(strDest);
15361536
}
15371537
}
@@ -1547,7 +1547,7 @@ void ThreadOpenConnections()
15471547
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-connect"])
15481548
{
15491549
CAddress addr;
1550-
OpenNetworkConnection(addr, NULL, strAddr.c_str());
1550+
OpenNetworkConnection(addr, false, NULL, strAddr.c_str());
15511551
for (int i = 0; i < 10 && i < nLoop; i++)
15521552
{
15531553
MilliSleep(500);
@@ -1631,7 +1631,7 @@ void ThreadOpenConnections()
16311631
}
16321632

16331633
if (addrConnect.IsValid())
1634-
OpenNetworkConnection(addrConnect, &grant);
1634+
OpenNetworkConnection(addrConnect, (int)setConnected.size() >= std::min(nMaxConnections - 1, 2), &grant);
16351635
}
16361636
}
16371637

@@ -1653,7 +1653,7 @@ void ThreadOpenAddedConnections()
16531653
BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
16541654
CAddress addr;
16551655
CSemaphoreGrant grant(*semOutbound);
1656-
OpenNetworkConnection(addr, &grant, strAddNode.c_str());
1656+
OpenNetworkConnection(addr, false, &grant, strAddNode.c_str());
16571657
MilliSleep(500);
16581658
}
16591659
MilliSleep(120000); // Retry every 2 minutes
@@ -1692,15 +1692,15 @@ void ThreadOpenAddedConnections()
16921692
BOOST_FOREACH(std::vector<CService>& vserv, lservAddressesToAdd)
16931693
{
16941694
CSemaphoreGrant grant(*semOutbound);
1695-
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), &grant);
1695+
OpenNetworkConnection(CAddress(vserv[i % vserv.size()]), false, &grant);
16961696
MilliSleep(500);
16971697
}
16981698
MilliSleep(120000); // Retry every 2 minutes
16991699
}
17001700
}
17011701

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

1717-
CNode* pnode = ConnectNode(addrConnect, pszDest);
1717+
CNode* pnode = ConnectNode(addrConnect, pszDest, fCountFailure);
17181718
boost::this_thread::interruption_point();
17191719

17201720
if (!pnode)

src/net.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ CNode* FindNode(const CNetAddr& ip);
8484
CNode* FindNode(const CSubNet& subNet);
8585
CNode* FindNode(const std::string& addrName);
8686
CNode* FindNode(const CService& ip);
87-
bool OpenNetworkConnection(const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
87+
bool OpenNetworkConnection(const CAddress& addrConnect, bool fCountFailure, CSemaphoreGrant *grantOutbound = NULL, const char *strDest = NULL, bool fOneShot = false);
8888
void MapPort(bool fUseUPnP);
8989
unsigned short GetListenPort();
9090
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)