Bitcoin Core 31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
blockchain_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2017-present 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 <chain.h>
6#include <node/blockstorage.h>
7#include <rpc/blockchain.h>
8#include <sync.h>
10#include <util/string.h>
11
12#include <boost/test/unit_test.hpp>
13
14#include <cstdlib>
15
16using 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 */
21static bool DoubleEquals(double a, double b, double epsilon)
22{
23 return std::abs(a - b) < epsilon;
24}
25
27{
29 block_index->nHeight = 46367;
30 block_index->nTime = 1269211443;
31 block_index->nBits = nbits;
32 return block_index;
33}
34
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 */
53
55
60
65
70
75
77{
78 TestDifficulty(0x12345678, 5913134931067755359633408.0);
79}
80
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());
96}
97
99{
101 const auto& chain = m_node.chainman->ActiveChain();
102 const 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
114{
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
121{
122 const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
123
124 // Check BlockStatus when doing InvalidateBlock()
126 auto* orig_tip = active.Tip();
127 int height_to_invalidate = orig_tip->nHeight - 10;
129 m_node.chainman->ActiveChainstate().InvalidateBlock(state, tip_to_invalidate);
130
131 // tip_to_invalidate just got invalidated, so it's BLOCK_FAILED_VALID
133
134 // check all ancestors of the invalidated block are validated up to BLOCK_VALID_TRANSACTIONS and are not invalid
135 auto pindex = tip_to_invalidate->pprev;
136 while (pindex) {
138 WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0));
139 pindex = pindex->pprev;
140 }
141
142 // check all descendants of the invalidated block are BLOCK_FAILED_VALID
143 pindex = orig_tip;
144 while (pindex && pindex != tip_to_invalidate) {
145 WITH_LOCK(::cs_main, assert(pindex->nStatus & BLOCK_FAILED_VALID));
146 pindex = pindex->pprev;
147 }
148}
149
node::NodeContext m_node
double GetDifficulty(const CBlockIndex &blockindex)
Get the difficulty of the net wrt to the given block index.
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.
BOOST_FIXTURE_TEST_CASE(get_prune_height, TestChain100Setup)
static bool DoubleEquals(double a, double b, double epsilon)
static CBlockIndex * CreateBlockIndexWithNbits(uint32_t nbits)
static void TestDifficulty(uint32_t nbits, double expected_difficulty)
BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
static void CheckGetPruneHeight(const node::BlockManager &blockman, const CChain &chain, int height) EXCLUSIVE_LOCKS_REQUIRED(
Prune chain from height down to genesis block and check that GetPruneHeight returns the correct value...
static void RejectDifficultyMismatch(double difficulty, double expected_difficulty)
@ BLOCK_VALID_TRANSACTIONS
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid,...
Definition chain.h:61
@ BLOCK_FAILED_VALID
stage after last reached validness failed
Definition chain.h:79
#define Assert(val)
Identity function.
Definition check.h:113
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition chain.h:94
uint64_t m_chain_tx_count
(memory only) Number of transactions in the chain up to and including this block.
Definition chain.h:129
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition chain.h:106
An in-memory indexed chain of blocks.
Definition chain.h:380
Maintains a tree of blocks (stored in m_block_index) which is consulted to determine where the most-w...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition cs_main.cpp:8
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition string.h:246
#define BOOST_CHECK_EQUAL(v1, v2)
Definition object.cpp:17
#define BOOST_CHECK(expr)
Definition object.cpp:16
Basic testing setup.
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
std::unique_ptr< ChainstateManager > chainman
Definition context.h:72
#define LOCK(cs)
Definition sync.h:258
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition sync.h:289
#define AssertLockHeld(cs)
Definition sync.h:136
#define EXCLUSIVE_LOCKS_REQUIRED(...)
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:73
assert(!tx.IsCoinBase())