Bitcoin ABC  0.26.3
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 
14 #include <cstdint>
15 #include <memory>
16 #include <optional>
17 #include <utility>
18 #include <vector>
19 
20 class InvalidAddrManVersionError : public std::ios_base::failure {
21 public:
22  InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) {}
23 };
24 
25 class AddrManImpl;
26 
28 static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
29 
68 class AddrMan {
69  const std::unique_ptr<AddrManImpl> m_impl;
70 
71 public:
72  AddrMan(std::vector<bool> asmap, int32_t consistency_check_ratio);
73 
75 
76  template <typename Stream> void Serialize(Stream &s_) const;
77 
78  template <typename Stream> void Unserialize(Stream &s_);
79 
81  size_t size() const;
82 
84  bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
85  int64_t nTimePenalty = 0);
86 
88  void Good(const CService &addr, bool test_before_evict = true,
89  int64_t nTime = GetAdjustedTime());
90 
92  void Attempt(const CService &addr, bool fCountFailure,
93  int64_t nTime = GetAdjustedTime());
94 
97  void ResolveCollisions();
98 
106  std::pair<CAddress, int64_t> SelectTriedCollision();
107 
115  std::pair<CAddress, int64_t> Select(bool newOnly = false) const;
116 
130  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
131  std::optional<Network> network) const;
132 
145  void Connected(const CService &addr, int64_t nTime = GetAdjustedTime());
146 
148  void SetServices(const CService &addr, ServiceFlags nServices);
149 
150  const std::vector<bool> &GetAsmap() const;
151 
152  void Clear();
153 
155  void MakeDeterministic();
156 
157  friend class AddrManTest;
158  friend class AddrManCorrupted;
159 };
160 
161 #endif // BITCOIN_ADDRMAN_H
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:28
Stochastic address manager.
Definition: addrman.h:68
friend class AddrManCorrupted
Definition: addrman.h:158
std::pair< CAddress, int64_t > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1337
void Clear()
Definition: addrman.cpp:1362
void MakeDeterministic()
Ensure that bucket placement is always the same for testing purposes.
Definition: addrman.cpp:1366
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:69
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:1345
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1358
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Add addresses to addrman's new table.
Definition: addrman.cpp:1319
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1333
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
We have successfully connected to this peer.
Definition: addrman.cpp:1350
friend class AddrManTest
Definition: addrman.h:157
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1329
AddrMan(std::vector< bool > asmap, int32_t consistency_check_ratio)
Definition: addrman.cpp:1290
void Serialize(Stream &s_) const
Definition: addrman.cpp:1298
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1315
void Unserialize(Stream &s_)
Definition: addrman.cpp:1302
std::pair< CAddress, int64_t > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1341
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1354
void Good(const CService &addr, bool test_before_evict=true, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible, possibly moving it from "new" to "tried".
Definition: addrman.cpp:1324
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:22
ServiceFlags
nServices flags.
Definition: protocol.h:339
const char * source
Definition: rpcconsole.cpp:53
int64_t GetAdjustedTime()
Definition: timedata.cpp:34