Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 156 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(peer_probabilities) { | BOOST_AUTO_TEST_CASE(peer_probabilities) { | ||||
// No peers. | // No peers. | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | ||||
const NodeId node0 = 42, node1 = 69, node2 = 37; | const NodeId node0 = 42, node1 = 69, node2 = 37; | ||||
// One peer, we always return it. | // One peer, we always return it. | ||||
Proof proof0 = buildRandomProof(100); | Proof proof0 = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
Delegation dg0 = DelegationBuilder(proof0).build(); | Delegation dg0 = DelegationBuilder(proof0).build(); | ||||
pm.addNode(node0, proof0, dg0); | pm.addNode(node0, proof0, dg0); | ||||
BOOST_CHECK_EQUAL(pm.selectNode(), node0); | BOOST_CHECK_EQUAL(pm.selectNode(), node0); | ||||
// Two peers, verify ratio. | // Two peers, verify ratio. | ||||
Proof proof1 = buildRandomProof(200); | Proof proof1 = buildRandomProof(2 * MIN_VALID_PROOF_SCORE); | ||||
Delegation dg1 = DelegationBuilder(proof1).build(); | Delegation dg1 = DelegationBuilder(proof1).build(); | ||||
pm.addNode(node1, proof1, dg1); | pm.addNode(node1, proof1, dg1); | ||||
std::unordered_map<PeerId, int> results = {}; | std::unordered_map<PeerId, int> results = {}; | ||||
for (int i = 0; i < 10000; i++) { | for (int i = 0; i < 10000; i++) { | ||||
size_t n = pm.selectNode(); | size_t n = pm.selectNode(); | ||||
BOOST_CHECK(n == node0 || n == node1); | BOOST_CHECK(n == node0 || n == node1); | ||||
results[n]++; | results[n]++; | ||||
} | } | ||||
BOOST_CHECK(abs(2 * results[0] - results[1]) < 500); | BOOST_CHECK(abs(2 * results[0] - results[1]) < 500); | ||||
// Three peers, verify ratio. | // Three peers, verify ratio. | ||||
Proof proof2 = buildRandomProof(100); | Proof proof2 = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
Delegation dg2 = DelegationBuilder(proof2).build(); | Delegation dg2 = DelegationBuilder(proof2).build(); | ||||
pm.addNode(node2, proof2, dg2); | pm.addNode(node2, proof2, dg2); | ||||
results.clear(); | results.clear(); | ||||
for (int i = 0; i < 10000; i++) { | for (int i = 0; i < 10000; i++) { | ||||
size_t n = pm.selectNode(); | size_t n = pm.selectNode(); | ||||
BOOST_CHECK(n == node0 || n == node1 || n == node2); | BOOST_CHECK(n == node0 || n == node1 || n == node2); | ||||
results[n]++; | results[n]++; | ||||
▲ Show 20 Lines • Show All 109 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(compact_slots) { | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 0); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 0); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(node_crud) { | BOOST_AUTO_TEST_CASE(node_crud) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
// Create one peer. | // Create one peer. | ||||
Proof proof = buildRandomProof(100000000); | Proof proof = buildRandomProof(100000000 * MIN_VALID_PROOF_SCORE); | ||||
Delegation dg = DelegationBuilder(proof).build(); | Delegation dg = DelegationBuilder(proof).build(); | ||||
BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | ||||
// Add 4 nodes. | // Add 4 nodes. | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
BOOST_CHECK(pm.addNode(i, proof, dg)); | BOOST_CHECK(pm.addNode(i, proof, dg)); | ||||
} | } | ||||
Show All 22 Lines | for (int i = 0; i < 100; i++) { | ||||
NodeId n = pm.selectNode(); | NodeId n = pm.selectNode(); | ||||
BOOST_CHECK(n == 0 || n == 3); | BOOST_CHECK(n == 0 || n == 3); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pm.updateNextRequestTime(n, std::chrono::steady_clock::now())); | pm.updateNextRequestTime(n, std::chrono::steady_clock::now())); | ||||
} | } | ||||
// Move a node from a peer to another. This peer has a very low score such | // Move a node from a peer to another. This peer has a very low score such | ||||
// as chances of being picked are 1 in a billion. | // as chances of being picked are 1 in a billion. | ||||
Proof altproof = buildRandomProof(1); | Proof altproof = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
Delegation altdg = DelegationBuilder(altproof).build(); | Delegation altdg = DelegationBuilder(altproof).build(); | ||||
BOOST_CHECK(pm.addNode(3, altproof, altdg)); | BOOST_CHECK(pm.addNode(3, altproof, altdg)); | ||||
int node3selected = 0; | int node3selected = 0; | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
NodeId n = pm.selectNode(); | NodeId n = pm.selectNode(); | ||||
if (n == 3) { | if (n == 3) { | ||||
// Selecting this node should be exceedingly unlikely. | // Selecting this node should be exceedingly unlikely. | ||||
▲ Show 20 Lines • Show All 83 Lines • Show Last 20 Lines |