Bitcoin ABC 0.32.5
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
77 READWRITE(AsBase<CAddress>(obj), obj.source,
78 Using<ChronoFormatter<int64_t>>(obj.m_last_success),
79 obj.nAttempts);
80 }
81
82 AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
83 : CAddress(addrIn), source(addrSource) {}
84
86
88 int GetTriedBucket(const uint256 &nKey,
89 const std::vector<bool> &asmap) const;
90
93 int GetNewBucket(const uint256 &nKey, const CNetAddr &src,
94 const std::vector<bool> &asmap) const;
95
98 int GetNewBucket(const uint256 &nKey,
99 const std::vector<bool> &asmap) const {
100 return GetNewBucket(nKey, source, asmap);
101 }
102
104 int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
105
108 bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
109
112 double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
113};
114
116public:
117 AddrManImpl(std::vector<bool> &&asmap, bool deterministic,
118 int32_t consistency_check_ratio);
119
120 ~AddrManImpl();
121
122 template <typename Stream>
123 void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
124
125 template <typename Stream>
126 void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
127
128 size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
129
130 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
131 std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs);
132
133 void Good(const CService &addr, bool test_before_evict, NodeSeconds time)
135
136 void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time)
138
140
141 std::pair<CAddress, NodeSeconds> SelectTriedCollision()
143
144 std::pair<CAddress, NodeSeconds> Select(bool newOnly) const
146
147 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
148 std::optional<Network> network) const
150
151 void Connected(const CService &addr, NodeSeconds time)
153
154 void SetServices(const CService &addr, ServiceFlags nServices)
156
157 const std::vector<bool> &GetAsmap() const;
158
159 friend class AddrManTest;
160 friend class AddrManCorrupted;
161
162private:
164 mutable Mutex cs;
165
167 mutable FastRandomContext insecure_rand GUARDED_BY(cs);
168
171
173 enum Format : uint8_t {
184 };
185
191 static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
192
199 static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
200
202 nid_type nIdCount GUARDED_BY(cs){0};
203
205 std::unordered_map<nid_type, AddrInfo> mapInfo GUARDED_BY(cs);
206
208 std::unordered_map<CService, nid_type, CServiceHash> mapAddr GUARDED_BY(cs);
209
213 mutable std::vector<nid_type> vRandom GUARDED_BY(cs);
214
215 // number of "tried" entries
216 int nTried GUARDED_BY(cs){0};
217
221
223 int nNew GUARDED_BY(cs){0};
224
228
231 NodeSeconds m_last_good GUARDED_BY(cs){1s};
232
235 std::set<nid_type> m_tried_collisions;
236
242
243 // Compressed IP->ASN mapping, loaded from a file when a node starts.
244 // Should be always empty if no file was provided.
245 // This mapping is then used for bucketing nodes in Addrman.
246 //
247 // If asmap is provided, nodes will be bucketed by
248 // AS they belong to, in order to make impossible for a node
249 // to connect to several nodes hosted in a single AS.
250 // This is done in response to Erebus attack, but also to generally
251 // diversify the connections every node creates,
252 // especially useful when a large fraction of nodes
253 // operate under a couple of cloud providers.
254 //
255 // If a new asmap was provided, the existing records
256 // would be re-bucketed accordingly.
257 const std::vector<bool> m_asmap;
258
261 bool deterministic = false;
262
264 AddrInfo *Find(const CService &addr, nid_type *pnId = nullptr)
266
269 AddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource,
270 nid_type *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
271
273 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const
275
278
281 void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
282
285
290 bool AddSingle(const CAddress &addr, const CNetAddr &source,
291 std::chrono::seconds time_penalty)
293
294 void Good_(const CService &addr, bool test_before_evict, NodeSeconds time)
296
297 bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr &source,
298 std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
299
300 void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time)
302
303 std::pair<CAddress, NodeSeconds> Select_(bool newOnly) const
305
306 std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct,
307 std::optional<Network> network) const
309
310 void Connected_(const CService &addr, NodeSeconds time)
312
313 void SetServices_(const CService &addr, ServiceFlags nServices)
315
317
320
323 void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
324
328};
329
330#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:98
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:82
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:160
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:191
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:948
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:223
nid_type vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "tried" buckets
Format
Serialization versions.
Definition: addrman_impl.h:173
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:183
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:175
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:181
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:179
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:177
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:216
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:241
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:231
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:202
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:164
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:261
std::set< nid_type > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries.
Definition: addrman_impl.h:235
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:159
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:199
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:257
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:170
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:569
Fast randomness source.
Definition: random.h:411
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
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:638
#define READWRITE(...)
Definition: serialize.h:189
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25