Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
// Copyright (c) 2020 The Bitcoin developers | // Copyright (c) 2020 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <avalanche/delegationbuilder.h> | |||||
#include <avalanche/peermanager.h> | #include <avalanche/peermanager.h> | ||||
#include <avalanche/proofbuilder.h> | #include <avalanche/proofbuilder.h> | ||||
#include <avalanche/test/util.h> | |||||
#include <script/standard.h> | #include <script/standard.h> | ||||
#include <validation.h> | #include <validation.h> | ||||
#include <avalanche/test/util.h> | |||||
#include <test/util/setup_common.h> | #include <test/util/setup_common.h> | ||||
#include <boost/test/unit_test.hpp> | #include <boost/test/unit_test.hpp> | ||||
using namespace avalanche; | using namespace avalanche; | ||||
BOOST_FIXTURE_TEST_SUITE(peermanager_tests, TestingSetup) | BOOST_FIXTURE_TEST_SUITE(peermanager_tests, TestingSetup) | ||||
▲ Show 20 Lines • Show All 139 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(peer_probabilities) { | ||||
// No peers. | // No peers. | ||||
PeerManager pm; | 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(100); | ||||
pm.addNode(node0, buildRandomProof(100), CPubKey()); | Delegation dg0 = DelegationBuilder(proof0).build(); | ||||
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. | ||||
pm.addNode(node1, buildRandomProof(200), CPubKey()); | Proof proof1 = buildRandomProof(200); | ||||
Delegation dg1 = DelegationBuilder(proof1).build(); | |||||
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. | ||||
pm.addNode(node2, buildRandomProof(100), CPubKey()); | Proof proof2 = buildRandomProof(100); | ||||
Delegation dg2 = DelegationBuilder(proof2).build(); | |||||
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 99 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(compact_slots) { | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(node_crud) { | BOOST_AUTO_TEST_CASE(node_crud) { | ||||
PeerManager pm; | PeerManager pm; | ||||
// Create one peer. | // Create one peer. | ||||
Proof proof = buildRandomProof(100000000); | Proof proof = buildRandomProof(100000000); | ||||
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, CPubKey())); | BOOST_CHECK(pm.addNode(i, proof, dg)); | ||||
} | } | ||||
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 < 4); | BOOST_CHECK(n >= 0 && n < 4); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pm.updateNextRequestTime(n, std::chrono::steady_clock::now())); | pm.updateNextRequestTime(n, std::chrono::steady_clock::now())); | ||||
} | } | ||||
Show All 17 Lines | for (int i = 0; i < 100; i++) { | ||||
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(1); | ||||
BOOST_CHECK(pm.addNode(3, altproof, CPubKey())); | Delegation altdg = DelegationBuilder(altproof).build(); | ||||
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. | ||||
BOOST_CHECK(node3selected++ < 1); | BOOST_CHECK(node3selected++ < 1); | ||||
} else { | } else { | ||||
▲ Show 20 Lines • Show All 81 Lines • Show Last 20 Lines |