Bitcoin ABC  0.26.3
P2P Digital Currency
proofcomparator_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 The Bitcoin developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
6 
7 #include <random.h>
8 
9 #include <avalanche/test/util.h>
10 #include <test/util/setup_common.h>
11 
12 #include <boost/test/unit_test.hpp>
13 
14 #include <cstdint>
15 #include <limits>
16 #include <memory>
17 
18 using namespace avalanche;
19 
20 BOOST_FIXTURE_TEST_SUITE(proofcomparator_tests, TestingSetup)
21 
22 BOOST_AUTO_TEST_CASE(proof_shared_pointer_comparator) {
23  Chainstate &active_chainstate = Assert(m_node.chainman)->ActiveChainstate();
24  uint32_t score = MIN_VALID_PROOF_SCORE;
25 
26  auto proofMinScore =
27  buildRandomProof(active_chainstate, MIN_VALID_PROOF_SCORE);
28  auto proofMaxScore = buildRandomProof(active_chainstate,
29  std::numeric_limits<uint32_t>::max());
30 
31  const ProofComparatorByScore comparator;
32 
33  auto prevProof = proofMinScore;
34  for (size_t i = 0; i < 100; i++) {
35  score += 1000 + GetRand<int>(10000);
36  auto higherScoreProof = buildRandomProof(active_chainstate, score);
37  BOOST_CHECK(comparator(higherScoreProof, proofMinScore));
38  BOOST_CHECK(comparator(higherScoreProof, prevProof));
39  BOOST_CHECK(!comparator(higherScoreProof, proofMaxScore));
40  prevProof = higherScoreProof;
41  }
42 
43  // Decrement slower than we incremented, so we don't have to check whether
44  // the score reached the minimal value.
45  for (size_t i = 0; i < 100; i++) {
46  score -= 1 + GetRand<int>(100);
47  auto lowerScoreProof = buildRandomProof(active_chainstate, score);
48  BOOST_CHECK(comparator(lowerScoreProof, proofMinScore));
49  BOOST_CHECK(!comparator(lowerScoreProof, prevProof));
50  BOOST_CHECK(!comparator(lowerScoreProof, proofMaxScore));
51  prevProof = lowerScoreProof;
52  }
53 
54  for (size_t i = 0; i < 100; i++) {
55  auto anotherProofMinScore =
56  buildRandomProof(active_chainstate, MIN_VALID_PROOF_SCORE);
57  BOOST_CHECK_EQUAL(comparator(anotherProofMinScore, proofMinScore),
58  anotherProofMinScore->getId() <
59  proofMinScore->getId());
60  }
61 }
62 
63 BOOST_AUTO_TEST_CASE(proofref_comparator_by_address) {
64  Chainstate &active_chainstate = Assert(m_node.chainman)->ActiveChainstate();
65  std::vector<ProofRef> proofs;
66  for (size_t i = 0; i < 100; i++) {
67  auto proof = buildRandomProof(active_chainstate, MIN_VALID_PROOF_SCORE);
68  proofs.push_back(std::move(proof));
69  }
70 
71  std::set<ProofRef, ProofRefComparatorByAddress> sortedProofs(proofs.begin(),
72  proofs.end());
73 
74  uintptr_t prevAddr = 0;
75  for (const auto &p : sortedProofs) {
76  BOOST_CHECK_GE(reinterpret_cast<uintptr_t>(p.get()), prevAddr);
77  prevAddr = reinterpret_cast<uintptr_t>(p.get());
78  }
79 }
80 
#define Assert(val)
Identity function.
Definition: check.h:84
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:654
ProofRef buildRandomProof(Chainstate &active_chainstate, uint32_t score, int height, const CKey &masterKey)
Definition: util.cpp:20
constexpr uint32_t MIN_VALID_PROOF_SCORE
Definition: util.h:17
NodeContext & m_node
Definition: interfaces.cpp:762
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_CASE(proof_shared_pointer_comparator)
BOOST_FIXTURE_TEST_SUITE(stakingrewards_tests, StakingRewardsActivationTestingSetup) BOOST_AUTO_TEST_CASE(isstakingrewardsactivated)
Compare proofs by score, then by id in case of equality.