Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
30
35
39
43class AddrInfo : public CAddress {
44public:
47
50
53
56
58 int nAttempts{0};
59
61 int nRefCount{0};
62
64 bool fInTried{false};
65
67 mutable int nRandomPos{-1};
68
71 READWRITE(obj.source,
72 Using<ChronoFormatter<int64_t>>(obj.m_last_success),
73 obj.nAttempts);
74 }
75
78
80
82 int GetTriedBucket(const uint256 &nKey,
83 const std::vector<bool> &asmap) const;
84
87 int GetNewBucket(const uint256 &nKey, const CNetAddr &src,
88 const std::vector<bool> &asmap) const;
89
92 int GetNewBucket(const uint256 &nKey,
93 const std::vector<bool> &asmap) const {
94 return GetNewBucket(nKey, source, asmap);
95 }
96
98 int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
99
102 bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
103
106 double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
107};
108
110public:
111 AddrManImpl(std::vector<bool> &&asmap, int32_t consistency_check_ratio);
112
113 ~AddrManImpl();
114
115 template <typename Stream>
117
118 template <typename Stream>
120
121 size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
122
123 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
124 std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs);
125
126 void Good(const CService &addr, bool test_before_evict, NodeSeconds time)
128
129 void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time)
131
133
134 std::pair<CAddress, NodeSeconds> SelectTriedCollision()
136
137 std::pair<CAddress, NodeSeconds> Select(bool newOnly) const
139
140 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
141 std::optional<Network> network) const
143
144 void Connected(const CService &addr, NodeSeconds time)
146
147 void SetServices(const CService &addr, ServiceFlags nServices)
149
150 const std::vector<bool> &GetAsmap() const;
151
153
155
156 friend class AddrManTest;
157 friend class AddrManCorrupted;
158
159private:
161 mutable Mutex cs;
162
165
168
182
189
196 static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
197
200
202 std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
203
205 std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
206
210 mutable std::vector<int> vRandom GUARDED_BY(cs);
211
212 // number of "tried" entries
214
217
219 int nNew GUARDED_BY(cs);
220
223
226
229 std::set<int> m_tried_collisions;
230
236
237 // Compressed IP->ASN mapping, loaded from a file when a node starts.
238 // Should be always empty if no file was provided.
239 // This mapping is then used for bucketing nodes in Addrman.
240 //
241 // If asmap is provided, nodes will be bucketed by
242 // AS they belong to, in order to make impossible for a node
243 // to connect to several nodes hosted in a single AS.
244 // This is done in response to Erebus attack, but also to generally
245 // diversify the connections every node creates,
246 // especially useful when a large fraction of nodes
247 // operate under a couple of cloud providers.
248 //
249 // If a new asmap was provided, the existing records
250 // would be re-bucketed accordingly.
251 const std::vector<bool> m_asmap;
252
255 bool deterministic = false;
256
258 AddrInfo *Find(const CService &addr, int *pnId = nullptr)
260
263 AddrInfo *Create(const CAddress &addr, const CNetAddr &addrSource,
264 int *pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
265
267 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const
269
271 void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
272
276
278 void MakeTried(AddrInfo &info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
279
284 bool AddSingle(const CAddress &addr, const CNetAddr &source,
285 std::chrono::seconds time_penalty)
287
288 void Good_(const CService &addr, bool test_before_evict, NodeSeconds time)
290
291 bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr &source,
293
294 void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time)
296
299
301 std::optional<Network> network) const
303
304 void Connected_(const CService &addr, NodeSeconds time)
306
307 void SetServices_(const CService &addr, ServiceFlags nServices)
309
311
314
317 void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
318
322};
323
324#endif // BITCOIN_ADDRMAN_IMPL_H
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2
Maximum allowed number of entries in buckets for new and tried addresses.
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
static constexpr int ADDRMAN_BUCKET_SIZE
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Extended statistics about a CAddress.
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
CNetAddr source
where knowledge about this address first came from
int GetNewBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "new" bucket this entry belongs, using its default source.
bool fInTried
in tried set? (memory only)
SERIALIZE_METHODS(AddrInfo, obj)
NodeSeconds m_last_success
last successful connection by us
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)
NodeSeconds m_last_try
last try whatsoever by us (memory only)
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)
int nRefCount
reference count in new sets (memory only)
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
void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs)
Clear a position in a "new" table.
Definition addrman.cpp:504
std::unordered_map< CService, int, CServiceHash > mapAddr GUARDED_BY(cs)
find an nId based on its network address and port.
std::pair< CAddress, NodeSeconds > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1219
friend class AddrManCorrupted
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:900
void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:743
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:935
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Format
Serialization versions.
@ V4_MULTIPORT
adds support for multiple ports per IP
@ V0_HISTORICAL
historic format, before commit e6b343d88
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
@ V2_ASMAP
for files including asmap version
@ V1_DETERMINISTIC
for pre-asmap files
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:141
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1237
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1228
int vvNew[ADDRMAN_NEW_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "new" buckets
int vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "tried" buckets
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1172
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:522
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1244
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:919
int nTried GUARDED_BY(cs)
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
const std::vector< bool > & GetAsmap() const
Definition addrman.cpp:1251
std::unordered_map< int, AddrInfo > mapInfo GUARDED_BY(cs)
table with information about all nIds
void MakeDeterministic() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1285
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition addrman.cpp:1046
NodeSeconds m_last_good GUARDED_BY(cs)
last time Good was called (memory only)
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition addrman.cpp:1064
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1178
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition addrman.cpp:432
Mutex cs
A mutex to protect the inner data structures.
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:1016
std::pair< CAddress, NodeSeconds > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:764
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1211
bool deterministic
Use deterministic bucket selection and inner loops randomization.
friend class AddrManTest
void Good_(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:665
static constexpr uint8_t INCOMPATIBILITY_BASE
The initial value of a field that is incremented every time an incompatible format change is made (su...
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition addrman.cpp:489
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition addrman.cpp:464
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1196
FastRandomContext insecure_rand
Source of random numbers for randomization in inner loops.
void Good(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1188
void Clear() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1255
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:247
std::set< int > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries.
int nIdCount GUARDED_BY(cs)
last used nId
const std::vector< bool > m_asmap
uint256 nKey
secret key to randomize bucket select with
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1204
AddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
find an entry, creating it if necessary.
Definition addrman.cpp:449
std::vector< int > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:856
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:727
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:581
A CService with information about it as peer.
Definition protocol.h:442
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:335
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
const char * source
#define READWRITEAS(type, obj)
Definition serialize.h:167
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition serialize.h:574
#define READWRITE(...)
Definition serialize.h:166
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition time.h:25