Bitcoin ABC  0.28.12
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2016 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 <protocol.h>
11 #include <streams.h>
12 #include <timedata.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 public:
23  InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) {}
24 };
25 
26 class AddrManImpl;
27 
29 static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
30 
69 class AddrMan {
70  const std::unique_ptr<AddrManImpl> m_impl;
71 
72 public:
73  AddrMan(std::vector<bool> asmap, int32_t consistency_check_ratio);
74 
76 
77  template <typename Stream> void Serialize(Stream &s_) const;
78 
79  template <typename Stream> void Unserialize(Stream &s_);
80 
82  size_t size() const;
83 
95  bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
96  std::chrono::seconds time_penalty = 0s);
97 
99  void Good(const CService &addr, bool test_before_evict = true,
100  NodeSeconds time = AdjustedTime());
101 
103  void Attempt(const CService &addr, bool fCountFailure,
104  NodeSeconds time = AdjustedTime());
105 
108  void ResolveCollisions();
109 
117  std::pair<CAddress, NodeSeconds> SelectTriedCollision();
118 
126  std::pair<CAddress, NodeSeconds> Select(bool newOnly = false) const;
127 
141  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
142  std::optional<Network> network) const;
143 
156  void Connected(const CService &addr, NodeSeconds time = AdjustedTime());
157 
159  void SetServices(const CService &addr, ServiceFlags nServices);
160 
161  const std::vector<bool> &GetAsmap() const;
162 
163  void Clear();
164 
166  void MakeDeterministic();
167 
168  friend class AddrManTest;
169  friend class AddrManCorrupted;
170 };
171 
172 #endif // BITCOIN_ADDRMAN_H
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:29
Stochastic address manager.
Definition: addrman.h:69
friend class AddrManCorrupted
Definition: addrman.h:169
void Clear()
Definition: addrman.cpp:1364
void MakeDeterministic()
Ensure that bucket placement is always the same for testing purposes.
Definition: addrman.cpp:1368
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:70
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:1347
void Good(const CService &addr, bool test_before_evict=true, NodeSeconds time=AdjustedTime())
Mark an entry as accessible, possibly moving it from "new" to "tried".
Definition: addrman.cpp:1325
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1360
std::pair< CAddress, NodeSeconds > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1343
void Connected(const CService &addr, NodeSeconds time=AdjustedTime())
We have successfully connected to this peer.
Definition: addrman.cpp:1352
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1335
friend class AddrManTest
Definition: addrman.h:168
AddrMan(std::vector< bool > asmap, int32_t consistency_check_ratio)
Definition: addrman.cpp:1291
void Serialize(Stream &s_) const
Definition: addrman.cpp:1299
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1316
void Unserialize(Stream &s_)
Definition: addrman.cpp:1303
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1339
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:1320
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1356
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=AdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1330
Network address.
Definition: netaddress.h:121
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
InvalidAddrManVersionError(std::string msg)
Definition: addrman.h:23
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
ServiceFlags
nServices flags.
Definition: protocol.h:335
const char * source
Definition: rpcconsole.cpp:53
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
NodeSeconds AdjustedTime()
Definition: timedata.h:70