Bitcoin ABC 0.31.2
P2P Digital Currency
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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#include <validation.h>
9
10#include <avalanche/test/util.h>
11#include <test/util/setup_common.h>
12
13#include <boost/test/unit_test.hpp>
14
15#include <cstdint>
16#include <limits>
17#include <memory>
18
19using namespace avalanche;
20
21BOOST_FIXTURE_TEST_SUITE(proofcomparator_tests, TestingSetup)
22
23BOOST_AUTO_TEST_CASE(proof_shared_pointer_comparator) {
24 Chainstate &active_chainstate = Assert(m_node.chainman)->ActiveChainstate();
25 uint32_t score = MIN_VALID_PROOF_SCORE;
26
27 auto proofMinScore =
28 buildRandomProof(active_chainstate, MIN_VALID_PROOF_SCORE);
29 auto proofMaxScore = buildRandomProof(active_chainstate,
30 std::numeric_limits<uint32_t>::max());
31
32 const ProofComparatorByScore comparator;
33
34 auto prevProof = proofMinScore;
35 for (size_t i = 0; i < 100; i++) {
36 score += 1000 + GetRand<int>(10000);
37 auto higherScoreProof = buildRandomProof(active_chainstate, score);
38 BOOST_CHECK(comparator(higherScoreProof, proofMinScore));
39 BOOST_CHECK(comparator(higherScoreProof, prevProof));
40 BOOST_CHECK(!comparator(higherScoreProof, proofMaxScore));
41 prevProof = higherScoreProof;
42 }
43
44 // Decrement slower than we incremented, so we don't have to check whether
45 // the score reached the minimal value.
46 for (size_t i = 0; i < 100; i++) {
47 score -= 1 + GetRand<int>(100);
48 auto lowerScoreProof = buildRandomProof(active_chainstate, score);
49 BOOST_CHECK(comparator(lowerScoreProof, proofMinScore));
50 BOOST_CHECK(!comparator(lowerScoreProof, prevProof));
51 BOOST_CHECK(!comparator(lowerScoreProof, proofMaxScore));
52 prevProof = lowerScoreProof;
53 }
54
55 for (size_t i = 0; i < 100; i++) {
56 auto anotherProofMinScore =
57 buildRandomProof(active_chainstate, MIN_VALID_PROOF_SCORE);
58 BOOST_CHECK_EQUAL(comparator(anotherProofMinScore, proofMinScore),
59 anotherProofMinScore->getId() <
60 proofMinScore->getId());
61 }
62}
63
64BOOST_AUTO_TEST_CASE(proofref_comparator_by_address) {
65 Chainstate &active_chainstate = Assert(m_node.chainman)->ActiveChainstate();
66 std::vector<ProofRef> proofs;
67 for (size_t i = 0; i < 100; i++) {
68 auto proof = buildRandomProof(active_chainstate, MIN_VALID_PROOF_SCORE);
69 proofs.push_back(std::move(proof));
70 }
71
72 std::set<ProofRef, ProofRefComparatorByAddress> sortedProofs(proofs.begin(),
73 proofs.end());
74
75 uintptr_t prevAddr = 0;
76 for (const auto &p : sortedProofs) {
77 BOOST_CHECK_GE(reinterpret_cast<uintptr_t>(p.get()), prevAddr);
78 prevAddr = reinterpret_cast<uintptr_t>(p.get());
79 }
80}
81
82BOOST_AUTO_TEST_SUITE_END()
#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:700
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:20
NodeContext & m_node
Definition: interfaces.cpp:815
#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)
Compare proofs by score, then by id in case of equality.