Bitcoin ABC 0.30.7
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 <util/time.h>
13
14#include <cstdint>
15#include <memory>
16#include <optional>
17#include <utility>
18#include <vector>
19
20class InvalidAddrManVersionError : public std::ios_base::failure {
21public:
22 InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) {}
23};
24
25class AddrManImpl;
26
28static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
29
68class AddrMan {
69 const std::unique_ptr<AddrManImpl> m_impl;
70
71public:
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
94 bool Add(const std::vector<CAddress> &vAddr, const CNetAddr &source,
95 std::chrono::seconds time_penalty = 0s);
96
98 void Good(const CService &addr, bool test_before_evict = true,
99 NodeSeconds time = Now<NodeSeconds>());
100
102 void Attempt(const CService &addr, bool fCountFailure,
103 NodeSeconds time = Now<NodeSeconds>());
104
107 void ResolveCollisions();
108
116 std::pair<CAddress, NodeSeconds> SelectTriedCollision();
117
125 std::pair<CAddress, NodeSeconds> Select(bool newOnly = false) const;
126
140 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct,
141 std::optional<Network> network) const;
142
155 void Connected(const CService &addr, NodeSeconds time = Now<NodeSeconds>());
156
158 void SetServices(const CService &addr, ServiceFlags nServices);
159
160 const std::vector<bool> &GetAsmap() const;
161
162 void Clear();
163
165 void MakeDeterministic();
166
167 friend class AddrManTest;
168 friend class AddrManCorrupted;
169};
170
171#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:168
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition: addrman.cpp:1350
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
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1328
std::pair< CAddress, NodeSeconds > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1341
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1333
friend class AddrManTest
Definition: addrman.h:167
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:1314
void Unserialize(Stream &s_)
Definition: addrman.cpp:1302
void Good(const CService &addr, bool test_before_evict=true, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as accessible, possibly moving it from "new" to "tried".
Definition: addrman.cpp:1323
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1337
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:1318
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1354
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
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
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