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) 2025-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 <interfaces/mining.h>7#include <node/mining_types.h>8#include <primitives/block.h>9#include <sync.h>10#include <test/util/common.h>11#include <test/util/setup_common.h>12#include <test/util/time.h>13#include <util/time.h>14#include <validation.h>16#include <boost/test/unit_test.hpp>18#include <memory>20using interfaces::BlockTemplate;21using interfaces::Mining;22using node::BlockWaitOptions;24namespace testnet4_miner_tests {26struct Testnet4MinerTestingSetup : public Testnet4Setup {27std::unique_ptr<Mining> MakeMining()28{29return interfaces::MakeMining(m_node, /*wait_loaded=*/false);30}31};32} // namespace testnet4_miner_tests34BOOST_FIXTURE_TEST_SUITE(testnet4_miner_tests, Testnet4MinerTestingSetup)36BOOST_AUTO_TEST_CASE(MiningInterface)37{38auto mining{MakeMining()};39BOOST_REQUIRE(mining);41std::unique_ptr<BlockTemplate> block_template;43// Set node time a few minutes past the testnet4 genesis block44const auto template_time{3min + WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->Time())};45FakeNodeClock clock{template_time};47block_template = mining->createNewBlock({}, /*cooldown=*/false);48BOOST_REQUIRE(block_template);50// The template should use the mocked system time51BOOST_REQUIRE_EQUAL(block_template->getBlockHeader().Time(), template_time);53const BlockWaitOptions wait_options{.timeout = MillisecondsDouble{0}, .fee_threshold = 1};55// waitNext() should return nullptr because there is no better template56auto should_be_nullptr = block_template->waitNext(wait_options);57BOOST_REQUIRE(should_be_nullptr == nullptr);59// This remains the case when exactly 20 minutes have gone by60clock += 17min;61should_be_nullptr = block_template->waitNext(wait_options);62BOOST_REQUIRE(should_be_nullptr == nullptr);64// One second later the difficulty drops and it returns a new template65// Note that we can't test the actual difficulty change, because the66// difficulty is already at 1.67clock += 1s;68block_template = block_template->waitNext(wait_options);69BOOST_REQUIRE(block_template);70}72BOOST_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.