|
9 | 9 | #include <cstdint>
|
10 | 10 | #include <net.h>
|
11 | 11 | #include <netbase.h>
|
| 12 | +#include <optional.h> |
12 | 13 | #include <serialize.h>
|
13 | 14 | #include <span.h>
|
14 | 15 | #include <streams.h>
|
|
21 | 22 |
|
22 | 23 | #include <boost/test/unit_test.hpp>
|
23 | 24 |
|
| 25 | +#include <algorithm> |
24 | 26 | #include <ios>
|
25 | 27 | #include <memory>
|
26 | 28 | #include <string>
|
@@ -781,4 +783,147 @@ BOOST_AUTO_TEST_CASE(PoissonNextSend)
|
781 | 783 | g_mock_deterministic_tests = false;
|
782 | 784 | }
|
783 | 785 |
|
| 786 | +std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_candidates, FastRandomContext& random_context) |
| 787 | +{ |
| 788 | + std::vector<NodeEvictionCandidate> candidates; |
| 789 | + for (int id = 0; id < n_candidates; ++id) { |
| 790 | + candidates.push_back({ |
| 791 | + /* id */ id, |
| 792 | + /* nTimeConnected */ static_cast<int64_t>(random_context.randrange(100)), |
| 793 | + /* nMinPingUsecTime */ static_cast<int64_t>(random_context.randrange(100)), |
| 794 | + /* nLastBlockTime */ static_cast<int64_t>(random_context.randrange(100)), |
| 795 | + /* nLastTXTime */ static_cast<int64_t>(random_context.randrange(100)), |
| 796 | + /* fRelevantServices */ random_context.randbool(), |
| 797 | + /* fRelayTxes */ random_context.randbool(), |
| 798 | + /* fBloomFilter */ random_context.randbool(), |
| 799 | + /* nKeyedNetGroup */ random_context.randrange(100), |
| 800 | + /* prefer_evict */ random_context.randbool(), |
| 801 | + /* m_is_local */ random_context.randbool(), |
| 802 | + }); |
| 803 | + } |
| 804 | + return candidates; |
| 805 | +} |
| 806 | + |
| 807 | +// Returns true if any of the node ids in node_ids are selected for eviction. |
| 808 | +bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, const std::vector<NodeId>& node_ids, FastRandomContext& random_context) |
| 809 | +{ |
| 810 | + Shuffle(candidates.begin(), candidates.end(), random_context); |
| 811 | + const Optional<NodeId> evicted_node_id = SelectNodeToEvict(std::move(candidates)); |
| 812 | + if (!evicted_node_id) { |
| 813 | + return false; |
| 814 | + } |
| 815 | + return std::find(node_ids.begin(), node_ids.end(), *evicted_node_id) != node_ids.end(); |
| 816 | +} |
| 817 | + |
| 818 | +// Create number_of_nodes random nodes, apply setup function candidate_setup_fn, |
| 819 | +// apply eviction logic and then return true if any of the node ids in node_ids |
| 820 | +// are selected for eviction. |
| 821 | +bool IsEvicted(const int number_of_nodes, std::function<void(NodeEvictionCandidate&)> candidate_setup_fn, const std::vector<NodeId>& node_ids, FastRandomContext& random_context) |
| 822 | +{ |
| 823 | + std::vector<NodeEvictionCandidate> candidates = GetRandomNodeEvictionCandidates(number_of_nodes, random_context); |
| 824 | + for (NodeEvictionCandidate& candidate : candidates) { |
| 825 | + candidate_setup_fn(candidate); |
| 826 | + } |
| 827 | + return IsEvicted(candidates, node_ids, random_context); |
| 828 | +} |
| 829 | + |
| 830 | +namespace { |
| 831 | +constexpr int NODE_EVICTION_TEST_ROUNDS{10}; |
| 832 | +constexpr int NODE_EVICTION_TEST_UP_TO_N_NODES{200}; |
| 833 | +} // namespace |
| 834 | + |
| 835 | +BOOST_AUTO_TEST_CASE(node_eviction_test) |
| 836 | +{ |
| 837 | + FastRandomContext random_context{true}; |
| 838 | + |
| 839 | + for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) { |
| 840 | + for (int number_of_nodes = 0; number_of_nodes < NODE_EVICTION_TEST_UP_TO_N_NODES; ++number_of_nodes) { |
| 841 | + // Four nodes with the highest keyed netgroup values should be |
| 842 | + // protected from eviction. |
| 843 | + BOOST_CHECK(!IsEvicted( |
| 844 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 845 | + candidate.nKeyedNetGroup = number_of_nodes - candidate.id; |
| 846 | + }, |
| 847 | + {0, 1, 2, 3}, random_context)); |
| 848 | + |
| 849 | + // Eight nodes with the lowest minimum ping time should be protected |
| 850 | + // from eviction. |
| 851 | + BOOST_CHECK(!IsEvicted( |
| 852 | + number_of_nodes, [](NodeEvictionCandidate& candidate) { |
| 853 | + candidate.nMinPingUsecTime = candidate.id; |
| 854 | + }, |
| 855 | + {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
| 856 | + |
| 857 | + // Four nodes that most recently sent us novel transactions accepted |
| 858 | + // into our mempool should be protected from eviction. |
| 859 | + BOOST_CHECK(!IsEvicted( |
| 860 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 861 | + candidate.nLastTXTime = number_of_nodes - candidate.id; |
| 862 | + }, |
| 863 | + {0, 1, 2, 3}, random_context)); |
| 864 | + |
| 865 | + // Up to eight non-tx-relay peers that most recently sent us novel |
| 866 | + // blocks should be protected from eviction. |
| 867 | + BOOST_CHECK(!IsEvicted( |
| 868 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 869 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 870 | + if (candidate.id <= 7) { |
| 871 | + candidate.fRelayTxes = false; |
| 872 | + candidate.fRelevantServices = true; |
| 873 | + } |
| 874 | + }, |
| 875 | + {0, 1, 2, 3, 4, 5, 6, 7}, random_context)); |
| 876 | + |
| 877 | + // Four peers that most recently sent us novel blocks should be |
| 878 | + // protected from eviction. |
| 879 | + BOOST_CHECK(!IsEvicted( |
| 880 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 881 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 882 | + }, |
| 883 | + {0, 1, 2, 3}, random_context)); |
| 884 | + |
| 885 | + // Combination of the previous two tests. |
| 886 | + BOOST_CHECK(!IsEvicted( |
| 887 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 888 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; |
| 889 | + if (candidate.id <= 7) { |
| 890 | + candidate.fRelayTxes = false; |
| 891 | + candidate.fRelevantServices = true; |
| 892 | + } |
| 893 | + }, |
| 894 | + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, random_context)); |
| 895 | + |
| 896 | + // Combination of all tests above. |
| 897 | + BOOST_CHECK(!IsEvicted( |
| 898 | + number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) { |
| 899 | + candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected |
| 900 | + candidate.nMinPingUsecTime = candidate.id; // 8 protected |
| 901 | + candidate.nLastTXTime = number_of_nodes - candidate.id; // 4 protected |
| 902 | + candidate.nLastBlockTime = number_of_nodes - candidate.id; // 4 protected |
| 903 | + }, |
| 904 | + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context)); |
| 905 | + |
| 906 | + // An eviction is expected given >= 29 random eviction candidates. The eviction logic protects at most |
| 907 | + // four peers by net group, eight by lowest ping time, four by last time of novel tx, up to eight non-tx-relay |
| 908 | + // peers by last novel block time, and four more peers by last novel block time. |
| 909 | + if (number_of_nodes >= 29) { |
| 910 | + BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
| 911 | + } |
| 912 | + |
| 913 | + // No eviction is expected given <= 20 random eviction candidates. The eviction logic protects at least |
| 914 | + // four peers by net group, eight by lowest ping time, four by last time of novel tx and four peers by last |
| 915 | + // novel block time. |
| 916 | + if (number_of_nodes <= 20) { |
| 917 | + BOOST_CHECK(!SelectNodeToEvict(GetRandomNodeEvictionCandidates(number_of_nodes, random_context))); |
| 918 | + } |
| 919 | + |
| 920 | + // Cases left to test: |
| 921 | + // * "Protect the half of the remaining nodes which have been connected the longest. [...]" |
| 922 | + // * "Pick out up to 1/4 peers that are localhost, sorted by longest uptime. [...]" |
| 923 | + // * "If any remaining peers are preferred for eviction consider only them. [...]" |
| 924 | + // * "Identify the network group with the most connections and youngest member. [...]" |
| 925 | + } |
| 926 | + } |
| 927 | +} |
| 928 | + |
784 | 929 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments