Bitcoin Core  23.99.0
P2P Digital Currency
addrman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <addrman.h>
7 #include <addrman_impl.h>
8 
9 #include <hash.h>
10 #include <logging.h>
11 #include <logging/timer.h>
12 #include <netaddress.h>
13 #include <protocol.h>
14 #include <random.h>
15 #include <serialize.h>
16 #include <streams.h>
17 #include <timedata.h>
18 #include <tinyformat.h>
19 #include <uint256.h>
20 #include <util/check.h>
21 
22 #include <cmath>
23 #include <optional>
24 
26 static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP{8};
28 static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
30 static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
32 static constexpr int64_t ADDRMAN_HORIZON_DAYS{30};
34 static constexpr int32_t ADDRMAN_RETRIES{3};
36 static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
38 static constexpr int64_t ADDRMAN_MIN_FAIL_DAYS{7};
40 static constexpr int64_t ADDRMAN_REPLACEMENT_HOURS{4};
42 static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
44 static constexpr int64_t ADDRMAN_TEST_WINDOW{40*60}; // 40 minutes
45 
46 int AddrInfo::GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
47 {
48  uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << GetKey()).GetCheapHash();
49  uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << netgroupman.GetGroup(*this) << (hash1 % ADDRMAN_TRIED_BUCKETS_PER_GROUP)).GetCheapHash();
50  return hash2 % ADDRMAN_TRIED_BUCKET_COUNT;
51 }
52 
53 int AddrInfo::GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const
54 {
55  std::vector<unsigned char> vchSourceGroupKey = netgroupman.GetGroup(src);
56  uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << netgroupman.GetGroup(*this) << vchSourceGroupKey).GetCheapHash();
57  uint64_t hash2 = (CHashWriter(SER_GETHASH, 0) << nKey << vchSourceGroupKey << (hash1 % ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP)).GetCheapHash();
58  return hash2 % ADDRMAN_NEW_BUCKET_COUNT;
59 }
60 
61 int AddrInfo::GetBucketPosition(const uint256& nKey, bool fNew, int nBucket) const
62 {
63  uint64_t hash1 = (CHashWriter(SER_GETHASH, 0) << nKey << (fNew ? uint8_t{'N'} : uint8_t{'K'}) << nBucket << GetKey()).GetCheapHash();
64  return hash1 % ADDRMAN_BUCKET_SIZE;
65 }
66 
67 bool AddrInfo::IsTerrible(int64_t nNow) const
68 {
69  if (nLastTry && nLastTry >= nNow - 60) // never remove things tried in the last minute
70  return false;
71 
72  if (nTime > nNow + 10 * 60) // came in a flying DeLorean
73  return true;
74 
75  if (nTime == 0 || nNow - nTime > ADDRMAN_HORIZON_DAYS * 24 * 60 * 60) // not seen in recent history
76  return true;
77 
78  if (nLastSuccess == 0 && nAttempts >= ADDRMAN_RETRIES) // tried N times and never a success
79  return true;
80 
81  if (nNow - nLastSuccess > ADDRMAN_MIN_FAIL_DAYS * 24 * 60 * 60 && nAttempts >= ADDRMAN_MAX_FAILURES) // N successive failures in the last week
82  return true;
83 
84  return false;
85 }
86 
87 double AddrInfo::GetChance(int64_t nNow) const
88 {
89  double fChance = 1.0;
90  int64_t nSinceLastTry = std::max<int64_t>(nNow - nLastTry, 0);
91 
92  // deprioritize very recent attempts away
93  if (nSinceLastTry < 60 * 10)
94  fChance *= 0.01;
95 
96  // deprioritize 66% after each failed attempt, but at most 1/28th to avoid the search taking forever or overly penalizing outages.
97  fChance *= pow(0.66, std::min(nAttempts, 8));
98 
99  return fChance;
100 }
101 
102 AddrManImpl::AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
103  : insecure_rand{deterministic}
104  , nKey{deterministic ? uint256{1} : insecure_rand.rand256()}
105  , m_consistency_check_ratio{consistency_check_ratio}
106  , m_netgroupman{netgroupman}
107 {
108  for (auto& bucket : vvNew) {
109  for (auto& entry : bucket) {
110  entry = -1;
111  }
112  }
113  for (auto& bucket : vvTried) {
114  for (auto& entry : bucket) {
115  entry = -1;
116  }
117  }
118 }
119 
121 {
122  nKey.SetNull();
123 }
124 
125 template <typename Stream>
126 void AddrManImpl::Serialize(Stream& s_) const
127 {
128  LOCK(cs);
129 
168  // Always serialize in the latest version (FILE_FORMAT).
169 
170  OverrideStream<Stream> s(&s_, s_.GetType(), s_.GetVersion() | ADDRV2_FORMAT);
171 
172  s << static_cast<uint8_t>(FILE_FORMAT);
173 
174  // Increment `lowest_compatible` iff a newly introduced format is incompatible with
175  // the previous one.
176  static constexpr uint8_t lowest_compatible = Format::V4_MULTIPORT;
177  s << static_cast<uint8_t>(INCOMPATIBILITY_BASE + lowest_compatible);
178 
179  s << nKey;
180  s << nNew;
181  s << nTried;
182 
183  int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
184  s << nUBuckets;
185  std::unordered_map<int, int> mapUnkIds;
186  int nIds = 0;
187  for (const auto& entry : mapInfo) {
188  mapUnkIds[entry.first] = nIds;
189  const AddrInfo& info = entry.second;
190  if (info.nRefCount) {
191  assert(nIds != nNew); // this means nNew was wrong, oh ow
192  s << info;
193  nIds++;
194  }
195  }
196  nIds = 0;
197  for (const auto& entry : mapInfo) {
198  const AddrInfo& info = entry.second;
199  if (info.fInTried) {
200  assert(nIds != nTried); // this means nTried was wrong, oh ow
201  s << info;
202  nIds++;
203  }
204  }
205  for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
206  int nSize = 0;
207  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
208  if (vvNew[bucket][i] != -1)
209  nSize++;
210  }
211  s << nSize;
212  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
213  if (vvNew[bucket][i] != -1) {
214  int nIndex = mapUnkIds[vvNew[bucket][i]];
215  s << nIndex;
216  }
217  }
218  }
219  // Store asmap checksum after bucket entries so that it
220  // can be ignored by older clients for backward compatibility.
222 }
223 
224 template <typename Stream>
225 void AddrManImpl::Unserialize(Stream& s_)
226 {
227  LOCK(cs);
228 
229  assert(vRandom.empty());
230 
231  Format format;
232  s_ >> Using<CustomUintFormatter<1>>(format);
233 
234  int stream_version = s_.GetVersion();
235  if (format >= Format::V3_BIP155) {
236  // Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress
237  // unserialize methods know that an address in addrv2 format is coming.
238  stream_version |= ADDRV2_FORMAT;
239  }
240 
241  OverrideStream<Stream> s(&s_, s_.GetType(), stream_version);
242 
243  uint8_t compat;
244  s >> compat;
245  if (compat < INCOMPATIBILITY_BASE) {
246  throw std::ios_base::failure(strprintf(
247  "Corrupted addrman database: The compat value (%u) "
248  "is lower than the expected minimum value %u.",
249  compat, INCOMPATIBILITY_BASE));
250  }
251  const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
252  if (lowest_compatible > FILE_FORMAT) {
254  "Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
255  "but the maximum supported by this version of %s is %u.",
256  uint8_t{format}, lowest_compatible, PACKAGE_NAME, uint8_t{FILE_FORMAT}));
257  }
258 
259  s >> nKey;
260  s >> nNew;
261  s >> nTried;
262  int nUBuckets = 0;
263  s >> nUBuckets;
264  if (format >= Format::V1_DETERMINISTIC) {
265  nUBuckets ^= (1 << 30);
266  }
267 
268  if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
269  throw std::ios_base::failure(
270  strprintf("Corrupt AddrMan serialization: nNew=%d, should be in [0, %d]",
271  nNew,
273  }
274 
275  if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nTried < 0) {
276  throw std::ios_base::failure(
277  strprintf("Corrupt AddrMan serialization: nTried=%d, should be in [0, %d]",
278  nTried,
280  }
281 
282  // Deserialize entries from the new table.
283  for (int n = 0; n < nNew; n++) {
284  AddrInfo& info = mapInfo[n];
285  s >> info;
286  mapAddr[info] = n;
287  info.nRandomPos = vRandom.size();
288  vRandom.push_back(n);
289  }
290  nIdCount = nNew;
291 
292  // Deserialize entries from the tried table.
293  int nLost = 0;
294  for (int n = 0; n < nTried; n++) {
295  AddrInfo info;
296  s >> info;
297  int nKBucket = info.GetTriedBucket(nKey, m_netgroupman);
298  int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
299  if (info.IsValid()
300  && vvTried[nKBucket][nKBucketPos] == -1) {
301  info.nRandomPos = vRandom.size();
302  info.fInTried = true;
303  vRandom.push_back(nIdCount);
304  mapInfo[nIdCount] = info;
305  mapAddr[info] = nIdCount;
306  vvTried[nKBucket][nKBucketPos] = nIdCount;
307  nIdCount++;
308  } else {
309  nLost++;
310  }
311  }
312  nTried -= nLost;
313 
314  // Store positions in the new table buckets to apply later (if possible).
315  // An entry may appear in up to ADDRMAN_NEW_BUCKETS_PER_ADDRESS buckets,
316  // so we store all bucket-entry_index pairs to iterate through later.
317  std::vector<std::pair<int, int>> bucket_entries;
318 
319  for (int bucket = 0; bucket < nUBuckets; ++bucket) {
320  int num_entries{0};
321  s >> num_entries;
322  for (int n = 0; n < num_entries; ++n) {
323  int entry_index{0};
324  s >> entry_index;
325  if (entry_index >= 0 && entry_index < nNew) {
326  bucket_entries.emplace_back(bucket, entry_index);
327  }
328  }
329  }
330 
331  // If the bucket count and asmap checksum haven't changed, then attempt
332  // to restore the entries to the buckets/positions they were in before
333  // serialization.
334  uint256 supplied_asmap_checksum{m_netgroupman.GetAsmapChecksum()};
335  uint256 serialized_asmap_checksum;
336  if (format >= Format::V2_ASMAP) {
337  s >> serialized_asmap_checksum;
338  }
339  const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
340  serialized_asmap_checksum == supplied_asmap_checksum};
341 
342  if (!restore_bucketing) {
343  LogPrint(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");
344  }
345 
346  for (auto bucket_entry : bucket_entries) {
347  int bucket{bucket_entry.first};
348  const int entry_index{bucket_entry.second};
349  AddrInfo& info = mapInfo[entry_index];
350 
351  // Don't store the entry in the new bucket if it's not a valid address for our addrman
352  if (!info.IsValid()) continue;
353 
354  // The entry shouldn't appear in more than
355  // ADDRMAN_NEW_BUCKETS_PER_ADDRESS. If it has already, just skip
356  // this bucket_entry.
357  if (info.nRefCount >= ADDRMAN_NEW_BUCKETS_PER_ADDRESS) continue;
358 
359  int bucket_position = info.GetBucketPosition(nKey, true, bucket);
360  if (restore_bucketing && vvNew[bucket][bucket_position] == -1) {
361  // Bucketing has not changed, using existing bucket positions for the new table
362  vvNew[bucket][bucket_position] = entry_index;
363  ++info.nRefCount;
364  } else {
365  // In case the new table data cannot be used (bucket count wrong or new asmap),
366  // try to give them a reference based on their primary source address.
367  bucket = info.GetNewBucket(nKey, m_netgroupman);
368  bucket_position = info.GetBucketPosition(nKey, true, bucket);
369  if (vvNew[bucket][bucket_position] == -1) {
370  vvNew[bucket][bucket_position] = entry_index;
371  ++info.nRefCount;
372  }
373  }
374  }
375 
376  // Prune new entries with refcount 0 (as a result of collisions or invalid address).
377  int nLostUnk = 0;
378  for (auto it = mapInfo.cbegin(); it != mapInfo.cend(); ) {
379  if (it->second.fInTried == false && it->second.nRefCount == 0) {
380  const auto itCopy = it++;
381  Delete(itCopy->first);
382  ++nLostUnk;
383  } else {
384  ++it;
385  }
386  }
387  if (nLost + nLostUnk > 0) {
388  LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions or invalid addresses\n", nLostUnk, nLost);
389  }
390 
391  const int check_code{CheckAddrman()};
392  if (check_code != 0) {
393  throw std::ios_base::failure(strprintf(
394  "Corrupt data. Consistency check failed with code %s",
395  check_code));
396  }
397 }
398 
399 AddrInfo* AddrManImpl::Find(const CService& addr, int* pnId)
400 {
402 
403  const auto it = mapAddr.find(addr);
404  if (it == mapAddr.end())
405  return nullptr;
406  if (pnId)
407  *pnId = (*it).second;
408  const auto it2 = mapInfo.find((*it).second);
409  if (it2 != mapInfo.end())
410  return &(*it2).second;
411  return nullptr;
412 }
413 
414 AddrInfo* AddrManImpl::Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId)
415 {
417 
418  int nId = nIdCount++;
419  mapInfo[nId] = AddrInfo(addr, addrSource);
420  mapAddr[addr] = nId;
421  mapInfo[nId].nRandomPos = vRandom.size();
422  vRandom.push_back(nId);
423  if (pnId)
424  *pnId = nId;
425  return &mapInfo[nId];
426 }
427 
428 void AddrManImpl::SwapRandom(unsigned int nRndPos1, unsigned int nRndPos2) const
429 {
431 
432  if (nRndPos1 == nRndPos2)
433  return;
434 
435  assert(nRndPos1 < vRandom.size() && nRndPos2 < vRandom.size());
436 
437  int nId1 = vRandom[nRndPos1];
438  int nId2 = vRandom[nRndPos2];
439 
440  const auto it_1{mapInfo.find(nId1)};
441  const auto it_2{mapInfo.find(nId2)};
442  assert(it_1 != mapInfo.end());
443  assert(it_2 != mapInfo.end());
444 
445  it_1->second.nRandomPos = nRndPos2;
446  it_2->second.nRandomPos = nRndPos1;
447 
448  vRandom[nRndPos1] = nId2;
449  vRandom[nRndPos2] = nId1;
450 }
451 
452 void AddrManImpl::Delete(int nId)
453 {
455 
456  assert(mapInfo.count(nId) != 0);
457  AddrInfo& info = mapInfo[nId];
458  assert(!info.fInTried);
459  assert(info.nRefCount == 0);
460 
461  SwapRandom(info.nRandomPos, vRandom.size() - 1);
462  vRandom.pop_back();
463  mapAddr.erase(info);
464  mapInfo.erase(nId);
465  nNew--;
466 }
467 
468 void AddrManImpl::ClearNew(int nUBucket, int nUBucketPos)
469 {
471 
472  // if there is an entry in the specified bucket, delete it.
473  if (vvNew[nUBucket][nUBucketPos] != -1) {
474  int nIdDelete = vvNew[nUBucket][nUBucketPos];
475  AddrInfo& infoDelete = mapInfo[nIdDelete];
476  assert(infoDelete.nRefCount > 0);
477  infoDelete.nRefCount--;
478  vvNew[nUBucket][nUBucketPos] = -1;
479  LogPrint(BCLog::ADDRMAN, "Removed %s from new[%i][%i]\n", infoDelete.ToString(), nUBucket, nUBucketPos);
480  if (infoDelete.nRefCount == 0) {
481  Delete(nIdDelete);
482  }
483  }
484 }
485 
486 void AddrManImpl::MakeTried(AddrInfo& info, int nId)
487 {
489 
490  // remove the entry from all new buckets
491  const int start_bucket{info.GetNewBucket(nKey, m_netgroupman)};
492  for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; ++n) {
493  const int bucket{(start_bucket + n) % ADDRMAN_NEW_BUCKET_COUNT};
494  const int pos{info.GetBucketPosition(nKey, true, bucket)};
495  if (vvNew[bucket][pos] == nId) {
496  vvNew[bucket][pos] = -1;
497  info.nRefCount--;
498  if (info.nRefCount == 0) break;
499  }
500  }
501  nNew--;
502 
503  assert(info.nRefCount == 0);
504 
505  // which tried bucket to move the entry to
506  int nKBucket = info.GetTriedBucket(nKey, m_netgroupman);
507  int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
508 
509  // first make space to add it (the existing tried entry there is moved to new, deleting whatever is there).
510  if (vvTried[nKBucket][nKBucketPos] != -1) {
511  // find an item to evict
512  int nIdEvict = vvTried[nKBucket][nKBucketPos];
513  assert(mapInfo.count(nIdEvict) == 1);
514  AddrInfo& infoOld = mapInfo[nIdEvict];
515 
516  // Remove the to-be-evicted item from the tried set.
517  infoOld.fInTried = false;
518  vvTried[nKBucket][nKBucketPos] = -1;
519  nTried--;
520 
521  // find which new bucket it belongs to
522  int nUBucket = infoOld.GetNewBucket(nKey, m_netgroupman);
523  int nUBucketPos = infoOld.GetBucketPosition(nKey, true, nUBucket);
524  ClearNew(nUBucket, nUBucketPos);
525  assert(vvNew[nUBucket][nUBucketPos] == -1);
526 
527  // Enter it into the new set again.
528  infoOld.nRefCount = 1;
529  vvNew[nUBucket][nUBucketPos] = nIdEvict;
530  nNew++;
531  LogPrint(BCLog::ADDRMAN, "Moved %s from tried[%i][%i] to new[%i][%i] to make space\n",
532  infoOld.ToString(), nKBucket, nKBucketPos, nUBucket, nUBucketPos);
533  }
534  assert(vvTried[nKBucket][nKBucketPos] == -1);
535 
536  vvTried[nKBucket][nKBucketPos] = nId;
537  nTried++;
538  info.fInTried = true;
539 }
540 
541 bool AddrManImpl::AddSingle(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty)
542 {
544 
545  if (!addr.IsRoutable())
546  return false;
547 
548  int nId;
549  AddrInfo* pinfo = Find(addr, &nId);
550 
551  // Do not set a penalty for a source's self-announcement
552  if (addr == source) {
553  nTimePenalty = 0;
554  }
555 
556  if (pinfo) {
557  // periodically update nTime
558  bool fCurrentlyOnline = (GetAdjustedTime() - addr.nTime < 24 * 60 * 60);
559  int64_t nUpdateInterval = (fCurrentlyOnline ? 60 * 60 : 24 * 60 * 60);
560  if (addr.nTime && (!pinfo->nTime || pinfo->nTime < addr.nTime - nUpdateInterval - nTimePenalty))
561  pinfo->nTime = std::max((int64_t)0, addr.nTime - nTimePenalty);
562 
563  // add services
564  pinfo->nServices = ServiceFlags(pinfo->nServices | addr.nServices);
565 
566  // do not update if no new information is present
567  if (!addr.nTime || (pinfo->nTime && addr.nTime <= pinfo->nTime))
568  return false;
569 
570  // do not update if the entry was already in the "tried" table
571  if (pinfo->fInTried)
572  return false;
573 
574  // do not update if the max reference count is reached
576  return false;
577 
578  // stochastic test: previous nRefCount == N: 2^N times harder to increase it
579  int nFactor = 1;
580  for (int n = 0; n < pinfo->nRefCount; n++)
581  nFactor *= 2;
582  if (nFactor > 1 && (insecure_rand.randrange(nFactor) != 0))
583  return false;
584  } else {
585  pinfo = Create(addr, source, &nId);
586  pinfo->nTime = std::max((int64_t)0, (int64_t)pinfo->nTime - nTimePenalty);
587  nNew++;
588  }
589 
590  int nUBucket = pinfo->GetNewBucket(nKey, source, m_netgroupman);
591  int nUBucketPos = pinfo->GetBucketPosition(nKey, true, nUBucket);
592  bool fInsert = vvNew[nUBucket][nUBucketPos] == -1;
593  if (vvNew[nUBucket][nUBucketPos] != nId) {
594  if (!fInsert) {
595  AddrInfo& infoExisting = mapInfo[vvNew[nUBucket][nUBucketPos]];
596  if (infoExisting.IsTerrible() || (infoExisting.nRefCount > 1 && pinfo->nRefCount == 0)) {
597  // Overwrite the existing new table entry.
598  fInsert = true;
599  }
600  }
601  if (fInsert) {
602  ClearNew(nUBucket, nUBucketPos);
603  pinfo->nRefCount++;
604  vvNew[nUBucket][nUBucketPos] = nId;
605  LogPrint(BCLog::ADDRMAN, "Added %s mapped to AS%i to new[%i][%i]\n",
606  addr.ToString(), m_netgroupman.GetMappedAS(addr), nUBucket, nUBucketPos);
607  } else {
608  if (pinfo->nRefCount == 0) {
609  Delete(nId);
610  }
611  }
612  }
613  return fInsert;
614 }
615 
616 bool AddrManImpl::Good_(const CService& addr, bool test_before_evict, int64_t nTime)
617 {
619 
620  int nId;
621 
622  nLastGood = nTime;
623 
624  AddrInfo* pinfo = Find(addr, &nId);
625 
626  // if not found, bail out
627  if (!pinfo) return false;
628 
629  AddrInfo& info = *pinfo;
630 
631  // update info
632  info.nLastSuccess = nTime;
633  info.nLastTry = nTime;
634  info.nAttempts = 0;
635  // nTime is not updated here, to avoid leaking information about
636  // currently-connected peers.
637 
638  // if it is already in the tried set, don't do anything else
639  if (info.fInTried) return false;
640 
641  // if it is not in new, something bad happened
642  if (!Assume(info.nRefCount > 0)) return false;
643 
644 
645  // which tried bucket to move the entry to
646  int tried_bucket = info.GetTriedBucket(nKey, m_netgroupman);
647  int tried_bucket_pos = info.GetBucketPosition(nKey, false, tried_bucket);
648 
649  // Will moving this address into tried evict another entry?
650  if (test_before_evict && (vvTried[tried_bucket][tried_bucket_pos] != -1)) {
652  m_tried_collisions.insert(nId);
653  }
654  // Output the entry we'd be colliding with, for debugging purposes
655  auto colliding_entry = mapInfo.find(vvTried[tried_bucket][tried_bucket_pos]);
656  LogPrint(BCLog::ADDRMAN, "Collision with %s while attempting to move %s to tried table. Collisions=%d\n",
657  colliding_entry != mapInfo.end() ? colliding_entry->second.ToString() : "",
658  addr.ToString(),
659  m_tried_collisions.size());
660  return false;
661  } else {
662  // move nId to the tried tables
663  MakeTried(info, nId);
664  LogPrint(BCLog::ADDRMAN, "Moved %s mapped to AS%i to tried[%i][%i]\n",
665  addr.ToString(), m_netgroupman.GetMappedAS(addr), tried_bucket, tried_bucket_pos);
666  return true;
667  }
668 }
669 
670 bool AddrManImpl::Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty)
671 {
672  int added{0};
673  for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++) {
674  added += AddSingle(*it, source, nTimePenalty) ? 1 : 0;
675  }
676  if (added > 0) {
677  LogPrint(BCLog::ADDRMAN, "Added %i addresses (of %i) from %s: %i tried, %i new\n", added, vAddr.size(), source.ToString(), nTried, nNew);
678  }
679  return added > 0;
680 }
681 
682 void AddrManImpl::Attempt_(const CService& addr, bool fCountFailure, int64_t nTime)
683 {
685 
686  AddrInfo* pinfo = Find(addr);
687 
688  // if not found, bail out
689  if (!pinfo)
690  return;
691 
692  AddrInfo& info = *pinfo;
693 
694  // update info
695  info.nLastTry = nTime;
696  if (fCountFailure && info.nLastCountAttempt < nLastGood) {
697  info.nLastCountAttempt = nTime;
698  info.nAttempts++;
699  }
700 }
701 
702 std::pair<CAddress, int64_t> AddrManImpl::Select_(bool newOnly) const
703 {
705 
706  if (vRandom.empty()) return {};
707 
708  if (newOnly && nNew == 0) return {};
709 
710  // Use a 50% chance for choosing between tried and new table entries.
711  if (!newOnly &&
712  (nTried > 0 && (nNew == 0 || insecure_rand.randbool() == 0))) {
713  // use a tried node
714  double fChanceFactor = 1.0;
715  while (1) {
716  // Pick a tried bucket, and an initial position in that bucket.
717  int nKBucket = insecure_rand.randrange(ADDRMAN_TRIED_BUCKET_COUNT);
718  int nKBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
719  // Iterate over the positions of that bucket, starting at the initial one,
720  // and looping around.
721  int i;
722  for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
723  if (vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break;
724  }
725  // If the bucket is entirely empty, start over with a (likely) different one.
726  if (i == ADDRMAN_BUCKET_SIZE) continue;
727  // Find the entry to return.
728  int nId = vvTried[nKBucket][(nKBucketPos + i) % ADDRMAN_BUCKET_SIZE];
729  const auto it_found{mapInfo.find(nId)};
730  assert(it_found != mapInfo.end());
731  const AddrInfo& info{it_found->second};
732  // With probability GetChance() * fChanceFactor, return the entry.
733  if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
734  LogPrint(BCLog::ADDRMAN, "Selected %s from tried\n", info.ToString());
735  return {info, info.nLastTry};
736  }
737  // Otherwise start over with a (likely) different bucket, and increased chance factor.
738  fChanceFactor *= 1.2;
739  }
740  } else {
741  // use a new node
742  double fChanceFactor = 1.0;
743  while (1) {
744  // Pick a new bucket, and an initial position in that bucket.
745  int nUBucket = insecure_rand.randrange(ADDRMAN_NEW_BUCKET_COUNT);
746  int nUBucketPos = insecure_rand.randrange(ADDRMAN_BUCKET_SIZE);
747  // Iterate over the positions of that bucket, starting at the initial one,
748  // and looping around.
749  int i;
750  for (i = 0; i < ADDRMAN_BUCKET_SIZE; ++i) {
751  if (vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE] != -1) break;
752  }
753  // If the bucket is entirely empty, start over with a (likely) different one.
754  if (i == ADDRMAN_BUCKET_SIZE) continue;
755  // Find the entry to return.
756  int nId = vvNew[nUBucket][(nUBucketPos + i) % ADDRMAN_BUCKET_SIZE];
757  const auto it_found{mapInfo.find(nId)};
758  assert(it_found != mapInfo.end());
759  const AddrInfo& info{it_found->second};
760  // With probability GetChance() * fChanceFactor, return the entry.
761  if (insecure_rand.randbits(30) < fChanceFactor * info.GetChance() * (1 << 30)) {
762  LogPrint(BCLog::ADDRMAN, "Selected %s from new\n", info.ToString());
763  return {info, info.nLastTry};
764  }
765  // Otherwise start over with a (likely) different bucket, and increased chance factor.
766  fChanceFactor *= 1.2;
767  }
768  }
769 }
770 
771 std::vector<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
772 {
774 
775  size_t nNodes = vRandom.size();
776  if (max_pct != 0) {
777  nNodes = max_pct * nNodes / 100;
778  }
779  if (max_addresses != 0) {
780  nNodes = std::min(nNodes, max_addresses);
781  }
782 
783  // gather a list of random nodes, skipping those of low quality
784  const int64_t now{GetAdjustedTime()};
785  std::vector<CAddress> addresses;
786  for (unsigned int n = 0; n < vRandom.size(); n++) {
787  if (addresses.size() >= nNodes)
788  break;
789 
790  int nRndPos = insecure_rand.randrange(vRandom.size() - n) + n;
791  SwapRandom(n, nRndPos);
792  const auto it{mapInfo.find(vRandom[n])};
793  assert(it != mapInfo.end());
794 
795  const AddrInfo& ai{it->second};
796 
797  // Filter by network (optional)
798  if (network != std::nullopt && ai.GetNetClass() != network) continue;
799 
800  // Filter for quality
801  if (ai.IsTerrible(now)) continue;
802 
803  addresses.push_back(ai);
804  }
805  LogPrint(BCLog::ADDRMAN, "GetAddr returned %d random addresses\n", addresses.size());
806  return addresses;
807 }
808 
809 void AddrManImpl::Connected_(const CService& addr, int64_t nTime)
810 {
812 
813  AddrInfo* pinfo = Find(addr);
814 
815  // if not found, bail out
816  if (!pinfo)
817  return;
818 
819  AddrInfo& info = *pinfo;
820 
821  // update info
822  int64_t nUpdateInterval = 20 * 60;
823  if (nTime - info.nTime > nUpdateInterval)
824  info.nTime = nTime;
825 }
826 
827 void AddrManImpl::SetServices_(const CService& addr, ServiceFlags nServices)
828 {
830 
831  AddrInfo* pinfo = Find(addr);
832 
833  // if not found, bail out
834  if (!pinfo)
835  return;
836 
837  AddrInfo& info = *pinfo;
838 
839  // update info
840  info.nServices = nServices;
841 }
842 
844 {
846 
847  for (std::set<int>::iterator it = m_tried_collisions.begin(); it != m_tried_collisions.end();) {
848  int id_new = *it;
849 
850  bool erase_collision = false;
851 
852  // If id_new not found in mapInfo remove it from m_tried_collisions
853  if (mapInfo.count(id_new) != 1) {
854  erase_collision = true;
855  } else {
856  AddrInfo& info_new = mapInfo[id_new];
857 
858  // Which tried bucket to move the entry to.
859  int tried_bucket = info_new.GetTriedBucket(nKey, m_netgroupman);
860  int tried_bucket_pos = info_new.GetBucketPosition(nKey, false, tried_bucket);
861  if (!info_new.IsValid()) { // id_new may no longer map to a valid address
862  erase_collision = true;
863  } else if (vvTried[tried_bucket][tried_bucket_pos] != -1) { // The position in the tried bucket is not empty
864 
865  // Get the to-be-evicted address that is being tested
866  int id_old = vvTried[tried_bucket][tried_bucket_pos];
867  AddrInfo& info_old = mapInfo[id_old];
868 
869  // Has successfully connected in last X hours
870  if (GetAdjustedTime() - info_old.nLastSuccess < ADDRMAN_REPLACEMENT_HOURS*(60*60)) {
871  erase_collision = true;
872  } else if (GetAdjustedTime() - info_old.nLastTry < ADDRMAN_REPLACEMENT_HOURS*(60*60)) { // attempted to connect and failed in last X hours
873 
874  // Give address at least 60 seconds to successfully connect
875  if (GetAdjustedTime() - info_old.nLastTry > 60) {
876  LogPrint(BCLog::ADDRMAN, "Replacing %s with %s in tried table\n", info_old.ToString(), info_new.ToString());
877 
878  // Replaces an existing address already in the tried table with the new address
879  Good_(info_new, false, GetAdjustedTime());
880  erase_collision = true;
881  }
882  } else if (GetAdjustedTime() - info_new.nLastSuccess > ADDRMAN_TEST_WINDOW) {
883  // If the collision hasn't resolved in some reasonable amount of time,
884  // just evict the old entry -- we must not be able to
885  // connect to it for some reason.
886  LogPrint(BCLog::ADDRMAN, "Unable to test; replacing %s with %s in tried table anyway\n", info_old.ToString(), info_new.ToString());
887  Good_(info_new, false, GetAdjustedTime());
888  erase_collision = true;
889  }
890  } else { // Collision is not actually a collision anymore
891  Good_(info_new, false, GetAdjustedTime());
892  erase_collision = true;
893  }
894  }
895 
896  if (erase_collision) {
897  m_tried_collisions.erase(it++);
898  } else {
899  it++;
900  }
901  }
902 }
903 
904 std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision_()
905 {
907 
908  if (m_tried_collisions.size() == 0) return {};
909 
910  std::set<int>::iterator it = m_tried_collisions.begin();
911 
912  // Selects a random element from m_tried_collisions
913  std::advance(it, insecure_rand.randrange(m_tried_collisions.size()));
914  int id_new = *it;
915 
916  // If id_new not found in mapInfo remove it from m_tried_collisions
917  if (mapInfo.count(id_new) != 1) {
918  m_tried_collisions.erase(it);
919  return {};
920  }
921 
922  const AddrInfo& newInfo = mapInfo[id_new];
923 
924  // which tried bucket to move the entry to
925  int tried_bucket = newInfo.GetTriedBucket(nKey, m_netgroupman);
926  int tried_bucket_pos = newInfo.GetBucketPosition(nKey, false, tried_bucket);
927 
928  const AddrInfo& info_old = mapInfo[vvTried[tried_bucket][tried_bucket_pos]];
929  return {info_old, info_old.nLastTry};
930 }
931 
932 std::optional<AddressPosition> AddrManImpl::FindAddressEntry_(const CAddress& addr)
933 {
935 
936  AddrInfo* addr_info = Find(addr);
937 
938  if (!addr_info) return std::nullopt;
939 
940  if(addr_info->fInTried) {
941  int bucket{addr_info->GetTriedBucket(nKey, m_netgroupman)};
942  return AddressPosition(/*tried_in=*/true,
943  /*multiplicity_in=*/1,
944  /*bucket_in=*/bucket,
945  /*position_in=*/addr_info->GetBucketPosition(nKey, false, bucket));
946  } else {
947  int bucket{addr_info->GetNewBucket(nKey, m_netgroupman)};
948  return AddressPosition(/*tried_in=*/false,
949  /*multiplicity_in=*/addr_info->nRefCount,
950  /*bucket_in=*/bucket,
951  /*position_in=*/addr_info->GetBucketPosition(nKey, true, bucket));
952  }
953 }
954 
955 void AddrManImpl::Check() const
956 {
958 
959  // Run consistency checks 1 in m_consistency_check_ratio times if enabled
960  if (m_consistency_check_ratio == 0) return;
961  if (insecure_rand.randrange(m_consistency_check_ratio) >= 1) return;
962 
963  const int err{CheckAddrman()};
964  if (err) {
965  LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
966  assert(false);
967  }
968 }
969 
971 {
973 
975  strprintf("new %i, tried %i, total %u", nNew, nTried, vRandom.size()), BCLog::ADDRMAN);
976 
977  std::unordered_set<int> setTried;
978  std::unordered_map<int, int> mapNew;
979 
980  if (vRandom.size() != (size_t)(nTried + nNew))
981  return -7;
982 
983  for (const auto& entry : mapInfo) {
984  int n = entry.first;
985  const AddrInfo& info = entry.second;
986  if (info.fInTried) {
987  if (!info.nLastSuccess)
988  return -1;
989  if (info.nRefCount)
990  return -2;
991  setTried.insert(n);
992  } else {
993  if (info.nRefCount < 0 || info.nRefCount > ADDRMAN_NEW_BUCKETS_PER_ADDRESS)
994  return -3;
995  if (!info.nRefCount)
996  return -4;
997  mapNew[n] = info.nRefCount;
998  }
999  const auto it{mapAddr.find(info)};
1000  if (it == mapAddr.end() || it->second != n) {
1001  return -5;
1002  }
1003  if (info.nRandomPos < 0 || (size_t)info.nRandomPos >= vRandom.size() || vRandom[info.nRandomPos] != n)
1004  return -14;
1005  if (info.nLastTry < 0)
1006  return -6;
1007  if (info.nLastSuccess < 0)
1008  return -8;
1009  }
1010 
1011  if (setTried.size() != (size_t)nTried)
1012  return -9;
1013  if (mapNew.size() != (size_t)nNew)
1014  return -10;
1015 
1016  for (int n = 0; n < ADDRMAN_TRIED_BUCKET_COUNT; n++) {
1017  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
1018  if (vvTried[n][i] != -1) {
1019  if (!setTried.count(vvTried[n][i]))
1020  return -11;
1021  const auto it{mapInfo.find(vvTried[n][i])};
1022  if (it == mapInfo.end() || it->second.GetTriedBucket(nKey, m_netgroupman) != n) {
1023  return -17;
1024  }
1025  if (it->second.GetBucketPosition(nKey, false, n) != i) {
1026  return -18;
1027  }
1028  setTried.erase(vvTried[n][i]);
1029  }
1030  }
1031  }
1032 
1033  for (int n = 0; n < ADDRMAN_NEW_BUCKET_COUNT; n++) {
1034  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
1035  if (vvNew[n][i] != -1) {
1036  if (!mapNew.count(vvNew[n][i]))
1037  return -12;
1038  const auto it{mapInfo.find(vvNew[n][i])};
1039  if (it == mapInfo.end() || it->second.GetBucketPosition(nKey, true, n) != i) {
1040  return -19;
1041  }
1042  if (--mapNew[vvNew[n][i]] == 0)
1043  mapNew.erase(vvNew[n][i]);
1044  }
1045  }
1046  }
1047 
1048  if (setTried.size())
1049  return -13;
1050  if (mapNew.size())
1051  return -15;
1052  if (nKey.IsNull())
1053  return -16;
1054 
1055  return 0;
1056 }
1057 
1058 size_t AddrManImpl::size() const
1059 {
1060  LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
1061  return vRandom.size();
1062 }
1063 
1064 bool AddrManImpl::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
1065 {
1066  LOCK(cs);
1067  Check();
1068  auto ret = Add_(vAddr, source, nTimePenalty);
1069  Check();
1070  return ret;
1071 }
1072 
1073 bool AddrManImpl::Good(const CService& addr, int64_t nTime)
1074 {
1075  LOCK(cs);
1076  Check();
1077  auto ret = Good_(addr, /*test_before_evict=*/true, nTime);
1078  Check();
1079  return ret;
1080 }
1081 
1082 void AddrManImpl::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
1083 {
1084  LOCK(cs);
1085  Check();
1086  Attempt_(addr, fCountFailure, nTime);
1087  Check();
1088 }
1089 
1091 {
1092  LOCK(cs);
1093  Check();
1095  Check();
1096 }
1097 
1098 std::pair<CAddress, int64_t> AddrManImpl::SelectTriedCollision()
1099 {
1100  LOCK(cs);
1101  Check();
1102  const auto ret = SelectTriedCollision_();
1103  Check();
1104  return ret;
1105 }
1106 
1107 std::pair<CAddress, int64_t> AddrManImpl::Select(bool newOnly) const
1108 {
1109  LOCK(cs);
1110  Check();
1111  const auto addrRet = Select_(newOnly);
1112  Check();
1113  return addrRet;
1114 }
1115 
1116 std::vector<CAddress> AddrManImpl::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1117 {
1118  LOCK(cs);
1119  Check();
1120  const auto addresses = GetAddr_(max_addresses, max_pct, network);
1121  Check();
1122  return addresses;
1123 }
1124 
1125 void AddrManImpl::Connected(const CService& addr, int64_t nTime)
1126 {
1127  LOCK(cs);
1128  Check();
1129  Connected_(addr, nTime);
1130  Check();
1131 }
1132 
1133 void AddrManImpl::SetServices(const CService& addr, ServiceFlags nServices)
1134 {
1135  LOCK(cs);
1136  Check();
1137  SetServices_(addr, nServices);
1138  Check();
1139 }
1140 
1141 std::optional<AddressPosition> AddrManImpl::FindAddressEntry(const CAddress& addr)
1142 {
1143  LOCK(cs);
1144  Check();
1145  auto entry = FindAddressEntry_(addr);
1146  Check();
1147  return entry;
1148 }
1149 
1150 AddrMan::AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio)
1151  : m_impl(std::make_unique<AddrManImpl>(netgroupman, deterministic, consistency_check_ratio)) {}
1152 
1153 AddrMan::~AddrMan() = default;
1154 
1155 template <typename Stream>
1156 void AddrMan::Serialize(Stream& s_) const
1157 {
1158  m_impl->Serialize<Stream>(s_);
1159 }
1160 
1161 template <typename Stream>
1162 void AddrMan::Unserialize(Stream& s_)
1163 {
1164  m_impl->Unserialize<Stream>(s_);
1165 }
1166 
1167 // explicit instantiation
1168 template void AddrMan::Serialize(CHashWriter& s) const;
1169 template void AddrMan::Serialize(CAutoFile& s) const;
1170 template void AddrMan::Serialize(CDataStream& s) const;
1171 template void AddrMan::Unserialize(CAutoFile& s);
1173 template void AddrMan::Unserialize(CDataStream& s);
1175 
1176 size_t AddrMan::size() const
1177 {
1178  return m_impl->size();
1179 }
1180 
1181 bool AddrMan::Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
1182 {
1183  return m_impl->Add(vAddr, source, nTimePenalty);
1184 }
1185 
1186 bool AddrMan::Good(const CService& addr, int64_t nTime)
1187 {
1188  return m_impl->Good(addr, nTime);
1189 }
1190 
1191 void AddrMan::Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
1192 {
1193  m_impl->Attempt(addr, fCountFailure, nTime);
1194 }
1195 
1197 {
1198  m_impl->ResolveCollisions();
1199 }
1200 
1201 std::pair<CAddress, int64_t> AddrMan::SelectTriedCollision()
1202 {
1203  return m_impl->SelectTriedCollision();
1204 }
1205 
1206 std::pair<CAddress, int64_t> AddrMan::Select(bool newOnly) const
1207 {
1208  return m_impl->Select(newOnly);
1209 }
1210 
1211 std::vector<CAddress> AddrMan::GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
1212 {
1213  return m_impl->GetAddr(max_addresses, max_pct, network);
1214 }
1215 
1216 void AddrMan::Connected(const CService& addr, int64_t nTime)
1217 {
1218  m_impl->Connected(addr, nTime);
1219 }
1220 
1221 void AddrMan::SetServices(const CService& addr, ServiceFlags nServices)
1222 {
1223  m_impl->SetServices(addr, nServices);
1224 }
1225 
1226 std::optional<AddressPosition> AddrMan::FindAddressEntry(const CAddress& addr)
1227 {
1228  return m_impl->FindAddressEntry(addr);
1229 }
NetGroupManager::GetGroup
std::vector< unsigned char > GetGroup(const CNetAddr &address) const
Get the canonical identifier of the network group for address.
Definition: netgroup.cpp:17
ADDRV2_FORMAT
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:33
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
AddrManImpl::ClearNew
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
Clear a position in a "new" table. This is the only place where entries are actually deleted.
Definition: addrman.cpp:468
AddrInfo::GetTriedBucket
int GetTriedBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:46
AddrInfo::nLastSuccess
int64_t nLastSuccess
last successful connection by us
Definition: addrman_impl.h:50
assert
assert(!tx.IsCoinBase())
tinyformat::format
void format(std::ostream &out, const char *fmt, const Args &... args)
Format list of arguments to the stream according to given format string.
Definition: tinyformat.h:1062
check.h
ADDRMAN_HORIZON_DAYS
static constexpr int64_t ADDRMAN_HORIZON_DAYS
How old addresses can maximally be.
Definition: addrman.cpp:32
AddrManImpl::Select
std::pair< CAddress, int64_t > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1107
AddrMan::AddrMan
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1150
AddrManImpl::Connected_
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:809
CHashVerifier
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:157
AddrManImpl::Serialize
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:126
addrman_impl.h
streams.h
AddrManImpl::AddSingle
bool AddSingle(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Attempt to add a single address to addrman's new table.
Definition: addrman.cpp:541
AddrManImpl::GetAddr_
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:771
NetGroupManager
Netgroup manager.
Definition: netgroup.h:16
timedata.h
AddrInfo::nAttempts
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:53
CNetAddr
Network address.
Definition: netaddress.h:117
source
const char * source
Definition: rpcconsole.cpp:67
uint256.h
AddrManImpl::SetServices
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1133
AddrManImpl::~AddrManImpl
~AddrManImpl()
Definition: addrman.cpp:120
AddrManImpl::ResolveCollisions
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1090
AddrMan::Unserialize
void Unserialize(Stream &s_)
Definition: addrman.cpp:1162
base_blob::SetNull
void SetNull()
Definition: uint256.h:41
AddrMan::size
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1176
AddrManImpl::FindAddressEntry_
std::optional< AddressPosition > FindAddressEntry_(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:932
AddrManImpl::Create
AddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Create a new entry and add it to the internal data structures mapInfo, mapAddr and vRandom.
Definition: addrman.cpp:414
LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE
#define LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE(end_msg, log_category)
Definition: timer.h:103
ADDRMAN_TRIED_BUCKET_COUNT
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:26
AddrInfo
Extended statistics about a CAddress.
Definition: addrman_impl.h:37
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
CAddress::nServices
ServiceFlags nServices
Serialized as uint64_t in V1, and as CompactSize in V2.
Definition: protocol.h:442
AddrInfo::nLastTry
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman_impl.h:41
AddrManImpl::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1116
ADDRMAN_MAX_FAILURES
static constexpr int32_t ADDRMAN_MAX_FAILURES
How many successive failures are allowed ...
Definition: addrman.cpp:36
AddrMan::m_impl
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:89
AddrMan::Connected
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
We have successfully connected to this peer.
Definition: addrman.cpp:1216
CAddress::nTime
uint32_t nTime
Always included in serialization.
Definition: protocol.h:440
ADDRMAN_BUCKET_SIZE
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:32
ADDRMAN_TRIED_BUCKETS_PER_GROUP
static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP
Over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread.
Definition: addrman.cpp:26
AddrManImpl::SetServices_
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:827
AssertLockHeld
AssertLockHeld(pool.cs)
ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP
static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP
Over how many buckets entries with new addresses originating from a single group are spread.
Definition: addrman.cpp:28
AddrManImpl::SelectTriedCollision_
std::pair< CAddress, int64_t > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:904
tinyformat.h
AddrManImpl::Find
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:399
CAutoFile
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:475
Assume
#define Assume(val)
Assume is the identity function.
Definition: check.h:86
protocol.h
random.h
CService::ToString
std::string ToString() const
Definition: netaddress.cpp:933
AddrMan::Good
bool Good(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an address record as accessible and attempt to move it to addrman's tried table.
Definition: addrman.cpp:1186
NetGroupManager::GetMappedAS
uint32_t GetMappedAS(const CNetAddr &address) const
Get the autonomous system on the BGP path to address.
Definition: netgroup.cpp:80
ADDRMAN_RETRIES
static constexpr int32_t ADDRMAN_RETRIES
After how many failed attempts we give up on a new node.
Definition: addrman.cpp:34
AddrMan::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Attempt to add one or more addresses to addrman's new table.
Definition: addrman.cpp:1181
PACKAGE_NAME
#define PACKAGE_NAME
Definition: bitcoin-config.h:353
CNetAddr::IsRoutable
bool IsRoutable() const
Definition: netaddress.cpp:484
AddrManImpl::Connected
void Connected(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1125
netaddress.h
timer.h
AddrManImpl::ResolveCollisions_
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:843
AddrInfo::IsTerrible
bool IsTerrible(int64_t nNow=GetAdjustedTime()) const
Determine whether the statistics about this entry are bad enough so that it can just be deleted.
Definition: addrman.cpp:67
AddrInfo::GetBucketPosition
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:61
LogPrintf
#define LogPrintf(...)
Definition: logging.h:190
CNetAddr::IsValid
bool IsValid() const
Definition: netaddress.cpp:445
AddrMan::Serialize
void Serialize(Stream &s_) const
Definition: addrman.cpp:1156
ADDRMAN_NEW_BUCKETS_PER_ADDRESS
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS
Maximum number of times an address can occur in the new table.
Definition: addrman.cpp:30
AddrManImpl::Attempt_
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:682
AddrMan::SelectTriedCollision
std::pair< CAddress, int64_t > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1201
uint256
256-bit opaque blob.
Definition: uint256.h:126
AddrInfo::nLastCountAttempt
int64_t nLastCountAttempt
last counted attempt (memory only)
Definition: addrman_impl.h:44
LogPrint
#define LogPrint(category,...)
Definition: logging.h:194
AddrManImpl::m_tried_collisions
std::set< int > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries. Test-before-evict disciplin...
Definition: addrman_impl.h:205
AddressPosition
Test-only struct, capturing info about an address in AddrMan.
Definition: addrman.h:33
AddrInfo::GetNewBucket
int GetNewBucket(const uint256 &nKey, const CNetAddr &src, const NetGroupManager &netgroupman) const
Calculate in which "new" bucket this entry belongs, given a certain source.
Definition: addrman.cpp:53
AddrManImpl::AddrManImpl
AddrManImpl(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:102
AddrMan::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1191
AddrManImpl::m_consistency_check_ratio
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:211
AddrManImpl::MakeTried
void MakeTried(AddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Move an entry from the "new" table(s) to the "tried" table.
Definition: addrman.cpp:486
OverrideStream
Definition: streams.h:27
AddrManImpl::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1064
AddrManImpl::nKey
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:153
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
AddrManImpl::Check
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:955
ADDRMAN_MIN_FAIL_DAYS
static constexpr int64_t ADDRMAN_MIN_FAIL_DAYS
...
Definition: addrman.cpp:38
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
ADDRMAN_SET_TRIED_COLLISION_SIZE
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE
The maximum number of tried addr collisions to store.
Definition: addrman.cpp:42
base_blob::IsNull
bool IsNull() const
Definition: uint256.h:33
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
AddrManImpl::INCOMPATIBILITY_BASE
static constexpr uint8_t INCOMPATIBILITY_BASE
The initial value of a field that is incremented every time an incompatible format change is made (su...
Definition: addrman_impl.h:176
AddrManImpl::Good_
bool Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:616
AddrManImpl::SwapRandom
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:428
AddrManImpl::FILE_FORMAT
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:169
AddrInfo::nRefCount
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:56
AddrManImpl::Add_
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:670
AddrManImpl::Unserialize
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:225
AddrManImpl::Good
bool Good(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1073
LOCK
#define LOCK(cs)
Definition: sync.h:235
AddrManImpl::Select_
std::pair< CAddress, int64_t > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:702
AddrInfo::GetChance
double GetChance(int64_t nNow=GetAdjustedTime()) const
Calculate the relative chance this entry should be given when selecting nodes to connect to.
Definition: addrman.cpp:87
AddrManImpl::FindAddressEntry
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1141
std
Definition: setup_common.h:33
AddrMan::Select
std::pair< CAddress, int64_t > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1206
CHashWriter
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:99
logging.h
ADDRMAN_NEW_BUCKET_COUNT
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:29
AddrManImpl::cs
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:147
hash.h
ADDRMAN_TEST_WINDOW
static constexpr int64_t ADDRMAN_TEST_WINDOW
The maximum time we'll spend trying to resolve a tried table collision, in seconds.
Definition: addrman.cpp:44
serialize.h
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:185
AddrManImpl::size
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1058
addrman.h
AddrManImpl::CheckAddrman
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:970
BCLog::ADDRMAN
@ ADDRMAN
Definition: logging.h:48
AddrManImpl::SelectTriedCollision
std::pair< CAddress, int64_t > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1098
AddrManImpl
Definition: addrman_impl.h:100
AddrMan::ResolveCollisions
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1196
AddrManImpl::m_netgroupman
const NetGroupManager & m_netgroupman
Reference to the netgroup manager.
Definition: addrman_impl.h:214
SER_GETHASH
@ SER_GETHASH
Definition: serialize.h:133
AddrMan::~AddrMan
~AddrMan()
AddrMan::FindAddressEntry
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr)
Test-only function Find the address record in AddrMan and return information about its position.
Definition: addrman.cpp:1226
AddrInfo::fInTried
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:59
ADDRMAN_REPLACEMENT_HOURS
static constexpr int64_t ADDRMAN_REPLACEMENT_HOURS
How recent a successful connection should be before we allow an address to be evicted from tried.
Definition: addrman.cpp:40
AddrMan::SetServices
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1221
InvalidAddrManVersionError
Definition: addrman.h:21
AddrMan::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.cpp:1211
AddrManImpl::Format
Format
Serialization versions.
Definition: addrman_impl.h:156
AddrInfo::nRandomPos
int nRandomPos
position in vRandom
Definition: addrman_impl.h:62
CService::GetKey
std::vector< unsigned char > GetKey() const
Definition: netaddress.cpp:911
NetGroupManager::GetAsmapChecksum
uint256 GetAsmapChecksum() const
Get a checksum identifying the asmap being used.
Definition: netgroup.cpp:10
AddrManImpl::Delete
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:452
AddrManImpl::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1082