Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 148 Lines • ▼ Show 20 Lines | for (int c = 0; c < 1000; c++) { | ||||
auto i = selectPeerImpl(slots, s, max); | auto i = selectPeerImpl(slots, s, max); | ||||
// /!\ Because of the way we construct the vector, the peer id is | // /!\ Because of the way we construct the vector, the peer id is | ||||
// always the index. This might not be the case in practice. | // always the index. This might not be the case in practice. | ||||
BOOST_CHECK(i == NO_PEER || slots[i].contains(s)); | BOOST_CHECK(i == NO_PEER || slots[i].contains(s)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static std::shared_ptr<Proof> getRandomProofPtr(uint32_t score) { | |||||
return std::make_shared<Proof>(buildRandomProof(score)); | |||||
} | |||||
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(MIN_VALID_PROOF_SCORE); | std::shared_ptr<Proof> proof0 = getRandomProofPtr(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(2 * MIN_VALID_PROOF_SCORE); | std::shared_ptr<Proof> proof1 = | ||||
Delegation dg1 = DelegationBuilder(proof1).build(); | getRandomProofPtr(2 * MIN_VALID_PROOF_SCORE); | ||||
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(MIN_VALID_PROOF_SCORE); | std::shared_ptr<Proof> proof2 = getRandomProofPtr(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]++; | ||||
} | } | ||||
BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500); | BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(remove_peer) { | BOOST_AUTO_TEST_CASE(remove_peer) { | ||||
// No peers. | // No peers. | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | ||||
// Add 4 peers. | // Add 4 peers. | ||||
std::array<PeerId, 8> peerids; | std::array<PeerId, 8> peerids; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
Proof p = buildRandomProof(100); | std::shared_ptr<Proof> p = getRandomProofPtr(100); | ||||
peerids[i] = pm.getPeerId(p); | peerids[i] = pm.getPeerId(p); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pm.addNode(InsecureRand32(), p, DelegationBuilder(p).build())); | pm.addNode(InsecureRand32(), p, DelegationBuilder(*p).build())); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
PeerId p = pm.selectPeer(); | PeerId p = pm.selectPeer(); | ||||
BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | ||||
Show All 13 Lines | BOOST_AUTO_TEST_CASE(remove_peer) { | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
PeerId p = pm.selectPeer(); | PeerId p = pm.selectPeer(); | ||||
BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]); | BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]); | ||||
} | } | ||||
// Add 4 more peers. | // Add 4 more peers. | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
Proof p = buildRandomProof(100); | std::shared_ptr<Proof> p = getRandomProofPtr(100); | ||||
peerids[i + 4] = pm.getPeerId(p); | peerids[i + 4] = pm.getPeerId(p); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pm.addNode(InsecureRand32(), p, DelegationBuilder(p).build())); | pm.addNode(InsecureRand32(), p, DelegationBuilder(*p).build())); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
BOOST_CHECK(pm.removePeer(peerids[0])); | BOOST_CHECK(pm.removePeer(peerids[0])); | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 100); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 100); | ||||
Show All 23 Lines | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(compact_slots) { | BOOST_AUTO_TEST_CASE(compact_slots) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
// Add 4 peers. | // Add 4 peers. | ||||
std::array<PeerId, 4> peerids; | std::array<PeerId, 4> peerids; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
Proof p = buildRandomProof(100); | std::shared_ptr<Proof> p = getRandomProofPtr(100); | ||||
peerids[i] = pm.getPeerId(p); | peerids[i] = pm.getPeerId(p); | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pm.addNode(InsecureRand32(), p, DelegationBuilder(p).build())); | pm.addNode(InsecureRand32(), p, DelegationBuilder(*p).build())); | ||||
} | } | ||||
// Remove all peers. | // Remove all peers. | ||||
for (auto p : peerids) { | for (auto p : peerids) { | ||||
pm.removePeer(p); | pm.removePeer(p); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 300); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 300); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 300); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 300); | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.compact(), 300); | BOOST_CHECK_EQUAL(pm.compact(), 300); | ||||
BOOST_CHECK(pm.verify()); | BOOST_CHECK(pm.verify()); | ||||
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(10000000 * MIN_VALID_PROOF_SCORE); | std::shared_ptr<Proof> proof = | ||||
Delegation dg = DelegationBuilder(proof).build(); | getRandomProofPtr(10000000 * MIN_VALID_PROOF_SCORE); | ||||
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)); | ||||
} | } | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
Show All 21 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 10 million. | // as chances of being picked are 1 in 10 million. | ||||
Proof altproof = buildRandomProof(MIN_VALID_PROOF_SCORE); | std::shared_ptr<Proof> altproof = getRandomProofPtr(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. | ||||
BOOST_CHECK(node3selected++ < 1); | BOOST_CHECK(node3selected++ < 1); | ||||
Show All 31 Lines | BOOST_AUTO_TEST_CASE(proof_conflict) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
const auto getPeerId = [&](const std::vector<COutPoint> &outpoints) { | const auto getPeerId = [&](const std::vector<COutPoint> &outpoints) { | ||||
ProofBuilder pb(0, 0, CPubKey()); | ProofBuilder pb(0, 0, CPubKey()); | ||||
for (const auto &o : outpoints) { | for (const auto &o : outpoints) { | ||||
pb.addUTXO(o, v, height, false, key); | pb.addUTXO(o, v, height, false, key); | ||||
} | } | ||||
return pm.getPeerId(pb.build()); | return pm.getPeerId(std::make_shared<Proof>(pb.build())); | ||||
}; | }; | ||||
// Add one peer. | // Add one peer. | ||||
const PeerId peer1 = getPeerId({COutPoint(txid1, 0)}); | const PeerId peer1 = getPeerId({COutPoint(txid1, 0)}); | ||||
BOOST_CHECK(peer1 != NO_PEER); | BOOST_CHECK(peer1 != NO_PEER); | ||||
// Same proof, same peer. | // Same proof, same peer. | ||||
BOOST_CHECK_EQUAL(getPeerId({COutPoint(txid1, 0)}), peer1); | BOOST_CHECK_EQUAL(getPeerId({COutPoint(txid1, 0)}), peer1); | ||||
Show All 34 Lines |