Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 198 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(remove_peer) { | BOOST_AUTO_TEST_CASE(remove_peer) { | ||||
// No peers. | // No peers. | ||||
PeerManager pm; | 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++) { | ||||
peerids[i] = pm.getPeer(buildRandomProof(100)); | peerids[i] = pm.getPeerId(buildRandomProof(100)); | ||||
} | } | ||||
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++) { | ||||
peerids[i + 4] = pm.getPeer(buildRandomProof(100)); | peerids[i + 4] = pm.getPeerId(buildRandomProof(100)); | ||||
} | } | ||||
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) { | ||||
PeerManager pm; | 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++) { | ||||
peerids[i] = pm.getPeer(buildRandomProof(100)); | peerids[i] = pm.getPeerId(buildRandomProof(100)); | ||||
} | } | ||||
// 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); | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(proof_conflict) { | ||||
PeerManager pm; | 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.getPeer(pb.build()); | return pm.getPeerId(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 |