Bitcoin Core  27.99.0
P2P Digital Currency
addrman_impl.h
Go to the documentation of this file.
1 // Copyright (c) 2021-2022 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 <timedata.h>
15 #include <uint256.h>
16 #include <util/time.h>
17 
18 #include <cstdint>
19 #include <optional>
20 #include <set>
21 #include <unordered_map>
22 #include <unordered_set>
23 #include <utility>
24 #include <vector>
25 
27 static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
30 static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
33 static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
34 static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
35 
39 class AddrInfo : public CAddress
40 {
41 public:
44 
47 
50 
53 
55  int nAttempts{0};
56 
58  int nRefCount{0};
59 
61  bool fInTried{false};
62 
64  mutable int nRandomPos{-1};
65 
67  {
68  READWRITE(AsBase<CAddress>(obj), obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
69  }
70 
71  AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
72  {
73  }
74 
76  {
77  }
78 
80  int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
81 
83  int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
84 
86  int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
87  {
88  return GetNewBucket(nKey, source, netgroupman);
89  }
90 
92  int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const;
93 
95  bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
96 
98  double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
99 };
100 
102 {
103 public:
104  AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
105 
106  ~AddrManImpl();
107 
108  template <typename Stream>
109  void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
110 
111  template <typename Stream>
112  void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
113 
114  size_t Size(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
115 
116  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
118 
119  bool Good(const CService& addr, NodeSeconds time)
121 
122  void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
124 
126 
127  std::pair<CAddress, NodeSeconds> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
128 
129  std::pair<CAddress, NodeSeconds> Select(bool new_only, std::optional<Network> network) const
131 
132  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const
134 
135  std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const
137 
138  void Connected(const CService& addr, NodeSeconds time)
140 
141  void SetServices(const CService& addr, ServiceFlags nServices)
143 
144  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
146 
147  friend class AddrManDeterministic;
148 
149 private:
151  mutable Mutex cs;
152 
154  mutable FastRandomContext insecure_rand GUARDED_BY(cs);
155 
158 
160  enum Format : uint8_t {
163  V2_ASMAP = 2,
164  V3_BIP155 = 3,
166  };
167 
173  static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
174 
180  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
181 
183  int nIdCount GUARDED_BY(cs){0};
184 
186  std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
187 
189  std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
190 
194  mutable std::vector<int> vRandom GUARDED_BY(cs);
195 
196  // number of "tried" entries
197  int nTried GUARDED_BY(cs){0};
198 
201 
203  int nNew GUARDED_BY(cs){0};
204 
207 
209  NodeSeconds m_last_good GUARDED_BY(cs){1s};
210 
212  std::set<int> m_tried_collisions;
213 
216 
219 
220  struct NewTriedCount {
221  size_t n_new;
222  size_t n_tried;
223  };
224 
226  std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
227 
229  AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
230 
232  AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
233 
235  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
236 
238  void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
239 
241  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
242 
244  void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
245 
248  bool AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
249 
250  bool Good_(const CService& addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
251 
252  bool Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
253 
254  void Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
255 
256  std::pair<CAddress, NodeSeconds> Select_(bool new_only, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
257 
262  int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
263 
264  std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
265 
266  std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs);
267 
268  void Connected_(const CService& addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
269 
270  void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
271 
273 
275 
277 
278  size_t Size_(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs);
279 
282  void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
283 
287 };
288 
289 #endif // BITCOIN_ADDRMAN_IMPL_H
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:28
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
Definition: addrman_impl.h:27
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2
Maximum allowed number of entries in buckets for new and tried addresses.
Definition: addrman_impl.h:33
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:34
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:31
Extended statistics about a CAddress.
Definition: addrman_impl.h:40
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:57
int GetTriedBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:50
int nRandomPos
position in vRandom
Definition: addrman_impl.h:64
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:49
int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:65
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:61
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:66
NodeSeconds m_last_success
last successful connection by us
Definition: addrman_impl.h:52
NodeSeconds m_last_count_attempt
last counted attempt (memory only)
Definition: addrman_impl.h:46
NodeSeconds m_last_try
last try whatsoever by us (memory only)
Definition: addrman_impl.h:43
int GetNewBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "new" bucket this entry belongs, using its default source.
Definition: addrman_impl.h:86
double GetChance(NodeSeconds now=Now< NodeSeconds >()) const
Calculate the relative chance this entry should be given when selecting nodes to connect to.
Definition: addrman.cpp:96
bool IsTerrible(NodeSeconds now=Now< NodeSeconds >()) const
Determine whether the statistics about this entry are bad enough so that it can just be deleted.
Definition: addrman.cpp:71
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:71
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:58
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:55
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:475
int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper to generalize looking up an addrman entry from either table.
Definition: addrman.cpp:791
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:870
void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:695
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:173
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:905
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:203
Format
Serialization versions.
Definition: addrman_impl.h:160
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:165
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:161
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:164
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:163
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:162
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:135
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1240
std::vector< int > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
FastRandomContext insecure_rand GUARDED_BY(cs)
Source of random numbers for randomization in inner loops.
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:493
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1248
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "tried" buckets
std::optional< AddressPosition > FindAddressEntry_(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:996
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:889
AddrManImpl(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:111
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:197
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1231
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:215
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:846
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:1041
NodeSeconds m_last_good GUARDED_BY(cs)
last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
Definition: addrman_impl.h:209
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:1056
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1170
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1256
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:403
std::unordered_map< Network, NewTriedCount > m_network_counts GUARDED_BY(cs)
Number of entries in addrman per network and new/tried table.
bool Good_(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:628
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:151
size_t Size_(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:1019
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:968
std::pair< CAddress, NodeSeconds > Select(bool new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1213
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1204
std::unordered_map< CService, int, CServiceHash > mapAddr GUARDED_BY(cs)
find an nId based on its network address and port.
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:180
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:458
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:434
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:808
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1188
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:233
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:209
int nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:183
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:157
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1196
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:418
const NetGroupManager & m_netgroupman
Reference to the netgroup manager.
Definition: addrman_impl.h:218
std::pair< CAddress, NodeSeconds > Select_(bool new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:715
std::unordered_map< int, AddrInfo > mapInfo GUARDED_BY(cs)
table with information about all nIds
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:683
bool Good(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1179
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1222
size_t Size(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1161
bool AddSingle(const CAddress &addr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Attempt to add a single address to addrman's new table.
Definition: addrman.cpp:552
A CService with information about it as peer.
Definition: protocol.h:332
Network address.
Definition: netaddress.h:112
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Fast randomness source.
Definition: random.h:145
Netgroup manager.
Definition: netgroup.h:16
256-bit opaque blob.
Definition: uint256.h:106
Network
A network type.
Definition: netaddress.h:32
ServiceFlags
nServices flags
Definition: protocol.h:274
const char * source
Definition: rpcconsole.cpp:59
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:510
#define READWRITE(...)
Definition: serialize.h:156
Location information for an address in AddrMan.
Definition: addrman.h:34
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:23