Bitcoin ABC  0.24.7
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2016 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 <util/system.h>
20 
21 #include <tinyformat.h>
22 
23 #include <cstdint>
24 #include <iostream>
25 #include <map>
26 #include <set>
27 #include <vector>
28 
32 class CAddrInfo : public CAddress {
33 public:
35  int64_t nLastTry{0};
36 
38  int64_t nLastCountAttempt{0};
39 
40 private:
43 
45  int64_t nLastSuccess{0};
46 
48  int nAttempts{0};
49 
51  int nRefCount{0};
52 
54  bool fInTried{false};
55 
57  int nRandomPos{-1};
58 
59  friend class CAddrMan;
60 
61 public:
63  READWRITEAS(CAddress, obj);
64  READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts);
65  }
66 
67  CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
68  : CAddress(addrIn), source(addrSource) {}
69 
71 
73  int GetTriedBucket(const uint256 &nKey,
74  const std::vector<bool> &asmap) const;
75 
78  int GetNewBucket(const uint256 &nKey, const CNetAddr &src,
79  const std::vector<bool> &asmap) const;
80 
84  const std::vector<bool> &asmap) const {
85  return GetNewBucket(nKey, source, asmap);
86  }
87 
89  int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
90 
93  bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
94 
97  double GetChance(int64_t nNow = GetAdjustedTime()) const;
98 };
99 
138 #define ADDRMAN_TRIED_BUCKET_COUNT_LOG2 8
140 
142 #define ADDRMAN_NEW_BUCKET_COUNT_LOG2 10
143 
145 #define ADDRMAN_BUCKET_SIZE_LOG2 6
146 
149 #define ADDRMAN_TRIED_BUCKETS_PER_GROUP 8
150 
153 #define ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP 64
154 
157 #define ADDRMAN_NEW_BUCKETS_PER_ADDRESS 8
158 
160 #define ADDRMAN_HORIZON_DAYS 30
161 
163 #define ADDRMAN_RETRIES 3
164 
166 #define ADDRMAN_MAX_FAILURES 10
167 
169 #define ADDRMAN_MIN_FAIL_DAYS 7
170 
173 #define ADDRMAN_REPLACEMENT_SECONDS (4 * 60 * 60)
174 
176 #define ADDRMAN_TRIED_BUCKET_COUNT (1 << ADDRMAN_TRIED_BUCKET_COUNT_LOG2)
177 #define ADDRMAN_NEW_BUCKET_COUNT (1 << ADDRMAN_NEW_BUCKET_COUNT_LOG2)
178 #define ADDRMAN_BUCKET_SIZE (1 << ADDRMAN_BUCKET_SIZE_LOG2)
179 
181 #define ADDRMAN_SET_TRIED_COLLISION_SIZE 10
182 
185 static const int64_t ADDRMAN_TEST_WINDOW = 40 * 60;
186 
190 class CAddrMan {
191  friend class CAddrManTest;
192 
193 protected:
196 
197 private:
199  enum Format : uint8_t {
205  V2_ASMAP = 2,
208  };
209 
215  static constexpr Format FILE_FORMAT = Format::V3_BIP155;
216 
223  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
224 
226  int nIdCount GUARDED_BY(cs);
227 
229  std::map<int, CAddrInfo> mapInfo GUARDED_BY(cs);
230 
232  std::map<CNetAddr, int> mapAddr GUARDED_BY(cs);
233 
235  std::vector<int> vRandom GUARDED_BY(cs);
236 
237  // number of "tried" entries
238  int nTried GUARDED_BY(cs);
239 
242 
244  int nNew GUARDED_BY(cs);
245 
248 
250  int64_t nLastGood GUARDED_BY(cs);
251 
254  std::set<int> m_tried_collisions;
255 
256 protected:
259 
262 
264  CAddrInfo *Find(const CNetAddr &addr, int *pnId = nullptr)
266 
269  CAddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource,
270  int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
271 
273  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2)
275 
277  void MakeTried(CAddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
278 
280  void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
281 
284  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
285 
287  void Good_(const CService &addr, bool test_before_evict, int64_t time)
289 
291  bool Add_(const CAddress &addr, const CNetAddr &source,
292  int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
293 
295  void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime)
297 
301 
305 
308 
309 #ifdef DEBUG_ADDRMAN
310  int Check_() EXCLUSIVE_LOCKS_REQUIRED(cs);
312 #endif
313 
315  void GetAddr_(std::vector<CAddress> &vAddr, size_t max_addresses,
316  size_t max_pct) EXCLUSIVE_LOCKS_REQUIRED(cs);
317 
319  void Connected_(const CService &addr, int64_t nTime)
321 
323  void SetServices_(const CService &addr, ServiceFlags nServices)
325 
326 public:
327  // Compressed IP->ASN mapping, loaded from a file when a node starts.
328  // Should be always empty if no file was provided.
329  // This mapping is then used for bucketing nodes in Addrman.
330  //
331  // If asmap is provided, nodes will be bucketed by
332  // AS they belong to, in order to make impossible for a node
333  // to connect to several nodes hosted in a single AS.
334  // This is done in response to Erebus attack, but also to generally
335  // diversify the connections every node creates,
336  // especially useful when a large fraction of nodes
337  // operate under a couple of cloud providers.
338  //
339  // If a new asmap was provided, the existing records
340  // would be re-bucketed accordingly.
341  std::vector<bool> m_asmap;
342 
343  // Read asmap from provided binary file
344  static std::vector<bool> DecodeAsmap(fs::path path);
345 
386  template <typename Stream> void Serialize(Stream &s_) const {
387  LOCK(cs);
388 
389  // Always serialize in the latest version (FILE_FORMAT).
390 
391  OverrideStream<Stream> s(&s_, s_.GetType(),
392  s_.GetVersion() | ADDRV2_FORMAT);
393 
394  s << static_cast<uint8_t>(FILE_FORMAT);
395 
396  // Increment `lowest_compatible` iff a newly introduced format is
397  // incompatible with the previous one.
398  static constexpr uint8_t lowest_compatible = Format::V3_BIP155;
399  s << static_cast<uint8_t>(INCOMPATIBILITY_BASE + lowest_compatible);
400 
401  s << nKey;
402  s << nNew;
403  s << nTried;
404 
405  int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
406  s << nUBuckets;
407  std::map<int, int> mapUnkIds;
408  int nIds = 0;
409  for (const auto &entry : mapInfo) {
410  mapUnkIds[entry.first] = nIds;
411  const CAddrInfo &info = entry.second;
412  if (info.nRefCount) {
413  // this means nNew was wrong, oh ow
414  assert(nIds != nNew);
415  s << info;
416  nIds++;
417  }
418  }
419  nIds = 0;
420  for (const auto &entry : mapInfo) {
421  const CAddrInfo &info = entry.second;
422  if (info.fInTried) {
423  // this means nTried was wrong, oh ow
424  assert(nIds != nTried);
425  s << info;
426  nIds++;
427  }
428  }
429  for (int bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
430  int nSize = 0;
431  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
432  if (vvNew[bucket][i] != -1) nSize++;
433  }
434  s << nSize;
435  for (int i = 0; i < ADDRMAN_BUCKET_SIZE; i++) {
436  if (vvNew[bucket][i] != -1) {
437  int nIndex = mapUnkIds[vvNew[bucket][i]];
438  s << nIndex;
439  }
440  }
441  }
442  // Store asmap version after bucket entries so that it
443  // can be ignored by older clients for backward compatibility.
444  uint256 asmap_version;
445  if (m_asmap.size() != 0) {
446  asmap_version = SerializeHash(m_asmap);
447  }
448  s << asmap_version;
449  }
450 
451  template <typename Stream> void Unserialize(Stream &s_) {
452  LOCK(cs);
453 
454  Clear();
455 
456  Format format;
457  s_ >> Using<CustomUintFormatter<1>>(format);
458 
459  int stream_version = s_.GetVersion();
460  if (format >= Format::V3_BIP155) {
461  // Add ADDRV2_FORMAT to the version so that the CNetAddr and
462  // CAddress unserialize methods know that an address in addrv2
463  // format is coming.
464  stream_version |= ADDRV2_FORMAT;
465  }
466 
467  OverrideStream<Stream> s(&s_, s_.GetType(), stream_version);
468 
469  uint8_t compat;
470  s >> compat;
471  const uint8_t lowest_compatible = compat - INCOMPATIBILITY_BASE;
472  if (lowest_compatible > FILE_FORMAT) {
473  throw std::ios_base::failure(strprintf(
474  "Unsupported format of addrman database: %u. It is compatible "
475  "with formats >=%u, "
476  "but the maximum supported by this version of %s is %u.",
477  format, lowest_compatible, PACKAGE_NAME,
478  static_cast<uint8_t>(FILE_FORMAT)));
479  }
480 
481  s >> nKey;
482  s >> nNew;
483  s >> nTried;
484  int nUBuckets = 0;
485  s >> nUBuckets;
486  if (format >= Format::V1_DETERMINISTIC) {
487  nUBuckets ^= (1 << 30);
488  }
489 
491  throw std::ios_base::failure(
492  "Corrupt CAddrMan serialization, nNew exceeds limit.");
493  }
494 
496  throw std::ios_base::failure(
497  "Corrupt CAddrMan serialization, nTried exceeds limit.");
498  }
499 
500  // Deserialize entries from the new table.
501  for (int n = 0; n < nNew; n++) {
502  CAddrInfo &info = mapInfo[n];
503  s >> info;
504  mapAddr[info] = n;
505  info.nRandomPos = vRandom.size();
506  vRandom.push_back(n);
507  }
508  nIdCount = nNew;
509 
510  // Deserialize entries from the tried table.
511  int nLost = 0;
512  for (int n = 0; n < nTried; n++) {
513  CAddrInfo info;
514  s >> info;
515  int nKBucket = info.GetTriedBucket(nKey, m_asmap);
516  int nKBucketPos = info.GetBucketPosition(nKey, false, nKBucket);
517  if (vvTried[nKBucket][nKBucketPos] == -1) {
518  info.nRandomPos = vRandom.size();
519  info.fInTried = true;
520  vRandom.push_back(nIdCount);
521  mapInfo[nIdCount] = info;
522  mapAddr[info] = nIdCount;
523  vvTried[nKBucket][nKBucketPos] = nIdCount;
524  nIdCount++;
525  } else {
526  nLost++;
527  }
528  }
529  nTried -= nLost;
530 
531  // Store positions in the new table buckets to apply later (if
532  // possible). Represents which entry belonged to which bucket when
533  // serializing
534  std::map<int, int> entryToBucket;
535 
536  for (int bucket = 0; bucket < nUBuckets; bucket++) {
537  int nSize = 0;
538  s >> nSize;
539  for (int n = 0; n < nSize; n++) {
540  int nIndex = 0;
541  s >> nIndex;
542  if (nIndex >= 0 && nIndex < nNew) {
543  entryToBucket[nIndex] = bucket;
544  }
545  }
546  }
547 
548  uint256 supplied_asmap_version;
549  if (m_asmap.size() != 0) {
550  supplied_asmap_version = SerializeHash(m_asmap);
551  }
552  uint256 serialized_asmap_version;
553  if (format >= Format::V2_ASMAP) {
554  s >> serialized_asmap_version;
555  }
556 
557  for (int n = 0; n < nNew; n++) {
558  CAddrInfo &info = mapInfo[n];
559  int bucket = entryToBucket[n];
560  int nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
561  if (format >= Format::V2_ASMAP &&
562  nUBuckets == ADDRMAN_NEW_BUCKET_COUNT &&
563  vvNew[bucket][nUBucketPos] == -1 &&
565  serialized_asmap_version == supplied_asmap_version) {
566  // Bucketing has not changed, using existing bucket positions
567  // for the new table
568  vvNew[bucket][nUBucketPos] = n;
569  info.nRefCount++;
570  } else {
571  // In case the new table data cannot be used (format unknown,
572  // bucket count wrong or new asmap), try to give them a
573  // reference based on their primary source address.
575  "Bucketing method was updated, re-bucketing addrman "
576  "entries from disk\n");
577  bucket = info.GetNewBucket(nKey, m_asmap);
578  nUBucketPos = info.GetBucketPosition(nKey, true, bucket);
579  if (vvNew[bucket][nUBucketPos] == -1) {
580  vvNew[bucket][nUBucketPos] = n;
581  info.nRefCount++;
582  }
583  }
584  }
585 
586  // Prune new entries with refcount 0 (as a result of collisions).
587  int nLostUnk = 0;
588  for (std::map<int, CAddrInfo>::const_iterator it = mapInfo.begin();
589  it != mapInfo.end();) {
590  if (it->second.fInTried == false && it->second.nRefCount == 0) {
591  std::map<int, CAddrInfo>::const_iterator itCopy = it++;
592  Delete(itCopy->first);
593  nLostUnk++;
594  } else {
595  it++;
596  }
597  }
598  if (nLost + nLostUnk > 0) {
600  "addrman lost %i new and %i tried addresses due to "
601  "collisions\n",
602  nLostUnk, nLost);
603  }
604 
605  Check();
606  }
607 
608  void Clear() {
609  LOCK(cs);
610  std::vector<int>().swap(vRandom);
612  for (size_t bucket = 0; bucket < ADDRMAN_NEW_BUCKET_COUNT; bucket++) {
613  for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
614  vvNew[bucket][entry] = -1;
615  }
616  }
617  for (size_t bucket = 0; bucket < ADDRMAN_TRIED_BUCKET_COUNT; bucket++) {
618  for (size_t entry = 0; entry < ADDRMAN_BUCKET_SIZE; entry++) {
619  vvTried[bucket][entry] = -1;
620  }
621  }
622 
623  nIdCount = 0;
624  nTried = 0;
625  nNew = 0;
626  // Initially at 1 so that "never" is strictly worse.
627  nLastGood = 1;
628  mapInfo.clear();
629  mapAddr.clear();
630  }
631 
632  CAddrMan() { Clear(); }
633 
635 
637  size_t size() const {
638  // TODO: Cache this in an atomic to avoid this overhead
639  LOCK(cs);
640  return vRandom.size();
641  }
642 
644  void Check() {
645 #ifdef DEBUG_ADDRMAN
646  {
647  LOCK(cs);
648  int err;
649  if ((err = Check_())) {
650  LogPrintf("ADDRMAN CONSISTENCY CHECK FAILED!!! err=%i\n", err);
651  }
652  }
653 #endif
654  }
655 
657  bool Add(const CAddress &addr, const CNetAddr &source,
658  int64_t nTimePenalty = 0) {
659  LOCK(cs);
660  bool fRet = false;
661  Check();
662  fRet |= Add_(addr, source, nTimePenalty);
663  Check();
664  if (fRet) {
665  LogPrint(BCLog::ADDRMAN, "Added %s from %s: %i tried, %i new\n",
666  addr.ToStringIPPort(), source.ToString(), nTried, nNew);
667  }
668  return fRet;
669  }
670 
672  bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
673  int64_t nTimePenalty = 0) {
674  LOCK(cs);
675  int nAdd = 0;
676  Check();
677  for (const CAddress &a : vAddr) {
678  nAdd += Add_(a, source, nTimePenalty) ? 1 : 0;
679  }
680  Check();
681  if (nAdd) {
683  "Added %i addresses from %s: %i tried, %i new\n", nAdd,
684  source.ToString(), nTried, nNew);
685  }
686  return nAdd > 0;
687  }
688 
690  void Good(const CService &addr, bool test_before_evict = true,
691  int64_t nTime = GetAdjustedTime()) {
692  LOCK(cs);
693  Check();
694  Good_(addr, test_before_evict, nTime);
695  Check();
696  }
697 
699  void Attempt(const CService &addr, bool fCountFailure,
700  int64_t nTime = GetAdjustedTime()) {
701  LOCK(cs);
702  Check();
703  Attempt_(addr, fCountFailure, nTime);
704  Check();
705  }
706 
710  LOCK(cs);
711  Check();
713  Check();
714  }
715 
719  CAddrInfo ret;
720  {
721  LOCK(cs);
722  Check();
723  ret = SelectTriedCollision_();
724  Check();
725  }
726  return ret;
727  }
728 
732  CAddrInfo Select(bool newOnly = false) {
733  CAddrInfo addrRet;
734  {
735  LOCK(cs);
736  Check();
737  addrRet = Select_(newOnly);
738  Check();
739  }
740  return addrRet;
741  }
742 
744  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct) {
745  Check();
746  std::vector<CAddress> vAddr;
747  {
748  LOCK(cs);
749  GetAddr_(vAddr, max_addresses, max_pct);
750  }
751  Check();
752  return vAddr;
753  }
754 
756  void Connected(const CService &addr, int64_t nTime = GetAdjustedTime()) {
757  LOCK(cs);
758  Check();
759  Connected_(addr, nTime);
760  Check();
761  }
762 
763  void SetServices(const CService &addr, ServiceFlags nServices) {
764  LOCK(cs);
765  Check();
766  SetServices_(addr, nServices);
767  Check();
768  }
769 };
770 
771 #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:115
CAddrMan::~CAddrMan
~CAddrMan()
Definition: addrman.h:634
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
CAddrMan::nKey
uint256 nKey
secret key to randomize bucket select with
Definition: addrman.h:258
ADDRV2_FORMAT
static const int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:32
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:29
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:223
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:175
FastRandomContext::rand256
uint256 rand256() noexcept
generate a random uint256.
Definition: random.cpp:680
CAddrInfo::CAddrInfo
CAddrInfo()
Definition: addrman.h:70
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:1111
CAddrInfo::source
CNetAddr source
where knowledge about this address first came from
Definition: addrman.h:42
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 (40 minutes)
Definition: addrman.h:185
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:301
CAddrMan::m_tried_collisions
std::set< int > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries.
Definition: addrman.h:254
CAddrMan::SwapRandom
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:128
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:157
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:148
CAddrMan::V1_DETERMINISTIC
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman.h:203
sync.h
CAddrMan::Add
bool Add(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty=0)
Add a single address.
Definition: addrman.h:657
timedata.h
CNetAddr
Network address.
Definition: netaddress.h:117
source
const char * source
Definition: rpcconsole.cpp:53
CAddrMan::Serialize
void Serialize(Stream &s_) const
Serialized format.
Definition: addrman.h:386
CAddrInfo::nLastSuccess
int64_t nLastSuccess
last successful connection by us
Definition: addrman.h:45
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:223
base_blob::SetNull
void SetNull()
Definition: uint256.h:39
CAddrMan::CAddrManTest
friend class CAddrManTest
Definition: addrman.h:191
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:84
CAddrMan::Select
CAddrInfo Select(bool newOnly=false)
Choose an address to connect to.
Definition: addrman.h:732
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:641
AnnotatedMixin< std::recursive_mutex >
clientversion.h
CAddrInfo::SERIALIZE_METHODS
SERIALIZE_METHODS(CAddrInfo, obj)
Definition: addrman.h:62
ServiceFlags
ServiceFlags
nServices flags.
Definition: protocol.h:314
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:54
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:385
READWRITEAS
#define READWRITEAS(type, obj)
Definition: serialize.h:180
CService::ToStringIPPort
std::string ToStringIPPort() const
Definition: netaddress.cpp:1012
CAddrMan::SetServices_
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update an entry's service bits.
Definition: addrman.cpp:621
tinyformat.h
CAddrMan::cs
RecursiveMutex cs
critical section to protect the inner data structures
Definition: addrman.h:195
CAddrInfo
Extended statistics about a CAddress.
Definition: addrman.h:32
CAddrInfo::nLastCountAttempt
int64_t nLastCountAttempt
last counted attempt (memory only)
Definition: addrman.h:38
protocol.h
random.h
CAddrMan::GUARDED_BY
int nIdCount GUARDED_BY(cs)
last used nId
CAddrMan::SelectTriedCollision_
CAddrInfo SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Return a random to-be-evicted tried table address.
Definition: addrman.cpp:717
ADDRMAN_BUCKET_SIZE
#define ADDRMAN_BUCKET_SIZE
Definition: addrman.h:178
ADDRMAN_TRIED_BUCKET_COUNT
#define ADDRMAN_TRIED_BUCKET_COUNT
Convenience.
Definition: addrman.h:176
netaddress.h
CAddrMan::ClearNew
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
Clear a position in a "new" table.
Definition: addrman.cpp:161
CAddrInfo::CAddrInfo
CAddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman.h:67
CAddrInfo::nRefCount
int nRefCount
reference count in new sets (memory only)
Definition: addrman.h:51
CAddrMan::insecure_rand
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
Definition: addrman.h:261
CAddrInfo::nRandomPos
int nRandomPos
position in vRandom
Definition: addrman.h:57
CAddrMan::FILE_FORMAT
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman.h:215
CAddrInfo::nLastTry
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman.h:35
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:46
uint256
256-bit opaque blob.
Definition: uint256.h:127
READWRITE
#define READWRITE(...)
Definition: serialize.h:179
CAddrMan::Format
Format
Serialization versions.
Definition: addrman.h:199
LogPrint
#define LogPrint(category,...)
Definition: logging.h:193
CAddrMan::Unserialize
void Unserialize(Stream &s_)
Definition: addrman.h:451
ADDRMAN_NEW_BUCKET_COUNT
#define ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman.h:177
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:175
CAddrInfo::GetTriedBucket
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:14
OverrideStream
Definition: streams.h:23
CAddrMan::Connected_
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry as currently-connected-to.
Definition: addrman.cpp:598
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:410
CAddrMan::Check
void Check()
Consistency check.
Definition: addrman.h:644
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
CAddrMan
Stochastical (IP) address manager.
Definition: addrman.h:190
CAddrMan::m_asmap
std::vector< bool > m_asmap
Definition: addrman.h:341
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:34
CAddrMan::V2_ASMAP
@ V2_ASMAP
for files including asmap version
Definition: addrman.h:205
CAddress
A CService with information about it as peer.
Definition: protocol.h:421
CAddrInfo::nAttempts
int nAttempts
connection attempts since last successful attempt
Definition: addrman.h:48
CAddrMan::V0_HISTORICAL
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman.h:201
CAddrMan::SelectTriedCollision
CAddrInfo SelectTriedCollision()
Randomly select an address in tried that another address is attempting to evict.
Definition: addrman.h:718
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
LOCK
#define LOCK(cs)
Definition: sync.h:241
CAddrMan::Connected
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as currently-connected-to.
Definition: addrman.h:756
CAddrMan::Clear
void Clear()
Definition: addrman.h:608
CAddrMan::CAddrMan
CAddrMan()
Definition: addrman.h:632
CAddrMan::Good
void Good(const CService &addr, bool test_before_evict=true, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible.
Definition: addrman.h:690
hash.h
CAddrMan::SetServices
void SetServices(const CService &addr, ServiceFlags nServices)
Definition: addrman.h:763
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:100
CAddrInfo::fInTried
bool fInTried
in tried set? (memory only)
Definition: addrman.h:54
CAddrMan::size
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.h:637
CAddrMan::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct)
Return a bunch of addresses, selected at random.
Definition: addrman.h:744
CAddrMan::V3_BIP155
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman.h:207
CAddrMan::DecodeAsmap
static std::vector< bool > DecodeAsmap(fs::path path)
Definition: addrman.cpp:746
CAddrMan::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Add multiple addresses.
Definition: addrman.h:672
CAddrMan::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.h:699
CAddrMan::GetAddr_
void GetAddr_(std::vector< CAddress > &vAddr, size_t max_addresses, size_t max_pct) EXCLUSIVE_LOCKS_REQUIRED(cs)
Select several addresses at once.
Definition: addrman.cpp:571
LogPrintf
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:175
CAddrMan::ResolveCollisions
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.h:709
FastRandomContext
Fast randomness source.
Definition: random.h:129
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:83