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 
24 static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
27 static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
30 static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
31 static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
32 
36 class AddrInfo : public CAddress
37 {
38 public:
40  int64_t nLastTry{0};
41 
43  int64_t nLastCountAttempt{0};
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  {
65  READWRITEAS(CAddress, obj);
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 {
101 public:
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;
142  friend class AddrManDeterministic;
143 
144 private:
146  mutable Mutex cs;
147 
149  mutable FastRandomContext insecure_rand GUARDED_BY(cs);
150 
153 
155  enum Format : uint8_t {
158  V2_ASMAP = 2,
159  V3_BIP155 = 3,
160  };
161 
167  static constexpr Format FILE_FORMAT = Format::V3_BIP155;
168 
174  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
175 
177  int nIdCount GUARDED_BY(cs){0};
178 
180  std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
181 
183  std::unordered_map<CNetAddr, int, CNetAddrHash> mapAddr GUARDED_BY(cs);
184 
188  mutable std::vector<int> vRandom GUARDED_BY(cs);
189 
190  // number of "tried" entries
191  int nTried GUARDED_BY(cs){0};
192 
195 
197  int nNew GUARDED_BY(cs){0};
198 
201 
203  int64_t nLastGood GUARDED_BY(cs){1};
204 
206  std::set<int> m_tried_collisions;
207 
210 
211  // Compressed IP->ASN mapping, loaded from a file when a node starts.
212  // Should be always empty if no file was provided.
213  // This mapping is then used for bucketing nodes in Addrman.
214  //
215  // If asmap is provided, nodes will be bucketed by
216  // AS they belong to, in order to make impossible for a node
217  // to connect to several nodes hosted in a single AS.
218  // This is done in response to Erebus attack, but also to generally
219  // diversify the connections every node creates,
220  // especially useful when a large fraction of nodes
221  // operate under a couple of cloud providers.
222  //
223  // If a new asmap was provided, the existing records
224  // would be re-bucketed accordingly.
225  const std::vector<bool> m_asmap;
226 
228  AddrInfo* Find(const CNetAddr& 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 
245  void Good_(const CService& addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
246 
247  bool Add_(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
248 
249  void Attempt_(const CService& addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
250 
251  std::pair<CAddress, int64_t> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
252 
253  std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
254 
255  void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
256 
257  void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
258 
260 
262 
264  void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
265 
269 };
270 
271 #endif // BITCOIN_ADDRMAN_IMPL_H
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
AddrManImpl::ClearNew
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::nLastSuccess
int64_t nLastSuccess
last successful connection by us
Definition: addrman_impl.h:49
AddrManImpl::Select
std::pair< CAddress, int64_t > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1074
AddrManImpl::Connected_
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:787
AddrManImpl::GUARDED_BY
FastRandomContext insecure_rand GUARDED_BY(cs)
Source of random numbers for randomization in inner loops.
AddrManImpl::Serialize
void Serialize(Stream &s_) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:130
AddrInfo::AddrInfo
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:69
AddrManImpl::GetAddr_
std::vector< CAddress > GetAddr_(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:749
sync.h
AddrManImpl::m_asmap
const std::vector< bool > m_asmap
Definition: addrman_impl.h:225
AddrInfo::nAttempts
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:52
CNetAddr
Network address.
Definition: netaddress.h:118
source
const char * source
Definition: rpcconsole.cpp:63
uint256.h
AddrManImpl::SetServices
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1100
AddrManImpl::~AddrManImpl
~AddrManImpl()
Definition: addrman.cpp:124
AddrManImpl::ResolveCollisions
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1057
ADDRMAN_NEW_BUCKET_COUNT_LOG2
static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2
Total number of buckets for new addresses.
Definition: addrman_impl.h:27
AddrManImpl::Create
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:419
AnnotatedMixin< std::mutex >
ADDRMAN_TRIED_BUCKET_COUNT
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:25
AddrInfo
Extended statistics about a CAddress.
Definition: addrman_impl.h:36
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
AddrInfo::nLastTry
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman_impl.h:40
AddrManImpl::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1083
READWRITEAS
#define READWRITEAS(type, obj)
Definition: serialize.h:148
Network
Network
A network type.
Definition: netaddress.h:45
ADDRMAN_BUCKET_SIZE
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman_impl.h:31
AddrManImpl::AddrManImpl
AddrManImpl(std::vector< bool > &&asmap, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:106
AddrManImpl::SetServices_
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:809
AddrManImpl::Find
AddrInfo * Find(const CNetAddr &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:404
AddrManImpl::SelectTriedCollision_
std::pair< CAddress, int64_t > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:890
AddrManImpl::GUARDED_BY
int nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:177
ADDRMAN_TRIED_BUCKET_COUNT_LOG2
static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2
Total number of buckets for tried addresses.
Definition: addrman_impl.h:24
AddrInfo::AddrInfo
AddrInfo()
Definition: addrman_impl.h:73
protocol.h
AddrInfo::SERIALIZE_METHODS
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:63
AddrManImpl::V3_BIP155
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:159
AddrManImpl::Good
void Good(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1041
AddrManImpl::Connected
void Connected(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1092
netaddress.h
AddrManImpl::ResolveCollisions_
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:829
AddrInfo::IsTerrible
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:71
AddrInfo::GetBucketPosition
int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const
Calculate in which position of a bucket to store this entry.
Definition: addrman.cpp:65
AddrManImpl::Attempt_
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:674
AddrInfo::source
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:46
AddrInfo::GetNewBucket
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:54
uint256
256-bit opaque blob.
Definition: uint256.h:124
READWRITE
#define READWRITE(...)
Definition: serialize.h:147
AddrInfo::nLastCountAttempt
int64_t nLastCountAttempt
last counted attempt (memory only)
Definition: addrman_impl.h:43
AddrManImpl::m_tried_collisions
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:203
AddrManImpl::ForceCheckAddrman
int ForceCheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:933
AddrManImpl::V0_HISTORICAL
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:156
AddrManImpl::m_consistency_check_ratio
const int32_t m_consistency_check_ratio
Perform consistency checks every m_consistency_check_ratio operations (if non-zero).
Definition: addrman_impl.h:209
AddrManImpl::MakeTried
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:490
AddrManImpl::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1027
AddrManImpl::nKey
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:152
AddrManImpl::Check
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:918
AddrManImpl::GUARDED_BY
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:197
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
AddrManImpl::INCOMPATIBILITY_BASE
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:174
AddrManImpl::SwapRandom
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:433
AddrManImpl::FILE_FORMAT
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:167
AddrInfo::nRefCount
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:55
AddrManImpl::Unserialize
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:233
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
AddrManImpl::Select_
std::pair< CAddress, int64_t > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:698
AddrInfo::GetChance
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:91
AddrManImpl::GetAsmap
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1108
logging.h
ADDRMAN_NEW_BUCKET_COUNT
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:28
AddrManImpl::cs
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:146
AddrManImpl::V1_DETERMINISTIC
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:157
ADDRMAN_BUCKET_SIZE_LOG2
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
AddrManTest
Definition: addrman_tests.cpp:80
serialize.h
AddrManDeterministic
Definition: addrman.cpp:112
AddrManImpl::size
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1021
AddrInfo::GetTriedBucket
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:44
AddrManImpl::GUARDED_BY
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:191
AddrManImpl::SelectTriedCollision
std::pair< CAddress, int64_t > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1065
AddrManImpl
Definition: addrman_impl.h:99
AddrManImpl::V2_ASMAP
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:158
AddrManImpl::Add_
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:599
AddrInfo::GetNewBucket
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
AddrInfo::fInTried
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:58
FastRandomContext
Fast randomness source.
Definition: random.h:119
AddrManImpl::Good_
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:543
AddrManImpl::Format
Format
Serialization versions.
Definition: addrman_impl.h:155
AddrInfo::nRandomPos
int nRandomPos
position in vRandom
Definition: addrman_impl.h:61
AddrManImpl::Delete
void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs)
Delete an entry. It must not be in tried, and have refcount 0.
Definition: addrman.cpp:457
AddrManImpl::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1049