Bitcoin Core  24.99.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 <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 
95 
96  template <typename Stream>
97  void Serialize(Stream& s_) const;
98 
99  template <typename Stream>
100  void Unserialize(Stream& s_);
101 
109  size_t Size(std::optional<Network> net = std::nullopt, std::optional<bool> in_new = std::nullopt) const;
110 
120  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
121 
129  bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
130 
132  void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
133 
135  void ResolveCollisions();
136 
144  std::pair<CAddress, NodeSeconds> SelectTriedCollision();
145 
153  std::pair<CAddress, NodeSeconds> Select(bool newOnly = false) const;
154 
164  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
165 
177  void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
178 
180  void SetServices(const CService& addr, ServiceFlags nServices);
181 
189  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
190 };
191 
192 #endif // BITCOIN_ADDRMAN_H
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:30
Stochastic address manager.
Definition: addrman.h:87
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition: addrman.cpp:1275
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:89
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:1270
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1250
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:1235
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:1285
std::pair< CAddress, NodeSeconds > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1265
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1255
bool Good(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
Mark an address record as accessible and attempt to move it to addrman's tried table.
Definition: addrman.cpp:1245
void Serialize(Stream &s_) const
Definition: addrman.cpp:1216
void Unserialize(Stream &s_)
Definition: addrman.cpp:1222
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1210
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1260
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's new table.
Definition: addrman.cpp:1240
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1280
A CService with information about it as peer.
Definition: protocol.h:361
Network address.
Definition: netaddress.h:120
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:520
InvalidAddrManVersionError(std::string msg)
Definition: addrman.h:24
Netgroup manager.
Definition: netgroup.h:16
ServiceFlags
nServices flags
Definition: protocol.h:273
const char * source
Definition: rpcconsole.cpp:58
Test-only struct, capturing info about an address in AddrMan.
Definition: addrman.h:33
const bool tried
Definition: addrman.h:35
bool operator==(AddressPosition other)
Definition: addrman.h:49
const int bucket
Definition: addrman.h:46
const int position
Definition: addrman.h:47
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition: addrman.h:53
const int multiplicity
Definition: addrman.h:40
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25