Bitcoin Core  22.99.0
P2P Digital Currency
addrman.h
Go to the documentation of this file.
1 // Copyright (c) 2012 Pieter Wuille
2 // Copyright (c) 2012-2020 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 AddrManImpl;
21 
23 static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS{0};
24 
54 class AddrMan
55 {
56  const std::unique_ptr<AddrManImpl> m_impl;
57 
58 public:
59  explicit AddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio);
60 
61  ~AddrMan();
62 
63  template <typename Stream>
64  void Serialize(Stream& s_) const;
65 
66  template <typename Stream>
67  void Unserialize(Stream& s_);
68 
70  size_t size() const;
71 
73  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
74 
76  void Good(const CService& addr, int64_t nTime = GetAdjustedTime());
77 
79  void Attempt(const CService& addr, bool fCountFailure, int64_t nTime = GetAdjustedTime());
80 
82  void ResolveCollisions();
83 
91  std::pair<CAddress, int64_t> SelectTriedCollision();
92 
100  std::pair<CAddress, int64_t> Select(bool newOnly = false) const;
101 
111  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
112 
124  void Connected(const CService& addr, int64_t nTime = GetAdjustedTime());
125 
127  void SetServices(const CService& addr, ServiceFlags nServices);
128 
129  const std::vector<bool>& GetAsmap() const;
130 
131  friend class AddrManTest;
132  friend class AddrManDeterministic;
133 };
134 
135 #endif // BITCOIN_ADDRMAN_H
AddrMan::AddrMan
AddrMan(std::vector< bool > asmap, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1113
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
AddrMan::Good
void Good(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an entry as accessible, possibly moving it from "new" to "tried".
Definition: addrman.cpp:1149
streams.h
timedata.h
CNetAddr
Network address.
Definition: netaddress.h:118
source
const char * source
Definition: rpcconsole.cpp:63
AddrMan::Unserialize
void Unserialize(Stream &s_)
Definition: addrman.cpp:1125
AddrMan
Stochastic address manager.
Definition: addrman.h:54
AddrMan::size
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1139
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
AddrMan::m_impl
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:56
AddrMan::Connected
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
We have successfully connected to this peer.
Definition: addrman.cpp:1179
protocol.h
AddrMan::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Add addresses to addrman's new table.
Definition: addrman.cpp:1144
netaddress.h
AddrMan::Serialize
void Serialize(Stream &s_) const
Definition: addrman.cpp:1119
AddrMan::SelectTriedCollision
std::pair< CAddress, int64_t > SelectTriedCollision()
Randomly select an address in the tried table that another address is attempting to evict.
Definition: addrman.cpp:1164
AddrMan::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1154
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
AddrMan::GetAsmap
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1189
AddrMan::Select
std::pair< CAddress, int64_t > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1169
AddrManTest
Definition: addrman_tests.cpp:80
AddrManDeterministic
Definition: addrman.cpp:112
DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:23
AddrManImpl
Definition: addrman_impl.h:99
AddrMan::ResolveCollisions
void ResolveCollisions()
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.cpp:1159
AddrMan::~AddrMan
~AddrMan()
AddrMan::SetServices
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1184
AddrMan::GetAddr
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:1174