Bitcoin Core  24.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  READWRITEAS(CAddress, obj);
69  READWRITE(obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
70  }
71 
72  AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
73  {
74  }
75 
77  {
78  }
79 
81  int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
82 
84  int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
85 
87  int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
88  {
89  return GetNewBucket(nKey, source, netgroupman);
90  }
91 
93  int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
94 
96  bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
97 
99  double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
100 };
101 
103 {
104 public:
105  AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
106 
107  ~AddrManImpl();
108 
109  template <typename Stream>
110  void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
111 
112  template <typename Stream>
113  void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
114 
115  size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
116 
117  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
119 
120  bool Good(const CService& addr, NodeSeconds time)
122 
123  void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
125 
127 
128  std::pair<CAddress, NodeSeconds> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
129 
130  std::pair<CAddress, NodeSeconds> Select(bool newOnly) const
132 
133  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
135 
136  void Connected(const CService& addr, NodeSeconds time)
138 
139  void SetServices(const CService& addr, ServiceFlags nServices)
141 
142  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
144 
145  friend class AddrManDeterministic;
146 
147 private:
149  mutable Mutex cs;
150 
152  mutable FastRandomContext insecure_rand GUARDED_BY(cs);
153 
156 
158  enum Format : uint8_t {
161  V2_ASMAP = 2,
162  V3_BIP155 = 3,
164  };
165 
171  static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
172 
178  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
179 
181  int nIdCount GUARDED_BY(cs){0};
182 
184  std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
185 
187  std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
188 
192  mutable std::vector<int> vRandom GUARDED_BY(cs);
193 
194  // number of "tried" entries
195  int nTried GUARDED_BY(cs){0};
196 
199 
201  int nNew GUARDED_BY(cs){0};
202 
205 
207  NodeSeconds m_last_good GUARDED_BY(cs){1s};
208 
210  std::set<int> m_tried_collisions;
211 
214 
217 
219  AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
220 
222  AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
223 
225  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
226 
228  void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
229 
231  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
232 
234  void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
235 
238  bool AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
239 
240  bool Good_(const CService& addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
241 
242  bool Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
243 
244  void Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
245 
246  std::pair<CAddress, NodeSeconds> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
247 
248  std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
249 
250  void Connected_(const CService& addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
251 
252  void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
253 
255 
257 
259 
262  void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
263 
267 };
268 
269 #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:53
int GetTriedBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:46
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
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:87
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:92
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:67
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:72
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:58
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:61
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:473
std::pair< CAddress, NodeSeconds > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1120
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:816
void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:689
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:171
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:851
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:201
Format
Serialization versions.
Definition: addrman_impl.h:158
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:163
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:159
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:162
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:161
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:160
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:131
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1138
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1129
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1071
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:491
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1146
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:942
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:835
AddrManImpl(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:107
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:195
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:213
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:965
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:207
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:980
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1077
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1154
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:404
bool Good_(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:623
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:149
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:914
std::pair< CAddress, NodeSeconds > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:709
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1111
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:178
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:457
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:433
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1095
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:230
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:207
int nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:181
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:155
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1103
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:419
const NetGroupManager & m_netgroupman
Reference to the netgroup manager.
Definition: addrman_impl.h:216
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:778
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:677
bool Good(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1086
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:546
A CService with information about it as peer.
Definition: protocol.h:361
Network address.
Definition: netaddress.h:120
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
Fast randomness source.
Definition: random.h:143
Netgroup manager.
Definition: netgroup.h:16
256-bit opaque blob.
Definition: uint256.h:119
Network
A network type.
Definition: netaddress.h:44
ServiceFlags
nServices flags
Definition: protocol.h:273
const char * source
Definition: rpcconsole.cpp:59
#define READWRITEAS(type, obj)
Definition: serialize.h:141
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:433
#define READWRITE(...)
Definition: serialize.h:140
Test-only struct, capturing info about an address in AddrMan.
Definition: addrman.h:33
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25