15 std::vector<unsigned char> vchKey = GetKey();
16 ss1 << nKey << vchKey;
17 uint64_t hash1 =
Hash(ss1.
begin(), ss1.
end()).GetLow64();
20 std::vector<unsigned char> vchGroupKey = GetGroup();
22 uint64_t hash2 =
Hash(ss2.
begin(), ss2.
end()).GetLow64();
29 std::vector<unsigned char> vchGroupKey = GetGroup();
30 std::vector<unsigned char> vchSourceGroupKey = src.
GetGroup();
31 ss1 << nKey << vchGroupKey << vchSourceGroupKey;
32 uint64_t hash1 =
Hash(ss1.
begin(), ss1.
end()).GetLow64();
36 uint64_t hash2 =
Hash(ss2.
begin(), ss2.
end()).GetLow64();
42 if (nLastTry && nLastTry >= nNow-60)
45 if (nTime > nNow + 10*60)
64 int64_t nSinceLastSeen = nNow - nTime;
65 int64_t nSinceLastTry = nNow - nLastTry;
67 if (nSinceLastSeen < 0) nSinceLastSeen = 0;
68 if (nSinceLastTry < 0) nSinceLastTry = 0;
70 fChance *= 600.0 / (600.0 + nSinceLastSeen);
73 if (nSinceLastTry < 60*10)
77 for (
int n=0; n<nAttempts; n++)
85 std::map<CNetAddr, int>::iterator it = mapAddr.find(addr);
86 if (it == mapAddr.end())
90 std::map<int, CAddrInfo>::iterator it2 = mapInfo.find((*it).second);
91 if (it2 != mapInfo.end())
92 return &(*it2).second;
99 mapInfo[nId] =
CAddrInfo(addr, addrSource);
101 mapInfo[nId].nRandomPos = vRandom.size();
102 vRandom.push_back(nId);
105 return &mapInfo[nId];
110 if (nRndPos1 == nRndPos2)
113 assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
115 int nId1 = vRandom[nRndPos1];
116 int nId2 = vRandom[nRndPos2];
118 assert(mapInfo.count(nId1) == 1);
119 assert(mapInfo.count(nId2) == 1);
121 mapInfo[nId1].nRandomPos = nRndPos2;
122 mapInfo[nId2].nRandomPos = nRndPos1;
124 vRandom[nRndPos1] = nId2;
125 vRandom[nRndPos2] = nId1;
130 std::vector<int> &vTried = vvTried[nKBucket];
134 int64_t nOldest = -1;
139 int nTemp = vTried[nPos];
140 vTried[nPos] = vTried[i];
142 assert(nOldest == -1 || mapInfo.count(nTemp) == 1);
143 if (nOldest == -1 || mapInfo[nTemp].nLastSuccess < mapInfo[nOldest].nLastSuccess) {
154 assert(nUBucket >= 0 && (
unsigned int)nUBucket < vvNew.size());
155 std::set<int> &vNew = vvNew[nUBucket];
158 for (std::set<int>::iterator it = vNew.begin(); it != vNew.end(); it++)
160 assert(mapInfo.count(*it));
162 if (info.IsTerrible())
164 if (--info.nRefCount == 0)
166 SwapRandom(info.nRandomPos, vRandom.size()-1);
181 for (std::set<int>::iterator it = vNew.begin(); it != vNew.end(); it++)
183 if (nI == n[0] || nI == n[1] || nI == n[2] || nI == n[3])
185 assert(nOldest == -1 || mapInfo.count(*it) == 1);
186 if (nOldest == -1 || mapInfo[*it].nTime < mapInfo[nOldest].nTime)
191 assert(mapInfo.count(nOldest) == 1);
193 if (--info.nRefCount == 0)
195 SwapRandom(info.nRandomPos, vRandom.size()-1);
198 mapInfo.erase(nOldest);
208 assert(vvNew[nOrigin].count(nId) == 1);
211 for (std::vector<std::set<int> >::iterator it = vvNew.begin(); it != vvNew.end(); it++)
213 if ((*it).erase(nId))
222 std::vector<int> &vTried = vvTried[nKBucket];
227 vTried.push_back(nId);
234 int nPos = SelectTried(nKBucket);
237 assert(mapInfo.count(vTried[nPos]) == 1);
238 int nUBucket = mapInfo[vTried[nPos]].GetNewBucket(nKey);
239 std::set<int> &vNew = vvNew[nUBucket];
242 CAddrInfo& infoOld = mapInfo[vTried[nPos]];
251 vNew.insert(vTried[nPos]);
254 vvNew[nOrigin].insert(vTried[nPos]);
292 for (
unsigned int n = 0; n < vvNew.size(); n++)
294 int nB = (n+nRnd) % vvNew.size();
295 std::set<int> &vNew = vvNew[nB];
305 if (nUBucket == -1)
return;
307 LogPrint(
"addrman",
"Moving %s to tried\n", addr.
ToString());
310 MakeTried(info, nId, nUBucket);
326 int64_t nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
327 if (addr.
nTime && (!pinfo->
nTime || pinfo->
nTime < addr.
nTime - nUpdateInterval - nTimePenalty))
328 pinfo->
nTime = max((int64_t)0, addr.
nTime - nTimePenalty);
349 if (nFactor > 1 && (
GetRandInt(nFactor) != 0))
352 pinfo = Create(addr, source, &nId);
353 pinfo->
nTime = max((int64_t)0, (int64_t)pinfo->
nTime - nTimePenalty);
359 std::set<int> &vNew = vvNew[nUBucket];
360 if (!vNew.count(nId))
365 vvNew[nUBucket].insert(nId);
394 double nCorTried = sqrt(nTried) * (100.0 - nUnkBias);
395 double nCorNew = sqrt(nNew) * nUnkBias;
396 if ((nCorTried + nCorNew)*
GetRandInt(1<<30)/(1<<30) < nCorTried)
399 double fChanceFactor = 1.0;
403 std::vector<int> &vTried = vvTried[nKBucket];
404 if (vTried.size() == 0)
continue;
406 assert(mapInfo.count(vTried[nPos]) == 1);
408 if (
GetRandInt(1<<30) < fChanceFactor*info.GetChance()*(1<<30))
410 fChanceFactor *= 1.2;
414 double fChanceFactor = 1.0;
418 std::set<int> &vNew = vvNew[nUBucket];
419 if (vNew.size() == 0)
continue;
421 std::set<int>::iterator it = vNew.begin();
424 assert(mapInfo.count(*it) == 1);
426 if (
GetRandInt(1<<30) < fChanceFactor*info.GetChance()*(1<<30))
428 fChanceFactor *= 1.2;
434 int CAddrMan::Check_()
436 std::set<int> setTried;
437 std::map<int, int> mapNew;
439 if (vRandom.size() != nTried + nNew)
return -7;
441 for (std::map<int, CAddrInfo>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++)
456 if (mapAddr[info] != n)
return -5;
462 if (setTried.size() != nTried)
return -9;
463 if (mapNew.size() != nNew)
return -10;
465 for (
int n=0; n<vvTried.size(); n++)
467 std::vector<int> &vTried = vvTried[n];
468 for (std::vector<int>::iterator it = vTried.begin(); it != vTried.end(); it++)
470 if (!setTried.count(*it))
return -11;
475 for (
int n=0; n<vvNew.size(); n++)
477 std::set<int> &vNew = vvNew[n];
478 for (std::set<int>::iterator it = vNew.begin(); it != vNew.end(); it++)
480 if (!mapNew.count(*it))
return -12;
481 if (--mapNew[*it] == 0)
486 if (setTried.size())
return -13;
487 if (mapNew.size())
return -15;
500 for (
unsigned int n = 0; n < vRandom.size(); n++)
502 if (vAddr.size() >= nNodes)
505 int nRndPos =
GetRandInt(vRandom.size() - n) + n;
506 SwapRandom(n, nRndPos);
507 assert(mapInfo.count(vRandom[n]) == 1);
509 const CAddrInfo& ai = mapInfo[vRandom[n]];
510 if (!ai.IsTerrible())
530 int64_t nUpdateInterval = 20 * 60;
531 if (nTime - info.
nTime > nUpdateInterval)
int GetNewBucket(const std::vector< unsigned char > &nKey, const CNetAddr &src) const
const_iterator begin() const
CAddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=NULL)
#define ADDRMAN_TRIED_BUCKETS_PER_GROUP
#define ADDRMAN_MIN_FAIL_DAYS
CAddress Select_(int nUnkBias)
#define ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT
Double ended buffer combining vector and stream-like interfaces.
#define ADDRMAN_TRIED_BUCKET_COUNT
double GetChance(int64_t nNow=GetAdjustedTime()) const
CAddrInfo * Find(const CNetAddr &addr, int *pnId=NULL)
Extended statistics about a CAddress.
int64_t GetAdjustedTime()
int SelectTried(int nKBucket)
A combination of a network address (CNetAddr) and a (TCP) port.
void MakeTried(CAddrInfo &info, int nId, int nOrigin)
A CService with information about it as peer.
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2)
std::string ToString() const
#define ADDRMAN_NEW_BUCKET_SIZE
uint256 Hash(const T1 pbegin, const T1 pend)
int ShrinkNew(int nUBucket)
std::vector< unsigned char > GetGroup() const
#define ADDRMAN_TRIED_BUCKET_SIZE
IP address (IPv6, or IPv4 using mapped IPv6 range (::FFFF:0:0/96))
#define ADDRMAN_GETADDR_MAX
void Good_(const CService &addr, int64_t nTime)
#define ADDRMAN_NEW_BUCKET_COUNT
int GetTriedBucket(const std::vector< unsigned char > &nKey) const
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty)
void Connected_(const CService &addr, int64_t nTime)
void GetAddr_(std::vector< CAddress > &vAddr)
#define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP
#define ADDRMAN_HORIZON_DAYS
void Attempt_(const CService &addr, int64_t nTime)
#define ADDRMAN_MAX_FAILURES
bool IsTerrible(int64_t nNow=GetAdjustedTime()) const
#define ADDRMAN_GETADDR_MAX_PCT
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS
const_iterator end() const