Skip to content

Commit 685c428

Browse files
test: Add unit testing of node eviction logic
1 parent ed73f8c commit 685c428

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed

src/test/net_tests.cpp

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdint>
1010
#include <net.h>
1111
#include <netbase.h>
12+
#include <optional.h>
1213
#include <serialize.h>
1314
#include <span.h>
1415
#include <streams.h>
@@ -21,6 +22,7 @@
2122

2223
#include <boost/test/unit_test.hpp>
2324

25+
#include <algorithm>
2426
#include <ios>
2527
#include <memory>
2628
#include <string>
@@ -771,4 +773,133 @@ BOOST_AUTO_TEST_CASE(PoissonNextSend)
771773
g_mock_deterministic_tests = false;
772774
}
773775

776+
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_candidates, FastRandomContext& random_context)
777+
{
778+
std::vector<NodeEvictionCandidate> candidates;
779+
for (int id = 0; id < n_candidates; ++id) {
780+
candidates.push_back({
781+
/* id */ id,
782+
/* nTimeConnected */ static_cast<int64_t>(random_context.randrange(100)),
783+
/* nMinPingUsecTime */ static_cast<int64_t>(random_context.randrange(100)),
784+
/* nLastBlockTime */ static_cast<int64_t>(random_context.randrange(100)),
785+
/* nLastTXTime */ static_cast<int64_t>(random_context.randrange(100)),
786+
/* fRelevantServices */ random_context.randbool(),
787+
/* fRelayTxes */ random_context.randbool(),
788+
/* fBloomFilter */ random_context.randbool(),
789+
/* nKeyedNetGroup */ random_context.randrange(100),
790+
/* prefer_evict */ random_context.randbool(),
791+
/* m_is_local */ random_context.randbool(),
792+
});
793+
}
794+
return candidates;
795+
}
796+
797+
// Returns true if any of the node ids in node_ids are selected for eviction.
798+
bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, const std::vector<NodeId>& node_ids, FastRandomContext& random_context)
799+
{
800+
Shuffle(candidates.begin(), candidates.end(), random_context);
801+
const Optional<NodeId> evicted_node_id = SelectNodeToEvict(std::move(candidates));
802+
if (!evicted_node_id) {
803+
return false;
804+
}
805+
return std::find(node_ids.begin(), node_ids.end(), *evicted_node_id) != node_ids.end();
806+
}
807+
808+
// Create number_of_nodes random nodes, apply setup function candidate_setup_fn,
809+
// apply eviction logic and then return true if any of the node ids in node_ids
810+
// are selected for eviction.
811+
bool IsEvicted(const int number_of_nodes, std::function<void(NodeEvictionCandidate&)> candidate_setup_fn, const std::vector<NodeId>& node_ids, FastRandomContext& random_context)
812+
{
813+
std::vector<NodeEvictionCandidate> candidates = GetRandomNodeEvictionCandidates(number_of_nodes, random_context);
814+
for (NodeEvictionCandidate& candidate : candidates) {
815+
candidate_setup_fn(candidate);
816+
}
817+
return IsEvicted(candidates, node_ids, random_context);
818+
}
819+
820+
namespace {
821+
constexpr int NODE_EVICTION_TEST_ROUNDS{10};
822+
constexpr int NODE_EVICTION_TEST_UP_TO_N_NODES{200};
823+
} // namespace
824+
825+
BOOST_AUTO_TEST_CASE(node_eviction_test)
826+
{
827+
FastRandomContext random_context{true};
828+
829+
for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) {
830+
for (int number_of_nodes = 0; number_of_nodes < NODE_EVICTION_TEST_UP_TO_N_NODES; ++number_of_nodes) {
831+
// Four nodes with the highest keyed netgroup values should be
832+
// protected from eviction.
833+
BOOST_CHECK(!IsEvicted(
834+
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
835+
candidate.nKeyedNetGroup = number_of_nodes - candidate.id;
836+
},
837+
{0, 1, 2, 3}, random_context));
838+
839+
// Eight nodes with the lowest minimum ping time should be protected
840+
// from eviction.
841+
BOOST_CHECK(!IsEvicted(
842+
number_of_nodes, [](NodeEvictionCandidate& candidate) {
843+
candidate.nMinPingUsecTime = candidate.id;
844+
},
845+
{0, 1, 2, 3, 4, 5, 6, 7}, random_context));
846+
847+
// Four nodes that most recently sent us novel transactions accepted
848+
// into our mempool should be protected from eviction.
849+
BOOST_CHECK(!IsEvicted(
850+
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
851+
candidate.nLastTXTime = number_of_nodes - candidate.id;
852+
},
853+
{0, 1, 2, 3}, random_context));
854+
855+
// Up to eight non-tx-relay peers that most recently sent us novel
856+
// blocks should be protected from eviction.
857+
BOOST_CHECK(!IsEvicted(
858+
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
859+
candidate.nLastBlockTime = number_of_nodes - candidate.id;
860+
if (candidate.id <= 7) {
861+
candidate.fRelayTxes = false;
862+
candidate.fRelevantServices = true;
863+
}
864+
},
865+
{0, 1, 2, 3, 4, 5, 6, 7}, random_context));
866+
867+
// Four peers that most recently sent us novel blocks should be
868+
// protected from eviction.
869+
BOOST_CHECK(!IsEvicted(
870+
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
871+
candidate.nLastBlockTime = number_of_nodes - candidate.id;
872+
},
873+
{0, 1, 2, 3}, random_context));
874+
875+
// Combination of the previous two tests.
876+
BOOST_CHECK(!IsEvicted(
877+
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
878+
candidate.nLastBlockTime = number_of_nodes - candidate.id;
879+
if (candidate.id <= 7) {
880+
candidate.fRelayTxes = false;
881+
candidate.fRelevantServices = true;
882+
}
883+
},
884+
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, random_context));
885+
886+
// Combination of all tests above.
887+
BOOST_CHECK(!IsEvicted(
888+
number_of_nodes, [number_of_nodes](NodeEvictionCandidate& candidate) {
889+
candidate.nKeyedNetGroup = number_of_nodes - candidate.id; // 4 protected
890+
candidate.nMinPingUsecTime = candidate.id; // 8 protected
891+
candidate.nLastTXTime = number_of_nodes - candidate.id; // 4 protected
892+
candidate.nLastBlockTime = number_of_nodes - candidate.id; // 4 protected
893+
},
894+
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19}, random_context));
895+
896+
// Cases left to test:
897+
// * "Protect the half of the remaining nodes which have been connected the longest. [...]"
898+
// * "Pick out up to 1/4 peers that are localhost, sorted by longest uptime. [...]"
899+
// * "If any remaining peers are preferred for eviction consider only them. [...]"
900+
// * "Identify the network group with the most connections and youngest member. [...]"
901+
}
902+
}
903+
}
904+
774905
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)