Bitcoin Core  22.99.0
P2P Digital Currency
blockchain_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <boost/test/unit_test.hpp>
6 
7 #include <stdlib.h>
8 
9 #include <chain.h>
10 #include <rpc/blockchain.h>
11 #include <test/util/setup_common.h>
12 #include <util/string.h>
13 
14 /* Equality between doubles is imprecise. Comparison should be done
15  * with a small threshold of tolerance, rather than exact equality.
16  */
17 static bool DoubleEquals(double a, double b, double epsilon)
18 {
19  return std::abs(a - b) < epsilon;
20 }
21 
22 static CBlockIndex* CreateBlockIndexWithNbits(uint32_t nbits)
23 {
24  CBlockIndex* block_index = new CBlockIndex();
25  block_index->nHeight = 46367;
26  block_index->nTime = 1269211443;
27  block_index->nBits = nbits;
28  return block_index;
29 }
30 
31 static void RejectDifficultyMismatch(double difficulty, double expected_difficulty) {
32  BOOST_CHECK_MESSAGE(
33  DoubleEquals(difficulty, expected_difficulty, 0.00001),
34  "Difficulty was " + ToString(difficulty)
35  + " but was expected to be " + ToString(expected_difficulty));
36 }
37 
38 /* Given a BlockIndex with the provided nbits,
39  * verify that the expected difficulty results.
40  */
41 static void TestDifficulty(uint32_t nbits, double expected_difficulty)
42 {
43  CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);
44  double difficulty = GetDifficulty(block_index);
45  delete block_index;
46 
47  RejectDifficultyMismatch(difficulty, expected_difficulty);
48 }
49 
51 
52 BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
53 {
54  TestDifficulty(0x1f111111, 0.000001);
55 }
56 
57 BOOST_AUTO_TEST_CASE(get_difficulty_for_low_target)
58 {
59  TestDifficulty(0x1ef88f6f, 0.000016);
60 }
61 
62 BOOST_AUTO_TEST_CASE(get_difficulty_for_mid_target)
63 {
64  TestDifficulty(0x1df88f6f, 0.004023);
65 }
66 
67 BOOST_AUTO_TEST_CASE(get_difficulty_for_high_target)
68 {
69  TestDifficulty(0x1cf88f6f, 1.029916);
70 }
71 
72 BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)
73 {
74  TestDifficulty(0x12345678, 5913134931067755359633408.0);
75 }
76 
ToString
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:87
CBlockIndex::nTime
uint32_t nTime
Definition: chain.h:192
setup_common.h
string.h
CBlockIndex::nBits
uint32_t nBits
Definition: chain.h:193
CBlockIndex::nHeight
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:150
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
DoubleEquals
static bool DoubleEquals(double a, double b, double epsilon)
Definition: blockchain_tests.cpp:17
GetDifficulty
double GetDifficulty(const CBlockIndex *blockindex)
Get the difficulty of the net wrt to the given block index.
Definition: blockchain.cpp:115
BasicTestingSetup
Basic testing setup.
Definition: setup_common.h:76
CreateBlockIndexWithNbits
static CBlockIndex * CreateBlockIndexWithNbits(uint32_t nbits)
Definition: blockchain_tests.cpp:22
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
Definition: blockchain_tests.cpp:52
blockchain.h
RejectDifficultyMismatch
static void RejectDifficultyMismatch(double difficulty, double expected_difficulty)
Definition: blockchain_tests.cpp:31
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:137
TestDifficulty
static void TestDifficulty(uint32_t nbits, double expected_difficulty)
Definition: blockchain_tests.cpp:41