Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 167 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)); | |||||
} | |||||
static void addNodeWithScore(avalanche::PeerManager &pm, NodeId node, | static void addNodeWithScore(avalanche::PeerManager &pm, NodeId node, | ||||
uint32_t score) { | uint32_t score) { | ||||
auto proof = getRandomProofPtr(score); | auto proof = buildRandomProof(score); | ||||
BOOST_CHECK_NE(pm.getPeerId(proof), NO_PEER); | BOOST_CHECK_NE(pm.getPeerId(proof), NO_PEER); | ||||
BOOST_CHECK(pm.addNode(node, proof->getId())); | BOOST_CHECK(pm.addNode(node, proof->getId())); | ||||
}; | }; | ||||
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); | ||||
Show All 32 Lines | |||||
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++) { | ||||
auto p = getRandomProofPtr(100); | auto p = buildRandomProof(100); | ||||
peerids[i] = pm.getPeerId(p); | peerids[i] = pm.getPeerId(p); | ||||
BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); | BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); | ||||
} | } | ||||
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++) { | ||||
Show All 15 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++) { | ||||
auto p = getRandomProofPtr(100); | auto p = buildRandomProof(100); | ||||
peerids[i + 4] = pm.getPeerId(p); | peerids[i + 4] = pm.getPeerId(p); | ||||
BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); | BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); | ||||
} | } | ||||
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])); | ||||
Show All 25 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++) { | ||||
auto p = getRandomProofPtr(100); | auto p = buildRandomProof(100); | ||||
peerids[i] = pm.getPeerId(p); | peerids[i] = pm.getPeerId(p); | ||||
BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); | BOOST_CHECK(pm.addNode(InsecureRand32(), p->getId())); | ||||
} | } | ||||
// Remove all peers. | // Remove all peers. | ||||
for (auto p : peerids) { | for (auto p : peerids) { | ||||
pm.removePeer(p); | pm.removePeer(p); | ||||
} | } | ||||
Show All 10 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. | ||||
auto proof = getRandomProofPtr(10000000 * MIN_VALID_PROOF_SCORE); | auto proof = buildRandomProof(10000000 * MIN_VALID_PROOF_SCORE); | ||||
BOOST_CHECK_NE(pm.getPeerId(proof), NO_PEER); | BOOST_CHECK_NE(pm.getPeerId(proof), NO_PEER); | ||||
BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | ||||
// Add 4 nodes. | // Add 4 nodes. | ||||
const ProofId &proofid = proof->getId(); | const ProofId &proofid = proof->getId(); | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
BOOST_CHECK(pm.addNode(i, proofid)); | BOOST_CHECK(pm.addNode(i, proofid)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | for (int i = 0; i < 100; i++) { | ||||
BOOST_CHECK( | BOOST_CHECK( | ||||
pm.updateNextRequestTime(n, std::chrono::steady_clock::now())); | pm.updateNextRequestTime(n, std::chrono::steady_clock::now())); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(node_binding) { | BOOST_AUTO_TEST_CASE(node_binding) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
auto proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); | auto proof = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
const ProofId &proofid = proof->getId(); | const ProofId &proofid = proof->getId(); | ||||
// Add a bunch of nodes with no associated peer | // Add a bunch of nodes with no associated peer | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
BOOST_CHECK(!pm.addNode(i, proofid)); | BOOST_CHECK(!pm.addNode(i, proofid)); | ||||
BOOST_CHECK(TestPeerManager::isNodePending(pm, i)); | BOOST_CHECK(TestPeerManager::isNodePending(pm, i)); | ||||
} | } | ||||
Show All 15 Lines | BOOST_AUTO_TEST_CASE(node_binding) { | ||||
// Add nodes when the peer already exists | // Add nodes when the peer already exists | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
BOOST_CHECK(pm.addNode(i, proofid)); | BOOST_CHECK(pm.addNode(i, proofid)); | ||||
BOOST_CHECK(!TestPeerManager::isNodePending(pm, i)); | BOOST_CHECK(!TestPeerManager::isNodePending(pm, i)); | ||||
BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid)); | BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid)); | ||||
} | } | ||||
auto alt_proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); | auto alt_proof = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
const ProofId &alt_proofid = alt_proof->getId(); | const ProofId &alt_proofid = alt_proof->getId(); | ||||
// Update some nodes from a known proof to an unknown proof | // Update some nodes from a known proof to an unknown proof | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
BOOST_CHECK(!pm.addNode(i, alt_proofid)); | BOOST_CHECK(!pm.addNode(i, alt_proofid)); | ||||
BOOST_CHECK(TestPeerManager::isNodePending(pm, i)); | BOOST_CHECK(TestPeerManager::isNodePending(pm, i)); | ||||
BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid)); | BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid)); | ||||
} | } | ||||
auto alt2_proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); | auto alt2_proof = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
const ProofId &alt2_proofid = alt2_proof->getId(); | const ProofId &alt2_proofid = alt2_proof->getId(); | ||||
// Update some nodes from an unknown proof to another unknown proof | // Update some nodes from an unknown proof to another unknown proof | ||||
for (int i = 0; i < 5; i++) { | for (int i = 0; i < 5; i++) { | ||||
BOOST_CHECK(!pm.addNode(i, alt2_proofid)); | BOOST_CHECK(!pm.addNode(i, alt2_proofid)); | ||||
BOOST_CHECK(TestPeerManager::isNodePending(pm, i)); | BOOST_CHECK(TestPeerManager::isNodePending(pm, i)); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 271 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(orphan_proofs) { | ||||
BOOST_CHECK(!isGoodPeer(proof1)); | BOOST_CHECK(!isGoodPeer(proof1)); | ||||
BOOST_CHECK(!pm.isOrphan(proof2->getId())); | BOOST_CHECK(!pm.isOrphan(proof2->getId())); | ||||
BOOST_CHECK(isGoodPeer(proof2)); | BOOST_CHECK(isGoodPeer(proof2)); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(dangling_node) { | BOOST_AUTO_TEST_CASE(dangling_node) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
auto proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); | auto proof = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
PeerId peerid = pm.getPeerId(proof); | PeerId peerid = pm.getPeerId(proof); | ||||
BOOST_CHECK_NE(peerid, NO_PEER); | BOOST_CHECK_NE(peerid, NO_PEER); | ||||
const TimePoint theFuture(std::chrono::steady_clock::now() + | const TimePoint theFuture(std::chrono::steady_clock::now() + | ||||
std::chrono::hours(24)); | std::chrono::hours(24)); | ||||
// Add nodes to this peer and update their request time far in the future | // Add nodes to this peer and update their request time far in the future | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
BOOST_CHECK(pm.addNode(i, proof->getId())); | BOOST_CHECK(pm.addNode(i, proof->getId())); | ||||
BOOST_CHECK(pm.updateNextRequestTime(i, theFuture)); | BOOST_CHECK(pm.updateNextRequestTime(i, theFuture)); | ||||
} | } | ||||
// Remove the peer | // Remove the peer | ||||
BOOST_CHECK(pm.removePeer(peerid)); | BOOST_CHECK(pm.removePeer(peerid)); | ||||
// Check the nodes are still there | // Check the nodes are still there | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
BOOST_CHECK(pm.forNode(i, [](const Node &n) { return true; })); | BOOST_CHECK(pm.forNode(i, [](const Node &n) { return true; })); | ||||
} | } | ||||
// Build a new one | // Build a new one | ||||
proof = getRandomProofPtr(MIN_VALID_PROOF_SCORE); | proof = buildRandomProof(MIN_VALID_PROOF_SCORE); | ||||
peerid = pm.getPeerId(proof); | peerid = pm.getPeerId(proof); | ||||
BOOST_CHECK_NE(peerid, NO_PEER); | BOOST_CHECK_NE(peerid, NO_PEER); | ||||
// Update the nodes with the new proof | // Update the nodes with the new proof | ||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
BOOST_CHECK(pm.addNode(i, proof->getId())); | BOOST_CHECK(pm.addNode(i, proof->getId())); | ||||
BOOST_CHECK(pm.forNode( | BOOST_CHECK(pm.forNode( | ||||
i, [&](const Node &n) { return n.nextRequestTime == theFuture; })); | i, [&](const Node &n) { return n.nextRequestTime == theFuture; })); | ||||
Show All 11 Lines | |||||
BOOST_AUTO_TEST_CASE(proof_accessors) { | BOOST_AUTO_TEST_CASE(proof_accessors) { | ||||
avalanche::PeerManager pm; | avalanche::PeerManager pm; | ||||
constexpr int numProofs = 10; | constexpr int numProofs = 10; | ||||
std::vector<std::shared_ptr<Proof>> proofs; | std::vector<std::shared_ptr<Proof>> proofs; | ||||
proofs.reserve(numProofs); | proofs.reserve(numProofs); | ||||
for (int i = 0; i < numProofs; i++) { | for (int i = 0; i < numProofs; i++) { | ||||
proofs.push_back(getRandomProofPtr(MIN_VALID_PROOF_SCORE)); | proofs.push_back(buildRandomProof(MIN_VALID_PROOF_SCORE)); | ||||
} | } | ||||
for (int i = 0; i < numProofs; i++) { | for (int i = 0; i < numProofs; i++) { | ||||
BOOST_CHECK(pm.registerProof(proofs[i])); | BOOST_CHECK(pm.registerProof(proofs[i])); | ||||
// Fail to add an existing proof | // Fail to add an existing proof | ||||
BOOST_CHECK(!pm.registerProof(proofs[i])); | BOOST_CHECK(!pm.registerProof(proofs[i])); | ||||
for (int added = 0; added <= i; added++) { | for (int added = 0; added <= i; added++) { | ||||
Show All 20 Lines |