Changeset View
Changeset View
Standalone View
Standalone View
src/test/net_tests.cpp
Show First 20 Lines • Show All 789 Lines • ▼ Show 20 Lines | for (int id = 0; id < n_candidates; ++id) { | ||||
/* 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)), | /* peerid */ PeerId(random_context.randrange(100)), | ||||
/* peerScore */ | |||||
static_cast<uint32_t>(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 132 Lines • ▼ Show 20 Lines | for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) { | ||||
} | } | ||||
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(candidate.id / 10); | candidate.peerid = PeerId(candidate.id / 10); | ||||
candidate.peerScore = 1; | |||||
}, | }, | ||||
protectedNodes, random_context)); | 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) { | ||||
Show All 12 Lines | for (int i = 0; i < NODE_EVICTION_TEST_ROUNDS; ++i) { | ||||
// 1 to 16 are the best from the remaining nodes. | // 1 to 16 are the best from the remaining nodes. | ||||
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(candidate.id / 42); | candidate.peerid = PeerId(candidate.id / 42); | ||||
candidate.peerScore = 1; | |||||
}, | }, | ||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||||
11, 12, 13, 14, 15, 16, 42, 84, 126, 168}, | 11, 12, 13, 14, 15, 16, 42, 84, 126, 168}, | ||||
random_context)); | random_context)); | ||||
// A peer with low score should not be protected from eviction. | |||||
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); | |||||
candidate.peerScore = candidate.peerid == 3 ? 1 : 500; | |||||
}, | |||||
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | |||||
10, 11, 12, 13, 14, 15, 16, 42, 84, 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 |