Bitcoin Core 22.99.0
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 <netaddress.h>
10#include <protocol.h>
11#include <serialize.h>
12#include <sync.h>
13#include <uint256.h>
14
15#include <cstdint>
16#include <optional>
17#include <set>
18#include <unordered_map>
19#include <unordered_set>
20#include <utility>
21#include <vector>
22
24static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
27static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
30static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
32
36class AddrInfo : public CAddress
37{
38public:
40 int64_t nLastTry{0};
41
44
47
49 int64_t nLastSuccess{0};
50
52 int nAttempts{0};
53
55 int nRefCount{0};
56
58 bool fInTried{false};
59
61 mutable int nRandomPos{-1};
62
64 {
66 READWRITE(obj.source, obj.nLastSuccess, obj.nAttempts);
67 }
68
69 AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
70 {
71 }
72
74 {
75 }
76
78 int GetTriedBucket(const uint256 &nKey, const std::vector<bool> &asmap) const;
79
81 int GetNewBucket(const uint256 &nKey, const CNetAddr& src, const std::vector<bool> &asmap) const;
82
84 int GetNewBucket(const uint256 &nKey, const std::vector<bool> &asmap) const
85 {
86 return GetNewBucket(nKey, source, asmap);
87 }
88
90 int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
91
93 bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
94
96 double GetChance(int64_t nNow = GetAdjustedTime()) const;
97};
98
100{
101public:
102 AddrManImpl(std::vector<bool>&& asmap, bool deterministic, int32_t consistency_check_ratio);
103
104 ~AddrManImpl();
105
106 template <typename Stream>
107 void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
108
109 template <typename Stream>
110 void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
111
112 size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
113
114 bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
116
117 void Good(const CService& addr, int64_t nTime)
119
120 void Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
122
124
125 std::pair<CAddress, int64_t> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
126
127 std::pair<CAddress, int64_t> Select(bool newOnly) const
129
130 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
132
133 void Connected(const CService& addr, int64_t nTime)
135
136 void SetServices(const CService& addr, ServiceFlags nServices)
138
139 const std::vector<bool>& GetAsmap() const;
140
141 friend class AddrManTest;
143
144private:
146 mutable Mutex cs;
147
149 mutable FastRandomContext insecure_rand GUARDED_BY(cs);
150
153
155 enum Format : uint8_t {
161 };
162
168 static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
169
175 static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
176
178 int nIdCount GUARDED_BY(cs){0};
179
181 std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
182
184 std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
185
189 mutable std::vector<int> vRandom GUARDED_BY(cs);
190
191 // number of "tried" entries
192 int nTried GUARDED_BY(cs){0};
193
196
198 int nNew GUARDED_BY(cs){0};
199
202
204 int64_t nLastGood GUARDED_BY(cs){1};
205
207 std::set<int> m_tried_collisions;
208
211
212 // Compressed IP->ASN mapping, loaded from a file when a node starts.
213 // Should be always empty if no file was provided.
214 // This mapping is then used for bucketing nodes in Addrman.
215 //
216 // If asmap is provided, nodes will be bucketed by
217 // AS they belong to, in order to make impossible for a node
218 // to connect to several nodes hosted in a single AS.
219 // This is done in response to Erebus attack, but also to generally
220 // diversify the connections every node creates,
221 // especially useful when a large fraction of nodes
222 // operate under a couple of cloud providers.
223 //
224 // If a new asmap was provided, the existing records
225 // would be re-bucketed accordingly.
226 const std::vector<bool> m_asmap;
227
229 AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
230
232 AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
233
235 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
236
238 void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
239
241 void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
242
244 void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
245
248 bool AddSingle(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
249
250 void Good_(const CService& addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
251
252 bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
253
254 void Attempt_(const CService& addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
255
256 std::pair<CAddress, int64_t> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
257
258 std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
259
260 void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
261
262 void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
263
265
267
269 void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
270
274};
275
276#endif // BITCOIN_ADDRMAN_IMPL_H
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:25
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
Definition: addrman_impl.h:24
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2
Maximum allowed number of entries in buckets for new and tried addresses.
Definition: addrman_impl.h:30
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
Definition: addrman_impl.h:27
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:31
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:28
Extended statistics about a CAddress.
Definition: addrman_impl.h:37
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:44
bool IsTerrible(int64_t nNow=GetAdjustedTime()) const
Determine whether the statistics about this entry are bad enough so that it can just be deleted.
Definition: addrman.cpp:65
int nRandomPos
position in vRandom
Definition: addrman_impl.h:61
double GetChance(int64_t nNow=GetAdjustedTime()) const
Calculate the relative chance this entry should be given when selecting nodes to connect to.
Definition: addrman.cpp:85
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:46
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:84
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:58
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:63
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:51
int64_t nLastCountAttempt
last counted attempt (memory only)
Definition: addrman_impl.h:43
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman_impl.h:40
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:69
int64_t nLastSuccess
last successful connection by us
Definition: addrman_impl.h:49
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:55
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:59
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:52
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:467
std::unordered_map< CService, int, CServiceHash > mapAddr GUARDED_BY(cs)
find an nId based on its network address and port.
int ForceCheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:947
void Good(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1050
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:168
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:843
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:198
Format
Serialization versions.
Definition: addrman_impl.h:155
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:160
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:156
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:159
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:158
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:157
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:124
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1092
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:1035
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:485
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1109
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1041
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1058
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:827
void Connected(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1101
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:192
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:210
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1117
std::unordered_map< int, AddrInfo > mapInfo GUARDED_BY(cs)
table with information about all nIds
std::pair< CAddress, int64_t > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1083
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio. Will std::abort if an inconsistency...
Definition: addrman.cpp:932
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:398
int64_t nLastGood GUARDED_BY(cs)
last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
Definition: addrman_impl.h:204
std::pair< CAddress, int64_t > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:904
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:146
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:615
std::pair< CAddress, int64_t > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:702
AddrManImpl(std::vector< bool > &&asmap, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:100
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:175
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:451
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:427
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:809
bool AddSingle(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Attempt to add a single address to addrman's new table.
Definition: addrman.cpp:540
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:227
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:204
int nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:178
const std::vector< bool > m_asmap
Definition: addrman_impl.h:226
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:152
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1066
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:413
std::vector< int > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
std::pair< CAddress, int64_t > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1074
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:670
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:771
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:682
A CService with information about it as peer.
Definition: protocol.h:359
Network address.
Definition: netaddress.h:119
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:523
Fast randomness source.
Definition: random.h:120
256-bit opaque blob.
Definition: uint256.h:124
Network
A network type.
Definition: netaddress.h:45
ServiceFlags
nServices flags
Definition: protocol.h:271
const char * source
Definition: rpcconsole.cpp:63
#define READWRITEAS(type, obj)
Definition: serialize.h:148
#define READWRITE(...)
Definition: serialize.h:147
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
int64_t GetAdjustedTime()
Definition: timedata.cpp:35