Bitcoin Core
31.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
test
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 <
util/time.h
>
9
#include <validation.h>
10
11
#include <
test/util/setup_common.h
>
12
13
#include <boost/test/unit_test.hpp>
14
15
using
interfaces::BlockTemplate
;
16
using
interfaces::Mining
;
17
using
node::BlockAssembler
;
18
using
node::BlockWaitOptions
;
19
20
namespace
testnet4_miner_tests
{
21
22
struct
Testnet4MinerTestingSetup
:
public
Testnet4Setup
{
23
std::unique_ptr<Mining>
MakeMining
()
24
{
25
return
interfaces::MakeMining
(
m_node
,
/*wait_loaded=*/
false
);
26
}
27
};
28
}
// namespace testnet4_miner_tests
29
30
BOOST_FIXTURE_TEST_SUITE
(
testnet4_miner_tests
, Testnet4MinerTestingSetup)
31
32
BOOST_AUTO_TEST_CASE
(
MiningInterface
)
33
{
34
auto
mining{MakeMining()};
35
BOOST_REQUIRE
(mining);
36
37
BlockAssembler::Options options;
38
options.include_dummy_extranonce =
true
;
39
std::unique_ptr<BlockTemplate>
block_template
;
40
41
// Set node time a few minutes past the testnet4 genesis block
42
const
int64_t
genesis_time
{
WITH_LOCK
(
cs_main
,
return
m_node
.
chainman
->ActiveChain().Tip()->GetBlockTime())};
43
SetMockTime
(
genesis_time
+ 3 * 60);
44
45
block_template
= mining->createNewBlock(options,
/*cooldown=*/
false
);
46
BOOST_REQUIRE
(
block_template
);
47
48
// The template should use the mocked system time
49
BOOST_REQUIRE_EQUAL
(
block_template
->getBlockHeader().nTime,
genesis_time
+ 3 * 60);
50
51
const
BlockWaitOptions
wait_options
{.
timeout
=
MillisecondsDouble
{0}, .fee_threshold = 1};
52
53
// waitNext() should return nullptr because there is no better template
54
auto
should_be_nullptr
=
block_template
->waitNext(
wait_options
);
55
BOOST_REQUIRE
(
should_be_nullptr
==
nullptr
);
56
57
// This remains the case when exactly 20 minutes have gone by
58
{
59
LOCK
(
cs_main
);
60
SetMockTime
(
m_node
.
chainman
->ActiveChain().Tip()->GetBlockTime() + 20 * 60);
61
}
62
should_be_nullptr
=
block_template
->waitNext(
wait_options
);
63
BOOST_REQUIRE
(
should_be_nullptr
==
nullptr
);
64
65
// One second later the difficulty drops and it returns a new template
66
// Note that we can't test the actual difficulty change, because the
67
// difficulty is already at 1.
68
{
69
LOCK
(
cs_main
);
70
SetMockTime
(
m_node
.
chainman
->ActiveChain().Tip()->GetBlockTime() + 20 * 60 + 1);
71
}
72
block_template
=
block_template
->waitNext(
wait_options
);
73
BOOST_REQUIRE
(
block_template
);
74
}
75
76
BOOST_AUTO_TEST_SUITE_END
()
m_node
node::NodeContext m_node
Definition
bitcoin-gui.cpp:43
interfaces::BlockTemplate
Block template interface.
Definition
mining.h:32
interfaces::Mining
Interface giving clients (RPC, Stratum v2 Template Provider in the future) ability to create block te...
Definition
mining.h:97
node::BlockAssembler
Generate a new block, without valid proof-of-work.
Definition
miner.h:61
cs_main
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
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
mining.h
miner.h
interfaces::MakeMining
std::unique_ptr< Mining > MakeMining(node::NodeContext &node, bool wait_loaded=true)
Return implementation of Mining interface.
Definition
interfaces.cpp:1024
testnet4_miner_tests
Definition
testnet4_miner_tests.cpp:20
setup_common.h
Testnet4Setup
Identical to TestingSetup, but chain set to testnet4.
Definition
setup_common.h:134
node::BlockWaitOptions
Definition
types.h:81
node::BlockWaitOptions::timeout
MillisecondsDouble timeout
How long to wait before returning nullptr instead of a new template.
Definition
types.h:86
node::NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition
context.h:72
testnet4_miner_tests::Testnet4MinerTestingSetup
Definition
testnet4_miner_tests.cpp:22
testnet4_miner_tests::Testnet4MinerTestingSetup::MakeMining
std::unique_ptr< Mining > MakeMining()
Definition
testnet4_miner_tests.cpp:23
LOCK
#define LOCK(cs)
Definition
sync.h:258
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition
sync.h:289
system.h
time.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(MiningInterface)
Definition
testnet4_miner_tests.cpp:32
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition
time.cpp:44
MillisecondsDouble
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition
time.h:94
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:73
Generated on Thu Apr 16 2026 09:42:38 for Bitcoin Core by
1.10.0