Bitcoin Core 31.99.0
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1// Copyright (c) 2012 Pieter Wuille
2// Copyright (c) 2012-present 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 <util/time.h>
12
13#include <cstddef>
14#include <cstdint>
15#include <ios>
16#include <memory>
17#include <optional>
18#include <string>
19#include <unordered_set>
20#include <utility>
21#include <vector>
22
23class NetGroupManager;
24
26static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP{8};
28static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
30static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
32static constexpr auto ADDRMAN_HORIZON{30 * 24h};
34static constexpr int32_t ADDRMAN_RETRIES{3};
36static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
38static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h};
40static constexpr auto ADDRMAN_REPLACEMENT{4h};
42static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
44static constexpr auto ADDRMAN_TEST_WINDOW{40min};
45
46class InvalidAddrManVersionError : public std::ios_base::failure
47{
48public:
49 InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
50};
51
52class AddrManImpl;
53class AddrInfo;
54
56static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
57
60 // Whether the address is in the new or tried table
61 const bool tried;
62
63 // Addresses in the tried table should always have a multiplicity of 1.
64 // Addresses in the new table can have multiplicity between 1 and
65 // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
66 const int multiplicity;
67
68 // If the address is in the new table, the bucket and position are
69 // populated based on the first source who sent the address.
70 // In certain edge cases, this may not be where the address is currently
71 // located.
72 const int bucket;
73 const int position;
74
75 bool operator==(const AddressPosition&) const = default;
76 explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
77 : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
78};
79
110{
111protected:
112 const std::unique_ptr<AddrManImpl> m_impl;
113
114public:
115 explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
116
118
119 template <typename Stream>
120 void Serialize(Stream& s_) const;
121
122 template <typename Stream>
123 void Unserialize(Stream& s_);
124
132 size_t Size(std::optional<Network> net = std::nullopt, std::optional<bool> in_new = std::nullopt) const;
133
146 bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
147
155 bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
156
158 void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
159
161 void ResolveCollisions();
162
170 std::pair<CAddress, NodeSeconds> SelectTriedCollision();
171
184 std::pair<CAddress, NodeSeconds> Select(bool new_only = false, const std::unordered_set<Network>& networks = {}) const;
185
196 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, bool filtered = true) const;
197
207 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const;
208
220 void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
221
223 void SetServices(const CService& addr, ServiceFlags nServices);
224
232 std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
233};
234
235#endif // BITCOIN_ADDRMAN_H
static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP
Over how many buckets entries with new addresses originating from a single group are spread.
Definition: addrman.h:28
static constexpr auto ADDRMAN_HORIZON
How old addresses can maximally be.
Definition: addrman.h:32
static constexpr int32_t ADDRMAN_MAX_FAILURES
How many successive failures are allowed ...
Definition: addrman.h:36
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:56
static constexpr auto ADDRMAN_MIN_FAIL
... in at least this duration
Definition: addrman.h:38
static constexpr auto ADDRMAN_TEST_WINDOW
The maximum time we'll spend trying to resolve a tried table collision.
Definition: addrman.h:44
static constexpr auto ADDRMAN_REPLACEMENT
How recent a successful connection should be before we allow an address to be evicted from tried.
Definition: addrman.h:40
static constexpr int32_t ADDRMAN_RETRIES
After how many failed attempts we give up on a new node.
Definition: addrman.h:34
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE
The maximum number of tried addr collisions to store.
Definition: addrman.h:42
static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP
Over how many buckets entries with tried addresses from a single group (/16 for IPv4) are spread.
Definition: addrman.h:26
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS
Maximum number of times an address can occur in the new table.
Definition: addrman.h:30
Extended statistics about a CAddress.
Definition: addrman_impl.h:46
Stochastic address manager.
Definition: addrman.h:110
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition: addrman.cpp:1324
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:1309
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:112
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1294
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:1279
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:1334
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:1319
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network, bool filtered=true) const
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.cpp:1314
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1299
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:1289
void Serialize(Stream &s_) const
Definition: addrman.cpp:1260
void Unserialize(Stream &s_)
Definition: addrman.cpp:1266
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1254
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1304
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:1284
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1329
A CService with information about it as peer.
Definition: protocol.h:387
Network address.
Definition: netaddress.h:113
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:530
InvalidAddrManVersionError(std::string msg)
Definition: addrman.h:49
Netgroup manager.
Definition: netgroup.h:17
ServiceFlags
nServices flags
Definition: protocol.h:321
const char * source
Definition: rpcconsole.cpp:63
Location information for an address in AddrMan.
Definition: addrman.h:59
const bool tried
Definition: addrman.h:61
const int bucket
Definition: addrman.h:72
bool operator==(const AddressPosition &) const =default
const int position
Definition: addrman.h:73
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition: addrman.h:76
const int multiplicity
Definition: addrman.h:66
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:35