Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
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 <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>
15
16
#include <boost/test/unit_test.hpp>
17
18
#include <memory>
19
20
using
interfaces::BlockTemplate
;
21
using
interfaces::Mining
;
22
using
node::BlockWaitOptions
;
23
24
namespace
testnet4_miner_tests
{
25
26
struct
Testnet4MinerTestingSetup
:
public
Testnet4Setup
{
27
std::unique_ptr<Mining>
MakeMining
()
28
{
29
return
interfaces::MakeMining
(
m_node
,
/*wait_loaded=*/
false
);
30
}
31
};
32
}
// namespace testnet4_miner_tests
33
34
BOOST_FIXTURE_TEST_SUITE
(
testnet4_miner_tests
, Testnet4MinerTestingSetup)
35
36
BOOST_AUTO_TEST_CASE
(MiningInterface)
37
{
38
auto
mining{MakeMining()};
39
BOOST_REQUIRE(mining);
40
41
std::unique_ptr<BlockTemplate> block_template;
42
43
// Set node time a few minutes past the testnet4 genesis block
44
const
auto
template_time{3min +
WITH_LOCK
(
cs_main
,
return
m_node.chainman->ActiveChain().Tip()->Time())};
45
FakeNodeClock
clock{template_time};
46
47
block_template = mining->createNewBlock({},
/*cooldown=*/
false
);
48
BOOST_REQUIRE(block_template);
49
50
// The template should use the mocked system time
51
BOOST_REQUIRE_EQUAL(block_template->getBlockHeader().Time(), template_time);
52
53
const
BlockWaitOptions
wait_options{.timeout =
MillisecondsDouble
{0}, .fee_threshold = 1};
54
55
// waitNext() should return nullptr because there is no better template
56
auto
should_be_nullptr = block_template->waitNext(wait_options);
57
BOOST_REQUIRE(should_be_nullptr ==
nullptr
);
58
59
// This remains the case when exactly 20 minutes have gone by
60
clock += 17min;
61
should_be_nullptr = block_template->waitNext(wait_options);
62
BOOST_REQUIRE(should_be_nullptr ==
nullptr
);
63
64
// One second later the difficulty drops and it returns a new template
65
// Note that we can't test the actual difficulty change, because the
66
// difficulty is already at 1.
67
clock += 1s;
68
block_template = block_template->waitNext(wait_options);
69
BOOST_REQUIRE(block_template);
70
}
71
72
BOOST_AUTO_TEST_SUITE_END
()
block.h
FakeNodeClock
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition
time.h:54
interfaces::BlockTemplate
Block template interface.
Definition
mining.h:31
interfaces::Mining
Definition
mining.h:108
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
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(ipc_tests)
Definition
ipc_tests.cpp:202
mining_types.h
interfaces::MakeMining
std::unique_ptr< Mining > MakeMining(const node::NodeContext &node, bool wait_loaded=true)
Definition
interfaces.cpp:1089
testnet4_miner_tests
Definition
testnet4_miner_tests.cpp:24
setup_common.h
BasicTestingSetup::m_node
node::NodeContext m_node
Definition
setup_common.h:60
Testnet4Setup::Testnet4Setup
Testnet4Setup()
Definition
setup_common.h:129
node::BlockWaitOptions
Definition
mining_types.h:94
testnet4_miner_tests::Testnet4MinerTestingSetup
Definition
testnet4_miner_tests.cpp:26
testnet4_miner_tests::Testnet4MinerTestingSetup::MakeMining
std::unique_ptr< Mining > MakeMining()
Definition
testnet4_miner_tests.cpp:27
sync.h
WITH_LOCK
#define WITH_LOCK(cs, code)
Definition
sync.h:299
common.h
time.h
MillisecondsDouble
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition
time.h:103
Generated on
for Bitcoin Core by
1.18.0