Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
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
38class AddrInfo : public CAddress
39{
40public:
43
46
49
52
54 int nAttempts{0};
55
57 int nRefCount{0};
58
60 bool fInTried{false};
61
63 mutable int nRandomPos{-1};
64
66 {
67 READWRITE(AsBase<CAddress>(obj), obj.source, Using<ChronoFormatter<int64_t>>(obj.m_last_success), obj.nAttempts);
68 }
69
70 AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource) : CAddress(addrIn), source(addrSource)
71 {
72 }
73
75 {
76 }
77
79 int GetTriedBucket(const uint256& nKey, const NetGroupManager& netgroupman) const;
80
82 int GetNewBucket(const uint256& nKey, const CNetAddr& src, const NetGroupManager& netgroupman) const;
83
85 int GetNewBucket(const uint256& nKey, const NetGroupManager& netgroupman) const
86 {
87 return GetNewBucket(nKey, source, netgroupman);
88 }
89
91 int GetBucketPosition(const uint256 &nKey, bool fNew, int bucket) const;
92
94 bool IsTerrible(NodeSeconds now = Now<NodeSeconds>()) const;
95
97 double GetChance(NodeSeconds now = Now<NodeSeconds>()) const;
98};
99
101{
102public:
103 AddrManImpl(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
104
105 ~AddrManImpl();
106
107 template <typename Stream>
108 void Serialize(Stream& s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
109
110 template <typename Stream>
111 void Unserialize(Stream& s_) EXCLUSIVE_LOCKS_REQUIRED(!cs);
112
113 size_t Size(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs);
114
115 bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty)
117
118 bool Good(const CService& addr, NodeSeconds time)
120
121 void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time)
123
125
126 std::pair<CAddress, NodeSeconds> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
127
128 std::pair<CAddress, NodeSeconds> Select(bool new_only, std::optional<Network> network) const
130
131 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const
133
134 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const
136
137 void Connected(const CService& addr, NodeSeconds time)
139
140 void SetServices(const CService& addr, ServiceFlags nServices)
142
143 std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
145
147
148private:
150 mutable Mutex cs;
151
153 mutable FastRandomContext insecure_rand GUARDED_BY(cs);
154
157
166
173
179 static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
180
182 int nIdCount GUARDED_BY(cs){0};
183
185 std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
186
188 std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
189
193 mutable std::vector<int> vRandom GUARDED_BY(cs);
194
195 // number of "tried" entries
196 int nTried GUARDED_BY(cs){0};
197
200
202 int nNew GUARDED_BY(cs){0};
203
206
208 NodeSeconds m_last_good GUARDED_BY(cs){1s};
209
211 std::set<int> m_tried_collisions;
212
215
218
220 size_t n_new;
221 size_t n_tried;
222 };
223
225 std::unordered_map<Network, NewTriedCount> m_network_counts GUARDED_BY(cs);
226
228 AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
229
231 AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
232
234 void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
235
237 void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
238
240 void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
241
243 void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
244
247 bool AddSingle(const CAddress& addr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
248
249 bool Good_(const CService& addr, bool test_before_evict, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
250
251 bool Add_(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
252
253 void Attempt_(const CService& addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
254
255 std::pair<CAddress, NodeSeconds> Select_(bool new_only, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
256
261 int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);
262
263 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);
264
265 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs);
266
268
269 void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
270
272
274
276
277 size_t Size_(std::optional<Network> net, std::optional<bool> in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs);
278
281 void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
282
286};
287
288#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 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
CNetAddr source
where knowledge about this address first came from
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)
SERIALIZE_METHODS(AddrInfo, obj)
NodeSeconds m_last_success
last successful connection by us
NodeSeconds m_last_count_attempt
last counted attempt (memory only)
NodeSeconds m_last_try
last try whatsoever by us (memory only)
int GetNewBucket(const uint256 &nKey, const NetGroupManager &netgroupman) const
Calculate in which "new" bucket this entry belongs, using its default source.
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)
int nRefCount
reference count in new sets (memory only)
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. This is the only place where entries are actually deleted.
Definition addrman.cpp:473
std::unordered_map< CService, int, CServiceHash > mapAddr GUARDED_BY(cs)
find an nId based on its network address and port.
int 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:789
void Connected_(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:868
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.
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:903
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:133
void Connected(const CService &addr, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1238
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
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:491
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1246
std::optional< AddressPosition > FindAddressEntry_(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:994
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:887
AddrManImpl(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition addrman.cpp:109
int nTried GUARDED_BY(cs)
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1229
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
std::unordered_map< int, AddrInfo > mapInfo GUARDED_BY(cs)
table with information about all nIds
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries_(bool from_tried) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:844
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition addrman.cpp:1039
NodeSeconds m_last_good GUARDED_BY(cs)
last time Good was called (memory only). Initially set to 1 so that "never" is strictly worse.
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition addrman.cpp:1054
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1168
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1254
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition addrman.cpp:401
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.
size_t Size_(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:1017
std::pair< CAddress, NodeSeconds > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:966
std::pair< CAddress, NodeSeconds > Select(bool new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1211
std::pair< CAddress, NodeSeconds > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1202
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:456
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition addrman.cpp:432
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:806
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1186
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:231
std::set< int > m_tried_collisions
Holds addrs inserted into tried table that collide with existing entries. Test-before-evict disciplin...
int nIdCount GUARDED_BY(cs)
last used nId
uint256 nKey
secret key to randomize bucket select with
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:1194
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:416
std::vector< int > vRandom GUARDED_BY(cs)
randomly-ordered vector of all nIds This is mutable because it is unobservable outside the class,...
const NetGroupManager & m_netgroupman
Reference to the netgroup manager.
std::pair< CAddress, NodeSeconds > Select_(bool new_only, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition addrman.cpp:713
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:1177
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:1220
size_t Size(std::optional< Network > net, std::optional< bool > in_new) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition addrman.cpp:1159
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:178
Network
A network type.
Definition netaddress.h:32
ServiceFlags
nServices flags
Definition protocol.h:309
const char * source
Out & AsBase(In &x)
Convert any argument to a reference to X, maintaining constness.
Definition serialize.h:144
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:34
#define EXCLUSIVE_LOCKS_REQUIRED(...)
T Now()
Return the current time point cast to the given precision.
Definition time.h:91
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition time.h:23