Bitcoin Core  29.1.0
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2022 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 <unordered_set>
19 #include <utility>
20 #include <vector>
21 
22 class InvalidAddrManVersionError : public std::ios_base::failure
23 {
24 public:
25  InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
26 };
27 
28 class AddrManImpl;
29 class AddrInfo;
30 
32 static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
33 
36  // Whether the address is in the new or tried table
37  const bool tried;
38 
39  // Addresses in the tried table should always have a multiplicity of 1.
40  // Addresses in the new table can have multiplicity between 1 and
41  // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
42  const int multiplicity;
43 
44  // If the address is in the new table, the bucket and position are
45  // populated based on the first source who sent the address.
46  // In certain edge cases, this may not be where the address is currently
47  // located.
48  const int bucket;
49  const int position;
50 
52  return std::tie(tried, multiplicity, bucket, position) ==
53  std::tie(other.tried, other.multiplicity, other.bucket, other.position);
54  }
55  explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
56  : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
57 };
58 
88 class AddrMan
89 {
90 protected:
91  const std::unique_ptr<AddrManImpl> m_impl;
92 
93 public:
94  explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
95 
96  ~AddrMan();
97 
98  template <typename Stream>
99  void Serialize(Stream& s_) const;
100 
101  template <typename Stream>
102  void Unserialize(Stream& s_);
103 
111  size_t Size(std::optional<Network> net = std::nullopt, std::optional<bool> in_new = std::nullopt) const;
112 
125  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
126 
134  bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
135 
137  void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
138 
140  void ResolveCollisions();
141 
149  std::pair<CAddress, NodeSeconds> SelectTriedCollision();
150 
163  std::pair<CAddress, NodeSeconds> Select(bool new_only = false, const std::unordered_set<Network>& networks = {}) const;
164 
175  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const;
176 
186  std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const;
187 
199  void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
200 
202  void SetServices(const CService& addr, ServiceFlags nServices);
203 
211  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
212 };
213 
214 #endif // BITCOIN_ADDRMAN_H
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:91
size_t Size(std::optional< Network > net=std::nullopt, std::optional< bool > in_new=std::nullopt) const
Return size information about addrman.
Definition: addrman.cpp:1297
ServiceFlags
nServices flags
Definition: protocol.h:309
void Unserialize(Stream &s_)
Definition: addrman.cpp:1284
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition: addrman.h:55
bool operator==(AddressPosition other)
Definition: addrman.h:51
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:1352
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.cpp:1332
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:1317
Stochastic address manager.
Definition: addrman.h:88
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
const int bucket
Definition: addrman.h:48
const char * source
Definition: rpcconsole.cpp:62
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:1302
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
Extended statistics about a CAddress.
Definition: addrman_impl.h:45
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1312
A CService with information about it as peer.
Definition: protocol.h:366
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition: addrman.cpp:1342
void Serialize(Stream &s_) const
Definition: addrman.cpp:1278
const int position
Definition: addrman.h:49
const int multiplicity
Definition: addrman.h:42
Network address.
Definition: netaddress.h:111
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict...
Definition: addrman.cpp:1322
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry&#39;s service bits.
Definition: addrman.cpp:1347
const bool tried
Definition: addrman.h:37
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:32
std::pair< CAddress, NodeSeconds > Select(bool new_only=false, const std::unordered_set< Network > &networks={}) const
Choose an address to connect to.
Definition: addrman.cpp:1327
InvalidAddrManVersionError(std::string msg)
Definition: addrman.h:25
Location information for an address in AddrMan.
Definition: addrman.h:35
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:1307
std::vector< std::pair< AddrInfo, AddressPosition > > GetEntries(bool from_tried) const
Returns an information-location pair for all addresses in the selected addrman table.
Definition: addrman.cpp:1337
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1272