Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
addrman.h
Go to the documentation of this file.
1// Copyright (c) 2012 Pieter Wuille
2// Copyright (c) 2012-2022 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 <utility>
19#include <vector>
20
21class InvalidAddrManVersionError : public std::ios_base::failure
22{
23public:
24 InvalidAddrManVersionError(std::string msg) : std::ios_base::failure(msg) { }
25};
26
27class AddrManImpl;
28class AddrInfo;
29
31static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
32
35 // Whether the address is in the new or tried table
36 const bool tried;
37
38 // Addresses in the tried table should always have a multiplicity of 1.
39 // Addresses in the new table can have multiplicity between 1 and
40 // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
41 const int multiplicity;
42
43 // If the address is in the new table, the bucket and position are
44 // populated based on the first source who sent the address.
45 // In certain edge cases, this may not be where the address is currently
46 // located.
47 const int bucket;
48 const int position;
49
51 return std::tie(tried, multiplicity, bucket, position) ==
52 std::tie(other.tried, other.multiplicity, other.bucket, other.position);
53 }
54 explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
55 : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
56};
57
88{
89protected:
90 const std::unique_ptr<AddrManImpl> m_impl;
91
92public:
93 explicit AddrMan(const NetGroupManager& netgroupman, bool deterministic, int32_t consistency_check_ratio);
94
96
97 template <typename Stream>
98 void Serialize(Stream& s_) const;
99
100 template <typename Stream>
101 void Unserialize(Stream& s_);
102
110 size_t Size(std::optional<Network> net = std::nullopt, std::optional<bool> in_new = std::nullopt) const;
111
124 bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, std::chrono::seconds time_penalty = 0s);
125
133 bool Good(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
134
136 void Attempt(const CService& addr, bool fCountFailure, NodeSeconds time = Now<NodeSeconds>());
137
139 void ResolveCollisions();
140
148 std::pair<CAddress, NodeSeconds> SelectTriedCollision();
149
162 std::pair<CAddress, NodeSeconds> Select(bool new_only = false, std::optional<Network> network = std::nullopt) const;
163
174 std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network, const bool filtered = true) const;
175
185 std::vector<std::pair<AddrInfo, AddressPosition>> GetEntries(bool from_tried) const;
186
198 void Connected(const CService& addr, NodeSeconds time = Now<NodeSeconds>());
199
201 void SetServices(const CService& addr, ServiceFlags nServices);
202
210 std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
211};
212
213#endif // BITCOIN_ADDRMAN_H
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition addrman.h:31
Extended statistics about a CAddress.
Stochastic address manager.
Definition addrman.h:88
std::pair< CAddress, NodeSeconds > Select(bool new_only=false, std::optional< Network > network=std::nullopt) const
Choose an address to connect to.
Definition addrman.cpp:1318
void Connected(const CService &addr, NodeSeconds time=Now< NodeSeconds >())
We have successfully connected to this peer.
Definition addrman.cpp:1333
const std::unique_ptr< AddrManImpl > m_impl
Definition addrman.h:90
void Attempt(const CService &addr, bool fCountFailure, NodeSeconds time=Now< NodeSeconds >())
Mark an entry as connection attempted to.
Definition addrman.cpp:1303
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:1288
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:1343
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:1328
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition addrman.cpp:1308
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:1298
void Serialize(Stream &s_) const
Definition addrman.cpp:1269
void Unserialize(Stream &s_)
Definition addrman.cpp:1275
AddrMan(const NetGroupManager &netgroupman, bool deterministic, int32_t consistency_check_ratio)
Definition addrman.cpp:1263
std::pair< CAddress, NodeSeconds > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition addrman.cpp:1313
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:1293
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition addrman.cpp:1338
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network, const bool filtered=true) const
Return all or many randomly selected addresses, optionally by network.
Definition addrman.cpp:1323
A CService with information about it as peer.
Definition protocol.h:367
Network address.
Definition netaddress.h:112
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:531
InvalidAddrManVersionError(std::string msg)
Definition addrman.h:24
Netgroup manager.
Definition netgroup.h:16
ServiceFlags
nServices flags
Definition protocol.h:309
const char * source
Location information for an address in AddrMan.
Definition addrman.h:34
const bool tried
Definition addrman.h:36
bool operator==(AddressPosition other)
Definition addrman.h:50
const int bucket
Definition addrman.h:47
const int position
Definition addrman.h:48
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition addrman.h:54
const int multiplicity
Definition addrman.h:41
T Now()
Return the current time point cast to the given precision.
Definition time.h:91
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition time.h:23