Bitcoin Core  29.1.0
P2P Digital Currency
blockchain_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017-2022 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 <chain.h>
8 #include <node/blockstorage.h>
9 #include <rpc/blockchain.h>
10 #include <sync.h>
11 #include <test/util/setup_common.h>
12 #include <util/string.h>
13 
14 #include <cstdlib>
15 
16 using util::ToString;
17 
18 /* Equality between doubles is imprecise. Comparison should be done
19  * with a small threshold of tolerance, rather than exact equality.
20  */
21 static bool DoubleEquals(double a, double b, double epsilon)
22 {
23  return std::abs(a - b) < epsilon;
24 }
25 
26 static CBlockIndex* CreateBlockIndexWithNbits(uint32_t nbits)
27 {
28  CBlockIndex* block_index = new CBlockIndex();
29  block_index->nHeight = 46367;
30  block_index->nTime = 1269211443;
31  block_index->nBits = nbits;
32  return block_index;
33 }
34 
35 static void RejectDifficultyMismatch(double difficulty, double expected_difficulty) {
36  BOOST_CHECK_MESSAGE(
37  DoubleEquals(difficulty, expected_difficulty, 0.00001),
38  "Difficulty was " + ToString(difficulty)
39  + " but was expected to be " + ToString(expected_difficulty));
40 }
41 
42 /* Given a BlockIndex with the provided nbits,
43  * verify that the expected difficulty results.
44  */
45 static void TestDifficulty(uint32_t nbits, double expected_difficulty)
46 {
47  CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);
48  double difficulty = GetDifficulty(*block_index);
49  delete block_index;
50 
51  RejectDifficultyMismatch(difficulty, expected_difficulty);
52 }
53 
55 
56 BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
57 {
58  TestDifficulty(0x1f111111, 0.000001);
59 }
60 
61 BOOST_AUTO_TEST_CASE(get_difficulty_for_low_target)
62 {
63  TestDifficulty(0x1ef88f6f, 0.000016);
64 }
65 
66 BOOST_AUTO_TEST_CASE(get_difficulty_for_mid_target)
67 {
68  TestDifficulty(0x1df88f6f, 0.004023);
69 }
70 
71 BOOST_AUTO_TEST_CASE(get_difficulty_for_high_target)
72 {
73  TestDifficulty(0x1cf88f6f, 1.029916);
74 }
75 
76 BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)
77 {
78  TestDifficulty(0x12345678, 5913134931067755359633408.0);
79 }
80 
83 static void CheckGetPruneHeight(node::BlockManager& blockman, CChain& chain, int height) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
84 {
86 
87  // Emulate pruning all blocks from `height` down to the genesis block
88  // by unsetting the `BLOCK_HAVE_DATA` flag from `nStatus`
89  for (CBlockIndex* it{chain[height]}; it != nullptr && it->nHeight > 0; it = it->pprev) {
90  it->nStatus &= ~BLOCK_HAVE_DATA;
91  }
92 
93  const auto prune_height{GetPruneHeight(blockman, chain)};
94  BOOST_REQUIRE(prune_height.has_value());
95  BOOST_CHECK_EQUAL(*prune_height, height);
96 }
97 
99 {
100  LOCK(::cs_main);
101  auto& chain = m_node.chainman->ActiveChain();
102  auto& blockman = m_node.chainman->m_blockman;
103 
104  // Fresh chain of 100 blocks without any pruned blocks, so std::nullopt should be returned
105  BOOST_CHECK(!GetPruneHeight(blockman, chain).has_value());
106 
107  // Start pruning
108  CheckGetPruneHeight(blockman, chain, 1);
109  CheckGetPruneHeight(blockman, chain, 99);
110  CheckGetPruneHeight(blockman, chain, 100);
111 }
112 
113 BOOST_AUTO_TEST_CASE(num_chain_tx_max)
114 {
115  CBlockIndex block_index{};
116  block_index.m_chain_tx_count = std::numeric_limits<uint64_t>::max();
117  BOOST_CHECK_EQUAL(block_index.m_chain_tx_count, std::numeric_limits<uint64_t>::max());
118 }
119 
AssertLockHeld(pool.cs)
node::NodeContext m_node
Definition: bitcoin-gui.cpp:42
An in-memory indexed chain of blocks.
Definition: chain.h:416
uint32_t nTime
Definition: chain.h:189
static bool DoubleEquals(double a, double b, double epsilon)
Basic testing setup.
Definition: setup_common.h:64
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block. ...
Definition: chain.h:176
#define LOCK(cs)
Definition: sync.h:257
BOOST_AUTO_TEST_SUITE_END()
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
Definition: blockstorage.h:138
std::optional< int > GetPruneHeight(const BlockManager &blockman, const CChain &chain)
Return height of highest block that has been pruned, or std::nullopt if no blocks have been pruned...
Definition: blockchain.cpp:814
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:140
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
static CBlockIndex * CreateBlockIndexWithNbits(uint32_t nbits)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:140
double GetDifficulty(const CBlockIndex &blockindex)
Get the difficulty of the net wrt to the given block index.
Definition: blockchain.cpp:91
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:153
static void TestDifficulty(uint32_t nbits, double expected_difficulty)
static void CheckGetPruneHeight(node::BlockManager &blockman, CChain &chain, int height) EXCLUSIVE_LOCKS_REQUIRED(
Prune chain from height down to genesis block and check that GetPruneHeight returns the correct value...
full block available in blk*.dat
Definition: chain.h:121
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
static void RejectDifficultyMismatch(double difficulty, double expected_difficulty)
uint32_t nBits
Definition: chain.h:190
BOOST_FIXTURE_TEST_CASE(get_prune_height, TestChain100Setup)
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:233
#define BOOST_CHECK(expr)
Definition: object.cpp:17