Bitcoin Core 31.99.0
P2P Digital Currency
testnet4_miner_tests.cpp
Go to the documentation of this file.
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>
10#include <test/util/time.h>
11#include <util/time.h>
12#include <validation.h>
13
14#include <boost/test/unit_test.hpp>
15
20
22
24 std::unique_ptr<Mining> MakeMining()
25 {
26 return interfaces::MakeMining(m_node, /*wait_loaded=*/false);
27 }
28};
29} // namespace testnet4_miner_tests
30
31BOOST_FIXTURE_TEST_SUITE(testnet4_miner_tests, Testnet4MinerTestingSetup)
32
33BOOST_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
node::NodeContext m_node
Definition: bitcoin-gui.cpp:47
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition: time.h:40
Block template interface.
Definition: mining.h:32
Interface giving clients (RPC, Stratum v2 Template Provider in the future) ability to create block te...
Definition: mining.h:97
Generate a new block, without valid proof-of-work.
Definition: miner.h:61
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::unique_ptr< Mining > MakeMining(node::NodeContext &node, bool wait_loaded=true)
Return implementation of Mining interface.
node::NodeContext m_node
Definition: setup_common.h:63
Identical to TestingSetup, but chain set to testnet4.
Definition: setup_common.h:131
MillisecondsDouble timeout
How long to wait before returning nullptr instead of a new template.
Definition: types.h:86
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:74
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:299
BOOST_AUTO_TEST_CASE(MiningInterface)
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:103