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) 2020-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 <chainparams.h>6#include <consensus/validation.h>7#include <interfaces/chain.h>8#include <test/util/common.h>9#include <test/util/setup_common.h>10#include <script/solver.h>11#include <validation.h>13#include <boost/test/unit_test.hpp>15using interfaces::FoundBlock;17BOOST_AUTO_TEST_SUITE(interfaces_tests)19BOOST_FIXTURE_TEST_CASE(findBlock, TestChain100Setup)20{21LOCK(Assert(m_node.chainman)->GetMutex());22auto& chain = m_node.chain;23const CChain& active = Assert(m_node.chainman)->ActiveChain();25uint256 hash;26BOOST_CHECK(chain->findBlock(active[10]->GetBlockHash(), FoundBlock().hash(hash)));27BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());29int height = -1;30BOOST_CHECK(chain->findBlock(active[20]->GetBlockHash(), FoundBlock().height(height)));31BOOST_CHECK_EQUAL(height, active[20]->nHeight);33CBlock data;34BOOST_CHECK(chain->findBlock(active[30]->GetBlockHash(), FoundBlock().data(data)));35BOOST_CHECK_EQUAL(data.GetHash(), active[30]->GetBlockHash());37int64_t time = -1;38BOOST_CHECK(chain->findBlock(active[40]->GetBlockHash(), FoundBlock().time(time)));39BOOST_CHECK_EQUAL(time, active[40]->GetBlockTime());41int64_t max_time = -1;42BOOST_CHECK(chain->findBlock(active[50]->GetBlockHash(), FoundBlock().maxTime(max_time)));43BOOST_CHECK_EQUAL(max_time, active[50]->GetBlockTimeMax());45int64_t mtp_time = -1;46BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), FoundBlock().mtpTime(mtp_time)));47BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast());49bool cur_active{false}, next_active{false};50uint256 next_hash;51BOOST_CHECK_EQUAL(active.Height(), 100);52BOOST_CHECK(chain->findBlock(active[99]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));53BOOST_CHECK(cur_active);54BOOST_CHECK(next_active);55BOOST_CHECK_EQUAL(next_hash, active[100]->GetBlockHash());56cur_active = next_active = false;57BOOST_CHECK(chain->findBlock(active[100]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));58BOOST_CHECK(cur_active);59BOOST_CHECK(!next_active);61BOOST_CHECK(!chain->findBlock({}, FoundBlock()));62}64BOOST_FIXTURE_TEST_CASE(findFirstBlockWithTimeAndHeight, TestChain100Setup)65{66LOCK(Assert(m_node.chainman)->GetMutex());67auto& chain = m_node.chain;68const CChain& active = Assert(m_node.chainman)->ActiveChain();69uint256 hash;70int height;71BOOST_CHECK(chain->findFirstBlockWithTimeAndHeight(/* min_time= */ 0, /* min_height= */ 5, FoundBlock().hash(hash).height(height)));72BOOST_CHECK_EQUAL(hash, active[5]->GetBlockHash());73BOOST_CHECK_EQUAL(height, 5);74BOOST_CHECK(!chain->findFirstBlockWithTimeAndHeight(/* min_time= */ active.Tip()->GetBlockTimeMax() + 1, /* min_height= */ 0));75}77BOOST_FIXTURE_TEST_CASE(findAncestorByHeight, TestChain100Setup)78{79LOCK(Assert(m_node.chainman)->GetMutex());80auto& chain = m_node.chain;81const CChain& active = Assert(m_node.chainman)->ActiveChain();82uint256 hash;83BOOST_CHECK(chain->findAncestorByHeight(active[20]->GetBlockHash(), 10, FoundBlock().hash(hash)));84BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());85BOOST_CHECK(!chain->findAncestorByHeight(active[10]->GetBlockHash(), 20));86}88BOOST_FIXTURE_TEST_CASE(findAncestorByHash, TestChain100Setup)89{90LOCK(Assert(m_node.chainman)->GetMutex());91auto& chain = m_node.chain;92const CChain& active = Assert(m_node.chainman)->ActiveChain();93int height = -1;94BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), active[10]->GetBlockHash(), FoundBlock().height(height)));95BOOST_CHECK_EQUAL(height, 10);96BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), active[20]->GetBlockHash()));97}99BOOST_FIXTURE_TEST_CASE(findCommonAncestor, TestChain100Setup)100{101auto& chain = m_node.chain;102const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};103auto* orig_tip = active.Tip();104for (int i = 0; i < 10; ++i) {105BlockValidationState state;106m_node.chainman->ActiveChainstate().InvalidateBlock(state, active.Tip());107}108BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight - 10);109coinbaseKey.MakeNewKey(true);110for (int i = 0; i < 20; ++i) {111CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));112}113BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight + 10);114uint256 fork_hash;115int fork_height;116int orig_height;117BOOST_CHECK(chain->findCommonAncestor(orig_tip->GetBlockHash(), active.Tip()->GetBlockHash(), FoundBlock().height(fork_height).hash(fork_hash), FoundBlock().height(orig_height)));118BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight);119BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10);120BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash());122uint256 active_hash, orig_hash;123BOOST_CHECK(!chain->findCommonAncestor(active.Tip()->GetBlockHash(), {}, {}, FoundBlock().hash(active_hash), {}));124BOOST_CHECK(!chain->findCommonAncestor({}, orig_tip->GetBlockHash(), {}, {}, FoundBlock().hash(orig_hash)));125BOOST_CHECK_EQUAL(active_hash, active.Tip()->GetBlockHash());126BOOST_CHECK_EQUAL(orig_hash, orig_tip->GetBlockHash());127}129BOOST_FIXTURE_TEST_CASE(hasBlocks, TestChain100Setup)130{131LOCK(::cs_main);132auto& chain = m_node.chain;133const CChain& active = Assert(m_node.chainman)->ActiveChain();135// Test ranges136BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));137BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));138BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));139BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));140BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));141active[5]->nStatus &= ~BLOCK_HAVE_DATA;142BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));143BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));144BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));145BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));146BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));147active[95]->nStatus &= ~BLOCK_HAVE_DATA;148BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));149BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));150BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));151BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));152BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));153active[50]->nStatus &= ~BLOCK_HAVE_DATA;154BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));155BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));156BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));157BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));158BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));160// Test edge cases161BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 49));162BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 5, 49));163BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 50));164}166BOOST_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.