node_eviction.cpp raw

   1  // Copyright (c) 2020-2022 The Limenka 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>
   7  #include <test/fuzz/FuzzedDataProvider.h>
   8  #include <test/fuzz/fuzz.h>
   9  #include <test/fuzz/util.h>
  10  #include <test/fuzz/util/net.h>
  11  #include <test/util/random.h>
  12  
  13  #include <algorithm>
  14  #include <cassert>
  15  #include <cstdint>
  16  #include <optional>
  17  #include <vector>
  18  
  19  FUZZ_TARGET(node_eviction)
  20  {
  21      SeedRandomStateForTest(SeedRand::ZEROS);
  22      FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
  23      std::vector<NodeEvictionCandidate> eviction_candidates;
  24      LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10000) {
  25          eviction_candidates.push_back({
  26              /*id=*/fuzzed_data_provider.ConsumeIntegral<NodeId>(),
  27              /*m_connected=*/std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
  28              /*m_min_ping_time=*/std::chrono::microseconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
  29              /*m_last_block_time=*/std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
  30              /*m_last_tx_time=*/std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int64_t>()},
  31              /*fRelevantServices=*/fuzzed_data_provider.ConsumeBool(),
  32              /*m_relay_txs=*/fuzzed_data_provider.ConsumeBool(),
  33              /*fBloomFilter=*/fuzzed_data_provider.ConsumeBool(),
  34              /*nKeyedNetGroup=*/fuzzed_data_provider.ConsumeIntegral<uint64_t>(),
  35              /*prefer_evict=*/fuzzed_data_provider.ConsumeBool(),
  36              /*m_is_local=*/fuzzed_data_provider.ConsumeBool(),
  37              /*m_network=*/fuzzed_data_provider.PickValueInArray(ALL_NETWORKS),
  38              /*m_noban=*/fuzzed_data_provider.ConsumeBool(),
  39              /*m_conn_type=*/fuzzed_data_provider.PickValueInArray(ALL_CONNECTION_TYPES),
  40          });
  41      }
  42      // Make a copy since eviction_candidates may be in some valid but otherwise
  43      // indeterminate state after the SelectNodeToEvict(&&) call.
  44      const std::vector<NodeEvictionCandidate> eviction_candidates_copy = eviction_candidates;
  45      const std::optional<NodeId> node_to_evict = SelectNodeToEvict(std::move(eviction_candidates), /*force=*/fuzzed_data_provider.ConsumeBool());
  46      if (node_to_evict) {
  47          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; }));
  48      }
  49  }
  50