Bitcoin Core  24.1.0
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_ADDRMAN_H
7 #define BITCOIN_ADDRMAN_H
8 
9 #include <netaddress.h>
10 #include <netgroup.h>
11 #include <protocol.h>
12 #include <streams.h>
13 #include <util/time.h>
14 
15 #include <cstdint>
16 #include <memory>
17 #include <optional>
18 #include <utility>
19 #include <vector>
20 
21 class InvalidAddrManVersionError : public std::ios_base::failure
22 {
23 public:
24  InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
25 };
26 
27 class AddrManImpl;
28 
30 static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
31 
34  // Whether the address is in the new or tried table
35  const bool tried;
36 
37  // Addresses in the tried table should always have a multiplicity of 1.
38  // Addresses in the new table can have multiplicity between 1 and
39  // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
40  const int multiplicity;
41 
42  // If the address is in the new table, the bucket and position are
43  // populated based on the first source who sent the address.
44  // In certain edge cases, this may not be where the address is currently
45  // located.
46  const int bucket;
47  const int position;
48 
50  return std::tie(tried, multiplicity, bucket, position) ==
51  std::tie(other.tried, other.multiplicity, other.bucket, other.position);
52  }
53  explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
54  : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
55 };
56 
86 class AddrMan
87 {
88 protected:
89  const std::unique_ptr<AddrManImpl> m_impl;
90 
91 public:
92  explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
93 
94  ~AddrMan();
95 
96  template <typename Stream>
97  void Serialize(Stream& s_) const;
98 
99  template <typename Stream>
100  void Unserialize(Stream& s_);
101 
103  size_t size() const;
104 
114  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
115 
123  bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
124 
126  void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
127 
129  void ResolveCollisions();
130 
138  std::pair<CAddress, NodeSeconds> SelectTriedCollision();
139 
147  std::pair<CAddress, NodeSeconds> Select(bool newOnly = false) const;
148 
158  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
159 
171  void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
172 
174  void SetServices(const CService& addr, ServiceFlags nServices);
175 
183  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
184 };
185 
186 #endif // BITCOIN_ADDRMAN_H
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:89
ServiceFlags
nServices flags
Definition: protocol.h:267
void Unserialize(Stream &s_)
Definition: addrman.cpp:1175
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition: addrman.h:53
bool operator==(AddressPosition other)
Definition: addrman.h:49
std::optional< AddressPosition > FindAddressEntry(const CAddress &addr)
Test-only function Find the address record in AddrMan and return information about its position...
Definition: addrman.cpp:1238
Netgroup manager.
Definition: netgroup.h:16
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions...
Definition: addrman.cpp:1208
Stochastic address manager.
Definition: addrman.h:86
std::pair< CAddress, NodeSeconds > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1218
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
const int bucket
Definition: addrman.h:46
const char * source
Definition: rpcconsole.cpp:59
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, std::chrono::seconds time_penalty=0s)
Attempt to add one or more addresses to addrman&#39;s new table.
Definition: addrman.cpp:1193
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1203
A CService with information about it as peer.
Definition: protocol.h:354
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition: addrman.cpp:1228
void Serialize(Stream &s_) const
Definition: addrman.cpp:1169
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.cpp:1223
const int position
Definition: addrman.h:47
const int multiplicity
Definition: addrman.h:40
Network address.
Definition: netaddress.h:117
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict...
Definition: addrman.cpp:1213
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry&#39;s service bits.
Definition: addrman.cpp:1233
const bool tried
Definition: addrman.h:35
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:30
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1188
InvalidAddrManVersionError(std::string msg)
Definition: addrman.h:24
Test-only struct, capturing info about an address in AddrMan.
Definition: addrman.h:33
bool Good(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
Mark an address record as accessible and attempt to move it to addrman&#39;s tried table.
Definition: addrman.cpp:1198
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1163