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) { | ||||
/* 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(), | ||||
/* fAvalanche */ random_context.randbool(), | /* fAvalanche */ random_context.randbool(), | ||||
/* availabilityScore */ double(random_context.randrange(-1)), | /* availabilityScore */ double(random_context.randrange(-1)), | ||||
/* peerid */ PeerId(random_context.randrange(100)), | |||||
}); | }); | ||||
} | } | ||||
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)); | ||||
// The best node from each avalanche peer should be protected 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.fAvalanche = true; | |||||
candidate.availabilityScore = | |||||
double(number_of_nodes - candidate.id); | |||||
candidate.peerid = PeerId(candidate.id / 10); | |||||
}, | |||||
protectedNodes, random_context)); | |||||
// 16 peers with the highest availability score should be protected | // 16 peers with the highest availability score should be protected | ||||
// from eviction. | // from eviction. | ||||
BOOST_CHECK(!IsEvicted( | BOOST_CHECK(!IsEvicted( | ||||
number_of_nodes, | number_of_nodes, | ||||
[number_of_nodes](NodeEvictionCandidate &candidate) { | [number_of_nodes](NodeEvictionCandidate &candidate) { | ||||
candidate.fAvalanche = true; | candidate.fAvalanche = true; | ||||
candidate.availabilityScore = | candidate.availabilityScore = | ||||
double(number_of_nodes - candidate.id); | double(number_of_nodes - candidate.id); | ||||
candidate.peerid = PeerId(); | |||||
}, | }, | ||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, | ||||
random_context)); | random_context)); | ||||
// Combination of the previous two tests. We expect 20 protected | |||||
// nodes (1 best node from each of the 4 avalanche peers + 16 best | |||||
// overall nodes from the remaining ones). | |||||
// Nodes 0, 42, 84, 126 and 168 are the best for their peer, nodes | |||||
// 1 to 16 are the best from the remaining nodes. | |||||
BOOST_CHECK(!IsEvicted( | |||||
number_of_nodes, | |||||
[number_of_nodes](NodeEvictionCandidate &candidate) { | |||||
candidate.fAvalanche = true; | |||||
candidate.availabilityScore = | |||||
double(number_of_nodes - candidate.id); | |||||
candidate.peerid = PeerId(candidate.id / 42); | |||||
}, | |||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | |||||
11, 12, 13, 14, 15, 16, 42, 84, 126, 168}, | |||||
random_context)); | |||||
// An eviction is expected given >= 49 random eviction candidates. | // An eviction is expected given >= 49 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 16 more by | // novel block time, four by last novel block time, and 16 more by | ||||
// avalanche availability score. | // avalanche availability score. | ||||
if (number_of_nodes >= 49) { | if (number_of_nodes >= 49) { | ||||
BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates( | BOOST_CHECK(SelectNodeToEvict(GetRandomNodeEvictionCandidates( | ||||
▲ Show 20 Lines • Show All 85 Lines • Show Last 20 Lines |