|
| 1 | +// Copyright (c) 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 <net.h> |
| 6 | +#include <test/util/setup_common.h> |
| 7 | + |
| 8 | +#include <boost/test/unit_test.hpp> |
| 9 | + |
| 10 | +#include <algorithm> |
| 11 | +#include <functional> |
| 12 | +#include <optional> |
| 13 | +#include <vector> |
| 14 | + |
| 15 | +BOOST_FIXTURE_TEST_SUITE(net_peer_eviction_tests, BasicTestingSetup) |
| 16 | + |
| 17 | +std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_candidates, FastRandomContext& random_context) |
| 18 | +{ |
| 19 | + std::vector<NodeEvictionCandidate> candidates; |
| 20 | + for (int id = 0; id < n_candidates; ++id) { |
| 21 | + candidates.push_back({ |
| 22 | + /* id */ id, |
| 23 | + /* nTimeConnected */ static_cast<int64_t>(random_context.randrange(100)), |
| 24 | + /* m_min_ping_time */ std::chrono::microseconds{random_context.randrange(100)}, |
| 25 | + /* nLastBlockTime */ static_cast<int64_t>(random_context.randrange(100)), |
| 26 | + /* nLastTXTime */ static_cast<int64_t>(random_context.randrange(100)), |
| 27 | + /* fRelevantServices */ random_context.randbool(), |
| 28 | + /* fRelayTxes */ random_context.randbool(), |
| 29 | + /* fBloomFilter */ random_context.randbool(), |
| 30 | + /* nKeyedNetGroup */ random_context.randrange(100), |
| 31 | + /* prefer_evict */ random_context.randbool(), |
| 32 | + /* m_is_local */ random_context.randbool(), |
| 33 | + }); |
| 34 | + } |
| 35 | + return candidates; |
| 36 | +} |
| 37 | + |
| 38 | +// Returns true if any of the node ids in node_ids are selected for eviction. |
| 39 | +bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, const std::vector<NodeId>& node_ids, FastRandomContext& random_context) |
| 40 | +{ |
| 41 | + Shuffle(candidates.begin(), candidates.end(), random_context); |
| 42 | + const std::optional<NodeId> evicted_node_id = SelectNodeToEvict(std::move(candidates)); |
| 43 | + if (!evicted_node_id) { |
| 44 | + return false; |
| 45 | + } |
| 46 | + return std::find(node_ids.begin(), node_ids.end(), *evicted_node_id) != node_ids.end(); |
| 47 | +} |
| 48 | + |
| 49 | +// Create number_of_nodes random nodes, apply setup function candidate_setup_fn, |
| 50 | +// apply eviction logic and then return true if any of the node ids in node_ids |
| 51 | +// are selected for eviction. |
| 52 | +bool IsEvicted(const int number_of_nodes, std::function<void(NodeEvictionCandidate&)> candidate_setup_fn, const std::vector<NodeId>& node_ids, FastRandomContext& random_context) |
| 53 | +{ |
| 54 | + std::vector<NodeEvictionCandidate> candidates = GetRandomNodeEvictionCandidates(number_of_nodes, random_context); |
| 55 | + for (NodeEvictionCandidate& candidate : candidates) { |
| 56 | + candidate_setup_fn(candidate); |
| 57 | + } |
| 58 | + return IsEvicted(candidates, node_ids, random_context); |
| 59 | +} |
| 60 | + |
| 61 | +namespace { |
| 62 | +constexpr int NODE_EVICTION_TEST_ROUNDS{10}; |
| 63 | +constexpr int NODE_EVICTION_TEST_UP_TO_N_NODES{200}; |
| 64 | +} // namespace |
| 65 | + |
| 66 | +BOOST_AUTO_TEST_CASE(peer_eviction_test) |
| 67 | +{ |
| 68 | + FastRandomContext random_context{true}; |
| 69 | + |
| 70 | + for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) { |
| 71 | + for (int number_of_nodes = 0; number_of_nodes < NODE_EVICTION_TEST_UP_TO_N_NODES; ++number_of_nodes) { |
| 72 | + // Four nodes with the highest keyed netgroup values should be |
| 73 | + // protected from eviction. |
| 74 | + BOOST_CHECK(!IsEvicted( |
| 75 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 76 | + candidate.nKeyedNetGroup = number_of_nodes - candidate.id; |
| 77 | + }, |
| 78 | + {0, 1, 2, 3}, random_context)); |
| 79 | + |
| 80 | + // Eight nodes with the lowest minimum ping time should be protected |
| 81 | + // from eviction. |
| 82 | + BOOST_CHECK(!IsEvicted( |
| 83 | + number_of_nodes, [](NodeEvictionCandidate& candidate) { |
| 84 | + candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; |
| 85 | + }, |
| 86 | + {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
| 87 | + |
| 88 | + // Four nodes that most recently sent us novel transactions accepted |
| 89 | + // into our mempool should be protected from eviction. |
| 90 | + BOOST_CHECK(!IsEvicted( |
| 91 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 92 | + candidate.nLastTXTime = number_of_nodes - candidate.id; |
| 93 | + }, |
| 94 | + {0, 1, 2, 3}, random_context)); |
| 95 | + |
| 96 | + // Up to eight non-tx-relay peers that most recently sent us novel |
| 97 | + // blocks should be protected from eviction. |
| 98 | + BOOST_CHECK(!IsEvicted( |
| 99 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 100 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 101 | + if (candidate.id <= 7) { |
| 102 | + candidate.fRelayTxes = false; |
| 103 | + candidate.fRelevantServices = true; |
| 104 | + } |
| 105 | + }, |
| 106 | + {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
| 107 | + |
| 108 | + // Four peers that most recently sent us novel blocks should be |
| 109 | + // protected from eviction. |
| 110 | + BOOST_CHECK(!IsEvicted( |
| 111 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 112 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 113 | + }, |
| 114 | + {0, 1, 2, 3}, random_context)); |
| 115 | + |
| 116 | + // Combination of the previous two tests. |
| 117 | + BOOST_CHECK(!IsEvicted( |
| 118 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 119 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 120 | + if (candidate.id <= 7) { |
| 121 | + candidate.fRelayTxes = false; |
| 122 | + candidate.fRelevantServices = true; |
| 123 | + } |
| 124 | + }, |
| 125 | + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, random_context)); |
| 126 | + |
| 127 | + // Combination of all tests above. |
| 128 | + BOOST_CHECK(!IsEvicted( |
| 129 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 130 | + candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected |
| 131 | + candidate.m_min_ping_time = std::chrono::microseconds{candidate.id}; // 8 protected |
| 132 | + candidate.nLastTXTime = number_of_nodes - candidate.id; // 4 protected |
| 133 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; // 4 protected |
| 134 | + }, |
| 135 | + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context)); |
| 136 | + |
| 137 | + // An eviction is expected given >= 29 random eviction candidates. The eviction logic protects at most |
| 138 | + // four peers by net group, eight by lowest ping time, four by last time of novel tx, up to eight non-tx-relay |
| 139 | + // peers by last novel block time, and four more peers by last novel block time. |
| 140 | + if (number_of_nodes >= 29) { |
| 141 | + BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
| 142 | + } |
| 143 | + |
| 144 | + // No eviction is expected given <= 20 random eviction candidates. The eviction logic protects at least |
| 145 | + // four peers by net group, eight by lowest ping time, four by last time of novel tx and four peers by last |
| 146 | + // novel block time. |
| 147 | + if (number_of_nodes <= 20) { |
| 148 | + BOOST_CHECK(!SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
| 149 | + } |
| 150 | + |
| 151 | + // Cases left to test: |
| 152 | + // * "Protect the half of the remaining nodes which have been connected the longest. [...]" |
| 153 | + // * "Pick out up to 1/4 peers that are localhost, sorted by longest uptime. [...]" |
| 154 | + // * "If any remaining peers are preferred for eviction consider only them. [...]" |
| 155 | + // * "Identify the network group with the most connections and youngest member. [...]" |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments