Changeset View
Changeset View
Standalone View
Standalone View
src/test/net_tests.cpp
Show First 20 Lines • Show All 788 Lines • ▼ Show 20 Lines | for (int id = 0; id < n_candidates; ++id) { | ||||
static_cast<int64_t>(random_context.randrange(100)), | static_cast<int64_t>(random_context.randrange(100)), | ||||
/* fRelevantServices */ random_context.randbool(), | /* fRelevantServices */ random_context.randbool(), | ||||
/* fRelayTxes */ random_context.randbool(), | /* fRelayTxes */ random_context.randbool(), | ||||
/* fBloomFilter */ random_context.randbool(), | /* fBloomFilter */ random_context.randbool(), | ||||
/* nKeyedNetGroup */ random_context.randrange(100), | /* nKeyedNetGroup */ random_context.randrange(100), | ||||
/* prefer_evict */ random_context.randbool(), | /* prefer_evict */ random_context.randbool(), | ||||
/* m_is_local */ random_context.randbool(), | /* m_is_local */ random_context.randbool(), | ||||
/* availabilityScore */ double(random_context.randrange(-1)), | /* availabilityScore */ double(random_context.randrange(-1)), | ||||
/* peerid */ PeerId(random_context.randrange(4)), | |||||
}); | }); | ||||
} | } | ||||
return candidates; | return candidates; | ||||
} | } | ||||
// Returns true if any of the node ids in node_ids are selected for eviction. | // Returns true if any of the node ids in node_ids are selected for eviction. | ||||
bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, | bool IsEvicted(std::vector<NodeEvictionCandidate> candidates, | ||||
const std::vector<NodeId> &node_ids, | const std::vector<NodeId> &node_ids, | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) { | ||||
number_of_nodes - candidate.id; // 4 protected | number_of_nodes - candidate.id; // 4 protected | ||||
candidate.nLastBlockTime = | candidate.nLastBlockTime = | ||||
number_of_nodes - candidate.id; // 4 protected | number_of_nodes - candidate.id; // 4 protected | ||||
}, | }, | ||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, | ||||
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, | 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}, | ||||
random_context)); | random_context)); | ||||
// 128 peers with the highest availability score should be protected | { | ||||
// The best node from each avalanche peer should be protected | |||||
// from eviction. | // from eviction. | ||||
std::vector<NodeId> protectedNodes; | |||||
for (NodeId id = 0; id < NODE_EVICTION_TEST_UP_TO_N_NODES; | |||||
id += 10) { | |||||
protectedNodes.emplace_back(id); | |||||
} | |||||
BOOST_CHECK(!IsEvicted( | |||||
number_of_nodes, | |||||
[number_of_nodes](NodeEvictionCandidate &candidate) { | |||||
candidate.availabilityScore = | |||||
double(number_of_nodes - candidate.id); | |||||
candidate.peerid = PeerId(candidate.id / 10); | |||||
}, | |||||
protectedNodes, random_context)); | |||||
} | |||||
{ | |||||
// 128 peers with the highest availability score should be | |||||
// protected from eviction. | |||||
std::vector<NodeId> protectedNodes(128); | std::vector<NodeId> protectedNodes(128); | ||||
std::iota(protectedNodes.begin(), protectedNodes.end(), 0); | std::iota(protectedNodes.begin(), protectedNodes.end(), 0); | ||||
BOOST_CHECK(!IsEvicted( | BOOST_CHECK(!IsEvicted( | ||||
number_of_nodes, | number_of_nodes, | ||||
[number_of_nodes](NodeEvictionCandidate &candidate) { | [number_of_nodes](NodeEvictionCandidate &candidate) { | ||||
candidate.availabilityScore = | candidate.availabilityScore = | ||||
double(number_of_nodes - candidate.id); | double(number_of_nodes - candidate.id); | ||||
candidate.peerid = PeerId(); | |||||
}, | }, | ||||
protectedNodes, random_context)); | protectedNodes, random_context)); | ||||
} | |||||
{ | |||||
// Combination of the previous two tests. We expect 132 | |||||
// protected nodes (1 best node from each of the 4 avalanche | |||||
// peers + 128 best overall nodes from the remaining ones). | |||||
// Nodes 0, 42, 84, 126 and 168 are the best for their peer, | |||||
// nodes 1 to 132 are the best from the remaining nodes. | |||||
std::vector<NodeId> protectedNodes(131); | |||||
std::iota(protectedNodes.begin(), protectedNodes.end(), 0); | |||||
protectedNodes.push_back(168); | |||||
BOOST_CHECK(!IsEvicted( | |||||
number_of_nodes, | |||||
[number_of_nodes](NodeEvictionCandidate &candidate) { | |||||
candidate.availabilityScore = | |||||
double(number_of_nodes - candidate.id); | |||||
candidate.peerid = PeerId(candidate.id / 42); | |||||
}, | |||||
protectedNodes, random_context)); | |||||
} | |||||
// An eviction is expected given >= 161 random eviction candidates. | // An eviction is expected given >= 165 random eviction candidates. | ||||
// The eviction logic protects at most four peers by net group, | // The eviction logic protects at most four peers by net group, | ||||
// eight by lowest ping time, four by last time of novel tx, four by | // eight by lowest ping time, four by last time of novel tx, four by | ||||
// last time of novel proof, up to eight non-tx-relay peers by last | // last time of novel proof, up to eight non-tx-relay peers by last | ||||
// novel block time, four by last novel block time, and 128 more by | // novel block time, four by last novel block time, 128 more by | ||||
// avalanche availability score. | // avalanche availability score and the best node for each of 4 | ||||
if (number_of_nodes >= 161) { | // avalanche peer. | ||||
if (number_of_nodes >= 165) { | |||||
BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates( | BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates( | ||||
number_of_nodes, random_context))); | number_of_nodes, random_context))); | ||||
} | } | ||||
// No eviction is expected given <= 24 random eviction candidates. | // No eviction is expected given <= 24 random eviction candidates. | ||||
// The eviction logic protects at least four peers by net group, | // The eviction logic protects at least four peers by net group, | ||||
// eight by lowest ping time, four by last time of novel tx, four by | // eight by lowest ping time, four by last time of novel tx, four by | ||||
// last time of novel proof, four peers by last novel block time. | // last time of novel proof, four peers by last novel block time. | ||||
▲ Show 20 Lines • Show All 78 Lines • Show Last 20 Lines |