Bitcoin Core  22.99.0
P2P Digital Currency
addrman.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-2021 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <addrdb.h>
6 #include <addrman.h>
7 #include <chainparams.h>
8 #include <merkleblock.h>
10 #include <test/fuzz/fuzz.h>
11 #include <test/fuzz/util.h>
12 #include <time.h>
13 #include <util/asmap.h>
14 
15 #include <cassert>
16 #include <cstdint>
17 #include <optional>
18 #include <string>
19 #include <vector>
20 
22 {
24 }
25 
26 FUZZ_TARGET_INIT(data_stream_addr_man, initialize_addrman)
27 {
28  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
29  CDataStream data_stream = ConsumeDataStream(fuzzed_data_provider);
30  CAddrMan addr_man(/* asmap */ std::vector<bool>(), /* deterministic */ false, /* consistency_check_ratio */ 0);
31  try {
32  ReadFromStream(addr_man, data_stream);
33  } catch (const std::exception&) {
34  }
35 }
36 
38 {
39 public:
41 
42  explicit CAddrManDeterministic(std::vector<bool> asmap, FuzzedDataProvider& fuzzed_data_provider)
43  : CAddrMan(std::move(asmap), /* deterministic */ true, /* consistency_check_ratio */ 0)
44  , m_fuzzed_data_provider(fuzzed_data_provider)
45  {
46  WITH_LOCK(cs, insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
47  }
48 
53  {
54  CNetAddr addr;
57  } else {
58  // The networks [1..6] correspond to CNetAddr::BIP155Network (private).
59  static const std::map<uint8_t, uint8_t> net_len_map = {{1, ADDR_IPV4_SIZE},
60  {2, ADDR_IPV6_SIZE},
61  {4, ADDR_TORV3_SIZE},
62  {5, ADDR_I2P_SIZE},
63  {6, ADDR_CJDNS_SIZE}};
64  uint8_t net = insecure_rand.randrange(5) + 1; // [1..5]
65  if (net == 3) {
66  net = 6;
67  }
68 
70 
71  s << net;
72  s << insecure_rand.randbytes(net_len_map.at(net));
73 
74  s >> addr;
75  }
76 
77  // Return a dummy IPv4 5.5.5.5 if we generated an invalid address.
78  if (!addr.IsValid()) {
79  in_addr v4_addr = {};
80  v4_addr.s_addr = 0x05050505;
81  addr = CNetAddr{v4_addr};
82  }
83 
84  return addr;
85  }
86 
90  void Fill()
91  {
92  LOCK(cs);
93 
94  // Add some of the addresses directly to the "tried" table.
95 
96  // 0, 1, 2, 3 corresponding to 0%, 100%, 50%, 33%
97  const size_t n = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 3);
98 
99  const size_t num_sources = m_fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, 50);
100  CNetAddr prev_source;
101  // Use insecure_rand inside the loops instead of m_fuzzed_data_provider because when
102  // the latter is exhausted it just returns 0.
103  for (size_t i = 0; i < num_sources; ++i) {
104  const auto source = RandAddr();
105  const size_t num_addresses = insecure_rand.randrange(500) + 1; // [1..500]
106 
107  for (size_t j = 0; j < num_addresses; ++j) {
108  const auto addr = CAddress{CService{RandAddr(), 8333}, NODE_NETWORK};
109  const auto time_penalty = insecure_rand.randrange(100000001);
110  Add_(addr, source, time_penalty);
111 
112  if (n > 0 && mapInfo.size() % n == 0) {
113  Good_(addr, false, GetTime());
114  }
115 
116  // Add 10% of the addresses from more than one source.
117  if (insecure_rand.randrange(10) == 0 && prev_source.IsValid()) {
118  Add_(addr, prev_source, time_penalty);
119  }
120  }
121  prev_source = source;
122  }
123  }
124 
133  {
134  LOCK2(cs, other.cs);
135 
136  if (mapInfo.size() != other.mapInfo.size() || nNew != other.nNew ||
137  nTried != other.nTried) {
138  return false;
139  }
140 
141  // Check that all values in `mapInfo` are equal to all values in `other.mapInfo`.
142  // Keys may be different.
143 
144  using CAddrInfoHasher = std::function<size_t(const CAddrInfo&)>;
145  using CAddrInfoEq = std::function<bool(const CAddrInfo&, const CAddrInfo&)>;
146 
147  CNetAddrHash netaddr_hasher;
148 
149  CAddrInfoHasher addrinfo_hasher = [&netaddr_hasher](const CAddrInfo& a) {
150  return netaddr_hasher(static_cast<CNetAddr>(a)) ^ netaddr_hasher(a.source) ^
151  a.nLastSuccess ^ a.nAttempts ^ a.nRefCount ^ a.fInTried;
152  };
153 
154  CAddrInfoEq addrinfo_eq = [](const CAddrInfo& lhs, const CAddrInfo& rhs) {
155  return static_cast<CNetAddr>(lhs) == static_cast<CNetAddr>(rhs) &&
156  lhs.source == rhs.source && lhs.nLastSuccess == rhs.nLastSuccess &&
157  lhs.nAttempts == rhs.nAttempts && lhs.nRefCount == rhs.nRefCount &&
158  lhs.fInTried == rhs.fInTried;
159  };
160 
161  using Addresses = std::unordered_set<CAddrInfo, CAddrInfoHasher, CAddrInfoEq>;
162 
163  const size_t num_addresses{mapInfo.size()};
164 
165  Addresses addresses{num_addresses, addrinfo_hasher, addrinfo_eq};
166  for (const auto& [id, addr] : mapInfo) {
167  addresses.insert(addr);
168  }
169 
170  Addresses other_addresses{num_addresses, addrinfo_hasher, addrinfo_eq};
171  for (const auto& [id, addr] : other.mapInfo) {
172  other_addresses.insert(addr);
173  }
174 
175  if (addresses != other_addresses) {
176  return false;
177  }
178 
179  auto IdsReferToSameAddress = [&](int id, int other_id) EXCLUSIVE_LOCKS_REQUIRED(cs, other.cs) {
180  if (id == -1 && other_id == -1) {
181  return true;
182  }
183  if ((id == -1 && other_id != -1) || (id != -1 && other_id == -1)) {
184  return false;
185  }
186  return mapInfo.at(id) == other.mapInfo.at(other_id);
187  };
188 
189  // Check that `vvNew` contains the same addresses as `other.vvNew`. Notice - `vvNew[i][j]`
190  // contains just an id and the address is to be found in `mapInfo.at(id)`. The ids
191  // themselves may differ between `vvNew` and `other.vvNew`.
192  for (size_t i = 0; i < ADDRMAN_NEW_BUCKET_COUNT; ++i) {
193  for (size_t j = 0; j < ADDRMAN_BUCKET_SIZE; ++j) {
194  if (!IdsReferToSameAddress(vvNew[i][j], other.vvNew[i][j])) {
195  return false;
196  }
197  }
198  }
199 
200  // Same for `vvTried`.
201  for (size_t i = 0; i < ADDRMAN_TRIED_BUCKET_COUNT; ++i) {
202  for (size_t j = 0; j < ADDRMAN_BUCKET_SIZE; ++j) {
203  if (!IdsReferToSameAddress(vvTried[i][j], other.vvTried[i][j])) {
204  return false;
205  }
206  }
207  }
208 
209  return true;
210  }
211 };
212 
213 [[nodiscard]] inline std::vector<bool> ConsumeAsmap(FuzzedDataProvider& fuzzed_data_provider) noexcept
214 {
215  std::vector<bool> asmap = ConsumeRandomLengthBitVector(fuzzed_data_provider);
216  if (!SanityCheckASMap(asmap, 128)) asmap.clear();
217  return asmap;
218 }
219 
221 {
222  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
223  SetMockTime(ConsumeTime(fuzzed_data_provider));
224  std::vector<bool> asmap = ConsumeAsmap(fuzzed_data_provider);
225  auto addr_man_ptr = std::make_unique<CAddrManDeterministic>(asmap, fuzzed_data_provider);
226  if (fuzzed_data_provider.ConsumeBool()) {
227  const std::vector<uint8_t> serialized_data{ConsumeRandomLengthByteVector(fuzzed_data_provider)};
228  CDataStream ds(serialized_data, SER_DISK, INIT_PROTO_VERSION);
229  const auto ser_version{fuzzed_data_provider.ConsumeIntegral<int32_t>()};
230  ds.SetVersion(ser_version);
231  try {
232  ds >> *addr_man_ptr;
233  } catch (const std::ios_base::failure&) {
234  addr_man_ptr = std::make_unique<CAddrManDeterministic>(asmap, fuzzed_data_provider);
235  }
236  }
237  CAddrManDeterministic& addr_man = *addr_man_ptr;
238  while (fuzzed_data_provider.ConsumeBool()) {
239  CallOneOf(
240  fuzzed_data_provider,
241  [&] {
242  addr_man.ResolveCollisions();
243  },
244  [&] {
245  (void)addr_man.SelectTriedCollision();
246  },
247  [&] {
248  std::vector<CAddress> addresses;
249  while (fuzzed_data_provider.ConsumeBool()) {
250  const std::optional<CAddress> opt_address = ConsumeDeserializable<CAddress>(fuzzed_data_provider);
251  if (!opt_address) {
252  break;
253  }
254  addresses.push_back(*opt_address);
255  }
256  const std::optional<CNetAddr> opt_net_addr = ConsumeDeserializable<CNetAddr>(fuzzed_data_provider);
257  if (opt_net_addr) {
258  addr_man.Add(addresses, *opt_net_addr, fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(0, 100000000));
259  }
260  },
261  [&] {
262  const std::optional<CService> opt_service = ConsumeDeserializable<CService>(fuzzed_data_provider);
263  if (opt_service) {
264  addr_man.Good(*opt_service, ConsumeTime(fuzzed_data_provider));
265  }
266  },
267  [&] {
268  const std::optional<CService> opt_service = ConsumeDeserializable<CService>(fuzzed_data_provider);
269  if (opt_service) {
270  addr_man.Attempt(*opt_service, fuzzed_data_provider.ConsumeBool(), ConsumeTime(fuzzed_data_provider));
271  }
272  },
273  [&] {
274  const std::optional<CService> opt_service = ConsumeDeserializable<CService>(fuzzed_data_provider);
275  if (opt_service) {
276  addr_man.Connected(*opt_service, ConsumeTime(fuzzed_data_provider));
277  }
278  },
279  [&] {
280  const std::optional<CService> opt_service = ConsumeDeserializable<CService>(fuzzed_data_provider);
281  if (opt_service) {
282  addr_man.SetServices(*opt_service, ConsumeWeakEnum(fuzzed_data_provider, ALL_SERVICE_FLAGS));
283  }
284  });
285  }
286  const CAddrMan& const_addr_man{addr_man};
287  (void)const_addr_man.GetAddr(
288  /* max_addresses */ fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
289  /* max_pct */ fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096),
290  /* network */ std::nullopt);
291  (void)const_addr_man.Select(fuzzed_data_provider.ConsumeBool());
292  (void)const_addr_man.size();
294  data_stream << const_addr_man;
295 }
296 
297 // Check that serialize followed by unserialize produces the same addrman.
299 {
300  FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
301  SetMockTime(ConsumeTime(fuzzed_data_provider));
302 
303  std::vector<bool> asmap = ConsumeAsmap(fuzzed_data_provider);
304  CAddrManDeterministic addr_man1{asmap, fuzzed_data_provider};
305  CAddrManDeterministic addr_man2{asmap, fuzzed_data_provider};
306 
308 
309  addr_man1.Fill();
310  data_stream << addr_man1;
311  data_stream >> addr_man2;
312  assert(addr_man1 == addr_man2);
313 }
ADDRV2_FORMAT
static constexpr int ADDRV2_FORMAT
A flag that is ORed into the protocol version to designate that addresses should be serialized in (un...
Definition: netaddress.h:34
ConsumeUInt256
uint256 ConsumeUInt256(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.h:153
LOCK2
#define LOCK2(cs1, cs2)
Definition: sync.h:227
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:539
merkleblock.h
CAddrMan::Good_
void Good_(const CService &addr, bool test_before_evict, int64_t time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Mark an entry "good", possibly moving it from "new" to "tried".
Definition: addrman.cpp:545
assert
assert(!tx.IsCoinBase())
SER_DISK
@ SER_DISK
Definition: serialize.h:139
CAddrInfo::source
CNetAddr source
where knowledge about this address first came from
Definition: addrman.h:39
CAddrManDeterministic::operator==
bool operator==(const CAddrManDeterministic &other)
Compare with another AddrMan.
Definition: addrman.cpp:132
CAddrMan::Add_
bool Add_(const CAddress &addr, const CNetAddr &source, int64_t nTimePenalty) EXCLUSIVE_LOCKS_REQUIRED(cs)
Add an entry to the "new" table.
Definition: addrman.cpp:601
asmap.h
CNetAddr
Network address.
Definition: netaddress.h:118
source
const char * source
Definition: rpcconsole.cpp:63
util.h
CAddrInfo::nLastSuccess
int64_t nLastSuccess
last successful connection by us
Definition: addrman.h:42
GetTime
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
CAddrMan::GetAddr
std::vector< CAddress > GetAddr(size_t max_addresses, size_t max_pct, std::optional< Network > network) const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return all or many randomly selected addresses, optionally by network.
Definition: addrman.h:243
ConsumeNetAddr
CNetAddr ConsumeNetAddr(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: util.cpp:373
NODE_NETWORK
@ NODE_NETWORK
Definition: protocol.h:277
chainparams.h
CAddrMan::SetServices
void SetServices(const CService &addr, ServiceFlags nServices) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Definition: addrman.h:264
ADDRMAN_TRIED_BUCKET_COUNT
static constexpr int ADDRMAN_TRIED_BUCKET_COUNT
Definition: addrman.h:129
ReadFromStream
void ReadFromStream(CAddrMan &addr, CDataStream &ssPeers)
Only used by tests.
Definition: addrdb.cpp:179
ConsumeRandomLengthBitVector
std::vector< bool > ConsumeRandomLengthBitVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:69
ConsumeTime
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
Definition: util.cpp:226
CAddrInfo
Extended statistics about a CAddress.
Definition: addrman.h:28
addrdb.h
initialize_addrman
void initialize_addrman()
Definition: addrman.cpp:21
CAddrManDeterministic::RandAddr
CNetAddr RandAddr() EXCLUSIVE_LOCKS_REQUIRED(cs)
Generate a random address.
Definition: addrman.cpp:52
CAddrManDeterministic
Definition: addrman.cpp:37
ADDR_TORV3_SIZE
static constexpr size_t ADDR_TORV3_SIZE
Size of TORv3 address (in bytes).
Definition: netaddress.h:101
CAddrMan::Connected
void Connected(const CService &addr, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Outer function for Connected_()
Definition: addrman.h:255
CallOneOf
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
Definition: util.h:40
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
ConsumeDataStream
CDataStream ConsumeDataStream(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:74
CBaseChainParams::REGTEST
static const std::string REGTEST
Definition: chainparamsbase.h:25
ALL_SERVICE_FLAGS
constexpr ServiceFlags ALL_SERVICE_FLAGS[]
Definition: net.h:41
FuzzedDataProvider.h
time.h
CAddrInfo::nRefCount
int nRefCount
reference count in new sets (memory only)
Definition: addrman.h:48
ConsumeRandomLengthByteVector
std::vector< uint8_t > ConsumeRandomLengthByteVector(FuzzedDataProvider &fuzzed_data_provider, const std::optional< size_t > &max_length=std::nullopt) noexcept
Definition: util.h:61
CNetAddr::IsValid
bool IsValid() const
Definition: netaddress.cpp:451
id
static NodeId id
Definition: denialofservice_tests.cpp:37
CAddrMan::ResolveCollisions
void ResolveCollisions() EXCLUSIVE_LOCKS_REQUIRED(!cs)
See if any to-be-evicted tried table entries have been tested and if so resolve the collisions.
Definition: addrman.h:203
SelectParams
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
Definition: chainparams.cpp:578
ConsumeAsmap
std::vector< bool > ConsumeAsmap(FuzzedDataProvider &fuzzed_data_provider) noexcept
Definition: addrman.cpp:213
CAddrMan::Add
bool Add(const std::vector< CAddress > &vAddr, const CNetAddr &source, int64_t nTimePenalty=0) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Add addresses to addrman's new table.
Definition: addrman.h:167
CDataStream::SetVersion
void SetVersion(int n)
Definition: streams.h:362
FuzzedDataProvider::remaining_bytes
size_t remaining_bytes()
Definition: FuzzedDataProvider.h:84
CAddrMan
Stochastical (IP) address manager.
Definition: addrman.h:142
CAddress
A CService with information about it as peer.
Definition: protocol.h:358
ADDR_IPV6_SIZE
static constexpr size_t ADDR_IPV6_SIZE
Size of IPv6 address (in bytes).
Definition: netaddress.h:97
ADDR_IPV4_SIZE
static constexpr size_t ADDR_IPV4_SIZE
Size of IPv4 address (in bytes).
Definition: netaddress.h:94
CAddrInfo::nAttempts
int nAttempts
connection attempts since last successful attempt
Definition: addrman.h:45
CAddrManDeterministic::CAddrManDeterministic
CAddrManDeterministic(std::vector< bool > asmap, FuzzedDataProvider &fuzzed_data_provider)
Definition: addrman.cpp:42
fuzz.h
EXCLUSIVE_LOCKS_REQUIRED
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CAddrMan::Attempt
void Attempt(const CService &addr, bool fCountFailure, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Mark an entry as connection attempted to.
Definition: addrman.h:193
ADDR_I2P_SIZE
static constexpr size_t ADDR_I2P_SIZE
Size of I2P address (in bytes).
Definition: netaddress.h:104
LOCK
#define LOCK(cs)
Definition: sync.h:226
CAddrManDeterministic::Fill
void Fill()
Fill this addrman with lots of addresses from lots of sources.
Definition: addrman.cpp:90
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
FuzzedDataProvider::ConsumeIntegral
T ConsumeIntegral()
Definition: FuzzedDataProvider.h:194
ADDRMAN_BUCKET_SIZE
static constexpr int ADDRMAN_BUCKET_SIZE
Definition: addrman.h:137
CAddrMan::Good
void Good(const CService &addr, int64_t nTime=GetAdjustedTime()) EXCLUSIVE_LOCKS_REQUIRED(!cs)
Mark an entry as accessible.
Definition: addrman.h:183
ADDRMAN_NEW_BUCKET_COUNT
static constexpr int ADDRMAN_NEW_BUCKET_COUNT
Definition: addrman.h:133
ADDR_CJDNS_SIZE
static constexpr size_t ADDR_CJDNS_SIZE
Size of CJDNS address (in bytes).
Definition: netaddress.h:107
CDataStream
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:204
CAddrManDeterministic::m_fuzzed_data_provider
FuzzedDataProvider & m_fuzzed_data_provider
Definition: addrman.cpp:40
addrman.h
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:138
CAddrInfo::fInTried
bool fInTried
in tried set? (memory only)
Definition: addrman.h:51
FuzzedDataProvider::ConsumeBool
bool ConsumeBool()
Definition: FuzzedDataProvider.h:288
ConsumeWeakEnum
WeakEnumType ConsumeWeakEnum(FuzzedDataProvider &fuzzed_data_provider, const WeakEnumType(&all_types)[size]) noexcept
Definition: util.h:115
CAddrMan::size
size_t size() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return the number of (unique) addresses in all tables.
Definition: addrman.h:159
CAddrMan::SelectTriedCollision
CAddrInfo SelectTriedCollision() EXCLUSIVE_LOCKS_REQUIRED(!cs)
Randomly select an address in tried that another address is attempting to evict.
Definition: addrman.h:213
CNetAddrHash
Definition: netaddress.h:470
FuzzedDataProvider::ConsumeIntegralInRange
T ConsumeIntegralInRange(T min, T max)
Definition: FuzzedDataProvider.h:204
FUZZ_TARGET_INIT
FUZZ_TARGET_INIT(data_stream_addr_man, initialize_addrman)
Definition: addrman.cpp:26
FastRandomContext
Fast randomness source.
Definition: random.h:119
CAddrMan::cs
Mutex cs
A mutex to protect the inner data structures.
Definition: addrman.h:277
SanityCheckASMap
bool SanityCheckASMap(const std::vector< bool > &asmap, int bits)
Definition: asmap.cpp:129
INIT_PROTO_VERSION
static const int INIT_PROTO_VERSION
initial proto version, to be increased after version/verack negotiation
Definition: version.h:15
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12