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;
359 const NodeId node0 = 42, node1 = 69, node2 = 37;
369 std::unordered_map<PeerId, int> results = {};
370 for (
int i = 0; i < 10000; i++) {
376 BOOST_CHECK(abs(2 * results[0] - results[1]) < 500);
382 for (
int i = 0; i < 10000; i++) {
384 BOOST_CHECK(n == node0 || n == node1 || n == node2);
388 BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500);
399 std::array<PeerId, 8> peerids;
400 for (
int i = 0; i < 4; i++) {
402 peerids[i] = TestPeerManager::registerAndGetPeerId(pm, p);
410 for (
int i = 0; i < 100; i++) {
412 BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] ||
427 for (
int i = 0; i < 100; i++) {
429 BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]);
433 for (
int i = 0; i < 4; i++) {
435 peerids[i + 4] = TestPeerManager::registerAndGetPeerId(pm, p);
458 for (
int i = 0; i < 100; i++) {
460 BOOST_CHECK(p == peerids[1] || p == peerids[3] || p == peerids[4] ||
461 p == peerids[5] || p == peerids[6]);
476 std::array<PeerId, 4> peerids;
477 for (
int i = 0; i < 4; i++) {
480 peerids[i] = TestPeerManager::registerAndGetPeerId(pm, p);
486 for (
auto p : peerids) {
493 for (
int i = 0; i < 100; i++) {
517 for (
int i = 0; i < 4; i++) {
522 for (
int i = 0; i < 100; i++) {
526 n, Now<SteadyMilliseconds>(), round++));
532 for (
int i = 0; i < 100; i++) {
536 n, Now<SteadyMilliseconds>(), round++));
541 1, Now<SteadyMilliseconds>() + std::chrono::hours(24), round++));
543 for (
int i = 0; i < 100; i++) {
547 n, Now<SteadyMilliseconds>(), round++));
554 int node3selected = 0;
555 for (
int i = 0; i < 100; i++) {
564 n, Now<SteadyMilliseconds>(), round++));
568 for (
int i = 0; i < 100; i++) {
595 for (
int i = 0; i < 10; i++) {
598 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
604 const PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
605 BOOST_CHECK_NE(peerid,
NO_PEER);
606 for (
int i = 0; i < 10; i++) {
607 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
608 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
615 for (
int i = 0; i < 5; i++) {
617 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
618 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
624 for (
int i = 0; i < 5; i++) {
626 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
627 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
633 const ProofId &alt_proofid = alt_proof->getId();
636 for (
int i = 0; i < 5; i++) {
639 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
640 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
647 const ProofId &alt2_proofid = alt2_proof->getId();
650 for (
int i = 0; i < 5; i++) {
653 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
659 for (
int i = 0; i < 5; i++) {
661 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
662 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
670 for (
int i = 0; i < 10; i++) {
671 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
672 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
679 for (
int i = 0; i < 10; i++) {
681 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
682 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
698 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
699 BOOST_CHECK_NE(peerid,
NO_PEER);
703 for (
int i = 0; i < 10; i++) {
705 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
706 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
721 for (
int i = 0; i < 10; i++) {
722 BOOST_CHECK(TestPeerManager::isNodePending(pm, i));
723 BOOST_CHECK(!TestPeerManager::nodeBelongToPeer(pm, i, peerid));
743 peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
744 BOOST_CHECK_NE(peerid,
NO_PEER);
745 for (
int i = 0; i < 10; i++) {
746 BOOST_CHECK(!TestPeerManager::isNodePending(pm, i));
747 BOOST_CHECK(TestPeerManager::nodeBelongToPeer(pm, i, peerid));
760 const int height = 100;
763 for (uint32_t i = 0; i < 10; i++) {
770 const auto getPeerId = [&](
const std::vector<COutPoint> &outpoints) {
771 return TestPeerManager::registerAndGetPeerId(
772 pm, buildProofWithOutpoints(key, outpoints, v, masterKey, 0, height,
777 const PeerId peer1 = getPeerId({COutPoint(txid1, 0)});
784 const PeerId peer2 = getPeerId({COutPoint(txid2, 0)});
788 const PeerId peer3 = getPeerId({COutPoint(txid1, 1)});
795 const PeerId peer4 = getPeerId({COutPoint(txid1, 2), COutPoint(txid2, 2)});
802 COutPoint o(txid1, 3);
805 !pm.
registerProof(TestProofBuilder::buildDuplicatedStakes(pb)));
827 int immatureHeight = 100;
829 auto registerImmature = [&](
const ProofRef &proof) {
835 auto checkImmature = [&](
const ProofRef &proof,
bool expectedImmature) {
852 std::vector<ProofRef> immatureProofs;
857 auto proof = buildProofWithOutpoints(
861 registerImmature(proof);
862 checkImmature(proof,
true);
863 immatureProofs.push_back(proof);
867 for (
auto i = 0; i < 100; i++) {
871 key, 0, immatureHeight);
874 registerImmature(proof);
875 checkImmature(proof,
true);
876 immatureProofs.push_back(proof);
878 immatureProofs.erase(immatureProofs.begin());
883 const COutPoint &outpoint =
884 immatureProofs.front()->getStakes()[0].getStake().getUTXO();
887 key, 1, immatureHeight);
888 registerImmature(proof);
889 checkImmature(proof,
true);
890 immatureProofs.push_back(proof);
892 immatureProofs.erase(immatureProofs.begin());
899 for (
const auto &proof : immatureProofs) {
900 checkImmature(proof,
false);
911 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
912 BOOST_CHECK_NE(peerid,
NO_PEER);
915 std::chrono::hours(24));
918 for (
int i = 0; i < 10; i++) {
928 for (
int i = 0; i < 10; i++) {
934 peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
935 BOOST_CHECK_NE(peerid,
NO_PEER);
938 for (
int i = 0; i < 10; i++) {
942 i, [&](
const Node &n) { return n.nextRequestTime == theFuture; }));
949 for (
int i = 0; i < 10; i++) {
958 constexpr int numProofs = 10;
960 std::vector<ProofRef> proofs;
961 proofs.reserve(numProofs);
962 for (
int i = 0; i < numProofs; i++) {
967 for (
int i = 0; i < numProofs; i++) {
975 ProofRegistrationResult::ALREADY_REGISTERED);
978 for (
int added = 0; added <= i; added++) {
979 auto proof = pm.
getProof(proofs[added]->getId());
988 const std::string badProofHex(
989 "96527eae083f1f24625f049d9e54bb9a21023beefdde700a6bc02036335b4df141c8b"
990 "c67bb05a971f5ac2745fd683797dde3002321023beefdde700a6bc02036335b4df141"
991 "c8bc67bb05a971f5ac2745fd683797dde3ac135da984db510334abe41134e3d4ef09a"
992 "d006b1152be8bc413182bf6f947eac1f8580fe265a382195aa2d73935cabf86d90a8f"
993 "666d0a62385ae24732eca51575");
996 BOOST_CHECK(Proof::FromHex(*badProof, badProofHex, error));
1011 const COutPoint conflictingOutpoint = createUtxo(active_chainstate, key);
1012 const COutPoint outpointToSend = createUtxo(active_chainstate, key);
1015 buildProofWithSequence(key, {conflictingOutpoint, outpointToSend}, 20);
1019 buildProofWithSequence(key, {conflictingOutpoint}, 10);
1022 BOOST_CHECK(state.GetResult() == ProofRegistrationResult::CONFLICTING);
1044 const uint32_t height = 100;
1045 const bool is_coinbase =
false;
1051 auto conflictingOutpoint = createUtxo(active_chainstate, key, amount);
1053 auto proof_base = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1056 auto checkPreferred = [&](
const ProofRef &candidate,
1057 const ProofRef &reference,
bool expectAccepted) {
1069 ProofRegistrationResult::CONFLICTING,
1083 checkPreferred(buildProofWithSequence(key, {conflictingOutpoint}, 9),
1086 checkPreferred(buildProofWithSequence(key, {conflictingOutpoint}, 11),
1089 auto buildProofFromAmounts = [&](
const CKey &master,
1090 std::vector<Amount> &&amounts) {
1091 std::vector<std::tuple<COutPoint, Amount>> outpointsWithAmount{
1092 {conflictingOutpoint, amount}};
1093 std::transform(amounts.begin(), amounts.end(),
1094 std::back_inserter(outpointsWithAmount),
1095 [&key, &active_chainstate](
const Amount amount) {
1096 return std::make_tuple(
1097 createUtxo(active_chainstate, key, amount),
1100 return buildProof(key, outpointsWithAmount, master, 0, height,
1104 auto proof_multiUtxo = buildProofFromAmounts(
1111 checkPreferred(buildProofFromAmounts(
1113 proof_multiUtxo,
false);
1117 proof_multiUtxo,
true);
1120 proof_multiUtxo,
true);
1125 proof_multiUtxo,
false);
1127 auto proofSimilar = buildProofFromAmounts(
1129 checkPreferred(proofSimilar, proof_multiUtxo,
1130 proofSimilar->getId() < proof_multiUtxo->getId());
1143 const COutPoint conflictingOutpoint = createUtxo(active_chainstate, key);
1144 const COutPoint matureOutpoint =
1147 auto immature10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1149 buildProofWithSequence(key, {conflictingOutpoint, matureOutpoint}, 20);
1159 auto proof30 = buildProofWithOutpoints(key, {matureOutpoint},
1187 const COutPoint conflictingOutpoint =
1190 auto proofSeq10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1191 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1192 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1219 auto now = GetTime<std::chrono::seconds>();
1223 for (
size_t i = 0; i < 10; i++) {
1230 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
1232 auto checkNextPossibleConflictTime = [&](std::chrono::seconds expected) {
1234 return p.nextPossibleConflictTime == expected;
1238 checkNextPossibleConflictTime(now);
1242 peerid, now - std::chrono::seconds{1}));
1243 checkNextPossibleConflictTime(now);
1246 peerid, now + std::chrono::seconds{1}));
1247 checkNextPossibleConflictTime(now + std::chrono::seconds{1});
1256 const COutPoint conflictingOutpoint =
1259 auto proofSeq10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1260 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1261 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1290 for (
size_t i = 0; i < 10; i++) {
1293 !pm.
registerProof(proofSeq10, RegistrationMode::FORCE_ACCEPT));
1301 for (
size_t i = 0; i < 10; i++) {
1303 pm.
registerProof(proofSeq30, RegistrationMode::FORCE_ACCEPT));
1309 pm.
registerProof(proofSeq10, RegistrationMode::FORCE_ACCEPT));
1322 const COutPoint conflictingOutpoint =
1325 auto proofSeq10 = buildProofWithSequence(key, {conflictingOutpoint}, 10);
1326 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1327 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1354 const COutPoint conflictingOutpoint =
1357 auto proofSeq20 = buildProofWithSequence(key, {conflictingOutpoint}, 20);
1358 auto proofSeq30 = buildProofWithSequence(key, {conflictingOutpoint}, 30);
1359 auto proofSeq40 = buildProofWithSequence(key, {conflictingOutpoint}, 40);
1361 int64_t conflictingProofCooldown = 100;
1363 strprintf(
"%d", conflictingProofCooldown));
1367 auto increaseMockTime = [&](int64_t s) {
1371 increaseMockTime(0);
1376 auto checkRegistrationFailure = [&](
const ProofRef &proof,
1385 checkRegistrationFailure(proofSeq20,
1386 ProofRegistrationResult::COOLDOWN_NOT_ELAPSED);
1390 checkRegistrationFailure(proofSeq40,
1391 ProofRegistrationResult::COOLDOWN_NOT_ELAPSED);
1395 increaseMockTime(conflictingProofCooldown);
1398 checkRegistrationFailure(proofSeq20, ProofRegistrationResult::CONFLICTING);
1402 checkRegistrationFailure(proofSeq40,
1403 ProofRegistrationResult::COOLDOWN_NOT_ELAPSED);
1408 increaseMockTime(conflictingProofCooldown);
1429 const COutPoint conflictingOutpoint =
1431 const COutPoint immatureOutpoint = createUtxo(active_chainstate, key);
1434 auto proofSeq10 = buildProofWithOutpoints(
1436 auto proofSeq20 = buildProofWithOutpoints(
1438 auto immature30 = buildProofWithSequence(
1439 key, {conflictingOutpoint, immatureOutpoint}, 30);
1450 for (
size_t i = 0; i < 10; i++) {
1459 auto checkRejectDefault = [&](
const ProofId &proofid) {
1461 const bool isImmature = pm.
isImmature(proofid);
1468 auto checkRejectInvalidate = [&](
const ProofId &proofid) {
1475 checkRejectDefault(immature30->getId());
1478 checkRejectInvalidate(immature30->getId());
1481 checkRejectDefault(proofSeq10->getId());
1482 checkRejectInvalidate(proofSeq10->getId());
1489 checkRejectDefault(proofSeq20->getId());
1496 checkRejectInvalidate(proofSeq10->getId());
1523 for (
size_t i = 0; i < 10; i++) {
1530 for (
size_t i = 0; i < 10; i++) {
1536 auto cooldownTimepoint = Now<SteadyMilliseconds>() + 10s;
1541 for (
size_t i = 0; i < 10; i++) {
1543 BOOST_CHECK_NE(selectedId,
NO_NODE);
1545 selectedId, cooldownTimepoint, round++));
1554 for (
size_t i = 0; i < 10; i++) {
1570 TestPeerManager::cleanupDanglingProofs(pm);
1577 TestPeerManager::cleanupDanglingProofs(pm);
1581 for (
size_t i = 0; i < 10; i++) {
1595 for (
size_t i = 0; i < 10; i++) {
1606 TestPeerManager::cleanupDanglingProofs(pm);
1612 TestPeerManager::cleanupDanglingProofs(pm);
1630 std::vector<uint32_t> expectedScores(10);
1632 std::generate(expectedScores.rbegin(), expectedScores.rend(),
1633 [n = 1]()
mutable { return n++ * MIN_VALID_PROOF_SCORE; });
1635 std::vector<ProofRef> proofs;
1636 proofs.reserve(expectedScores.size());
1637 for (uint32_t score : expectedScores) {
1643 for (
auto &proof : proofs) {
1647 auto peersScores = TestPeerManager::getOrderedScores(pm);
1648 BOOST_CHECK_EQUAL_COLLECTIONS(peersScores.begin(), peersScores.end(),
1649 expectedScores.begin(), expectedScores.end());
1664 const COutPoint peer1ConflictingOutput =
1665 createUtxo(active_chainstate, key, amount1, 99);
1666 const COutPoint peer1SecondaryOutpoint =
1667 createUtxo(active_chainstate, key, amount2, 99);
1669 auto peer1Proof1 = buildProof(
1671 {{peer1ConflictingOutput, amount1}, {peer1SecondaryOutpoint, amount2}},
1674 buildProof(key, {{peer1ConflictingOutput, amount1}}, key, 20, 99);
1679 {{peer1ConflictingOutput, amount1},
1680 {createUtxo(active_chainstate, key, amount1), amount1}},
1683 const uint32_t peer1Score1 = Proof::amountToScore(amount1 + amount2);
1684 const uint32_t peer1Score2 = Proof::amountToScore(amount1);
1702 auto checkRejectDefault = [&](
const ProofId &proofid) {
1704 const bool isImmature = pm.
isImmature(proofid);
1711 auto checkRejectInvalidate = [&](
const ProofId &proofid) {
1718 checkRejectDefault(peer1Proof3->getId());
1722 checkRejectInvalidate(peer1Proof3->getId());
1726 checkRejectDefault(peer1Proof1->getId());
1727 checkRejectInvalidate(peer1Proof1->getId());
1736 checkRejectDefault(peer1Proof2->getId());
1743 checkRejectInvalidate(peer1Proof1->getId());
1753 PeerId peerid2 = TestPeerManager::registerAndGetPeerId(pm, peer2Proof1);
1764 TestPeerManager::getPeerIdForProofId(pm, peer1Proof2->getId());
1773 const auto checkScores = [&pm](uint32_t known, uint32_t connected) {
1787 PeerId peerid1 = TestPeerManager::registerAndGetPeerId(pm, proof1);
1788 checkScores(score1, 0);
1792 const ProofId &proofid1 = proof1->getId();
1793 const uint8_t nodesToAdd = 10;
1794 for (
int i = 0; i < nodesToAdd; i++) {
1797 checkScores(score1, score1);
1801 for (
int i = 0; i < nodesToAdd - 1; i++) {
1803 checkScores(score1, score1);
1808 checkScores(score1, 0);
1814 checkScores(score1, score1);
1818 PeerId peerid2 = TestPeerManager::registerAndGetPeerId(pm, proof2);
1819 checkScores(score1 + score2, score1);
1822 checkScores(score1 + score2, score1 + score2);
1827 checkScores(score1 + score2, score1 + score2);
1829 checkScores(score1 + score2, score2);
1833 checkScores(score2, score2);
1837 checkScores(score2, 0);
1846 peerid1 = TestPeerManager::registerAndGetPeerId(pm, proof1);
1847 checkScores(score1, 0);
1848 peerid2 = TestPeerManager::registerAndGetPeerId(pm, proof2);
1849 checkScores(score1 + score2, 0);
1852 checkScores(score1 + score2, score1);
1855 checkScores(score1 + score2, score1 + score2);
1858 checkScores(score1, score1);
1868 struct ProofComparatorById {
1873 using ProofSetById = std::set<ProofRef, ProofComparatorById>;
1875 ProofSetById expectedProofs;
1877 auto matchExpectedContent = [&](
const auto &tree) {
1878 auto it = expectedProofs.
begin();
1879 return tree.forEachLeaf([&](
auto pLeaf) {
1880 return it != expectedProofs.end() &&
1881 pLeaf->getId() == (*it++)->getId();
1886 const int64_t sequence = 10;
1891 for (
size_t i = 0; i < 10; i++) {
1892 auto outpoint = createUtxo(active_chainstate, key);
1893 auto proof = buildProofWithSequence(key, {{outpoint}}, sequence);
1895 expectedProofs.insert(std::move(proof));
1905 ProofSetById addedProofs;
1906 std::vector<COutPoint> outpointsToSpend;
1907 for (
size_t i = 0; i < 10; i++) {
1908 auto outpoint = createUtxo(active_chainstate, key);
1909 auto proof = buildProofWithSequence(key, {{outpoint}}, sequence);
1911 addedProofs.insert(std::move(proof));
1912 outpointsToSpend.push_back(std::move(outpoint));
1919 expectedProofs.
insert(addedProofs.begin(), addedProofs.end());
1926 for (
const auto &outpoint : outpointsToSpend) {
1938 for (
const auto &proof : addedProofs) {
1944 std::vector<ProofRef> conflictingProofs;
1945 std::vector<COutPoint> conflictingOutpoints;
1946 for (
size_t i = 0; i < 10; i++) {
1947 auto outpoint = createUtxo(active_chainstate, key);
1948 auto proof = buildProofWithSequence(key, {{outpoint}}, sequence);
1950 conflictingProofs.push_back(std::move(proof));
1951 conflictingOutpoints.push_back(std::move(outpoint));
1955 expectedProofs.
insert(conflictingProofs.begin(), conflictingProofs.end());
1959 for (
size_t i = 0; i < 10; i += 2) {
1962 key, {{conflictingOutpoints[i]}}, sequence - 1)));
1965 auto replacementProof = buildProofWithSequence(
1966 key, {{conflictingOutpoints[i + 1]}}, sequence + 1);
1969 BOOST_CHECK(expectedProofs.insert(replacementProof).second);
1983 auto addNode = [&](
NodeId nodeid) {
1991 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2008 const auto now = GetTime<std::chrono::seconds>();
2009 auto mocktime = now;
2011 auto elapseTime = [&](std::chrono::seconds seconds) {
2012 mocktime += seconds;
2019 const size_t numProofs = 10;
2021 std::vector<COutPoint> outpoints(numProofs);
2022 std::vector<ProofRef> proofs(numProofs);
2023 std::vector<ProofRef> conflictingProofs(numProofs);
2024 for (
size_t i = 0; i < numProofs; i++) {
2026 proofs[i] = buildProofWithSequence(key, {outpoints[i]}, 2);
2027 conflictingProofs[i] = buildProofWithSequence(key, {outpoints[i]}, 1);
2042 return peer.node_count;
2050 TestPeerManager::cleanupDanglingProofs(pm);
2051 for (
size_t i = 0; i < numProofs; i++) {
2058 TestPeerManager::cleanupDanglingProofs(pm);
2059 for (
size_t i = 0; i < numProofs; i++) {
2060 const bool hasNodeAttached = i % 2;
2078 conflictingProofs[0]->getId(),
2083 TestPeerManager::cleanupDanglingProofs(pm);
2084 for (
size_t i = 0; i < numProofs; i++) {
2085 const bool hasNodeAttached = i % 2;
2095 hasNodeAttached || i == 0);
2103 for (
size_t i = 1; i < numProofs; i += 2) {
2107 return peer.node_count == 0;
2112 conflictingProofs[0]->getId(),
2115 TestPeerManager::cleanupDanglingProofs(pm);
2116 for (
size_t i = 0; i < numProofs; i++) {
2117 const bool hadNodeAttached = i % 2;
2134 TestPeerManager::cleanupDanglingProofs(pm);
2136 for (
size_t i = 0; i < numProofs; i++) {
2153 BOOST_CHECK(state.GetResult() == ProofRegistrationResult::MISSING_UTXO);
2160 const int64_t tipTime =
2168 100,
false, tipTime + 1);
2170 1, 100,
false, tipTime + 2);
2182 for (int64_t i = 0; i < 6; i++) {
2183 SetMockTime(proofToExpire->getExpirationTime() + i);
2184 CreateAndProcessBlock({}, CScript());
2188 ->GetMedianTimePast(),
2189 proofToExpire->getExpirationTime());
2205 auto buildProofWithAmountAndPayout = [&](
Amount amount,
2206 const CScript &payoutScript) {
2208 COutPoint utxo = createUtxo(active_chainstate, key, amount);
2209 return buildProof(key, {{std::move(utxo), amount}},
2215 std::vector<std::pair<ProofId, CScript>> winners;
2221 auto now = GetTime<std::chrono::seconds>();
2223 prevBlock.
nTime = now.count();
2232 size_t numProofs = 8;
2233 std::vector<ProofRef> proofs;
2234 proofs.reserve(numProofs);
2235 for (
size_t i = 0; i < numProofs; i++) {
2241 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
2242 BOOST_CHECK_NE(peerid,
NO_PEER);
2247 proofs.emplace_back(std::move(proof));
2254 prevBlock.
nTime = now.count();
2260 BOOST_CHECK_LE(winners.size(), numProofs);
2263 for (
size_t i = 0; i < numProofs; i++) {
2264 BOOST_CHECK(TestPeerManager::isFlaky(pm, proofs[i]->getId()));
2266 BOOST_CHECK_LE(winners.size(), numProofs);
2271 BOOST_CHECK(!TestPeerManager::isFlaky(pm, proofs[i]->getId()));
2273 BOOST_CHECK_LE(winners.size(), numProofs - i);
2278 BOOST_CHECK_LE(winners.size(), 1);
2284 const size_t loop_iters =
2285 size_t(-1.0 * std::log(100000.0) /
2286 std::log((
double(numProofs) - 1) / numProofs)) +
2288 BOOST_CHECK_GT(loop_iters, numProofs);
2289 std::unordered_map<std::string, size_t> winningCounts;
2290 for (
size_t i = 0; i < loop_iters; i++) {
2301 for (
size_t i = 0; i < numProofs; i++) {
2302 for (
size_t j = 0; j < numProofs; j++) {
2310 BOOST_CHECK_GT(3. / numProofs, 0.3);
2311 for (
size_t i = 0; i < numProofs; i++) {
2315 proofs[(i - 1 + numProofs) % numProofs]->getId(), nodeid,
false));
2317 proofs[(i + numProofs) % numProofs]->getId(), nodeid,
false));
2319 proofs[(i + 1 + numProofs) % numProofs]->getId(), nodeid,
false));
2324 for (
const auto &proof : proofs) {
2330 for (
const auto &proof : proofs) {
2331 for (
NodeId nodeid = 0; nodeid <
NodeId(numProofs); nodeid++) {
2340 for (
size_t numWinner = 1; numWinner < 4; numWinner++) {
2342 CScript lastWinner = winners[numWinner - 1].second;
2346 for (
const auto &proof : proofs) {
2348 winnerProofId = proof->
getId();
2354 for (
NodeId nodeid = 0; nodeid <
NodeId(numProofs); nodeid++) {
2357 BOOST_CHECK(TestPeerManager::isFlaky(pm, winnerProofId));
2367 CScript lastWinner = winners[3].second;
2370 for (
const auto &proof : proofs) {
2372 winnerProofId = proof->
getId();
2378 for (
NodeId nodeid = 0; nodeid <
NodeId(numProofs); nodeid++) {
2387 for (
auto &proof : proofs) {
2402 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
2403 BOOST_CHECK_NE(peerid,
NO_PEER);
2429 prevBlock.
nTime = now.count();
2444 for (
size_t i = 0; i < 4; i++) {
2453 PeerId peerid = TestPeerManager::registerAndGetPeerId(pm, proof);
2454 BOOST_CHECK_NE(peerid,
NO_PEER);
2456 return peer.registration_time == now + i * 30min;
2464 proofs.push_back(proof);
2469 prevBlock.
nTime = now.count();
2477 prevBlock.
nTime = now.count();
2480 auto checkRegistrationTime =
2481 [&](
const std::pair<ProofId, CScript> &winner) {
2485 (now - 60min).count());
2496 prevBlock.
nTime = now.count();
2499 checkRegistrationTime(winners[0]);
2507 prevBlock.
nTime = now.count();
2509 BOOST_CHECK_LE(winners.size(), 3);
2510 checkRegistrationTime(winners[0]);
2518 prevBlock.
nTime = now.count();
2520 BOOST_CHECK_LE(winners.size(), 3);
2521 checkRegistrationTime(winners[0]);
2528 prevBlock.
nTime = now.count();
2530 BOOST_CHECK_LE(winners.size(), 2);
2531 checkRegistrationTime(winners[0]);
2537 prevBlock.
nTime = now.count();
2540 checkRegistrationTime(winners[0]);
2548 auto mockTime = GetTime<std::chrono::seconds>();
2556 auto checkRemoteProof =
2558 const bool expectedPresent,
2559 const std::chrono::seconds &expectedlastUpdate) {
2563 TestPeerManager::getRemoteProof(pm, proofid, nodeid);
2569 expectedlastUpdate.count());
2627 checkRemoteProof(proofid, 0,
true, mockTime);
2643 checkRemoteProof(proofid, 0,
true, mockTime);
2653 std::vector<ProofRef> proofs;
2658 proofs.push_back(proof);
2671 checkRemoteProof(proofid, 0,
true, mockTime);
2701 auto mockTime = GetTime<std::chrono::seconds>();
2710 for (
NodeId nodeid = 0; nodeid < 12; nodeid++) {
2724 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2727 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2737 TestPeerManager::setLocalProof(pm, localProof);
2745 TestPeerManager::setLocalProof(pm,
ProofRef());
2751 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2754 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2770 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2773 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2781 for (
NodeId nodeid = 0; nodeid < 5; nodeid++) {
2784 for (
NodeId nodeid = 5; nodeid < 12; nodeid++) {
2791 TestPeerManager::clearPeers(pm);
2802 for (
NodeId nodeid = 1; nodeid < 6; nodeid++) {
2809 for (
NodeId nodeid = 1; nodeid < 6; nodeid++) {
2826 auto mockTime = GetTime<std::chrono::seconds>();
2830 std::vector<ProofRef> proofs;
2831 for (
size_t i = 0; i < 10; i++) {
2834 proofs.push_back(proof);
2838 TestPeerManager::cleanupDanglingProofs(pm);
2839 for (
const auto &proof : proofs) {
2849 TestPeerManager::cleanupDanglingProofs(pm);
2850 for (
const auto &proof : proofs) {
2856 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2863 for (
const auto &proof : proofs) {
2869 for (
const auto &proof : proofs) {
2875 std::unordered_set<ProofRef, SaltedProofHasher> registeredProofs;
2876 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2877 for (
const auto &proof : proofs) {
2885 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2886 for (
const auto &proof : proofs) {
2892 for (
const auto &proof : proofs) {
2894 !TestPeerManager::getRemotePresenceStatus(pm, proof->
getId())
2899 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2901 for (
const auto &proof : proofs) {
2910 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2912 for (
const auto &proof : proofs) {
2918 for (
NodeId nodeid = 0; nodeid < 10; nodeid++) {
2919 for (
const auto &proof : proofs) {
2924 TestPeerManager::cleanupDanglingProofs(pm, registeredProofs);
2925 for (
const auto &proof : proofs) {
2938 auto mockTime = GetTime<std::chrono::seconds>();
2941 std::vector<ProofRef> proofs;
2942 for (
size_t i = 0; i < 10; i++) {
2949 auto peerid = TestPeerManager::getPeerIdForProofId(pm, proof->getId());
2953 peerid, mockTime + std::chrono::seconds{100 + i}));
2960 proofs.push_back(proof);
2965 const fs::path testDumpPath =
"test_avapeers_dump.dat";
2968 TestPeerManager::clearPeers(pm);
2970 std::unordered_set<ProofRef, SaltedProofHasher> registeredProofs;
2974 auto findProofIndex = [&proofs](
const ProofId &proofid) {
2975 for (
size_t i = 0; i < proofs.size(); i++) {
2976 if (proofs[i]->getId() == proofid) {
2986 for (
const auto &proof : registeredProofs) {
2988 size_t i = findProofIndex(proofid);
2990 BOOST_CHECK_EQUAL(peer.hasFinalized, i < 5);
2991 BOOST_CHECK_EQUAL(peer.registration_time.count(),
2992 (mockTime + std::chrono::seconds{i}).count());
2994 peer.nextPossibleConflictTime.count(),
2995 (mockTime + std::chrono::seconds{100 + i}).count());
3001 TestPeerManager::clearPeers(pm);
3014 registeredProofs.insert(proofs[0]);
3024 file << static_cast<uint64_t>(-1);
3025 file << uint64_t{0};
3030 registeredProofs.insert(proofs[0]);
3041 const uint64_t now =
GetTime();
3043 file << static_cast<uint64_t>(1);
3044 file << uint64_t{2};
3061 proofs[0]->getId());
3073 auto utxo = createUtxo(active_chainstate, key);
3076 GetTime<std::chrono::seconds>().count() + 1000000);
3087 TestPeerManager::cleanupDanglingProofs(pm);
3125 TestPeerManager::cleanupDanglingProofs(pm);
3131 for (int64_t i = 0; i < 6; i++) {
3133 CreateAndProcessBlock({}, CScript());
3137 ->GetMedianTimePast(),
3147BOOST_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
bool addNode(NodeId nodeid, const ProofId &proofid, size_t max_elements)
Node API.
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 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)
static constexpr size_t DEFAULT_AVALANCHE_MAX_ELEMENT_POLL
Maximum item that can be polled at once.
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