1 // Copyright (c) 2012 Pieter Wuille
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
11 int CAddrInfo::GetTriedBucket(const std::vector<unsigned char> &nKey) const
13 CDataStream ss1(SER_GETHASH, 0);
14 std::vector<unsigned char> vchKey = GetKey();
15 ss1 << nKey << vchKey;
16 uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64();
18 CDataStream ss2(SER_GETHASH, 0);
19 std::vector<unsigned char> vchGroupKey = GetGroup();
20 ss2 << nKey << vchGroupKey << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP);
21 uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64();
22 return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
25 int CAddrInfo::GetNewBucket(const std::vector<unsigned char> &nKey, const CNetAddr& src) const
27 CDataStream ss1(SER_GETHASH, 0);
28 std::vector<unsigned char> vchGroupKey = GetGroup();
29 std::vector<unsigned char> vchSourceGroupKey = src.GetGroup();
30 ss1 << nKey << vchGroupKey << vchSourceGroupKey;
31 uint64_t hash1 = Hash(ss1.begin(), ss1.end()).Get64();
33 CDataStream ss2(SER_GETHASH, 0);
34 ss2 << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP);
35 uint64_t hash2 = Hash(ss2.begin(), ss2.end()).Get64();
36 return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
39 bool CAddrInfo::IsTerrible(int64_t nNow) const
41 if (nLastTry && nLastTry >= nNow-60) // never remove things tried the last minute
44 if (nTime > nNow + 10*60) // came in a flying DeLorean
47 if (nTime==0 || nNow-nTime > ADDRMAN_HORIZON_DAYS*nOneDay) // not seen in over a month
50 if (nLastSuccess==0 && nAttempts>=ADDRMAN_RETRIES) // tried three times and never a success
53 if (nNow-nLastSuccess > ADDRMAN_MIN_FAIL_DAYS*nOneDay && nAttempts>=ADDRMAN_MAX_FAILURES) // 10 successive failures in the last week
59 double CAddrInfo::GetChance(int64_t nNow) const
63 int64_t nSinceLastSeen = nNow - nTime;
64 int64_t nSinceLastTry = nNow - nLastTry;
66 if (nSinceLastSeen < 0) nSinceLastSeen = 0;
67 if (nSinceLastTry < 0) nSinceLastTry = 0;
69 fChance *= 600.0 / (600.0 + nSinceLastSeen);
71 // deprioritize very recent attempts away
72 if (nSinceLastTry < 60*10)
75 // deprioritize 50% after each failed attempt
76 for (int n=0; n<nAttempts; n++)
82 CAddrInfo* CAddrMan::Find(const CNetAddr& addr, int *pnId)
84 std::map<CNetAddr, int>::iterator it = mapAddr.find(addr);
85 if (it == mapAddr.end())
89 std::map<int, CAddrInfo>::iterator it2 = mapInfo.find((*it).second);
90 if (it2 != mapInfo.end())
91 return &(*it2).second;
95 CAddrInfo* CAddrMan::Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId)
98 mapInfo[nId] = CAddrInfo(addr, addrSource);
100 mapInfo[nId].nRandomPos = vRandom.size();
101 vRandom.push_back(nId);
104 return &mapInfo[nId];
107 void CAddrMan::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2)
109 if (nRndPos1 == nRndPos2)
112 assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
114 int nId1 = vRandom[nRndPos1];
115 int nId2 = vRandom[nRndPos2];
117 assert(mapInfo.count(nId1) == 1);
118 assert(mapInfo.count(nId2) == 1);
120 mapInfo[nId1].nRandomPos = nRndPos2;
121 mapInfo[nId2].nRandomPos = nRndPos1;
123 vRandom[nRndPos1] = nId2;
124 vRandom[nRndPos2] = nId1;
127 int CAddrMan::SelectTried(int nKBucket)
129 std::vector<int> &vTried = vvTried[nKBucket];
131 // random shuffle the first few elements (using the entire list)
132 // find the least recently tried among them
133 int64_t nOldest = -1;
135 for (unsigned int i = 0; i < ADDRMAN_TRIED_ENTRIES_INSPECT_ON_EVICT && i < vTried.size(); i++)
137 int nPos = GetRandInt(vTried.size() - i) + i;
138 int nTemp = vTried[nPos];
139 vTried[nPos] = vTried[i];
141 assert(nOldest == -1 || mapInfo.count(nTemp) == 1);
142 if (nOldest == -1 || mapInfo[nTemp].nLastSuccess < mapInfo[nOldest].nLastSuccess) {
151 int CAddrMan::ShrinkNew(int nUBucket)
153 assert(nUBucket >= 0 && (unsigned int)nUBucket < vvNew.size());
154 std::set<int> &vNew = vvNew[nUBucket];
156 // first look for deletable items
157 for (std::set<int>::iterator it = vNew.begin(); it != vNew.end(); it++)
159 assert(mapInfo.count(*it));
160 CAddrInfo &info = mapInfo[*it];
161 if (info.IsTerrible())
163 if (--info.nRefCount == 0)
165 SwapRandom(info.nRandomPos, vRandom.size()-1);
176 // otherwise, select four randomly, and pick the oldest of those to replace
177 int n[4] = {GetRandInt(vNew.size()), GetRandInt(vNew.size()), GetRandInt(vNew.size()), GetRandInt(vNew.size())};
180 for (std::set<int>::iterator it = vNew.begin(); it != vNew.end(); it++)
182 if (nI == n[0] || nI == n[1] || nI == n[2] || nI == n[3])
184 assert(nOldest == -1 || mapInfo.count(*it) == 1);
185 if (nOldest == -1 || mapInfo[*it].nTime < mapInfo[nOldest].nTime)
190 assert(mapInfo.count(nOldest) == 1);
191 CAddrInfo &info = mapInfo[nOldest];
192 if (--info.nRefCount == 0)
194 SwapRandom(info.nRandomPos, vRandom.size()-1);
197 mapInfo.erase(nOldest);
205 void CAddrMan::MakeTried(CAddrInfo& info, int nId, int nOrigin)
207 assert(vvNew[nOrigin].count(nId) == 1);
209 // remove the entry from all new buckets
210 for (std::vector<std::set<int> >::iterator it = vvNew.begin(); it != vvNew.end(); it++)
212 if ((*it).erase(nId))
217 assert(info.nRefCount == 0);
219 // what tried bucket to move the entry to
220 int nKBucket = info.GetTriedBucket(nKey);
221 std::vector<int> &vTried = vvTried[nKBucket];
223 // first check whether there is place to just add it
224 if (vTried.size() < ADDRMAN_TRIED_BUCKET_SIZE)
226 vTried.push_back(nId);
228 info.fInTried = true;
232 // otherwise, find an item to evict
233 int nPos = SelectTried(nKBucket);
235 // find which new bucket it belongs to
236 assert(mapInfo.count(vTried[nPos]) == 1);
237 int nUBucket = mapInfo[vTried[nPos]].GetNewBucket(nKey);
238 std::set<int> &vNew = vvNew[nUBucket];
240 // remove the to-be-replaced tried entry from the tried set
241 CAddrInfo& infoOld = mapInfo[vTried[nPos]];
242 infoOld.fInTried = false;
243 infoOld.nRefCount = 1;
244 // do not update nTried, as we are going to move something else there immediately
246 // check whether there is place in that one,
247 if (vNew.size() < ADDRMAN_NEW_BUCKET_SIZE)
249 // if so, move it back there
250 vNew.insert(vTried[nPos]);
252 // otherwise, move it to the new bucket nId came from (there is certainly place there)
253 vvNew[nOrigin].insert(vTried[nPos]);
258 // we just overwrote an entry in vTried; no need to update nTried
259 info.fInTried = true;
263 void CAddrMan::Good_(const CService &addr, int64_t nTime)
265 // printf("Good: addr=%s\n", addr.ToString().c_str());
268 CAddrInfo *pinfo = Find(addr, &nId);
270 // if not found, bail out
274 CAddrInfo &info = *pinfo;
276 // check whether we are talking about the exact same CService (including same port)
281 info.nLastSuccess = nTime;
282 info.nLastTry = nTime;
286 // if it is already in the tried set, don't do anything else
290 // find a bucket it is in now
291 int nRnd = GetRandInt(vvNew.size());
293 for (unsigned int n = 0; n < vvNew.size(); n++)
295 int nB = (n+nRnd) % vvNew.size();
296 std::set<int> &vNew = vvNew[nB];
304 // if no bucket is found, something bad happened;
305 // TODO: maybe re-add the node, but for now, just bail out
306 if (nUBucket == -1) return;
308 printf("Moving %s to tried\n", addr.ToString().c_str());
310 // move nId to the tried tables
311 MakeTried(info, nId, nUBucket);
314 bool CAddrMan::Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty)
316 if (!addr.IsRoutable())
321 CAddrInfo *pinfo = Find(addr, &nId);
325 // periodically update nTime
326 bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < nOneDay);
327 int64_t nUpdateInterval = (fCurrentlyOnline ? nOneHour : nOneDay);
328 if (addr.nTime && (!pinfo->nTime || pinfo->nTime < addr.nTime - nUpdateInterval - nTimePenalty))
329 pinfo->nTime = max((int64_t)0, addr.nTime - nTimePenalty);
332 pinfo->nServices |= addr.nServices;
334 // do not update if no new information is present
335 if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
338 // do not update if the entry was already in the "tried" table
342 // do not update if the max reference count is reached
343 if (pinfo->nRefCount == ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
346 // stochastic test: previous nRefCount == N: 2^N times harder to increase it
348 for (int n=0; n<pinfo->nRefCount; n++)
350 if (nFactor > 1 && (GetRandInt(nFactor) != 0))
353 pinfo = Create(addr, source, &nId);
354 pinfo->nTime = max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
355 // printf("Added %s [nTime=%fhr]\n", pinfo->ToString().c_str(), (GetAdjustedTime() - pinfo->nTime) / 3600.0);
360 int nUBucket = pinfo->GetNewBucket(nKey, source);
361 std::set<int> &vNew = vvNew[nUBucket];
362 if (!vNew.count(nId))
365 if (vNew.size() == ADDRMAN_NEW_BUCKET_SIZE)
367 vvNew[nUBucket].insert(nId);
372 void CAddrMan::Attempt_(const CService &addr, int64_t nTime)
374 CAddrInfo *pinfo = Find(addr);
376 // if not found, bail out
380 CAddrInfo &info = *pinfo;
382 // check whether we are talking about the exact same CService (including same port)
387 info.nLastTry = nTime;
391 CAddress CAddrMan::Select_(int nUnkBias)
396 double nCorTried = sqrt(nTried) * (100.0 - nUnkBias);
397 double nCorNew = sqrt(nNew) * nUnkBias;
398 if ((nCorTried + nCorNew)*GetRandInt(1<<30)/(1<<30) < nCorTried)
401 double fChanceFactor = 1.0;
404 int nKBucket = GetRandInt(vvTried.size());
405 std::vector<int> &vTried = vvTried[nKBucket];
406 if (vTried.size() == 0) continue;
407 int nPos = GetRandInt(vTried.size());
408 assert(mapInfo.count(vTried[nPos]) == 1);
409 CAddrInfo &info = mapInfo[vTried[nPos]];
410 if (GetRandInt(1<<30) < fChanceFactor*info.GetChance()*(1<<30))
412 fChanceFactor *= 1.2;
416 double fChanceFactor = 1.0;
419 int nUBucket = GetRandInt(vvNew.size());
420 std::set<int> &vNew = vvNew[nUBucket];
421 if (vNew.size() == 0) continue;
422 int nPos = GetRandInt(vNew.size());
423 std::set<int>::iterator it = vNew.begin();
426 assert(mapInfo.count(*it) == 1);
427 CAddrInfo &info = mapInfo[*it];
428 if (GetRandInt(1<<30) < fChanceFactor*info.GetChance()*(1<<30))
430 fChanceFactor *= 1.2;
436 int CAddrMan::Check_()
438 std::set<int> setTried;
439 std::map<int, int> mapNew;
441 if (vRandom.size() != nTried + nNew) return -7;
443 for (std::map<int, CAddrInfo>::iterator it = mapInfo.begin(); it != mapInfo.end(); it++)
446 CAddrInfo &info = (*it).second;
450 if (!info.nLastSuccess) return -1;
451 if (info.nRefCount) return -2;
454 if (info.nRefCount < 0 || info.nRefCount > ADDRMAN_NEW_BUCKETS_PER_ADDRESS) return -3;
455 if (!info.nRefCount) return -4;
456 mapNew[n] = info.nRefCount;
458 if (mapAddr[info] != n) return -5;
459 if (info.nRandomPos<0 || info.nRandomPos>=vRandom.size() || vRandom[info.nRandomPos] != n) return -14;
460 if (info.nLastTry < 0) return -6;
461 if (info.nLastSuccess < 0) return -8;
464 if (setTried.size() != nTried) return -9;
465 if (mapNew.size() != nNew) return -10;
467 for (int n=0; n<vvTried.size(); n++)
469 std::vector<int> &vTried = vvTried[n];
470 for (std::vector<int>::iterator it = vTried.begin(); it != vTried.end(); it++)
472 if (!setTried.count(*it)) return -11;
477 for (int n=0; n<vvNew.size(); n++)
479 std::set<int> &vNew = vvNew[n];
480 for (std::set<int>::iterator it = vNew.begin(); it != vNew.end(); it++)
482 if (!mapNew.count(*it)) return -12;
483 if (--mapNew[*it] == 0)
488 if (setTried.size()) return -13;
489 if (mapNew.size()) return -15;
495 void CAddrMan::GetAddr_(std::vector<CAddress> &vAddr)
497 size_t nNodes = ADDRMAN_GETADDR_MAX_PCT*vRandom.size()/100;
498 if (nNodes > ADDRMAN_GETADDR_MAX)
499 nNodes = ADDRMAN_GETADDR_MAX;
501 // perform a random shuffle over the first nNodes elements of vRandom (selecting from all)
502 for (unsigned int n = 0; n<nNodes; n++)
504 int nRndPos = GetRandInt(vRandom.size() - n) + n;
505 SwapRandom(n, nRndPos);
506 assert(mapInfo.count(vRandom[n]) == 1);
507 vAddr.push_back(mapInfo[vRandom[n]]);
511 void CAddrMan::GetOnlineAddr_(std::vector<CAddrInfo> &vAddr)
513 for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin(); it != mapInfo.end(); it++)
515 CAddrInfo addr = it->second;
516 bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < nOneDay);
517 if (fCurrentlyOnline)
518 vAddr.push_back(addr);
522 void CAddrMan::Connected_(const CService &addr, int64_t nTime)
524 CAddrInfo *pinfo = Find(addr);
526 // if not found, bail out
530 CAddrInfo &info = *pinfo;
532 // check whether we are talking about the exact same CService (including same port)
537 int64_t nUpdateInterval = 20 * 60;
538 if (nTime - info.nTime > nUpdateInterval)