Bitcoin Core  28.1.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 <uint256.h>
15 #include <util/time.h>
16 
17 #include <cstdint>
18 #include <optional>
19 #include <set>
20 #include <unordered_map>
21 #include <unordered_set>
22 #include <utility>
23 #include <vector>
24 
26 static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
29 static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
32 static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
33 static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
34 
40 using nid_type = int64_t;
41 
45 class AddrInfo : public CAddress
46 {
47 public:
50 
53 
56 
59 
61  int nAttempts{0};
62 
64  int nRefCount{0};
65 
67  bool fInTried{false};
68 
70  mutable int nRandomPos{-1};
71 
73  {
74  READWRITE(AsBase<CAddress>(obj), obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
75  }
76 
77  AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
78  {
79  }
80 
82  {
83  }
84 
86  int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
87 
89  int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
90 
92  int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
93  {
94  return GetNewBucket(nKey, source, netgroupman);
95  }
96 
98  int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const;
99 
101  bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
102 
104  double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
105 };
106 
108 {
109 public:
110  AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
111 
112  ~AddrManImpl();
113 
114  template <typename Stream>
115  void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
116 
117  template <typename Stream>
118  void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
119 
120  size_t Size(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
121 
122  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
124 
125  bool Good(const CService& addr, NodeSeconds time)
127 
128  void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
130 
132 
133  std::pair<CAddress, NodeSeconds> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
134 
135  std::pair<CAddress, NodeSeconds> Select(bool new_only, std::optional<Network> network) const
137 
138  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const
140 
141  std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const
143 
144  void Connected(const CService& addr, NodeSeconds time)
146 
147  void SetServices(const CService& addr, ServiceFlags nServices)
149 
150  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
152 
153  friend class AddrManDeterministic;
154 
155 private:
157  mutable Mutex cs;
158 
160  mutable FastRandomContext insecure_rand GUARDED_BY(cs);
161 
164 
166  enum Format : uint8_t {
169  V2_ASMAP = 2,
170  V3_BIP155 = 3,
172  };
173 
179  static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
180 
186  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
187 
189  nid_type nIdCount GUARDED_BY(cs){0};
190 
192  std::unordered_map<nid_type, AddrInfo> mapInfo GUARDED_BY(cs);
193 
195  std::unordered_map<CService, nid_type, CServiceHash> mapAddr GUARDED_BY(cs);
196 
200  mutable std::vector<nid_type> vRandom GUARDED_BY(cs);
201 
202  // number of "tried" entries
203  int nTried GUARDED_BY(cs){0};
204 
207 
209  int nNew GUARDED_BY(cs){0};
210 
213 
215  NodeSeconds m_last_good GUARDED_BY(cs){1s};
216 
218  std::set<nid_type> m_tried_collisions;
219 
222 
225 
226  struct NewTriedCount {
227  size_t n_new;
228  size_t n_tried;
229  };
230 
232  std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
233 
235  AddrInfo* Find(const CService& addr, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
236 
238  AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
239 
241  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
242 
245 
247  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
248 
251 
254  bool AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
255 
256  bool Good_(const CService& addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
257 
258  bool Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
259 
260  void Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
261 
262  std::pair<CAddress, NodeSeconds> Select_(bool new_only, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
263 
268  nid_type GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
269 
270  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);
271 
272  std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs);
273 
274  void Connected_(const CService& addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
275 
276  void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
277 
279 
281 
283 
284  size_t Size_(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs);
285 
288  void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
289 
293 };
294 
295 #endif // BITCOIN_ADDRMAN_IMPL_H
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1239
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:186
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:869
ServiceFlags
nServices flags
Definition: protocol.h:309
const NetGroupManager & m_netgroupman
Reference to the netgroup manager.
Definition: addrman_impl.h:224
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:77
bool Good_(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:626
adds support for multiple ports per IP
Definition: addrman_impl.h:171
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:92
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:33
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:432
std::pair< CAddress, NodeSeconds > Select(bool new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1212
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:845
int64_t nid_type
User-defined type for the internally used nIds This used to be int, making it feasible for attackers ...
Definition: addrman_impl.h:40
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:27
void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:693
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:681
size_t Size_(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:1018
std::pair< CAddress, NodeSeconds > Select_(bool new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:713
int nRandomPos
position in vRandom
Definition: addrman_impl.h:70
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
Definition: addrman_impl.h:29
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:1055
size_t Size(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1160
int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:63
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
Definition: addrman_impl.h:26
Netgroup manager.
Definition: netgroup.h:16
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:170
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class...
Definition: serialize.h:495
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:163
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:967
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1195
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:23
FastRandomContext insecure_rand GUARDED_BY(cs)
Source of random numbers for randomization in inner loops.
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:61
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:904
const char * source
Definition: rpcconsole.cpp:60
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1203
NodeSeconds m_last_try
last try whatsoever by us (memory only)
Definition: addrman_impl.h:49
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:64
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1187
historic format, before commit e6b343d88
Definition: addrman_impl.h:167
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
Fast randomness source.
Definition: random.h:376
Extended statistics about a CAddress.
Definition: addrman_impl.h:45
NodeSeconds m_last_count_attempt
last counted attempt (memory only)
Definition: addrman_impl.h:52
A CService with information about it as peer.
Definition: protocol.h:366
nid_type 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:790
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:133
Network
A network type.
Definition: netaddress.h:32
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1169
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: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:473
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:1040
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:179
Format
Serialization versions.
Definition: addrman_impl.h:166
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:69
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:55
Network address.
Definition: netaddress.h:111
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:807
256-bit opaque blob.
Definition: uint256.h:178
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:231
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
bool Good(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1178
AddrManImpl(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:109
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:94
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1230
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:30
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:72
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:209
for files including asmap version
Definition: addrman_impl.h:169
void Delete(nid_type nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:456
AddrInfo * Find(const CService &addr, nid_type *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:401
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:203
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:67
std::optional< AddressPosition > FindAddressEntry_(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:995
void MakeTried(AddrInfo &info, nid_type nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Move an entry from the "new" table(s) to the "tried" table.
Definition: addrman.cpp:491
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&#39;s new table.
Definition: addrman.cpp:550
AddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, nid_type *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:416
nid_type nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:189
Location information for an address in AddrMan.
Definition: addrman.h:34
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:157
for pre-asmap files
Definition: addrman_impl.h:168
std::set< nid_type > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries. Test-before-evict disciplin...
Definition: addrman_impl.h:215
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:221
#define READWRITE(...)
Definition: serialize.h:156
int GetTriedBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:48
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1255
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1247
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:1221
NodeSeconds m_last_success
last successful connection by us
Definition: addrman_impl.h:58
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2
Maximum allowed number of entries in buckets for new and tried addresses.
Definition: addrman_impl.h:32
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:888