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 <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 <unordered_set>
19#include <utility>
20#include <vector>
21
23static constexpr uint32_t ADDRMAN_TRIED_BUCKETS_PER_GROUP{8};
25static constexpr uint32_t ADDRMAN_NEW_BUCKETS_PER_SOURCE_GROUP{64};
27static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS{8};
29static constexpr auto ADDRMAN_HORIZON{30 * 24h};
31static constexpr int32_t ADDRMAN_RETRIES{3};
33static constexpr int32_t ADDRMAN_MAX_FAILURES{10};
35static constexpr auto ADDRMAN_MIN_FAIL{7 * 24h};
37static constexpr auto ADDRMAN_REPLACEMENT{4h};
39static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE{10};
41static constexpr auto ADDRMAN_TEST_WINDOW{40min};
42
43class InvalidAddrManVersionError : public std::ios_base::failure
44{
45public:
46 InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
47};
48
49class AddrManImpl;
50class AddrInfo;
51
53static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
54
57 // Whether the address is in the new or tried table
58 const bool tried;
59
60 // Addresses in the tried table should always have a multiplicity of 1.
61 // Addresses in the new table can have multiplicity between 1 and
62 // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
63 const int multiplicity;
64
65 // If the address is in the new table, the bucket and position are
66 // populated based on the first source who sent the address.
67 // In certain edge cases, this may not be where the address is currently
68 // located.
69 const int bucket;
70 const int position;
71
73 return std::tie(tried, multiplicity, bucket, position) ==
74 std::tie(other.tried, other.multiplicity, other.bucket, other.position);
75 }
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:25
static constexpr auto ADDRMAN_HORIZON
How old addresses can maximally be.
Definition: addrman.h:29
static constexpr int32_t ADDRMAN_MAX_FAILURES
How many successive failures are allowed ...
Definition: addrman.h:33
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:53
static constexpr auto ADDRMAN_MIN_FAIL
... in at least this duration
Definition: addrman.h:35
static constexpr auto ADDRMAN_TEST_WINDOW
The maximum time we'll spend trying to resolve a tried table collision.
Definition: addrman.h:41
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:37
static constexpr int32_t ADDRMAN_RETRIES
After how many failed attempts we give up on a new node.
Definition: addrman.h:31
static constexpr size_t ADDRMAN_SET_TRIED_COLLISION_SIZE
The maximum number of tried addr collisions to store.
Definition: addrman.h:39
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:23
static constexpr int32_t ADDRMAN_NEW_BUCKETS_PER_ADDRESS
Maximum number of times an address can occur in the new table.
Definition: addrman.h:27
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:1322
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:1307
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:1292
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:1277
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:1332
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:1317
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:1312
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1297
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:1287
void Serialize(Stream &s_) const
Definition: addrman.cpp:1258
void Unserialize(Stream &s_)
Definition: addrman.cpp:1264
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1252
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1302
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:1282
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1327
A CService with information about it as peer.
Definition: protocol.h:367
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:46
Netgroup manager.
Definition: netgroup.h:17
Definition: common.h:29
ServiceFlags
nServices flags
Definition: protocol.h:309
const char * source
Definition: rpcconsole.cpp:62
Location information for an address in AddrMan.
Definition: addrman.h:56
const bool tried
Definition: addrman.h:58
bool operator==(AddressPosition other)
Definition: addrman.h:72
const int bucket
Definition: addrman.h:69
const int position
Definition: addrman.h:70
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition: addrman.h:76
const int multiplicity
Definition: addrman.h:63
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:27