21#include <validation.h>
23#include <test/util/blockindex.h>
24#include <test/util/random.h>
25#include <test/util/setup_common.h>
27#include <boost/test/unit_test.hpp>
31#include <unordered_map>
37 struct TestPeerManager {
41 return node.peerid == peerid;
46 auto &pendingNodesView = pm.
pendingNodes.get<by_nodeid>();
47 return pendingNodesView.find(nodeid) != pendingNodesView.end();
52 auto &pview = pm.
peers.get<by_proofid>();
53 auto it = pview.find(proofid);
54 return it == pview.end() ?
NO_PEER : it->peerid;
60 return getPeerIdForProofId(pm, proof->
getId());
63 static std::vector<uint32_t> getOrderedScores(
const PeerManager &pm) {
64 std::vector<uint32_t> scores;
66 auto &peerView = pm.
peers.get<by_score>();
67 for (
const Peer &peer : peerView) {
68 scores.push_back(peer.getScore());
74 static void cleanupDanglingProofs(
76 std::unordered_set<ProofRef, SaltedProofHasher> ®isteredProofs) {
80 static void cleanupDanglingProofs(
PeerManager &pm) {
81 std::unordered_set<ProofRef, SaltedProofHasher> dummy;
85 static std::optional<RemoteProof> getRemoteProof(
const PeerManager &pm,
88 auto it = pm.
remoteProofs.find(boost::make_tuple(proofid, nodeid));
92 return std::make_optional(*it);
96 return pm.
peers.size();
99 static std::optional<bool>
105 std::vector<PeerId> peerIds;
106 for (
auto &peer : pm.
peers) {
107 peerIds.push_back(peer.peerid);
109 for (
const PeerId &peerid : peerIds) {
127 uint32_t height = 100,
bool is_coinbase =
false) {
136 static COutPoint createUtxo(
Chainstate &chainstate,
const CKey &key,
138 uint32_t height = 100,
139 bool is_coinbase =
false) {
141 addCoin(chainstate, outpoint, key, amount, height, is_coinbase);
146 buildProof(
const CKey &key,
147 const std::vector<std::tuple<COutPoint, Amount>> &outpoints,
149 int64_t sequence = 1, uint32_t height = 100,
150 bool is_coinbase =
false, int64_t expirationTime = 0,
152 ProofBuilder pb(sequence, expirationTime, master, payoutScript);
153 for (
const auto &[outpoint, amount] : outpoints) {
154 BOOST_CHECK(pb.addUTXO(outpoint, amount, height, is_coinbase, key));
159 template <
typename... Args>
161 buildProofWithOutpoints(
const CKey &key,
162 const std::vector<COutPoint> &outpoints,
163 Amount amount, Args &&...args) {
164 std::vector<std::tuple<COutPoint, Amount>> outpointsWithAmount;
166 outpoints.begin(), outpoints.end(),
167 std::back_inserter(outpointsWithAmount),
168 [amount](
const auto &o) { return std::make_tuple(o, amount); });
169 return buildProof(key, outpointsWithAmount,
170 std::forward<Args>(args)...);
174 buildProofWithSequence(
const CKey &key,
175 const std::vector<COutPoint> &outpoints,
184struct PeerManagerFixture :
public TestChain100Setup {
185 PeerManagerFixture() {
188 ~PeerManagerFixture() {
195struct NoCoolDownFixture :
public PeerManagerFixture {
196 NoCoolDownFixture() {
199 ~NoCoolDownFixture() {
205BOOST_FIXTURE_TEST_SUITE(peermanager_tests, PeerManagerFixture)
213 const std::vector<Slot> oneslot = {{100, 100, 23}};
231 const std::vector<Slot> twoslots = {{100, 100, 69}, {300, 100, 42}};
260 std::vector<Slot> slots;
264 for (
int i = 0; i < 100; i++) {
265 slots.emplace_back(max, 1, i);
272 for (
int i = 0; i < 100; i++) {
280 slots[99] = slots[99].withScore(101);
281 max = slots[99].getStop();
284 for (
int i = 0; i < 100; i++) {
295 for (
int i = 0; i < 100; i++) {
296 slots[i] = slots[i].withStart(slots[i].getStart() + 100);
299 slots[0] =
Slot(1, slots[0].getStop() - 1, slots[0].getPeerId());
300 slots[99] = slots[99].withScore(1);
301 max = slots[99].getStop();
308 for (
int i = 0; i < 100; i++) {
315 for (
int c = 0; c < 1000; c++) {
316 size_t size = m_rng.randbits(10) + 1;
317 std::vector<Slot> slots;
320 uint64_t max = m_rng.randbits(3);
323 max += m_rng.randbits(3);
327 for (
size_t i = 0; i < size; i++) {
328 const uint64_t start = next();
329 const uint32_t score = m_rng.randbits(3);
331 slots.emplace_back(start, score, i);
334 for (
int k = 0; k < 100; k++) {
335 uint64_t s = max > 0 ? m_rng.randrange(max) : 0;
358 const NodeId node0 = 42, node1 = 69, node2 = 37;
368 std::unordered_map<PeerId, int> results = {};
369 for (
int i = 0; i < 10000; i++) {
375 BOOST_CHECK(abs(2 * results[0] - results[1]) < 500);
381 for (
int i = 0; i < 10000; i++) {
383 BOOST_CHECK(n == node0 || n == node1 || n == node2);
387 BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500);
398 std::array<PeerId, 8> peerids;
399 for (
int i = 0; i < 4; i++) {
401 peerids[i] = TestPeerManager::registerAndGetPeerId(pm, p);
408 for (
int i = 0; i < 100; i++) {
410 BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] ||
425 for (
int i = 0; i < 100; i++) {
427 BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]);
431 for (
int i = 0; i < 4; i++) {
433 peerids[i + 4] = TestPeerManager::registerAndGetPeerId(pm, p);
455 for (
int i = 0; i < 100; i++) {
457 BOOST_CHECK(p == peerids[1] || p == peerids[3] || p == peerids[4] ||
458 p == peerids[5] || p == peerids[6]);
473 std::array<PeerId, 4> peerids;
474 for (
int i = 0; i < 4; i++) {
477 peerids[i] = TestPeerManager::registerAndGetPeerId(pm, p);
482 for (
auto p : peerids) {
489 for (
int i = 0; i < 100; i++) {
513 for (
int i = 0; i < 4; i++) {
518 for (
int i = 0; i < 100; i++) {
522 n, Now<SteadyMilliseconds>(), round++));
528 for (
int i = 0; i < 100; i++) {
532 n, Now<SteadyMilliseconds>(), round++));
537 1, Now<SteadyMilliseconds>() + std::chrono::hours(24), round++));
539 for (
int i = 0; i < 100; i++) {
543 n, Now<SteadyMilliseconds>(), round++));
550 int node3selected = 0;
551 for (
int i = 0; i < 100; i++) {
560 n, Now<SteadyMilliseconds>(), round++));
564 for (
int i = 0; i < 100; i++) {
591 for (
int i = 0; i < 10; i++) {
593 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
599 const PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
600 BOOST_CHECK_NE(peerid,
NO_PEER);
601 for (
int i = 0; i < 10; i++) {
602 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
603 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
610 for (
int i = 0; i < 5; i++) {
612 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
613 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
619 for (
int i = 0; i < 5; i++) {
621 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
622 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
628 const ProofId &alt_proofid = alt_proof->getId();
631 for (
int i = 0; i < 5; i++) {
633 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
634 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
641 const ProofId &alt2_proofid = alt2_proof->getId();
644 for (
int i = 0; i < 5; i++) {
646 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
652 for (
int i = 0; i < 5; i++) {
654 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
655 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
663 for (
int i = 0; i < 10; i++) {
664 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
665 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
672 for (
int i = 0; i < 10; i++) {
674 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
675 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
691 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
692 BOOST_CHECK_NE(peerid,
NO_PEER);
696 for (
int i = 0; i < 10; i++) {
698 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
699 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
714 for (
int i = 0; i < 10; i++) {
715 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
716 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
736 peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
737 BOOST_CHECK_NE(peerid,
NO_PEER);
738 for (
int i = 0; i < 10; i++) {
739 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
740 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
753 const int height = 100;
756 for (uint32_t i = 0; i < 10; i++) {
763 const auto getPeerId = [&](
const std::vector<COutPoint> &outpoints) {
764 return TestPeerManager::registerAndGetPeerId(
765 pm, buildProofWithOutpoints(key, outpoints, v, masterKey, 0, height,
770 const PeerId peer1 = getPeerId({COutPoint(txid1, 0)});
777 const PeerId peer2 = getPeerId({COutPoint(txid2, 0)});
781 const PeerId peer3 = getPeerId({COutPoint(txid1, 1)});
788 const PeerId peer4 = getPeerId({COutPoint(txid1, 2), COutPoint(txid2, 2)});
795 COutPoint o(txid1, 3);
798 !pm.
registerProof(TestProofBuilder::buildDuplicatedStakes(pb)));
820 int immatureHeight = 100;
822 auto registerImmature = [&](
const ProofRef &proof) {
828 auto checkImmature = [&](
const ProofRef &proof,
bool expectedImmature) {
845 std::vector<ProofRef> immatureProofs;
850 auto proof = buildProofWithOutpoints(
854 registerImmature(proof);
855 checkImmature(proof,
true);
856 immatureProofs.push_back(proof);
860 for (
auto i = 0; i < 100; i++) {
864 key, 0, immatureHeight);
867 registerImmature(proof);
868 checkImmature(proof,
true);
869 immatureProofs.push_back(proof);
871 immatureProofs.erase(immatureProofs.begin());
876 const COutPoint &outpoint =
877 immatureProofs.front()->getStakes()[0].getStake().getUTXO();
880 key, 1, immatureHeight);
881 registerImmature(proof);
882 checkImmature(proof,
true);
883 immatureProofs.push_back(proof);
885 immatureProofs.erase(immatureProofs.begin());
892 for (
const auto &proof : immatureProofs) {
893 checkImmature(proof,
false);
904 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
905 BOOST_CHECK_NE(peerid,
NO_PEER);
908 std::chrono::hours(24));
911 for (
int i = 0; i < 10; i++) {
920 for (
int i = 0; i < 10; i++) {
926 peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
927 BOOST_CHECK_NE(peerid,
NO_PEER);
930 for (
int i = 0; i < 10; i++) {
933 i, [&](
const Node &n) { return n.nextRequestTime == theFuture; }));
940 for (
int i = 0; i < 10; i++) {
949 constexpr int numProofs = 10;
951 std::vector<ProofRef> proofs;
952 proofs.reserve(numProofs);
953 for (
int i = 0; i < numProofs; i++) {
958 for (
int i = 0; i < numProofs; i++) {
966 ProofRegistrationResult::ALREADY_REGISTERED);
969 for (
int added = 0; added <= i; added++) {
970 auto proof = pm.
getProof(proofs[added]->getId());
979 const std::string badProofHex(
980 "96527eae083f1f24625f049d9e54bb9a21023beefdde700a6bc02036335b4df141c8b"
981 "c67bb05a971f5ac2745fd683797dde3002321023beefdde700a6bc02036335b4df141"
982 "c8bc67bb05a971f5ac2745fd683797dde3ac135da984db510334abe41134e3d4ef09a"
983 "d006b1152be8bc413182bf6f947eac1f8580fe265a382195aa2d73935cabf86d90a8f"
984 "666d0a62385ae24732eca51575");
987 BOOST_CHECK(Proof::FromHex(*badProof, badProofHex, error));
1002 const COutPoint conflictingOutpoint = createUtxo(active_chainstate, key);
1003 const COutPoint outpointToSend = createUtxo(active_chainstate, key);
1006 buildProofWithSequence(key, {conflictingOutpoint, outpointToSend}, 20);
1010 buildProofWithSequence(key, {conflictingOutpoint}, 10);
1013 BOOST_CHECK(state.GetResult() == ProofRegistrationResult::CONFLICTING);
1035 const uint32_t height = 100;
1036 const bool is_coinbase =
false;
1042 auto conflictingOutpoint = createUtxo(active_chainstate, key, amount);
1044 auto proof_base = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1047 auto checkPreferred = [&](
const ProofRef &candidate,
1048 const ProofRef &reference,
bool expectAccepted) {
1060 ProofRegistrationResult::CONFLICTING,
1074 checkPreferred(buildProofWithSequence(key, {conflictingOutpoint}, 9),
1077 checkPreferred(buildProofWithSequence(key, {conflictingOutpoint}, 11),
1080 auto buildProofFromAmounts = [&](
const CKey &master,
1081 std::vector<Amount> &&amounts) {
1082 std::vector<std::tuple<COutPoint, Amount>> outpointsWithAmount{
1083 {conflictingOutpoint, amount}};
1084 std::transform(amounts.begin(), amounts.end(),
1085 std::back_inserter(outpointsWithAmount),
1086 [&key, &active_chainstate](
const Amount amount) {
1087 return std::make_tuple(
1088 createUtxo(active_chainstate, key, amount),
1091 return buildProof(key, outpointsWithAmount, master, 0, height,
1095 auto proof_multiUtxo = buildProofFromAmounts(
1102 checkPreferred(buildProofFromAmounts(
1104 proof_multiUtxo,
false);
1108 proof_multiUtxo,
true);
1111 proof_multiUtxo,
true);
1116 proof_multiUtxo,
false);
1118 auto proofSimilar = buildProofFromAmounts(
1120 checkPreferred(proofSimilar, proof_multiUtxo,
1121 proofSimilar->getId() < proof_multiUtxo->getId());
1134 const COutPoint conflictingOutpoint = createUtxo(active_chainstate, key);
1135 const COutPoint matureOutpoint =
1138 auto immature10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1140 buildProofWithSequence(key, {conflictingOutpoint, matureOutpoint}, 20);
1150 auto proof30 = buildProofWithOutpoints(key, {matureOutpoint},
1178 const COutPoint conflictingOutpoint =
1181 auto proofSeq10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1182 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1183 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1210 auto now = GetTime<std::chrono::seconds>();
1214 for (
size_t i = 0; i < 10; i++) {
1221 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
1223 auto checkNextPossibleConflictTime = [&](std::chrono::seconds expected) {
1225 return p.nextPossibleConflictTime == expected;
1229 checkNextPossibleConflictTime(now);
1233 peerid, now - std::chrono::seconds{1}));
1234 checkNextPossibleConflictTime(now);
1237 peerid, now + std::chrono::seconds{1}));
1238 checkNextPossibleConflictTime(now + std::chrono::seconds{1});
1247 const COutPoint conflictingOutpoint =
1250 auto proofSeq10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1251 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1252 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1281 for (
size_t i = 0; i < 10; i++) {
1284 !pm.
registerProof(proofSeq10, RegistrationMode::FORCE_ACCEPT));
1292 for (
size_t i = 0; i < 10; i++) {
1294 pm.
registerProof(proofSeq30, RegistrationMode::FORCE_ACCEPT));
1300 pm.
registerProof(proofSeq10, RegistrationMode::FORCE_ACCEPT));
1313 const COutPoint conflictingOutpoint =
1316 auto proofSeq10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1317 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1318 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1345 const COutPoint conflictingOutpoint =
1348 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1349 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1350 auto proofSeq40 = buildProofWithSequence(key, {conflictingOutpoint}, 40);
1352 int64_t conflictingProofCooldown = 100;
1354 strprintf(
"%d", conflictingProofCooldown));
1358 auto increaseMockTime = [&](int64_t s) {
1362 increaseMockTime(0);
1367 auto checkRegistrationFailure = [&](
const ProofRef &proof,
1376 checkRegistrationFailure(proofSeq20,
1377 ProofRegistrationResult::COOLDOWN_NOT_ELAPSED);
1381 checkRegistrationFailure(proofSeq40,
1382 ProofRegistrationResult::COOLDOWN_NOT_ELAPSED);
1386 increaseMockTime(conflictingProofCooldown);
1389 checkRegistrationFailure(proofSeq20, ProofRegistrationResult::CONFLICTING);
1393 checkRegistrationFailure(proofSeq40,
1394 ProofRegistrationResult::COOLDOWN_NOT_ELAPSED);
1399 increaseMockTime(conflictingProofCooldown);
1420 const COutPoint conflictingOutpoint =
1422 const COutPoint immatureOutpoint = createUtxo(active_chainstate, key);
1425 auto proofSeq10 = buildProofWithOutpoints(
1427 auto proofSeq20 = buildProofWithOutpoints(
1429 auto immature30 = buildProofWithSequence(
1430 key, {conflictingOutpoint, immatureOutpoint}, 30);
1441 for (
size_t i = 0; i < 10; i++) {
1450 auto checkRejectDefault = [&](
const ProofId &proofid) {
1452 const bool isImmature = pm.
isImmature(proofid);
1459 auto checkRejectInvalidate = [&](
const ProofId &proofid) {
1466 checkRejectDefault(immature30->getId());
1469 checkRejectInvalidate(immature30->getId());
1472 checkRejectDefault(proofSeq10->getId());
1473 checkRejectInvalidate(proofSeq10->getId());
1480 checkRejectDefault(proofSeq20->getId());
1487 checkRejectInvalidate(proofSeq10->getId());
1514 for (
size_t i = 0; i < 10; i++) {
1521 for (
size_t i = 0; i < 10; i++) {
1527 auto cooldownTimepoint = Now<SteadyMilliseconds>() + 10s;
1532 for (
size_t i = 0; i < 10; i++) {
1534 BOOST_CHECK_NE(selectedId,
NO_NODE);
1536 selectedId, cooldownTimepoint, round++));
1545 for (
size_t i = 0; i < 10; i++) {
1561 TestPeerManager::cleanupDanglingProofs(pm);
1568 TestPeerManager::cleanupDanglingProofs(pm);
1572 for (
size_t i = 0; i < 10; i++) {
1586 for (
size_t i = 0; i < 10; i++) {
1596 TestPeerManager::cleanupDanglingProofs(pm);
1602 TestPeerManager::cleanupDanglingProofs(pm);
1620 std::vector<uint32_t> expectedScores(10);
1622 std::generate(expectedScores.rbegin(), expectedScores.rend(),
1623 [n = 1]()
mutable { return n++ * MIN_VALID_PROOF_SCORE; });
1625 std::vector<ProofRef> proofs;
1626 proofs.reserve(expectedScores.size());
1627 for (uint32_t score : expectedScores) {
1633 for (
auto &proof : proofs) {
1637 auto peersScores = TestPeerManager::getOrderedScores(pm);
1638 BOOST_CHECK_EQUAL_COLLECTIONS(peersScores.begin(), peersScores.end(),
1639 expectedScores.begin(), expectedScores.end());
1654 const COutPoint peer1ConflictingOutput =
1655 createUtxo(active_chainstate, key, amount1, 99);
1656 const COutPoint peer1SecondaryOutpoint =
1657 createUtxo(active_chainstate, key, amount2, 99);
1659 auto peer1Proof1 = buildProof(
1661 {{peer1ConflictingOutput, amount1}, {peer1SecondaryOutpoint, amount2}},
1664 buildProof(key, {{peer1ConflictingOutput, amount1}}, key, 20, 99);
1669 {{peer1ConflictingOutput, amount1},
1670 {createUtxo(active_chainstate, key, amount1), amount1}},
1673 const uint32_t peer1Score1 = Proof::amountToScore(amount1 + amount2);
1674 const uint32_t peer1Score2 = Proof::amountToScore(amount1);
1692 auto checkRejectDefault = [&](
const ProofId &proofid) {
1694 const bool isImmature = pm.
isImmature(proofid);
1701 auto checkRejectInvalidate = [&](
const ProofId &proofid) {
1708 checkRejectDefault(peer1Proof3->getId());
1712 checkRejectInvalidate(peer1Proof3->getId());
1716 checkRejectDefault(peer1Proof1->getId());
1717 checkRejectInvalidate(peer1Proof1->getId());
1726 checkRejectDefault(peer1Proof2->getId());
1733 checkRejectInvalidate(peer1Proof1->getId());
1743 PeerId peerid2 = TestPeerManager::registerAndGetPeerId(pm, peer2Proof1);
1754 TestPeerManager::getPeerIdForProofId(pm, peer1Proof2->getId());
1763 const auto checkScores = [&pm](uint32_t known, uint32_t connected) {
1777 PeerId peerid1 = TestPeerManager::registerAndGetPeerId(pm, proof1);
1778 checkScores(score1, 0);
1782 const ProofId &proofid1 = proof1->getId();
1783 const uint8_t nodesToAdd = 10;
1784 for (
int i = 0; i < nodesToAdd; i++) {
1786 checkScores(score1, score1);
1790 for (
int i = 0; i < nodesToAdd - 1; i++) {
1792 checkScores(score1, score1);
1797 checkScores(score1, 0);
1803 checkScores(score1, score1);
1807 PeerId peerid2 = TestPeerManager::registerAndGetPeerId(pm, proof2);
1808 checkScores(score1 + score2, score1);
1810 checkScores(score1 + score2, score1 + score2);
1815 checkScores(score1 + score2, score1 + score2);
1817 checkScores(score1 + score2, score2);
1821 checkScores(score2, score2);
1825 checkScores(score2, 0);
1834 peerid1 = TestPeerManager::registerAndGetPeerId(pm, proof1);
1835 checkScores(score1, 0);
1836 peerid2 = TestPeerManager::registerAndGetPeerId(pm, proof2);
1837 checkScores(score1 + score2, 0);
1839 checkScores(score1 + score2, score1);
1841 checkScores(score1 + score2, score1 + score2);
1844 checkScores(score1, score1);
1854 struct ProofComparatorById {
1859 using ProofSetById = std::set<ProofRef, ProofComparatorById>;
1861 ProofSetById expectedProofs;
1863 auto matchExpectedContent = [&](
const auto &tree) {
1864 auto it = expectedProofs.
begin();
1865 return tree.forEachLeaf([&](
auto pLeaf) {
1866 return it != expectedProofs.end() &&
1867 pLeaf->getId() == (*it++)->getId();
1872 const int64_t sequence = 10;
1877 for (
size_t i = 0; i < 10; i++) {
1878 auto outpoint = createUtxo(active_chainstate, key);
1879 auto proof = buildProofWithSequence(key, {{outpoint}}, sequence);
1881 expectedProofs.insert(std::move(proof));
1891 ProofSetById addedProofs;
1892 std::vector<COutPoint> outpointsToSpend;
1893 for (
size_t i = 0; i < 10; i++) {
1894 auto outpoint = createUtxo(active_chainstate, key);
1895 auto proof = buildProofWithSequence(key, {{outpoint}}, sequence);
1897 addedProofs.insert(std::move(proof));
1898 outpointsToSpend.push_back(std::move(outpoint));
1905 expectedProofs.
insert(addedProofs.begin(), addedProofs.end());
1912 for (
const auto &outpoint : outpointsToSpend) {
1924 for (
const auto &proof : addedProofs) {
1930 std::vector<ProofRef> conflictingProofs;
1931 std::vector<COutPoint> conflictingOutpoints;
1932 for (
size_t i = 0; i < 10; i++) {
1933 auto outpoint = createUtxo(active_chainstate, key);
1934 auto proof = buildProofWithSequence(key, {{outpoint}}, sequence);
1936 conflictingProofs.push_back(std::move(proof));
1937 conflictingOutpoints.push_back(std::move(outpoint));
1941 expectedProofs.
insert(conflictingProofs.begin(), conflictingProofs.end());
1945 for (
size_t i = 0; i < 10; i += 2) {
1948 key, {{conflictingOutpoints[i]}}, sequence - 1)));
1951 auto replacementProof = buildProofWithSequence(
1952 key, {{conflictingOutpoints[i + 1]}}, sequence + 1);
1955 BOOST_CHECK(expectedProofs.insert(replacementProof).second);
1969 auto addNode = [&](
NodeId nodeid) {
1976 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
1993 const auto now = GetTime<std::chrono::seconds>();
1994 auto mocktime = now;
1996 auto elapseTime = [&](std::chrono::seconds seconds) {
1997 mocktime += seconds;
2004 const size_t numProofs = 10;
2006 std::vector<COutPoint> outpoints(numProofs);
2007 std::vector<ProofRef> proofs(numProofs);
2008 std::vector<ProofRef> conflictingProofs(numProofs);
2009 for (
size_t i = 0; i < numProofs; i++) {
2011 proofs[i] = buildProofWithSequence(key, {outpoints[i]}, 2);
2012 conflictingProofs[i] = buildProofWithSequence(key, {outpoints[i]}, 1);
2026 return peer.node_count;
2034 TestPeerManager::cleanupDanglingProofs(pm);
2035 for (
size_t i = 0; i < numProofs; i++) {
2042 TestPeerManager::cleanupDanglingProofs(pm);
2043 for (
size_t i = 0; i < numProofs; i++) {
2044 const bool hasNodeAttached = i % 2;
2061 conflictingProofs[0]->getId(),
2066 TestPeerManager::cleanupDanglingProofs(pm);
2067 for (
size_t i = 0; i < numProofs; i++) {
2068 const bool hasNodeAttached = i % 2;
2078 hasNodeAttached || i == 0);
2086 for (
size_t i = 1; i < numProofs; i += 2) {
2090 return peer.node_count == 0;
2095 conflictingProofs[0]->getId(),
2098 TestPeerManager::cleanupDanglingProofs(pm);
2099 for (
size_t i = 0; i < numProofs; i++) {
2100 const bool hadNodeAttached = i % 2;
2117 TestPeerManager::cleanupDanglingProofs(pm);
2119 for (
size_t i = 0; i < numProofs; i++) {
2136 BOOST_CHECK(state.GetResult() == ProofRegistrationResult::MISSING_UTXO);
2143 const int64_t tipTime =
2151 100,
false, tipTime + 1);
2153 1, 100,
false, tipTime + 2);
2165 for (int64_t i = 0; i < 6; i++) {
2166 SetMockTime(proofToExpire->getExpirationTime() + i);
2167 CreateAndProcessBlock({}, CScript());
2171 ->GetMedianTimePast(),
2172 proofToExpire->getExpirationTime());
2188 auto buildProofWithAmountAndPayout = [&](
Amount amount,
2189 const CScript &payoutScript) {
2191 COutPoint utxo = createUtxo(active_chainstate, key, amount);
2192 return buildProof(key, {{std::move(utxo), amount}},
2198 std::vector<std::pair<ProofId, CScript>> winners;
2204 auto now = GetTime<std::chrono::seconds>();
2206 prevBlock.
nTime = now.count();
2215 size_t numProofs = 8;
2216 std::vector<ProofRef> proofs;
2217 proofs.reserve(numProofs);
2218 for (
size_t i = 0; i < numProofs; i++) {
2224 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
2225 BOOST_CHECK_NE(peerid,
NO_PEER);
2230 proofs.emplace_back(std::move(proof));
2237 prevBlock.
nTime = now.count();
2243 BOOST_CHECK_LE(winners.size(), numProofs);
2246 for (
size_t i = 0; i < numProofs; i++) {
2247 BOOST_CHECK(TestPeerManager::isFlaky(pm, proofs[i]->getId()));
2249 BOOST_CHECK_LE(winners.size(), numProofs);
2253 BOOST_CHECK(!TestPeerManager::isFlaky(pm, proofs[i]->getId()));
2255 BOOST_CHECK_LE(winners.size(), numProofs - i);
2260 BOOST_CHECK_LE(winners.size(), 1);
2266 const size_t loop_iters =
2267 size_t(-1.0 * std::log(100000.0) /
2268 std::log((
double(numProofs) - 1) / numProofs)) +
2270 BOOST_CHECK_GT(loop_iters, numProofs);
2271 std::unordered_map<std::string, size_t> winningCounts;
2272 for (
size_t i = 0; i < loop_iters; i++) {
2283 for (
size_t i = 0; i < numProofs; i++) {
2284 for (
size_t j = 0; j < numProofs; j++) {
2292 BOOST_CHECK_GT(3. / numProofs, 0.3);
2293 for (
size_t i = 0; i < numProofs; i++) {
2297 proofs[(i - 1 + numProofs) % numProofs]->getId(), nodeid,
false));
2299 proofs[(i + numProofs) % numProofs]->getId(), nodeid,
false));
2301 proofs[(i + 1 + numProofs) % numProofs]->getId(), nodeid,
false));
2306 for (
const auto &proof : proofs) {
2312 for (
const auto &proof : proofs) {
2313 for (
NodeId nodeid = 0; nodeid <
NodeId(numProofs); nodeid++) {
2322 for (
size_t numWinner = 1; numWinner < 4; numWinner++) {
2324 CScript lastWinner = winners[numWinner - 1].second;
2328 for (
const auto &proof : proofs) {
2330 winnerProofId = proof->
getId();
2336 for (
NodeId nodeid = 0; nodeid <
NodeId(numProofs); nodeid++) {
2339 BOOST_CHECK(TestPeerManager::isFlaky(pm, winnerProofId));
2349 CScript lastWinner = winners[3].second;
2352 for (
const auto &proof : proofs) {
2354 winnerProofId = proof->
getId();
2360 for (
NodeId nodeid = 0; nodeid <
NodeId(numProofs); nodeid++) {
2369 for (
auto &proof : proofs) {
2384 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
2385 BOOST_CHECK_NE(peerid,
NO_PEER);
2411 prevBlock.
nTime = now.count();
2426 for (
size_t i = 0; i < 4; i++) {
2435 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
2436 BOOST_CHECK_NE(peerid,
NO_PEER);
2438 return peer.registration_time == now + i * 30min;
2445 proofs.push_back(proof);
2450 prevBlock.
nTime = now.count();
2458 prevBlock.
nTime = now.count();
2461 auto checkRegistrationTime =
2462 [&](
const std::pair<ProofId, CScript> &winner) {
2466 (now - 60min).count());
2477 prevBlock.
nTime = now.count();
2480 checkRegistrationTime(winners[0]);
2488 prevBlock.
nTime = now.count();
2490 BOOST_CHECK_LE(winners.size(), 3);
2491 checkRegistrationTime(winners[0]);
2499 prevBlock.
nTime = now.count();
2501 BOOST_CHECK_LE(winners.size(), 3);
2502 checkRegistrationTime(winners[0]);
2509 prevBlock.
nTime = now.count();
2511 BOOST_CHECK_LE(winners.size(), 2);
2512 checkRegistrationTime(winners[0]);
2518 prevBlock.
nTime = now.count();
2521 checkRegistrationTime(winners[0]);
2529 auto mockTime = GetTime<std::chrono::seconds>();
2537 auto checkRemoteProof =
2539 const bool expectedPresent,
2540 const std::chrono::seconds &expectedlastUpdate) {
2544 TestPeerManager::getRemoteProof(pm, proofid, nodeid);
2550 expectedlastUpdate.count());
2606 checkRemoteProof(proofid, 0,
true, mockTime);
2622 checkRemoteProof(proofid, 0,
true, mockTime);
2632 std::vector<ProofRef> proofs;
2637 proofs.push_back(proof);
2650 checkRemoteProof(proofid, 0,
true, mockTime);
2680 auto mockTime = GetTime<std::chrono::seconds>();
2689 for (
NodeId nodeid = 0; nodeid < 12; nodeid++) {
2702 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2705 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2715 TestPeerManager::setLocalProof(pm, localProof);
2723 TestPeerManager::setLocalProof(pm,
ProofRef());
2729 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2732 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2747 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2750 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2758 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2761 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2768 TestPeerManager::clearPeers(pm);
2778 for (
NodeId nodeid = 1; nodeid < 6; nodeid++) {
2784 for (
NodeId nodeid = 1; nodeid < 6; nodeid++) {
2801 auto mockTime = GetTime<std::chrono::seconds>();
2805 std::vector<ProofRef> proofs;
2806 for (
size_t i = 0; i < 10; i++) {
2809 proofs.push_back(proof);
2813 TestPeerManager::cleanupDanglingProofs(pm);
2814 for (
const auto &proof : proofs) {
2824 TestPeerManager::cleanupDanglingProofs(pm);
2825 for (
const auto &proof : proofs) {
2831 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2837 for (
const auto &proof : proofs) {
2843 for (
const auto &proof : proofs) {
2849 std::unordered_set<ProofRef, SaltedProofHasher> registeredProofs;
2850 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2851 for (
const auto &proof : proofs) {
2859 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2860 for (
const auto &proof : proofs) {
2866 for (
const auto &proof : proofs) {
2868 !TestPeerManager::getRemotePresenceStatus(pm, proof->
getId())
2873 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2875 for (
const auto &proof : proofs) {
2884 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2886 for (
const auto &proof : proofs) {
2892 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2893 for (
const auto &proof : proofs) {
2898 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2899 for (
const auto &proof : proofs) {
2912 auto mockTime = GetTime<std::chrono::seconds>();
2915 std::vector<ProofRef> proofs;
2916 for (
size_t i = 0; i < 10; i++) {
2923 auto peerid = TestPeerManager::getPeerIdForProofId(pm, proof->getId());
2927 peerid, mockTime + std::chrono::seconds{100 + i}));
2934 proofs.push_back(proof);
2939 const fs::path testDumpPath =
"test_avapeers_dump.dat";
2942 TestPeerManager::clearPeers(pm);
2944 std::unordered_set<ProofRef, SaltedProofHasher> registeredProofs;
2948 auto findProofIndex = [&proofs](
const ProofId &proofid) {
2949 for (
size_t i = 0; i < proofs.size(); i++) {
2950 if (proofs[i]->getId() == proofid) {
2960 for (
const auto &proof : registeredProofs) {
2962 size_t i = findProofIndex(proofid);
2964 BOOST_CHECK_EQUAL(peer.hasFinalized, i < 5);
2965 BOOST_CHECK_EQUAL(peer.registration_time.count(),
2966 (mockTime + std::chrono::seconds{i}).count());
2968 peer.nextPossibleConflictTime.count(),
2969 (mockTime + std::chrono::seconds{100 + i}).count());
2975 TestPeerManager::clearPeers(pm);
2988 registeredProofs.insert(proofs[0]);
2998 file << static_cast<uint64_t>(-1);
2999 file << uint64_t{0};
3004 registeredProofs.insert(proofs[0]);
3015 const uint64_t now =
GetTime();
3017 file << static_cast<uint64_t>(1);
3018 file << uint64_t{2};
3035 proofs[0]->getId());
3047 auto utxo = createUtxo(active_chainstate, key);
3050 GetTime<std::chrono::seconds>().count() + 1000000);
3061 TestPeerManager::cleanupDanglingProofs(pm);
3099 TestPeerManager::cleanupDanglingProofs(pm);
3105 for (int64_t i = 0; i < 6; i++) {
3107 CreateAndProcessBlock({}, CScript());
3111 ->GetMedianTimePast(),
3121BOOST_AUTO_TEST_SUITE_END()
static constexpr PeerId NO_PEER
#define Assert(val)
Identity function.
void ForceSetArg(const std::string &strArg, const std::string &strValue)
void ClearForcedArg(const std::string &strArg)
Remove a forced arg setting, used only in testing.
Non-refcounted RAII wrapper for FILE*.
The block chain is a tree shaped structure starting with the genesis block at the root,...
const BlockHash * phashBlock
pointer to the hash of the block, if any.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
void AddCoin(const COutPoint &outpoint, Coin coin, bool possible_overwrite)
Add a coin.
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr)
Spend a coin.
An encapsulated secp256k1 private key.
static CKey MakeCompressedKey()
Produce a valid compressed key.
CPubKey GetPubKey() const
Compute the public key from a private key.
An output of a transaction.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CCoinsViewCache & CoinsTip() EXCLUSIVE_LOCKS_REQUIRED(
bool InvalidateBlock(BlockValidationState &state, CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex
Mark a block as invalid.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
RecursiveMutex & GetMutex() const LOCK_RETURNED(
Alias for cs_main.
CBlockIndex * ActiveTip() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
int ActiveHeight() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex())
static RCUPtr make(Args &&...args)
Construct a new object that is owned by the pointer.
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
bool selectStakingRewardWinner(const CBlockIndex *pprev, std::vector< std::pair< ProofId, CScript > > &winners)
Deterministically select a list of payout scripts based on the proof set and the previous block hash.
bool removeNode(NodeId nodeid)
bool setFinalized(PeerId peerid)
Latch on that this peer has a finalized proof.
bool dumpPeersToFile(const fs::path &dumpPath) const
RemoteProofSet remoteProofs
Remember which node sent which proof so we have an image of the proof set of our peers.
uint64_t getFragmentation() const
uint32_t getConnectedPeersScore() const
bool updateNextRequestTimeForResponse(NodeId nodeid, const Response &response)
bool isDangling(const ProofId &proofid) const
std::optional< bool > getRemotePresenceStatus(const ProofId &proofid) const
Get the presence remote status of a proof.
bool shouldRequestMoreNodes()
Returns true if we encountered a lack of node since the last call.
bool exists(const ProofId &proofid) const
Return true if the (valid) proof exists, but only for non-dangling proofs.
size_t getNodeCount() const
PendingNodeSet pendingNodes
bool verify() const
Perform consistency check on internal data structures.
bool forNode(NodeId nodeid, Callable &&func) const
bool hasRemoteProofStatus(const ProofId &proofid) const
bool forPeer(const ProofId &proofid, Callable &&func) const
uint32_t getTotalPeersScore() const
bool latchAvaproofsSent(NodeId nodeid)
Flag that a node did send its compact proofs.
bool addNode(NodeId nodeid, const ProofId &proofid)
Node API.
bool updateNextRequestTimeForPoll(NodeId nodeid, SteadyMilliseconds timeout, uint64_t round)
uint64_t getSlotCount() const
bool loadPeersFromFile(const fs::path &dumpPath, std::unordered_set< ProofRef, SaltedProofHasher > ®isteredProofs)
std::unordered_set< ProofRef, SaltedProofHasher > updatedBlockTip()
Update the peer set when a new block is connected.
const ProofRadixTree & getShareableProofsSnapshot() const
bool isBoundToPeer(const ProofId &proofid) const
size_t getPendingNodeCount() const
bool saveRemoteProof(const ProofId &proofid, const NodeId nodeid, const bool present)
uint64_t compact()
Trigger maintenance of internal data structures.
void forEachPeer(Callable &&func) const
bool isFlaky(const ProofId &proofid) const
bool removePeer(const PeerId peerid)
Remove an existing peer.
bool isImmature(const ProofId &proofid) const
bool rejectProof(const ProofId &proofid, RejectionMode mode=RejectionMode::DEFAULT)
RegistrationMode
Registration mode.
static constexpr size_t MAX_REMOTE_PROOFS
PeerId selectPeer() const
Randomly select a peer to poll.
bool isInConflictingPool(const ProofId &proofid) const
bool isRemotelyPresentProof(const ProofId &proofid) const
void cleanupDanglingProofs(std::unordered_set< ProofRef, SaltedProofHasher > ®isteredProofs)
ProofRef getProof(const ProofId &proofid) const
bool registerProof(const ProofRef &proof, ProofRegistrationState ®istrationState, RegistrationMode mode=RegistrationMode::DEFAULT)
bool updateNextPossibleConflictTime(PeerId peerid, const std::chrono::seconds &nextTime)
Proof and Peer related API.
bool addUTXO(COutPoint utxo, Amount amount, uint32_t height, bool is_coinbase, CKey key)
int64_t getExpirationTime() const
const CScript & getPayoutScript() const
const ProofId & getId() const
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const uint256 ZERO
static void addCoin(const Amount nValue, const CWallet &wallet, std::vector< std::unique_ptr< CWalletTx > > &wtxs)
std::string FormatScript(const CScript &script)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool FileCommit(FILE *file)
Ensure file contents are fully committed to disk, using a platform-specific feature analogous to fsyn...
static RPCHelpMan generate()
static constexpr Amount PROOF_DUST_THRESHOLD
Minimum amount per utxo.
static constexpr uint32_t AVALANCHE_MAX_IMMATURE_PROOFS
Maximum number of immature proofs the peer manager will accept from the network.
const CScript UNSPENDABLE_ECREG_PAYOUT_SCRIPT
ProofRef buildRandomProof(Chainstate &active_chainstate, uint32_t score, int height, const CKey &masterKey)
constexpr uint32_t MIN_VALID_PROOF_SCORE
PeerId selectPeerImpl(const std::vector< Slot > &slots, const uint64_t slot, const uint64_t max)
Internal methods that are exposed for testing purposes.
RCUPtr< const Proof > ProofRef
FILE * fopen(const fs::path &p, const char *mode)
static constexpr NodeId NO_NODE
Special NodeId that represent no node.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static void addNodeWithScore(Chainstate &active_chainstate, avalanche::PeerManager &pm, NodeId node, uint32_t score)
BOOST_AUTO_TEST_CASE(select_peer_linear)
BOOST_FIXTURE_TEST_CASE(conflicting_proof_rescan, NoCoolDownFixture)
void Shuffle(I first, I last, R &&rng)
More efficient than using std::shuffle on a FastRandomContext.
uint256 GetRandHash() noexcept
========== CONVENIENCE FUNCTIONS FOR COMMONLY USED RANDOMNESS ==========
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
A BlockHash is a unqiue identifier for a block.
bool insert(const RCUPtr< T > &value)
Insert a value into the tree.
A TxId is the identifier of a transaction.
Compare conflicting proofs.
std::chrono::seconds registration_time
static constexpr auto DANGLING_TIMEOUT
Consider dropping the peer if no node is attached after this timeout expired.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds