Changeset View
Changeset View
Standalone View
Standalone View
src/test/net_tests.cpp
Show All 19 Lines | |||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <cmath> | #include <cmath> | ||||
#include <ios> | #include <ios> | ||||
#include <memory> | #include <memory> | ||||
#include <numeric> | |||||
#include <string> | #include <string> | ||||
class CAddrManSerializationMock : public CAddrMan { | class CAddrManSerializationMock : public CAddrMan { | ||||
public: | public: | ||||
virtual void Serialize(CDataStream &s) const = 0; | virtual void Serialize(CDataStream &s) const = 0; | ||||
//! Ensure that bucket placement is always the same for testing purposes. | //! Ensure that bucket placement is always the same for testing purposes. | ||||
void MakeDeterministic() { | void MakeDeterministic() { | ||||
▲ Show 20 Lines • Show All 751 Lines • ▼ Show 20 Lines | for (int id = 0; id < n_candidates; ++id) { | ||||
/* nLastTXTime */ | /* nLastTXTime */ | ||||
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)), | |||||
}); | }); | ||||
} | } | ||||
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)); | ||||
// An eviction is expected given >= 33 random eviction candidates. | // 128 peers with the highest availability score should be protected | ||||
// from eviction. | |||||
std::vector<NodeId> protectedNodes(128); | |||||
std::iota(protectedNodes.begin(), protectedNodes.end(), 0); | |||||
BOOST_CHECK(!IsEvicted( | |||||
number_of_nodes, | |||||
[number_of_nodes](NodeEvictionCandidate &candidate) { | |||||
candidate.availabilityScore = | |||||
double(number_of_nodes - candidate.id); | |||||
}, | |||||
protectedNodes, random_context)); | |||||
// An eviction is expected given >= 161 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, and four more peers by last novel block time. | // novel block time, four by last novel block time, and 128 more by | ||||
if (number_of_nodes >= 33) { | // avalanche availability score. | ||||
if (number_of_nodes >= 161) { | |||||
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 |