Branch data Line data Source code
1 : : // Copyright (c) 2020-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 <test/util/validation.h>
6 : :
7 : : #include <coins.h>
8 : : #include <consensus/consensus.h>
9 : : #include <node/block_template_manager.h>
10 : : #include <node/blockstorage.h>
11 : : #include <node/mining_types.h>
12 : : #include <test/util/mining.h>
13 : : #include <test/util/script.h>
14 : : #include <test/util/setup_common.h>
15 : : #include <test/util/time.h>
16 : : #include <test/util/txmempool.h>
17 : : #include <txmempool.h>
18 : : #include <util/check.h>
19 : : #include <util/time.h>
20 : : #include <validation.h>
21 : : #include <validationinterface.h>
22 : :
23 : : #include <memory>
24 : : #include <utility>
25 : : #include <vector>
26 : :
27 : : using kernel::ChainstateRole;
28 : :
29 : 254 : void TestBlockManager::CleanupForFuzzing()
30 : : {
31 : 254 : m_dirty_blockindex.clear();
32 : 254 : m_dirty_fileinfo.clear();
33 : 254 : m_blockfile_info.resize(1);
34 : 254 : }
35 : :
36 : 8439 : void TestChainstateManager::DisableNextWrite()
37 : : {
38 : 8439 : struct TestChainstate : public Chainstate {
39 : 8439 : void ResetNextWrite() { m_next_write = NodeClock::time_point::max() - 1s; }
40 : : };
41 : 8439 : LOCK(::cs_main);
42 [ + + ]: 16878 : for (const auto& cs : m_chainstates) {
43 : 8439 : static_cast<TestChainstate&>(*cs).ResetNextWrite();
44 : : }
45 : 8439 : }
46 : :
47 : 12341 : void TestChainstateManager::ResetIbd()
48 : : {
49 : 12341 : m_cached_is_ibd = true;
50 [ - + ]: 12341 : assert(IsInitialBlockDownload());
51 : 12341 : }
52 : :
53 : 7318 : void TestChainstateManager::JumpOutOfIbd()
54 : : {
55 [ - + ]: 7318 : Assert(IsInitialBlockDownload());
56 : 7318 : m_cached_is_ibd = false;
57 [ - + ]: 7318 : Assert(!IsInitialBlockDownload());
58 : 7318 : }
59 : :
60 : 0 : void ValidationInterfaceTest::BlockConnected(
61 : : const ChainstateRole& role,
62 : : CValidationInterface& obj,
63 : : const std::shared_ptr<const CBlock>& block,
64 : : const CBlockIndex* pindex)
65 : : {
66 : 0 : obj.BlockConnected(role, block, pindex);
67 : 0 : }
68 : 3643 : void TestChainstateManager::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)
69 : : {
70 : 3643 : struct TestChainstate : public Chainstate {
71 : 3643 : void CallInvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
72 : : {
73 : 3643 : InvalidBlockFound(pindex, state);
74 : : }
75 : : };
76 : :
77 : 3643 : static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidBlockFound(pindex, state);
78 : 3643 : }
79 : :
80 : 956 : void TestChainstateManager::InvalidChainFound(CBlockIndex* pindexNew)
81 : : {
82 : 956 : struct TestChainstate : public Chainstate {
83 : 956 : void CallInvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
84 : : {
85 : 956 : InvalidChainFound(pindexNew);
86 : : }
87 : : };
88 : :
89 : 956 : static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidChainFound(pindexNew);
90 : 956 : }
91 : :
92 : 5179 : CBlockIndex* TestChainstateManager::FindMostWorkChain()
93 : : {
94 : 5179 : struct TestChainstate : public Chainstate {
95 : 5179 : CBlockIndex* CallFindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
96 : : {
97 : 5179 : return FindMostWorkChain();
98 : : }
99 : : };
100 : :
101 : 5179 : return static_cast<TestChainstate*>(&ActiveChainstate())->CallFindMostWorkChain();
102 : : }
103 : :
104 : 254 : void TestChainstateManager::ResetBestInvalid()
105 : : {
106 : 254 : m_best_invalid = nullptr;
107 : 254 : }
108 : :
109 : 1233 : std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup, FakeNodeClock& node_clock)
110 : : {
111 : 1233 : node_clock.set(setup.m_node.chainman->GetParams().GenesisBlock().Time());
112 : :
113 [ + - ]: 1233 : bilingual_str error{};
114 [ + - ]: 1233 : setup.m_node.block_template_manager.reset();
115 [ + - ]: 1233 : setup.m_node.mempool.reset();
116 [ + - + - ]: 1233 : setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);
117 [ - + ]: 1233 : Assert(error.empty());
118 : :
119 [ + - ]: 1233 : setup.m_node.chainman.reset();
120 [ + - ]: 1233 : setup.m_make_chainman();
121 [ + - ]: 1233 : setup.LoadVerifyActivateChainstate();
122 [ + - ]: 1233 : setup.CreateBlockTemplateManager();
123 : :
124 [ + - ]: 1233 : node::BlockCreateOptions options;
125 : 1233 : options.coinbase_output_script = P2WSH_OP_TRUE;
126 : :
127 : 1233 : std::vector<std::pair<COutPoint, CAmount>> mature_coinbase;
128 [ + + ]: 247833 : for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
129 [ + - ]: 246600 : COutPoint prevout{MineBlock(setup.m_node, options)};
130 [ + + ]: 246600 : if (i < COINBASE_MATURITY) {
131 [ + - ]: 123300 : LOCK(cs_main);
132 [ + - + - : 123300 : CAmount subsidy{setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};
+ - ]
133 [ + - ]: 123300 : mature_coinbase.emplace_back(prevout, subsidy);
134 : 123300 : }
135 : : }
136 : 1233 : return mature_coinbase;
137 : 2466 : }
|