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 <logging/timer.h>
10 #include <netaddress.h>
11 #include <protocol.h>
12 #include <serialize.h>
13 #include <sync.h>
14 #include <uint256.h>
15 
16 #include <cstdint>
17 #include <optional>
18 #include <set>
19 #include <unordered_map>
20 #include <unordered_set>
21 #include <utility>
22 #include <vector>
23 
25 static constexpr int32_t ADDRMAN_TRIED_BUCKET_COUNT_LOG2{8};
28 static constexpr int32_t ADDRMAN_NEW_BUCKET_COUNT_LOG2{10};
31 static constexpr int32_t ADDRMAN_BUCKET_SIZE_LOG2{6};
32 static constexpr int ADDRMAN_BUCKET_SIZE{1 << ADDRMAN_BUCKET_SIZE_LOG2};
33 
37 class AddrInfo : public CAddress
38 {
39 public:
41  int64_t nLastTry{0};
42 
44  int64_t nLastCountAttempt{0};
45 
48 
50  int64_t nLastSuccess{0};
51 
53  int nAttempts{0};
54 
56  int nRefCount{0};
57 
59  bool fInTried{false};
60 
62  mutable int nRandomPos{-1};
63 
65  {
66  READWRITEAS(CAddress, obj);
67  READWRITE(obj.source, obj.nLastSuccess, 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 std::vector<bool> &asmap) const;
80 
82  int GetNewBucket(const uint256 &nKey, const CNetAddr& src, const std::vector<bool> &asmap) const;
83 
85  int GetNewBucket(const uint256 &nKey, const std::vector<bool> &asmap) const
86  {
87  return GetNewBucket(nKey, source, asmap);
88  }
89 
91  int GetBucketPosition(const uint256 &nKey, bool fNew, int nBucket) const;
92 
94  bool IsTerrible(int64_t nNow = GetAdjustedTime()) const;
95 
97  double GetChance(int64_t nNow = GetAdjustedTime()) const;
98 };
99 
101 {
102 public:
103  AddrManImpl(std::vector<bool>&& asmap, 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() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
114 
115  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty)
117 
118  bool Good(const CService& addr, int64_t nTime)
120 
121  void Attempt(const CService& addr, bool fCountFailure, int64_t nTime)
123 
125 
126  std::pair<CAddress, int64_t> SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs);
127 
128  std::pair<CAddress, int64_t> Select(bool newOnly) const
130 
131  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const
133 
134  void Connected(const CService& addr, int64_t nTime)
136 
137  void SetServices(const CService& addr, ServiceFlags nServices)
139 
140  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr)
142 
143  const std::vector<bool>& GetAsmap() const;
144 
145  friend class AddrManDeterministic;
146 
147 private:
149  mutable Mutex cs;
150 
152  mutable FastRandomContext insecure_rand GUARDED_BY(cs);
153 
156 
158  enum Format : uint8_t {
161  V2_ASMAP = 2,
162  V3_BIP155 = 3,
164  };
165 
171  static constexpr Format FILE_FORMAT = Format::V4_MULTIPORT;
172 
178  static constexpr uint8_t INCOMPATIBILITY_BASE = 32;
179 
181  int nIdCount GUARDED_BY(cs){0};
182 
184  std::unordered_map<int, AddrInfo> mapInfo GUARDED_BY(cs);
185 
187  std::unordered_map<CService, int, CServiceHash> mapAddr GUARDED_BY(cs);
188 
192  mutable std::vector<int> vRandom GUARDED_BY(cs);
193 
194  // number of "tried" entries
195  int nTried GUARDED_BY(cs){0};
196 
199 
201  int nNew GUARDED_BY(cs){0};
202 
205 
207  int64_t nLastGood GUARDED_BY(cs){1};
208 
210  std::set<int> m_tried_collisions;
211 
214 
215  // Compressed IP->ASN mapping, loaded from a file when a node starts.
216  // Should be always empty if no file was provided.
217  // This mapping is then used for bucketing nodes in Addrman.
218  //
219  // If asmap is provided, nodes will be bucketed by
220  // AS they belong to, in order to make impossible for a node
221  // to connect to several nodes hosted in a single AS.
222  // This is done in response to Erebus attack, but also to generally
223  // diversify the connections every node creates,
224  // especially useful when a large fraction of nodes
225  // operate under a couple of cloud providers.
226  //
227  // If a new asmap was provided, the existing records
228  // would be re-bucketed accordingly.
229  const std::vector<bool> m_asmap;
230 
232  AddrInfo* Find(const CService& addr, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
233 
235  AddrInfo* Create(const CAddress& addr, const CNetAddr& addrSource, int* pnId = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
236 
238  void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs);
239 
241  void Delete(int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
242 
244  void ClearNew(int nUBucket, int nUBucketPos) EXCLUSIVE_LOCKS_REQUIRED(cs);
245 
247  void MakeTried(AddrInfo& info, int nId) EXCLUSIVE_LOCKS_REQUIRED(cs);
248 
251  bool AddSingle(const CAddress& addr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
252 
253  bool Good_(const CService& addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs);
254 
255  bool Add_(const std::vector<CAddress> &vAddr, const CNetAddr& source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs);
256 
257  void Attempt_(const CService& addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
258 
259  std::pair<CAddress, int64_t> Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs);
260 
261  std::vector<CAddress> GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> network) const EXCLUSIVE_LOCKS_REQUIRED(cs);
262 
263  void Connected_(const CService& addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs);
264 
265  void SetServices_(const CService& addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs);
266 
268 
270 
272 
275  void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs);
276 
280 };
281 
282 #endif // BITCOIN_ADDRMAN_IMPL_H
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:528
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:469
AddrInfo::nLastSuccess
int64_t nLastSuccess
last successful connection by us
Definition: addrman_impl.h:50
AddrManImpl::Select
std::pair< CAddress, int64_t > Select(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1108
AddrManImpl::Connected_
void Connected_(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:810
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:126
AddrInfo::AddrInfo
AddrInfo(const CAddress &addrIn, const CNetAddr &addrSource)
Definition: addrman_impl.h:70
AddrManImpl::AddSingle
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:542
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:772
AddrManImpl::V4_MULTIPORT
@ V4_MULTIPORT
adds support for multiple ports per IP
Definition: addrman_impl.h:163
sync.h
AddrManImpl::m_asmap
const std::vector< bool > m_asmap
Definition: addrman_impl.h:229
AddrInfo::nAttempts
int nAttempts
connection attempts since last successful attempt
Definition: addrman_impl.h:53
CNetAddr
Network address.
Definition: netaddress.h:118
source
const char * source
Definition: rpcconsole.cpp:64
uint256.h
AddrManImpl::SetServices
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1134
AddrManImpl::~AddrManImpl
~AddrManImpl()
Definition: addrman.cpp:120
AddrManImpl::ResolveCollisions
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1091
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:28
AddrManImpl::FindAddressEntry_
std::optional< AddressPosition > FindAddressEntry_(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:933
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:415
AnnotatedMixin< std::mutex >
ADDRMAN_TRIED_BUCKET_COUNT
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman_impl.h:26
AddrInfo
Extended statistics about a CAddress.
Definition: addrman_impl.h:37
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
AddrInfo::nLastTry
int64_t nLastTry
last try whatsoever by us (memory only)
Definition: addrman_impl.h:41
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:1117
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:32
AddrManImpl::AddrManImpl
AddrManImpl(std::vector< bool > &&asmap, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:102
AddrManImpl::SetServices_
void SetServices_(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:828
AddrManImpl::SelectTriedCollision_
std::pair< CAddress, int64_t > SelectTriedCollision_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:905
AddrManImpl::GUARDED_BY
int nIdCount GUARDED_BY(cs)
last used nId
Definition: addrman_impl.h:181
AddrManImpl::Find
AddrInfo * Find(const CService &addr, int *pnId=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Find an entry.
Definition: addrman.cpp:400
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:25
AddrInfo::AddrInfo
AddrInfo()
Definition: addrman_impl.h:74
protocol.h
AddrInfo::SERIALIZE_METHODS
SERIALIZE_METHODS(AddrInfo, obj)
Definition: addrman_impl.h:64
AddrManImpl::V3_BIP155
@ V3_BIP155
same as V2_ASMAP plus addresses are in BIP155 format
Definition: addrman_impl.h:162
AddrManImpl::Connected
void Connected(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1126
netaddress.h
timer.h
AddrManImpl::ResolveCollisions_
void ResolveCollisions_() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:844
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:67
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:61
AddrManImpl::Attempt_
void Attempt_(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:683
AddrInfo::source
CNetAddr source
where knowledge about this address first came from
Definition: addrman_impl.h:47
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:53
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:44
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:207
AddressPosition
Test-only struct, capturing info about an address in AddrMan.
Definition: addrman.h:26
AddrManImpl::V0_HISTORICAL
@ V0_HISTORICAL
historic format, before commit e6b343d88
Definition: addrman_impl.h:159
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:213
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:487
AddrManImpl::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1065
AddrManImpl::nKey
uint256 nKey
secret key to randomize bucket select with
Definition: addrman_impl.h:155
AddrManImpl::Check
void Check() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Consistency check, taking into account m_consistency_check_ratio.
Definition: addrman.cpp:956
AddrManImpl::GUARDED_BY
int nNew GUARDED_BY(cs)
number of (unique) "new" entries
Definition: addrman_impl.h:201
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:178
AddrManImpl::Good_
bool Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:617
AddrManImpl::SwapRandom
void SwapRandom(unsigned int nRandomPos1, unsigned int nRandomPos2) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Swap two elements in vRandom.
Definition: addrman.cpp:429
AddrManImpl::FILE_FORMAT
static constexpr Format FILE_FORMAT
The maximum format this software knows it can unserialize.
Definition: addrman_impl.h:171
AddrInfo::nRefCount
int nRefCount
reference count in new sets (memory only)
Definition: addrman_impl.h:56
AddrManImpl::Add_
bool Add_(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:671
AddrManImpl::Unserialize
void Unserialize(Stream &s_) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:229
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
AddrManImpl::Good
bool Good(const CService &addr, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1074
AddrManImpl::Select_
std::pair< CAddress, int64_t > Select_(bool newOnly) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: addrman.cpp:703
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:87
AddrManImpl::FindAddressEntry
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1142
std
Definition: setup_common.h:33
AddrManImpl::GetAsmap
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1151
logging.h
ADDRMAN_NEW_BUCKET_COUNT
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman_impl.h:29
AddrManImpl::cs
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman_impl.h:149
AddrManImpl::V1_DETERMINISTIC
@ V1_DETERMINISTIC
for pre-asmap files
Definition: addrman_impl.h:160
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:31
serialize.h
AddrManDeterministic
Definition: addrman.cpp:124
AddrManImpl::size
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1059
AddrManImpl::CheckAddrman
int CheckAddrman() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Perform consistency check, regardless of m_consistency_check_ratio.
Definition: addrman.cpp:971
AddrInfo::GetTriedBucket
int GetTriedBucket(const uint256 &nKey, const std::vector< bool > &asmap) const
Calculate in which "tried" bucket this entry belongs.
Definition: addrman.cpp:46
AddrManImpl::GUARDED_BY
int nTried GUARDED_BY(cs)
Definition: addrman_impl.h:195
AddrManImpl::SelectTriedCollision
std::pair< CAddress, int64_t > SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1099
AddrManImpl
Definition: addrman_impl.h:100
AddrManImpl::V2_ASMAP
@ V2_ASMAP
for files including asmap version
Definition: addrman_impl.h:161
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:85
AddrInfo::fInTried
bool fInTried
in tried set? (memory only)
Definition: addrman_impl.h:59
FastRandomContext
Fast randomness source.
Definition: random.h:119
AddrManImpl::Format
Format
Serialization versions.
Definition: addrman_impl.h:158
AddrInfo::nRandomPos
int nRandomPos
position in vRandom
Definition: addrman_impl.h:62
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:453
AddrManImpl::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.cpp:1083