Bitcoin Core  22.99.0
P2P Digital Currency
node_eviction.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 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 <net.h>
6 #include <protocol.h>
8 #include <test/fuzz/fuzz.h>
9 #include <test/fuzz/util.h>
10 
11 #include <algorithm>
12 #include <cassert>
13 #include <cstdint>
14 #include <optional>
15 #include <vector>
16 
17 FUZZ_TARGET(node_eviction)
18 {
19  FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
20  std::vector<NodeEvictionCandidate> eviction_candidates;
21  while (fuzzed_data_provider.ConsumeBool()) {
22  eviction_candidates.push_back({
23  /* id */ fuzzed_data_provider.ConsumeIntegral<NodeId>(),
24  /* nTimeConnected */ fuzzed_data_provider.ConsumeIntegral<int64_t>(),
25  /* m_min_ping_time */ std::chrono::microseconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
26  /* nLastBlockTime */ fuzzed_data_provider.ConsumeIntegral<int64_t>(),
27  /* nLastTXTime */ fuzzed_data_provider.ConsumeIntegral<int64_t>(),
28  /* fRelevantServices */ fuzzed_data_provider.ConsumeBool(),
29  /* fRelayTxes */ fuzzed_data_provider.ConsumeBool(),
30  /* fBloomFilter */ fuzzed_data_provider.ConsumeBool(),
31  /* nKeyedNetGroup */ fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
32  /* prefer_evict */ fuzzed_data_provider.ConsumeBool(),
33  /* m_is_local */ fuzzed_data_provider.ConsumeBool(),
34  /* m_network */ fuzzed_data_provider.PickValueInArray(ALL_NETWORKS),
35  });
36  }
37  // Make a copy since eviction_candidates may be in some valid but otherwise
38  // indeterminate state after the SelectNodeToEvict(&&) call.
39  const std::vector<NodeEvictionCandidate> eviction_candidates_copy = eviction_candidates;
40  const std::optional<NodeId> node_to_evict = SelectNodeToEvict(std::move(eviction_candidates));
41  if (node_to_evict) {
42  assert(std::any_of(eviction_candidates_copy.begin(), eviction_candidates_copy.end(), [&node_to_evict](const NodeEvictionCandidate& eviction_candidate) { return *node_to_evict == eviction_candidate.id; }));
43  }
44 }
NodeId
int64_t NodeId
Definition: net.h:87
assert
assert(!tx.IsCoinBase())
util.h
NodeEvictionCandidate
Definition: net.h:1190
SelectNodeToEvict
std::optional< NodeId > SelectNodeToEvict(std::vector< NodeEvictionCandidate > &&vEvictionCandidates)
Select an inbound peer to evict after filtering out (protecting) peers having distinct,...
Definition: net.cpp:965
FUZZ_TARGET
FUZZ_TARGET(node_eviction)
Definition: node_eviction.cpp:17
FuzzedDataProvider.h
fuzz.h
FuzzedDataProvider
Definition: FuzzedDataProvider.h:31
ALL_NETWORKS
constexpr auto ALL_NETWORKS
Definition: net.h:72