Bitcoin ABC  0.26.3
P2P Digital Currency
addrman_impl.h
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_ADDRMAN_IMPL_H
6 #define BITCOIN_ADDRMAN_IMPL_H
7 
8 #include <logging.h>
9 #include <logging/timer.h>
10 #include <netaddress.h>
11 #include <protocol.h>
12 #include <serialize.h>
13 #include <sync.h>
14 #include <uint256.h>
15 
16 #include <cstdint>
17 #include <optional>
18 #include <set>
19 #include <unordered_map>
20 #include <unordered_set>
21 #include <utility>
22 #include <vector>
23 
25 static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
26 static constexpr int ADDRMAN_TRIED_BUCKET_COUNT{
28 
30 static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
31 static constexpr int ADDRMAN_NEW_BUCKET_COUNT{1
33 
35 static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
36 static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
37 
41 class AddrInfo : public CAddress {
42 public:
44  int64_t nLastTry{0};
45 
47  int64_t nLastCountAttempt{0};
48 
51 
53  int64_t nLastSuccess{0};
54 
56  int nAttempts{0};
57 
59  int nRefCount{0};
60 
62  bool fInTried{false};
63 
65  mutable int nRandomPos{-1};
66 
68  READWRITEAS(CAddress, obj);
69  READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts);
70  }
71 
72  AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
73  : CAddress(addrIn), source(addrSource) {}
74 
75  AddrInfo() : CAddress(), source() {}
76 
78  int GetTriedBucket(const uint256 &nKey,
79  const std::vector<bool> &asmap) const;
80 
83  int GetNewBucket(const uint256 &nKey, const CNetAddr &src,
84  const std::vector<bool> &asmap) const;
85 
88  int GetNewBucket(const uint256 &nKey,
89  const std::vector<bool> &asmap) const {
90  return GetNewBucket(nKey, source, asmap);
91  }
92 
94  int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
95 
98  bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
99 
102  double GetChance(int64_t nNow = GetAdjustedTime()) const;
103 };
104 
105 class AddrManImpl {
106 public:
107  AddrManImpl(std::vector<bool> &&asmap, int32_t consistency_check_ratio);
108 
109  ~AddrManImpl();
110 
111  template <typename Stream>
112  void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
113 
114  template <typename Stream>
115  void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
116 
117  size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
118 
119  bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
120  int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs);
121 
122  void Good(const CService &addr, bool test_before_evict, int64_t nTime)
124 
125  void Attempt(const CService &addr, bool fCountFailure, int64_t nTime)
127 
129 
130  std::pair<CAddress, int64_t> SelectTriedCollision()
132 
133  std::pair<CAddress, int64_t> Select(bool newOnly) const
135 
136  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
137  std::optional<Network> network) const
139 
140  void Connected(const CService &addr, int64_t nTime)
142 
143  void SetServices(const CService &addr, ServiceFlags nServices)
145 
146  const std::vector<bool> &GetAsmap() const;
147 
149 
151 
152  friend class AddrManTest;
153  friend class AddrManCorrupted;
154 
155 private:
157  mutable Mutex cs;
158 
161 
164 
166  enum Format : uint8_t {
172  V2_ASMAP = 2,
177  };
178 
184  static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
185 
192  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
193 
195  int nIdCount GUARDED_BY(cs);
196 
198  std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
199 
201  std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
202 
206  mutable std::vector<int> vRandom GUARDED_BY(cs);
207 
208  // number of "tried" entries
209  int nTried GUARDED_BY(cs);
210 
213 
215  int nNew GUARDED_BY(cs);
216 
219 
221  int64_t nLastGood GUARDED_BY(cs);
222 
225  std::set<int> m_tried_collisions;
226 
232 
233  // Compressed IP->ASN mapping, loaded from a file when a node starts.
234  // Should be always empty if no file was provided.
235  // This mapping is then used for bucketing nodes in Addrman.
236  //
237  // If asmap is provided, nodes will be bucketed by
238  // AS they belong to, in order to make impossible for a node
239  // to connect to several nodes hosted in a single AS.
240  // This is done in response to Erebus attack, but also to generally
241  // diversify the connections every node creates,
242  // especially useful when a large fraction of nodes
243  // operate under a couple of cloud providers.
244  //
245  // If a new asmap was provided, the existing records
246  // would be re-bucketed accordingly.
247  const std::vector<bool> m_asmap;
248 
251  bool deterministic = false;
252 
254  AddrInfo *Find(const CService &addr, int *pnId = nullptr)
256 
259  AddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource,
260  int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
261 
263  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const
265 
267  void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
268 
271  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
272 
274  void MakeTried(AddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
275 
276  void Good_(const CService &addr, bool test_before_evict, int64_t time)
278 
279  bool Add_(const CAddress &addr, const CNetAddr &source,
280  int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
281 
282  void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime)
284 
285  std::pair<CAddress, int64_t> Select_(bool newOnly) const
287 
288  std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct,
289  std::optional<Network> network) const
291 
292  void Connected_(const CService &addr, int64_t nTime)
294 
295  void SetServices_(const CService &addr, ServiceFlags nServices)
297 
299 
300  std::pair<CAddress, int64_t> SelectTriedCollision_()
302 
305  void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
306 
310 };
311 
312 #endif // BITCOIN_ADDRMAN_IMPL_H
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:26
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
Definition: addrman_impl.h:25
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2
Maximum allowed number of entries in buckets for new and tried addresses.
Definition: addrman_impl.h:35
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
Definition: addrman_impl.h:30
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:36
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:31
Extended statistics about a CAddress.
Definition: addrman_impl.h:41
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:58
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:90
int nRandomPos
position in vRandom
Definition: addrman_impl.h:65
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:120
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:50
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_impl.h:88
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:62
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:67
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:69
int64_t nLastCountAttempt
last counted attempt (memory only)
Definition: addrman_impl.h:47
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman_impl.h:44
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:72
int64_t nLastSuccess
last successful connection by us
Definition: addrman_impl.h:53
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:59
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:82
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:56
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
Clear a position in a "new" table.
Definition: addrman.cpp:508
friend class AddrManCorrupted
Definition: addrman_impl.h:153
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:184
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:928
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Format
Serialization versions.
Definition: addrman_impl.h:166
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:176
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:168
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:174
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:172
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:170
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:145
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1228
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1165
std::vector< int > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
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:526
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1244
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "tried" buckets
AddrManImpl(std::vector< bool > &&asmap, int32_t consistency_check_ratio)
Definition: addrman.cpp:136
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1171
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:647
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1196
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:912
void Connected(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1237
int nTried GUARDED_BY(cs)
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:231
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1251
std::pair< CAddress, int64_t > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1219
void MakeDeterministic() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1285
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:1039
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:1057
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:436
int64_t nLastGood GUARDED_BY(cs)
last time Good was called (memory only)
std::pair< CAddress, int64_t > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:1009
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:157
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:585
std::pair< CAddress, int64_t > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:757
bool deterministic
Use deterministic bucket selection and inner loops randomization.
Definition: addrman_impl.h:251
std::unordered_map< CService, int, CServiceHash > mapAddr GUARDED_BY(cs)
find an nId based on its network address and port.
friend class AddrManTest
Definition: addrman_impl.h:152
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "new" buckets
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:192
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:493
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:468
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
Definition: addrman_impl.h:160
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:893
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1255
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:251
std::set< int > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries.
Definition: addrman_impl.h:225
int nIdCount GUARDED_BY(cs)
last used nId
void Good(const CService &addr, bool test_before_evict, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1188
const std::vector< bool > m_asmap
Definition: addrman_impl.h:247
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:163
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1204
AddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
find an entry, creating it if necessary.
Definition: addrman.cpp:453
std::pair< CAddress, int64_t > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1211
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:849
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:736
std::unordered_map< int, AddrInfo > mapInfo GUARDED_BY(cs)
table with information about all nIds
A CService with information about it as peer.
Definition: protocol.h:445
Network address.
Definition: netaddress.h:121
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:544
Fast randomness source.
Definition: random.h:129
256-bit opaque blob.
Definition: uint256.h:127
Network
A network type.
Definition: netaddress.h:44
ServiceFlags
nServices flags.
Definition: protocol.h:338
const char * source
Definition: rpcconsole.cpp:53
#define READWRITEAS(type, obj)
Definition: serialize.h:181
#define READWRITE(...)
Definition: serialize.h:180
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetAdjustedTime()
Definition: timedata.cpp:34