testnet4_miner_tests.cpp
1 // Copyright (c) 2025-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 <common/system.h> 6 #include <interfaces/mining.h> 7 #include <node/miner.h> 8 #include <test/util/common.h> 9 #include <test/util/setup_common.h> 10 #include <test/util/time.h> 11 #include <util/time.h> 12 #include <validation.h> 13 14 #include <boost/test/unit_test.hpp> 15 16 using interfaces::BlockTemplate; 17 using interfaces::Mining; 18 using node::BlockAssembler; 19 using node::BlockWaitOptions; 20 21 namespace testnet4_miner_tests { 22 23 struct Testnet4MinerTestingSetup : public Testnet4Setup { 24 std::unique_ptr<Mining> MakeMining() 25 { 26 return interfaces::MakeMining(m_node, /*wait_loaded=*/false); 27 } 28 }; 29 } // namespace testnet4_miner_tests 30 31 BOOST_FIXTURE_TEST_SUITE(testnet4_miner_tests, Testnet4MinerTestingSetup) 32 33 BOOST_AUTO_TEST_CASE(MiningInterface) 34 { 35 auto mining{MakeMining()}; 36 BOOST_REQUIRE(mining); 37 38 BlockAssembler::Options options; 39 options.include_dummy_extranonce = true; 40 std::unique_ptr<BlockTemplate> block_template; 41 42 // Set node time a few minutes past the testnet4 genesis block 43 const auto template_time{3min + WITH_LOCK(cs_main, return m_node.chainman->ActiveChain().Tip()->Time())}; 44 NodeClockContext clock_ctx{template_time}; 45 46 block_template = mining->createNewBlock(options, /*cooldown=*/false); 47 BOOST_REQUIRE(block_template); 48 49 // The template should use the mocked system time 50 BOOST_REQUIRE_EQUAL(block_template->getBlockHeader().Time(), template_time); 51 52 const BlockWaitOptions wait_options{.timeout = MillisecondsDouble{0}, .fee_threshold = 1}; 53 54 // waitNext() should return nullptr because there is no better template 55 auto should_be_nullptr = block_template->waitNext(wait_options); 56 BOOST_REQUIRE(should_be_nullptr == nullptr); 57 58 // This remains the case when exactly 20 minutes have gone by 59 clock_ctx += 17min; 60 should_be_nullptr = block_template->waitNext(wait_options); 61 BOOST_REQUIRE(should_be_nullptr == nullptr); 62 63 // One second later the difficulty drops and it returns a new template 64 // Note that we can't test the actual difficulty change, because the 65 // difficulty is already at 1. 66 clock_ctx += 1s; 67 block_template = block_template->waitNext(wait_options); 68 BOOST_REQUIRE(block_template); 69 } 70 71 BOOST_AUTO_TEST_SUITE_END()