Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2017-present The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or http://www.opensource.org/licenses/mit-license.php.5#include <chain.h>6#include <node/blockstorage.h>7#include <rpc/blockchain.h>8#include <sync.h>9#include <test/util/setup_common.h>10#include <util/string.h>12#include <boost/test/unit_test.hpp>14#include <cstdlib>16using util::ToString;18/* Equality between doubles is imprecise. Comparison should be done19* with a small threshold of tolerance, rather than exact equality.20*/21static bool DoubleEquals(double a, double b, double epsilon)22{23return std::abs(a - b) < epsilon;24}26static CBlockIndex* CreateBlockIndexWithNbits(uint32_t nbits)27{28CBlockIndex* block_index = new CBlockIndex();29block_index->nHeight = 46367;30block_index->nTime = 1269211443;31block_index->nBits = nbits;32return block_index;33}35static void RejectDifficultyMismatch(double difficulty, double expected_difficulty) {36BOOST_CHECK_MESSAGE(37DoubleEquals(difficulty, expected_difficulty, 0.00001),38"Difficulty was " + ToString(difficulty)39+ " but was expected to be " + ToString(expected_difficulty));40}42/* Given a BlockIndex with the provided nbits,43* verify that the expected difficulty results.44*/45static void TestDifficulty(uint32_t nbits, double expected_difficulty)46{47CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);48double difficulty = GetDifficulty(*block_index);49delete block_index;51RejectDifficultyMismatch(difficulty, expected_difficulty);52}54BOOST_FIXTURE_TEST_SUITE(blockchain_tests, BasicTestingSetup)56BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)57{58TestDifficulty(0x1f111111, 0.000001);59}61BOOST_AUTO_TEST_CASE(get_difficulty_for_low_target)62{63TestDifficulty(0x1ef88f6f, 0.000016);64}66BOOST_AUTO_TEST_CASE(get_difficulty_for_mid_target)67{68TestDifficulty(0x1df88f6f, 0.004023);69}71BOOST_AUTO_TEST_CASE(get_difficulty_for_high_target)72{73TestDifficulty(0x1cf88f6f, 1.029916);74}76BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)77{78TestDifficulty(0x12345678, 5913134931067755359633408.0);79}81//! Prune chain from height down to genesis block and check that82//! GetPruneHeight returns the correct value83static void CheckGetPruneHeight(const node::BlockManager& blockman, const CChain& chain, int height) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)84{85AssertLockHeld(::cs_main);87// Emulate pruning all blocks from `height` down to the genesis block88// by unsetting the `BLOCK_HAVE_DATA` flag from `nStatus`89for (CBlockIndex* it{chain[height]}; it != nullptr && it->nHeight > 0; it = it->pprev) {90it->nStatus &= ~BLOCK_HAVE_DATA;91}93const auto prune_height{GetPruneHeight(blockman, chain)};94BOOST_REQUIRE(prune_height.has_value());95BOOST_CHECK_EQUAL(*prune_height, height);96}98BOOST_FIXTURE_TEST_CASE(get_prune_height, TestChain100Setup)99{100LOCK(::cs_main);101const auto& chain = m_node.chainman->ActiveChain();102const auto& blockman = m_node.chainman->m_blockman;104// Fresh chain of 100 blocks without any pruned blocks, so std::nullopt should be returned105BOOST_CHECK(!GetPruneHeight(blockman, chain).has_value());107// Start pruning108CheckGetPruneHeight(blockman, chain, 1);109CheckGetPruneHeight(blockman, chain, 99);110CheckGetPruneHeight(blockman, chain, 100);111}113BOOST_AUTO_TEST_CASE(num_chain_tx_max)114{115CBlockIndex block_index{};116block_index.m_chain_tx_count = std::numeric_limits<uint64_t>::max();117BOOST_CHECK_EQUAL(block_index.m_chain_tx_count, std::numeric_limits<uint64_t>::max());118}120BOOST_FIXTURE_TEST_CASE(invalidate_block, TestChain100Setup)121{122const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};124// Check BlockStatus when doing InvalidateBlock()125BlockValidationState state;126auto* orig_tip = active.Tip();127int height_to_invalidate = orig_tip->nHeight - 10;128auto* tip_to_invalidate = active[height_to_invalidate];129m_node.chainman->ActiveChainstate().InvalidateBlock(state, tip_to_invalidate);131// tip_to_invalidate just got invalidated, so it's BLOCK_FAILED_VALID132WITH_LOCK(::cs_main, assert(tip_to_invalidate->nStatus & BLOCK_FAILED_VALID));134// check all ancestors of the invalidated block are validated up to BLOCK_VALID_TRANSACTIONS and are not invalid135auto pindex = tip_to_invalidate->pprev;136while (pindex) {137WITH_LOCK(::cs_main, assert(pindex->IsValid(BLOCK_VALID_TRANSACTIONS)));138WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0));139pindex = pindex->pprev;140}142// check all descendants of the invalidated block are BLOCK_FAILED_VALID143pindex = orig_tip;144while (pindex && pindex != tip_to_invalidate) {145WITH_LOCK(::cs_main, assert(pindex->nStatus & BLOCK_FAILED_VALID));146pindex = pindex->pprev;147}148}150BOOST_AUTO_TEST_SUITE_END()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.