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_SCORE_DUST); | ||||
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_SCORE_DUST); | ||||
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_SCORE_DUST); | ||||
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_SCORE_DUST); | ||||
majcosta: if MIN_SCORE_DUST is 100, this should be 1000000 * MIN_SCORE_DUST or changes test behavior | |||||
PiRKAuthorUnsubmitted Done Inline ActionsI haven't studied how the score affects the probability a node gets selected, but I assumed that it is proportional to the value staked. So in this case, if I multiply the previous test values by the same constant, it should give the same result. PiRK: I haven't studied how the score affects the probability a node gets selected, but I assumed… | |||||
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 21 Lines | BOOST_AUTO_TEST_CASE(node_crud) { | ||||
for (int i = 0; i < 100; i++) { | 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. | ||||
PiRKAuthorUnsubmitted Done Inline ActionsIf I understand the test correctly, this comment is incorrect. It should be "1 in 100 million" PiRK: If I understand the test correctly, this comment is incorrect. It should be "1 in 100 million"… | |||||
Proof altproof = buildRandomProof(1); | Proof altproof = buildRandomProof(MIN_SCORE_DUST); | ||||
majcostaUnsubmitted Not Done Inline Actionsshould be MIN_SCORE_DUST/100, no? majcosta: should be MIN_SCORE_DUST/100, no? | |||||
PiRKAuthorUnsubmitted Done Inline ActionsIt has to be a valid proof as low as possible. MIN_SCORE is meant to be the corresponding score to that lowest possible stake. PiRK: It has to be a valid proof as low as possible. MIN_SCORE is meant to be the corresponding score… | |||||
PiRKAuthorUnsubmitted Done Inline ActionsThe buildRandomProof function takes a score, not an amount. PiRK: The buildRandomProof function takes a score, not an amount. | |||||
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 |
if MIN_SCORE_DUST is 100, this should be 1000000 * MIN_SCORE_DUST or changes test behavior