Bitcoin ABC 0.32.4
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 <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
27static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
28static constexpr int ADDRMAN_TRIED_BUCKET_COUNT{
30
32static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
33static constexpr int ADDRMAN_NEW_BUCKET_COUNT{1
35
37static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
39
45using nid_type = int64_t;
46
50class AddrInfo : public CAddress {
51public:
54
57
60
63
65 int nAttempts{0};
66
68 int nRefCount{0};
69
71 bool fInTried{false};
72
74 mutable int nRandomPos{-1};
75
78 READWRITE(obj.source,
79 Using<ChronoFormatter<int64_t>>(obj.m_last_success),
80 obj.nAttempts);
81 }
82
83 AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
84 : CAddress(addrIn), source(addrSource) {}
85
87
89 int GetTriedBucket(const uint256 &nKey,
90 const std::vector<bool> &asmap) const;
91
94 int GetNewBucket(const uint256 &nKey, const CNetAddr &src,
95 const std::vector<bool> &asmap) const;
96
99 int GetNewBucket(const uint256 &nKey,
100 const std::vector<bool> &asmap) const {
101 return GetNewBucket(nKey, source, asmap);
102 }
103
105 int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
106
109 bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
110
113 double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
114};
115
117public:
118 AddrManImpl(std::vector<bool> &&asmap, bool deterministic,
119 int32_t consistency_check_ratio);
120
121 ~AddrManImpl();
122
123 template <typename Stream>
124 void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
125
126 template <typename Stream>
127 void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
128
129 size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
130
131 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
132 std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs);
133
134 void Good(const CService &addr, bool test_before_evict, NodeSeconds time)
136
137 void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time)
139
141
142 std::pair<CAddress, NodeSeconds> SelectTriedCollision()
144
145 std::pair<CAddress, NodeSeconds> Select(bool newOnly) const
147
148 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
149 std::optional<Network> network) const
151
152 void Connected(const CService &addr, NodeSeconds time)
154
155 void SetServices(const CService &addr, ServiceFlags nServices)
157
158 const std::vector<bool> &GetAsmap() const;
159
160 friend class AddrManTest;
161 friend class AddrManCorrupted;
162
163private:
165 mutable Mutex cs;
166
168 mutable FastRandomContext insecure_rand GUARDED_BY(cs);
169
172
174 enum Format : uint8_t {
185 };
186
192 static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
193
200 static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
201
203 nid_type nIdCount GUARDED_BY(cs){0};
204
206 std::unordered_map<nid_type, AddrInfo> mapInfo GUARDED_BY(cs);
207
209 std::unordered_map<CService, nid_type, CServiceHash> mapAddr GUARDED_BY(cs);
210
214 mutable std::vector<nid_type> vRandom GUARDED_BY(cs);
215
216 // number of "tried" entries
217 int nTried GUARDED_BY(cs){0};
218
222
224 int nNew GUARDED_BY(cs){0};
225
229
232 NodeSeconds m_last_good GUARDED_BY(cs){1s};
233
236 std::set<nid_type> m_tried_collisions;
237
243
244 // Compressed IP->ASN mapping, loaded from a file when a node starts.
245 // Should be always empty if no file was provided.
246 // This mapping is then used for bucketing nodes in Addrman.
247 //
248 // If asmap is provided, nodes will be bucketed by
249 // AS they belong to, in order to make impossible for a node
250 // to connect to several nodes hosted in a single AS.
251 // This is done in response to Erebus attack, but also to generally
252 // diversify the connections every node creates,
253 // especially useful when a large fraction of nodes
254 // operate under a couple of cloud providers.
255 //
256 // If a new asmap was provided, the existing records
257 // would be re-bucketed accordingly.
258 const std::vector<bool> m_asmap;
259
262 bool deterministic = false;
263
265 AddrInfo *Find(const CService &addr, nid_type *pnId = nullptr)
267
270 AddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource,
271 nid_type *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
272
274 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const
276
279
282 void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
283
286
291 bool AddSingle(const CAddress &addr, const CNetAddr &source,
292 std::chrono::seconds time_penalty)
294
295 void Good_(const CService &addr, bool test_before_evict, NodeSeconds time)
297
298 bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr &source,
299 std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
300
301 void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time)
303
304 std::pair<CAddress, NodeSeconds> Select_(bool newOnly) const
306
307 std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct,
308 std::optional<Network> network) const
310
311 void Connected_(const CService &addr, NodeSeconds time)
313
314 void SetServices_(const CService &addr, ServiceFlags nServices)
316
318
321
324 void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
325
329};
330
331#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:37
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
Definition: addrman_impl.h:32
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:38
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:45
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:33
Extended statistics about a CAddress.
Definition: addrman_impl.h:50
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:55
int nRandomPos
position in vRandom
Definition: addrman_impl.h:74
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:59
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:99
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:71
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:76
NodeSeconds m_last_success
last successful connection by us
Definition: addrman_impl.h:62
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:64
NodeSeconds m_last_count_attempt
last counted attempt (memory only)
Definition: addrman_impl.h:56
NodeSeconds m_last_try
last try whatsoever by us (memory only)
Definition: addrman_impl.h:53
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:117
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:86
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:83
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:68
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:77
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:65
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
Clear a position in a "new" table.
Definition: addrman.cpp:517
AddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, nid_type *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
find an entry, creating it if necessary.
Definition: addrman.cpp:462
std::pair< CAddress, NodeSeconds > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1232
friend class AddrManCorrupted
Definition: addrman_impl.h:161
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:913
void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:756
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:192
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:948
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:224
nid_type vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "tried" buckets
Format
Serialization versions.
Definition: addrman_impl.h:174
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:184
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:176
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:182
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:180
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:178
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:154
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:502
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1250
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1241
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1185
nid_type vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "new" buckets
FastRandomContext insecure_rand GUARDED_BY(cs)
Source of random numbers for randomization in inner loops.
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1257
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:535
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:932
std::unordered_map< CService, nid_type, CServiceHash > mapAddr GUARDED_BY(cs)
find an nId based on its network address and port.
AddrInfo * Find(const CService &addr, nid_type *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:445
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:217
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:242
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1264
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:1059
NodeSeconds m_last_good GUARDED_BY(cs)
last time Good was called (memory only).
Definition: addrman_impl.h:232
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:1077
nid_type nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:203
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1191
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:165
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:1029
std::pair< CAddress, NodeSeconds > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:777
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1224
bool deterministic
Use deterministic bucket selection and inner loops randomization.
Definition: addrman_impl.h:262
std::set< nid_type > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries.
Definition: addrman_impl.h:236
std::vector< nid_type > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
friend class AddrManTest
Definition: addrman_impl.h:160
void Good_(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:678
AddrManImpl(std::vector< bool > &&asmap, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:132
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:200
std::unordered_map< nid_type, AddrInfo > mapInfo GUARDED_BY(cs)
table with information about all nIds
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:477
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1209
void Good(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1201
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:260
const std::vector< bool > m_asmap
Definition: addrman_impl.h:258
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:171
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1217
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:869
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:740
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:594
A CService with information about it as peer.
Definition: protocol.h:443
Network address.
Definition: netaddress.h:121
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
Fast randomness source.
Definition: random.h:156
256-bit opaque blob.
Definition: uint256.h:129
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
Network
A network type.
Definition: netaddress.h:44
ServiceFlags
nServices flags.
Definition: protocol.h:336
const char * source
Definition: rpcconsole.cpp:56
#define READWRITEAS(type, obj)
Definition: serialize.h:169
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:564
#define READWRITE(...)
Definition: serialize.h:168
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25