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-2021 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 
27  // Whether the address is in the new or tried table
28  const bool tried;
29 
30  // Addresses in the tried table should always have a multiplicity of 1.
31  // Addresses in the new table can have multiplicity between 1 and
32  // ADDRMAN_NEW_BUCKETS_PER_ADDRESS
33  const int multiplicity;
34 
35  // If the address is in the new table, the bucket and position are
36  // populated based on the first source who sent the address.
37  // In certain edge cases, this may not be where the address is currently
38  // located.
39  const int bucket;
40  const int position;
41 
43  return std::tie(tried, multiplicity, bucket, position) ==
44  std::tie(other.tried, other.multiplicity, other.bucket, other.position);
45  }
46  explicit AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
47  : tried{tried_in}, multiplicity{multiplicity_in}, bucket{bucket_in}, position{position_in} {}
48 };
49 
79 class AddrMan
80 {
81 protected:
82  const std::unique_ptr<AddrManImpl> m_impl;
83 
84 public:
85  explicit AddrMan(std::vector<bool> asmap, bool deterministic, int32_t consistency_check_ratio);
86 
87  ~AddrMan();
88 
89  template <typename Stream>
90  void Serialize(Stream& s_) const;
91 
92  template <typename Stream>
93  void Unserialize(Stream& s_);
94 
96  size_t size() const;
97 
107  bool Add(const std::vector<CAddress>& vAddr, const CNetAddr& source, int64_t nTimePenalty = 0);
108 
116  bool Good(const CService& addr, int64_t nTime = GetAdjustedTime());
117 
119  void Attempt(const CService& addr, bool fCountFailure, int64_t nTime = GetAdjustedTime());
120 
122  void ResolveCollisions();
123 
131  std::pair<CAddress, int64_t> SelectTriedCollision();
132 
140  std::pair<CAddress, int64_t> Select(bool newOnly = false) const;
141 
151  std::vector<CAddress> GetAddr(size_t max_addresses, size_t max_pct, std::optional<Network> network) const;
152 
164  void Connected(const CService& addr, int64_t nTime = GetAdjustedTime());
165 
167  void SetServices(const CService& addr, ServiceFlags nServices);
168 
169  const std::vector<bool>& GetAsmap() const;
170 
178  std::optional<AddressPosition> FindAddressEntry(const CAddress& addr);
179 };
180 
181 #endif // BITCOIN_ADDRMAN_H
AddrMan::AddrMan
AddrMan(std::vector< bool > asmap, bool deterministic, int32_t consistency_check_ratio)
Definition: addrman.cpp:1156
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:528
AddressPosition::operator==
bool operator==(AddressPosition other)
Definition: addrman.h:42
streams.h
timedata.h
CNetAddr
Network address.
Definition: netaddress.h:118
source
const char * source
Definition: rpcconsole.cpp:64
AddrMan::Unserialize
void Unserialize(Stream &s_)
Definition: addrman.cpp:1168
AddrMan
Stochastic address manager.
Definition: addrman.h:79
AddrMan::size
size_t size() const
Return the number of (unique) addresses in all tables.
Definition: addrman.cpp:1182
AddressPosition::multiplicity
const int multiplicity
Definition: addrman.h:33
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:271
AddrMan::m_impl
const std::unique_ptr< AddrManImpl > m_impl
Definition: addrman.h:82
AddrMan::Connected
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime())
We have successfully connected to this peer.
Definition: addrman.cpp:1222
AddressPosition::tried
const bool tried
Definition: addrman.h:28
protocol.h
AddrMan::Good
bool Good(const CService &addr, int64_t nTime=GetAdjustedTime())
Mark an address record as accessible and attempt to move it to addrman's tried table.
Definition: addrman.cpp:1192
AddrMan::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0)
Attempt to add one or more addresses to addrman's new table.
Definition: addrman.cpp:1187
netaddress.h
AddrMan::Serialize
void Serialize(Stream &s_) const
Definition: addrman.cpp:1162
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:1207
AddressPosition
Test-only struct, capturing info about an address in AddrMan.
Definition: addrman.h:26
AddressPosition::AddressPosition
AddressPosition(bool tried_in, int multiplicity_in, int bucket_in, int position_in)
Definition: addrman.h:46
AddressPosition::position
const int position
Definition: addrman.h:40
AddrMan::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime())
Mark an entry as connection attempted to.
Definition: addrman.cpp:1197
GetAdjustedTime
int64_t GetAdjustedTime()
Definition: timedata.cpp:35
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
AddrMan::GetAsmap
const std::vector< bool > & GetAsmap() const
Definition: addrman.cpp:1232
AddrMan::Select
std::pair< CAddress, int64_t > Select(bool newOnly=false) const
Choose an address to connect to.
Definition: addrman.cpp:1212
DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
static constexpr int32_t DEFAULT_ADDRMAN_CONSISTENCY_CHECKS
Default for -checkaddrman.
Definition: addrman.h:23
AddressPosition::bucket
const int bucket
Definition: addrman.h:39
AddrManImpl
Definition: addrman_impl.h:100
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:1202
AddrMan::~AddrMan
~AddrMan()
AddrMan::FindAddressEntry
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:1237
AddrMan::SetServices
void SetServices(const CService &addr, ServiceFlags nServices)
Update an entry's service bits.
Definition: addrman.cpp:1227
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:1217