Bitcoin Core  22.99.0
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2020 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 #ifndef BITCOIN_ADDRMAN_H
7 #define BITCOIN_ADDRMAN_H
8 
9 #include <clientversion.h>
10 #include <config/bitcoin-config.h>
11 #include <fs.h>
12 #include <hash.h>
13 #include <netaddress.h>
14 #include <protocol.h>
15 #include <random.h>
16 #include <streams.h>
17 #include <sync.h>
18 #include <timedata.h>
19 #include <tinyformat.h>
20 #include <util/system.h>
21 
22 #include <iostream>
23 #include <optional>
24 #include <set>
25 #include <stdint.h>
26 #include <unordered_map>
27 #include <vector>
28 
32 class CAddrInfo : public CAddress
33 {
34 public:
36  int64_t nLastTry{0};
37 
39  int64_t nLastCountAttempt{0};
40 
41 private:
44 
46  int64_t nLastSuccess{0};
47 
49  int nAttempts{0};
50 
52  int nRefCount{0};
53 
55  bool fInTried{false};
56 
58  int nRandomPos{-1};
59 
60  friend class CAddrMan;
61 
62 public:
63 
65  {
66  READWRITEAS(CAddress, obj);
67  READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts);
68  }
69 
70  CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
71  {
72  }
73 
75  {
76  }
77 
79  int GetTriedBucket(const uint256 &nKey, const std::vector<bool> &asmap) const;
80 
82  int GetNewBucket(const uint256 &nKey, const CNetAddr& src, const std::vector<bool> &asmap) const;
83 
85  int GetNewBucket(const uint256 &nKey, const std::vector<bool> &asmap) const
86  {
87  return GetNewBucket(nKey, source, asmap);
88  }
89 
91  int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
92 
94  bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
95 
97  double GetChance(int64_t nNow = GetAdjustedTime()) const;
98 };
99 
126 #define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8
128 
130 #define ADDRMAN_NEW_BUCKET_COUNT_LOG2 10
131 
133 #define ADDRMAN_BUCKET_SIZE_LOG2 6
134 
136 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
137 
139 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
140 
142 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
143 
145 #define ADDRMAN_HORIZON_DAYS 30
146 
148 #define ADDRMAN_RETRIES 3
149 
151 #define ADDRMAN_MAX_FAILURES 10
152 
154 #define ADDRMAN_MIN_FAIL_DAYS 7
155 
157 #define ADDRMAN_REPLACEMENT_HOURS 4
158 
160 #define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2)
161 #define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2)
162 #define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2)
163 
165 #define ADDRMAN_SET_TRIED_COLLISION_SIZE 10
166 
168 static const int64_t ADDRMAN_TEST_WINDOW = 40*60; // 40 minutes
169 
173 class CAddrMan
174 {
175 public:
176  // Compressed IP->ASN mapping, loaded from a file when a node starts.
177  // Should be always empty if no file was provided.
178  // This mapping is then used for bucketing nodes in Addrman.
179  //
180  // If asmap is provided, nodes will be bucketed by
181  // AS they belong to, in order to make impossible for a node
182  // to connect to several nodes hosted in a single AS.
183  // This is done in response to Erebus attack, but also to generally
184  // diversify the connections every node creates,
185  // especially useful when a large fraction of nodes
186  // operate under a couple of cloud providers.
187  //
188  // If a new asmap was provided, the existing records
189  // would be re-bucketed accordingly.
190  std::vector<bool> m_asmap;
191 
192  // Read asmap from provided binary file
193  static std::vector<bool> DecodeAsmap(fs::path path);
194 
232  template <typename Stream>
233  void Serialize(Stream& s_) const
235  {
236  LOCK(cs);
237 
238  // Always serialize in the latest version (FILE_FORMAT).
239 
240  OverrideStream<Stream> s(&s_, s_.GetType(), s_.GetVersion() | ADDRV2_FORMAT);
241 
242  s << static_cast<uint8_t>(FILE_FORMAT);
243 
244  // Increment `lowest_compatible` iff a newly introduced format is incompatible with
245  // the previous one.
246  static constexpr uint8_t lowest_compatible = Format::V3_BIP155;
247  s << static_cast<uint8_t>(INCOMPATIBILITY_BASE + lowest_compatible);
248 
249  s << nKey;
250  s << nNew;
251  s << nTried;
252 
253  int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
254  s << nUBuckets;
255  std::unordered_map<int, int> mapUnkIds;
256  int nIds = 0;
257  for (const auto& entry : mapInfo) {
258  mapUnkIds[entry.first] = nIds;
259  const CAddrInfo &info = entry.second;
260  if (info.nRefCount) {
261  assert(nIds != nNew); // this means nNew was wrong, oh ow
262  s << info;
263  nIds++;
264  }
265  }
266  nIds = 0;
267  for (const auto& entry : mapInfo) {
268  const CAddrInfo &info = entry.second;
269  if (info.fInTried) {
270  assert(nIds != nTried); // this means nTried was wrong, oh ow
271  s << info;
272  nIds++;
273  }
274  }
275  for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
276  int nSize = 0;
277  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
278  if (vvNew[bucket][i] != -1)
279  nSize++;
280  }
281  s << nSize;
282  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
283  if (vvNew[bucket][i] != -1) {
284  int nIndex = mapUnkIds[vvNew[bucket][i]];
285  s << nIndex;
286  }
287  }
288  }
289  // Store asmap checksum after bucket entries so that it
290  // can be ignored by older clients for backward compatibility.
291  uint256 asmap_checksum;
292  if (m_asmap.size() != 0) {
293  asmap_checksum = SerializeHash(m_asmap);
294  }
295  s << asmap_checksum;
296  }
297 
298  template <typename Stream>
299  void Unserialize(Stream& s_)
301  {
302  LOCK(cs);
303 
304  assert(vRandom.empty());
305 
306  Format format;
307  s_ >> Using<CustomUintFormatter<1>>(format);
308 
309  int stream_version = s_.GetVersion();
310  if (format >= Format::V3_BIP155) {
311  // Add ADDRV2_FORMAT to the version so that the CNetAddr and CAddress
312  // unserialize methods know that an address in addrv2 format is coming.
313  stream_version |= ADDRV2_FORMAT;
314  }
315 
316  OverrideStream<Stream> s(&s_, s_.GetType(), stream_version);
317 
318  uint8_t compat;
319  s >> compat;
320  const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
321  if (lowest_compatible > FILE_FORMAT) {
322  throw std::ios_base::failure(strprintf(
323  "Unsupported format of addrman database: %u. It is compatible with formats >=%u, "
324  "but the maximum supported by this version of %s is %u.",
325  format, lowest_compatible, PACKAGE_NAME, static_cast<uint8_t>(FILE_FORMAT)));
326  }
327 
328  s >> nKey;
329  s >> nNew;
330  s >> nTried;
331  int nUBuckets = 0;
332  s >> nUBuckets;
333  if (format >= Format::V1_DETERMINISTIC) {
334  nUBuckets ^= (1 << 30);
335  }
336 
337  if (nNew > ADDRMAN_NEW_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nNew < 0) {
338  throw std::ios_base::failure(
339  strprintf("Corrupt CAddrMan serialization: nNew=%d, should be in [0, %u]",
340  nNew,
342  }
343 
344  if (nTried > ADDRMAN_TRIED_BUCKET_COUNT * ADDRMAN_BUCKET_SIZE || nTried < 0) {
345  throw std::ios_base::failure(
346  strprintf("Corrupt CAddrMan serialization: nTried=%d, should be in [0, %u]",
347  nTried,
349  }
350 
351  // Deserialize entries from the new table.
352  for (int n = 0; n < nNew; n++) {
353  CAddrInfo &info = mapInfo[n];
354  s >> info;
355  mapAddr[info] = n;
356  info.nRandomPos = vRandom.size();
357  vRandom.push_back(n);
358  }
359  nIdCount = nNew;
360 
361  // Deserialize entries from the tried table.
362  int nLost = 0;
363  for (int n = 0; n < nTried; n++) {
364  CAddrInfo info;
365  s >> info;
366  int nKBucket = info.GetTriedBucket(nKey, m_asmap);
367  int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
368  if (vvTried[nKBucket][nKBucketPos] == -1) {
369  info.nRandomPos = vRandom.size();
370  info.fInTried = true;
371  vRandom.push_back(nIdCount);
372  mapInfo[nIdCount] = info;
373  mapAddr[info] = nIdCount;
374  vvTried[nKBucket][nKBucketPos] = nIdCount;
375  nIdCount++;
376  } else {
377  nLost++;
378  }
379  }
380  nTried -= nLost;
381 
382  // Store positions in the new table buckets to apply later (if possible).
383  // An entry may appear in up to ADDRMAN_NEW_BUCKETS_PER_ADDRESS buckets,
384  // so we store all bucket-entry_index pairs to iterate through later.
385  std::vector<std::pair<int, int>> bucket_entries;
386 
387  for (int bucket = 0; bucket < nUBuckets; ++bucket) {
388  int num_entries{0};
389  s >> num_entries;
390  for (int n = 0; n < num_entries; ++n) {
391  int entry_index{0};
392  s >> entry_index;
393  if (entry_index >= 0 && entry_index < nNew) {
394  bucket_entries.emplace_back(bucket, entry_index);
395  }
396  }
397  }
398 
399  // If the bucket count and asmap checksum haven't changed, then attempt
400  // to restore the entries to the buckets/positions they were in before
401  // serialization.
402  uint256 supplied_asmap_checksum;
403  if (m_asmap.size() != 0) {
404  supplied_asmap_checksum = SerializeHash(m_asmap);
405  }
406  uint256 serialized_asmap_checksum;
407  if (format >= Format::V2_ASMAP) {
408  s >> serialized_asmap_checksum;
409  }
410  const bool restore_bucketing{nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
411  serialized_asmap_checksum == supplied_asmap_checksum};
412 
413  if (!restore_bucketing) {
414  LogPrint(BCLog::ADDRMAN, "Bucketing method was updated, re-bucketing addrman entries from disk\n");
415  }
416 
417  for (auto bucket_entry : bucket_entries) {
418  int bucket{bucket_entry.first};
419  const int entry_index{bucket_entry.second};
420  CAddrInfo& info = mapInfo[entry_index];
421 
422  // The entry shouldn't appear in more than
423  // ADDRMAN_NEW_BUCKETS_PER_ADDRESS. If it has already, just skip
424  // this bucket_entry.
425  if (info.nRefCount >= ADDRMAN_NEW_BUCKETS_PER_ADDRESS) continue;
426 
427  int bucket_position = info.GetBucketPosition(nKey, true, bucket);
428  if (restore_bucketing && vvNew[bucket][bucket_position] == -1) {
429  // Bucketing has not changed, using existing bucket positions for the new table
430  vvNew[bucket][bucket_position] = entry_index;
431  ++info.nRefCount;
432  } else {
433  // In case the new table data cannot be used (bucket count wrong or new asmap),
434  // try to give them a reference based on their primary source address.
435  bucket = info.GetNewBucket(nKey, m_asmap);
436  bucket_position = info.GetBucketPosition(nKey, true, bucket);
437  if (vvNew[bucket][bucket_position] == -1) {
438  vvNew[bucket][bucket_position] = entry_index;
439  ++info.nRefCount;
440  }
441  }
442  }
443 
444  // Prune new entries with refcount 0 (as a result of collisions).
445  int nLostUnk = 0;
446  for (auto it = mapInfo.cbegin(); it != mapInfo.cend(); ) {
447  if (it->second.fInTried == false && it->second.nRefCount == 0) {
448  const auto itCopy = it++;
449  Delete(itCopy->first);
450  ++nLostUnk;
451  } else {
452  ++it;
453  }
454  }
455  if (nLost + nLostUnk > 0) {
456  LogPrint(BCLog::ADDRMAN, "addrman lost %i new and %i tried addresses due to collisions\n", nLostUnk, nLost);
457  }
458 
459  RemoveInvalid();
460 
461  Check();
462  }
463 
464  void Clear()
466  {
467  LOCK(cs);
468  std::vector<int>().swap(vRandom);
470  for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
471  for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
472  vvNew[bucket][entry] = -1;
473  }
474  }
475  for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
476  for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
477  vvTried[bucket][entry] = -1;
478  }
479  }
480 
481  nIdCount = 0;
482  nTried = 0;
483  nNew = 0;
484  nLastGood = 1; //Initially at 1 so that "never" is strictly worse.
485  mapInfo.clear();
486  mapAddr.clear();
487  }
488 
490  {
491  Clear();
492  }
493 
495  {
496  nKey.SetNull();
497  }
498 
500  size_t size() const
502  {
503  LOCK(cs); // TODO: Cache this in an atomic to avoid this overhead
504  return vRandom.size();
505  }
506 
508  bool Add(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty = 0)
510  {
511  LOCK(cs);
512  bool fRet = false;
513  Check();
514  fRet |= Add_(addr, source, nTimePenalty);
515  Check();
516  if (fRet) {
517  LogPrint(BCLog::ADDRMAN, "Added %s from %s: %i tried, %i new\n", addr.ToStringIPPort(), source.ToString(), nTried, nNew);
518  }
519  return fRet;
520  }
521 
523  bool Add(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty = 0)
525  {
526  LOCK(cs);
527  int nAdd = 0;
528  Check();
529  for (std::vector<CAddress>::const_iterator it = vAddr.begin(); it != vAddr.end(); it++)
530  nAdd += Add_(*it, source, nTimePenalty) ? 1 : 0;
531  Check();
532  if (nAdd) {
533  LogPrint(BCLog::ADDRMAN, "Added %i addresses from %s: %i tried, %i new\n", nAdd, source.ToString(), nTried, nNew);
534  }
535  return nAdd > 0;
536  }
537 
539  void Good(const CService &addr, int64_t nTime = GetAdjustedTime())
541  {
542  LOCK(cs);
543  Check();
544  Good_(addr, /* test_before_evict */ true, nTime);
545  Check();
546  }
547 
549  void Attempt(const CService &addr, bool fCountFailure, int64_t nTime = GetAdjustedTime())
551  {
552  LOCK(cs);
553  Check();
554  Attempt_(addr, fCountFailure, nTime);
555  Check();
556  }
557 
561  {
562  LOCK(cs);
563  Check();
565  Check();
566  }
567 
571  {
572  LOCK(cs);
573  Check();
574  const CAddrInfo ret = SelectTriedCollision_();
575  Check();
576  return ret;
577  }
578 
582  CAddrInfo Select(bool newOnly = false)
584  {
585  LOCK(cs);
586  Check();
587  const CAddrInfo addrRet = Select_(newOnly);
588  Check();
589  return addrRet;
590  }
591 
599  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network)
601  {
602  LOCK(cs);
603  Check();
604  std::vector<CAddress> vAddr;
605  GetAddr_(vAddr, max_addresses, max_pct, network);
606  Check();
607  return vAddr;
608  }
609 
611  void Connected(const CService &addr, int64_t nTime = GetAdjustedTime())
613  {
614  LOCK(cs);
615  Check();
616  Connected_(addr, nTime);
617  Check();
618  }
619 
620  void SetServices(const CService &addr, ServiceFlags nServices)
622  {
623  LOCK(cs);
624  Check();
625  SetServices_(addr, nServices);
626  Check();
627  }
628 
629 protected:
632 
635 
636 private:
638  mutable Mutex cs;
639 
641  enum Format : uint8_t {
644  V2_ASMAP = 2,
645  V3_BIP155 = 3,
646  };
647 
653  static constexpr Format FILE_FORMAT = Format::V3_BIP155;
654 
660  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
661 
663  int nIdCount GUARDED_BY(cs);
664 
666  std::unordered_map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
667 
669  std::unordered_map<CNetAddr, int, CNetAddrHash> mapAddr GUARDED_BY(cs);
670 
672  std::vector<int> vRandom GUARDED_BY(cs);
673 
674  // number of "tried" entries
675  int nTried GUARDED_BY(cs);
676 
679 
681  int nNew GUARDED_BY(cs);
682 
685 
687  int64_t nLastGood GUARDED_BY(cs);
688 
690  std::set<int> m_tried_collisions;
691 
693  CAddrInfo* Find(const CNetAddr& addr, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
694 
697  CAddrInfo* Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
698 
700  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs);
701 
703  void MakeTried(CAddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
704 
706  void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
707 
709  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
710 
712  void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
713 
715  bool Add_(const CAddress &addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
716 
718  void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
719 
722 
725 
728 
730  void Check()
732  {
733 #ifdef DEBUG_ADDRMAN
735  const int err = Check_();
736  if (err) {
737  LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
738  }
739 #endif
740  }
741 
742 #ifdef DEBUG_ADDRMAN
743  int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
745 #endif
746 
755  void GetAddr_(std::vector<CAddress>& vAddr, size_t max_addresses, size_t max_pct, std::optional<Network> network) EXCLUSIVE_LOCKS_REQUIRED(cs);
756 
768  void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
769 
771  void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
772 
775 
776  friend class CAddrManTest;
777 };
778 
779 #endif // BITCOIN_ADDRMAN_H
CAddrMan::Create
CAddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
find an entry, creating it if necessary.
Definition: addrman.cpp:127
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:34
CAddrMan::~CAddrMan
~CAddrMan()
Definition: addrman.h:494
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:560
CAddrMan::nKey
uint256 nKey
secret key to randomize bucket select with
Definition: addrman.h:631
CAddrInfo::GetNewBucket
int GetNewBucket(const uint256 &nKey, const CNetAddr &src, const std::vector< bool > &asmap) const
Calculate in which "new" bucket this entry belongs, given a certain source.
Definition: addrman.cpp:28
CAddrMan::Good_
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry "good", possibly moving it from "new" to "tried".
Definition: addrman.cpp:246
SerializeHash
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:192
assert
assert(!tx.IsCoinBase())
FastRandomContext::rand256
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:615
CAddrInfo::CAddrInfo
CAddrInfo()
Definition: addrman.h:74
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
CAddrInfo::source
CNetAddr source
where knowledge about this address first came from
Definition: addrman.h:43
ADDRMAN_TEST_WINDOW
static const int64_t ADDRMAN_TEST_WINDOW
the maximum time we'll spend trying to resolve a tried table collision, in seconds
Definition: addrman.h:168
CAddrMan::Add_
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Add an entry to the "new" table.
Definition: addrman.cpp:314
CAddrMan::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.h:690
CAddrMan::SwapRandom
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:141
fs.h
streams.h
ADDRMAN_NEW_BUCKETS_PER_ADDRESS
#define ADDRMAN_NEW_BUCKETS_PER_ADDRESS
in how many buckets for entries with new addresses a single address may occur
Definition: addrman.h:142
CAddrMan::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:163
CAddrMan::V1_DETERMINISTIC
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman.h:643
CAddrMan::GetAddr_
void GetAddr_(std::vector< CAddress > &vAddr, size_t max_addresses, size_t max_pct, std::optional< Network > network) EXCLUSIVE_LOCKS_REQUIRED(cs)
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.cpp:542
sync.h
timedata.h
CNetAddr
Network address.
Definition: netaddress.h:121
source
const char * source
Definition: rpcconsole.cpp:63
CAddrInfo::nLastSuccess
int64_t nLastSuccess
last successful connection by us
Definition: addrman.h:46
CAddrMan::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.h:660
base_blob::SetNull
void SetNull()
Definition: uint256.h:39
CAddrMan::CAddrManTest
friend class CAddrManTest
Definition: addrman.h:776
CAddrInfo::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:65
CAddrMan::ResolveCollisions_
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:618
AnnotatedMixin< std::mutex >
clientversion.h
CAddrInfo::SERIALIZE_METHODS
SERIALIZE_METHODS(CAddrInfo, obj)
Definition: addrman.h:64
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
CAddrInfo::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:45
CAddrMan::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.h:599
CAddrMan::Attempt_
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry as attempted to connect.
Definition: addrman.cpp:389
bitcoin-config.h
READWRITEAS
#define READWRITEAS(type, obj)
Definition: serialize.h:148
CService::ToStringIPPort
std::string ToStringIPPort() const
Definition: netaddress.cpp:1028
AssertLockHeld
AssertLockHeld(pool.cs)
CAddrMan::SetServices
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.h:620
CAddrMan::SetServices_
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update an entry's service bits.
Definition: addrman.cpp:598
tinyformat.h
CAddrInfo
Extended statistics about a CAddress.
Definition: addrman.h:32
CAddrInfo::nLastCountAttempt
int64_t nLastCountAttempt
last counted attempt (memory only)
Definition: addrman.h:39
CAddrMan::Select
CAddrInfo Select(bool newOnly=false) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Choose an address to connect to.
Definition: addrman.h:582
protocol.h
random.h
CAddrMan::Add
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Add a single address.
Definition: addrman.h:508
CAddrMan::Clear
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.h:464
CAddrMan::GUARDED_BY
int nIdCount GUARDED_BY(cs)
last used nId
CAddrMan::Connected
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Outer function for Connected_()
Definition: addrman.h:611
CAddrMan::SelectTriedCollision_
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Return a random to-be-evicted tried table address.
Definition: addrman.cpp:679
ADDRMAN_BUCKET_SIZE
#define ADDRMAN_BUCKET_SIZE
Definition: addrman.h:162
PACKAGE_NAME
#define PACKAGE_NAME
Definition: bitcoin-config.h:368
ADDRMAN_TRIED_BUCKET_COUNT
#define ADDRMAN_TRIED_BUCKET_COUNT
Convenience.
Definition: addrman.h:160
netaddress.h
CAddrMan::Serialize
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Serialized format.
Definition: addrman.h:233
CAddrMan::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:179
CAddrInfo::CAddrInfo
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman.h:70
CAddrInfo::nRefCount
int nRefCount
reference count in new sets (memory only)
Definition: addrman.h:52
LogPrintf
#define LogPrintf(...)
Definition: logging.h:184
CAddrMan::insecure_rand
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
Definition: addrman.h:634
CAddrInfo::nRandomPos
int nRandomPos
position in vRandom
Definition: addrman.h:58
CAddrMan::ResolveCollisions
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.h:559
CAddrMan::FILE_FORMAT
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman.h:653
CAddrInfo::nLastTry
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman.h:36
CAddrInfo::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:39
CAddrMan::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Add multiple addresses.
Definition: addrman.h:523
uint256
256-bit opaque blob.
Definition: uint256.h:124
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
CAddrMan::Format
Format
Serialization versions.
Definition: addrman.h:641
CAddrMan::RemoveInvalid
void RemoveInvalid() EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove invalid addresses.
Definition: addrman.cpp:80
LogPrint
#define LogPrint(category,...)
Definition: logging.h:188
CAddrMan::Check
void Check() EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check.
Definition: addrman.h:730
ADDRMAN_NEW_BUCKET_COUNT
#define ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman.h:161
CAddrMan::MakeTried
void MakeTried(CAddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Move an entry from the "new" table(s) to the "tried" table.
Definition: addrman.cpp:196
CAddrInfo::GetTriedBucket
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:18
OverrideStream
Definition: streams.h:26
CAddrMan::Connected_
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
We have successfully connected to this peer.
Definition: addrman.cpp:576
system.h
CAddrMan::Select_
CAddrInfo Select_(bool newOnly) EXCLUSIVE_LOCKS_REQUIRED(cs)
Select an address to connect to, if newOnly is set to true, only the new table is selected from.
Definition: addrman.cpp:413
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
CAddrMan
Stochastical (IP) address manager.
Definition: addrman.h:173
CAddrMan::m_asmap
std::vector< bool > m_asmap
Definition: addrman.h:190
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:34
CAddrMan::V2_ASMAP
@ V2_ASMAP
for files including asmap version
Definition: addrman.h:644
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
CAddrInfo::nAttempts
int nAttempts
connection attempts since last successful attempt
Definition: addrman.h:49
CAddrMan::V0_HISTORICAL
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman.h:642
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CAddrMan::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Mark an entry as connection attempted to.
Definition: addrman.h:549
LOCK
#define LOCK(cs)
Definition: sync.h:232
CAddrMan::Good
void Good(const CService &addr, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Mark an entry as accessible.
Definition: addrman.h:539
CAddrMan::CAddrMan
CAddrMan()
Definition: addrman.h:489
hash.h
BCLog::ADDRMAN
@ ADDRMAN
Definition: logging.h:47
CAddrMan::Find
CAddrInfo * Find(const CNetAddr &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:112
CAddrInfo::fInTried
bool fInTried
in tried set? (memory only)
Definition: addrman.h:55
CAddrMan::Unserialize
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.h:299
CAddrMan::V3_BIP155
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman.h:645
CAddrMan::DecodeAsmap
static std::vector< bool > DecodeAsmap(fs::path path)
Definition: addrman.cpp:708
CAddrMan::size
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return the number of (unique) addresses in all tables.
Definition: addrman.h:500
CAddrMan::SelectTriedCollision
CAddrInfo SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Randomly select an address in tried that another address is attempting to evict.
Definition: addrman.h:569
FastRandomContext
Fast randomness source.
Definition: random.h:119
CAddrMan::cs
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman.h:638
CAddrInfo::GetNewBucket
int GetNewBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "new" bucket this entry belongs, using its default source.
Definition: addrman.h:85