Bitcoin Core 28.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 <uint256.h>
15#include <util/time.h>
16
17#include <cstdint>
18#include <optional>
19#include <set>
20#include <unordered_map>
21#include <unordered_set>
22#include <utility>
23#include <vector>
24
26static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
29static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
32static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
34
40using nid_type = int64_t;
41
45class AddrInfo : public CAddress
46{
47public:
50
53
56
59
61 int nAttempts{0};
62
64 int nRefCount{0};
65
67 bool fInTried{false};
68
70 mutable int nRandomPos{-1};
71
73 {
74 READWRITE(AsBase<CAddress>(obj), obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
75 }
76
77 AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
78 {
79 }
80
82 {
83 }
84
86 int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
87
89 int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
90
92 int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
93 {
94 return GetNewBucket(nKey, source, netgroupman);
95 }
96
98 int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const;
99
101 bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
102
104 double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
105};
106
108{
109public:
110 AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
111
112 ~AddrManImpl();
113
114 template <typename Stream>
115 void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
116
117 template <typename Stream>
118 void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
119
120 size_t Size(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
121
122 bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
124
125 bool Good(const CService& addr, NodeSeconds time)
127
128 void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
130
132
133 std::pair<CAddress, NodeSeconds> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
134
135 std::pair<CAddress, NodeSeconds> Select(bool new_only, const std::unordered_set<Network>& networks) const
137
138 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const
140
141 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const
143
144 void Connected(const CService& addr, NodeSeconds time)
146
147 void SetServices(const CService& addr, ServiceFlags nServices)
149
150 std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
152
154
155private:
157 mutable Mutex cs;
158
160 mutable FastRandomContext insecure_rand GUARDED_BY(cs);
161
164
166 enum Format : uint8_t {
172 };
173
179 static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
180
186 static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
187
189 nid_type nIdCount GUARDED_BY(cs){0};
190
192 std::unordered_map<nid_type, AddrInfo> mapInfo GUARDED_BY(cs);
193
195 std::unordered_map<CService, nid_type, CServiceHash> mapAddr GUARDED_BY(cs);
196
200 mutable std::vector<nid_type> vRandom GUARDED_BY(cs);
201
202 // number of "tried" entries
203 int nTried GUARDED_BY(cs){0};
204
207
209 int nNew GUARDED_BY(cs){0};
210
213
215 NodeSeconds m_last_good GUARDED_BY(cs){1s};
216
218 std::set<nid_type> m_tried_collisions;
219
222
225
227 size_t n_new;
228 size_t n_tried;
229 };
230
232 std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
233
235 AddrInfo* Find(const CService& addr, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
236
238 AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, nid_type* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
239
241 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
242
245
247 void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
248
251
254 bool AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
255
256 bool Good_(const CService& addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
257
258 bool Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
259
260 void Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
261
262 std::pair<CAddress, NodeSeconds> Select_(bool new_only, const std::unordered_set<Network>& networks) const EXCLUSIVE_LOCKS_REQUIRED(cs);
263
268 nid_type GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
269
270 std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
271
272 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs);
273
275
276 void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
277
279
281
283
284 size_t Size_(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs);
285
288 void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
289
293};
294
295#endif // BITCOIN_ADDRMAN_IMPL_H
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:27
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
Definition: addrman_impl.h:26
static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2
Maximum allowed number of entries in buckets for new and tried addresses.
Definition: addrman_impl.h:32
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
Definition: addrman_impl.h:29
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:33
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:40
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:30
Extended statistics about a CAddress.
Definition: addrman_impl.h:46
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:55
int GetTriedBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:48
int nRandomPos
position in vRandom
Definition: addrman_impl.h:70
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:55
int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:63
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:67
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:72
NodeSeconds m_last_success
last successful connection by us
Definition: addrman_impl.h:58
NodeSeconds m_last_count_attempt
last counted attempt (memory only)
Definition: addrman_impl.h:52
NodeSeconds m_last_try
last try whatsoever by us (memory only)
Definition: addrman_impl.h:49
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:92
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:94
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:69
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:77
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:64
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:61
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
AddrInfo * Create(const CAddress &addr, const CNetAddr &addrSource, nid_type *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:416
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:877
void Attempt_(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:693
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:179
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:912
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:209
nid_type vvTried[ADDRMAN_TRIED_BUCKET_COUNT][ADDRMAN_BUCKET_SIZE] GUARDED_BY(cs)
list of "tried" buckets
Format
Serialization versions.
Definition: addrman_impl.h:166
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:171
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:167
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:170
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:169
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:168
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:133
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:456
std::pair< CAddress, NodeSeconds > Select_(bool new_only, const std::unordered_set< Network > &networks) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:713
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1247
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:1255
std::optional< AddressPosition > FindAddressEntry_(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:1003
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:491
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:896
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:401
AddrManImpl(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:109
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:203
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1238
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:221
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:853
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:1048
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:215
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:1063
nid_type nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:189
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1177
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1263
bool Good_(const CService &addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:626
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:157
size_t Size_(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:1026
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:975
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1211
std::set< nid_type > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries. Test-before-evict disciplin...
Definition: addrman_impl.h:218
std::vector< nid_type > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
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:186
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:432
std::pair< CAddress, NodeSeconds > Select(bool new_only, const std::unordered_set< Network > &networks) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1220
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:812
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1195
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:231
nid_type GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper to generalize looking up an addrman entry from either table.
Definition: addrman.cpp:795
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:163
std::unordered_map< Network, NewTriedCount > m_network_counts GUARDED_BY(cs)
Number of entries in addrman per network and new/tried table.
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1203
const NetGroupManager & m_netgroupman
Reference to the netgroup manager.
Definition: addrman_impl.h:224
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:681
bool Good(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1186
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1229
size_t Size(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1168
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:550
A CService with information about it as peer.
Definition: protocol.h:367
Network address.
Definition: netaddress.h:112
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:531
Fast randomness source.
Definition: random.h:377
Netgroup manager.
Definition: netgroup.h:16
256-bit opaque blob.
Definition: uint256.h:190
Network
A network type.
Definition: netaddress.h:32
ServiceFlags
nServices flags
Definition: protocol.h:309
const char * source
Definition: rpcconsole.cpp:62
static Wrapper< Formatter, T & > Using(T &&t)
Cause serialization/deserialization of an object to be done using a specified formatter class.
Definition: serialize.h:495
#define READWRITE(...)
Definition: serialize.h:156
Location information for an address in AddrMan.
Definition: addrman.h:35
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25