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 <index/base.h>
10 : : #include <index/blockfilterindex.h>
11 : : #include <interfaces/chain.h>
12 : : #include <node/block_template_manager.h>
13 : : #include <node/blockstorage.h>
14 : : #include <node/context.h>
15 : : #include <node/mining_types.h>
16 : : #include <test/util/mining.h>
17 : : #include <test/util/script.h>
18 : : #include <test/util/setup_common.h>
19 : : #include <test/util/time.h>
20 : : #include <test/util/txmempool.h>
21 : : #include <txmempool.h>
22 : : #include <util/byte_units.h>
23 : : #include <util/check.h>
24 : : #include <util/time.h>
25 : : #include <validation.h>
26 : : #include <validationinterface.h>
27 : :
28 : : #include <memory>
29 : : #include <utility>
30 : : #include <vector>
31 : :
32 : : using kernel::ChainstateRole;
33 : :
34 : 0 : void TestBlockManager::CleanupForFuzzing()
35 : : {
36 : 0 : m_dirty_blockindex.clear();
37 : 0 : m_dirty_fileinfo.clear();
38 : 0 : m_blockfile_info.resize(1);
39 : 0 : }
40 : :
41 : 0 : void TestChainstateManager::DisableNextWrite()
42 : : {
43 : 0 : struct TestChainstate : public Chainstate {
44 : 0 : void ResetNextWrite() { m_next_write = NodeClock::time_point::max() - 1s; }
45 : : };
46 : 0 : LOCK(::cs_main);
47 [ # # ]: 0 : for (const auto& cs : m_chainstates) {
48 : 0 : static_cast<TestChainstate&>(*cs).ResetNextWrite();
49 : : }
50 : 0 : }
51 : :
52 : 2 : void TestChainstateManager::ResetIbd()
53 : : {
54 : 2 : m_cached_is_ibd = true;
55 [ - + ]: 2 : assert(IsInitialBlockDownload());
56 : 2 : }
57 : :
58 : 1 : void TestChainstateManager::JumpOutOfIbd()
59 : : {
60 [ - + ]: 1 : Assert(IsInitialBlockDownload());
61 : 1 : m_cached_is_ibd = false;
62 [ - + ]: 1 : Assert(!IsInitialBlockDownload());
63 : 1 : }
64 : :
65 : 4 : void ValidationInterfaceTest::BlockConnected(
66 : : const ChainstateRole& role,
67 : : CValidationInterface& obj,
68 : : const std::shared_ptr<const CBlock>& block,
69 : : const CBlockIndex* pindex)
70 : : {
71 : 4 : obj.BlockConnected(role, block, pindex);
72 : 4 : }
73 : 0 : void TestChainstateManager::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state)
74 : : {
75 : 0 : struct TestChainstate : public Chainstate {
76 : 0 : void CallInvalidBlockFound(CBlockIndex* pindex, const BlockValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
77 : : {
78 : 0 : InvalidBlockFound(pindex, state);
79 : : }
80 : : };
81 : :
82 : 0 : static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidBlockFound(pindex, state);
83 : 0 : }
84 : :
85 : 0 : void TestChainstateManager::InvalidChainFound(CBlockIndex* pindexNew)
86 : : {
87 : 0 : struct TestChainstate : public Chainstate {
88 : 0 : void CallInvalidChainFound(CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
89 : : {
90 : 0 : InvalidChainFound(pindexNew);
91 : : }
92 : : };
93 : :
94 : 0 : static_cast<TestChainstate*>(&ActiveChainstate())->CallInvalidChainFound(pindexNew);
95 : 0 : }
96 : :
97 : 0 : CBlockIndex* TestChainstateManager::FindMostWorkChain()
98 : : {
99 : 0 : struct TestChainstate : public Chainstate {
100 : 0 : CBlockIndex* CallFindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
101 : : {
102 : 0 : return FindMostWorkChain();
103 : : }
104 : : };
105 : :
106 : 0 : return static_cast<TestChainstate*>(&ActiveChainstate())->CallFindMostWorkChain();
107 : : }
108 : :
109 : 0 : void TestChainstateManager::ResetBestInvalid()
110 : : {
111 : 0 : m_best_invalid = nullptr;
112 : 0 : }
113 : :
114 : 0 : std::vector<std::pair<COutPoint, CAmount>> ResetChainmanAndMempool(TestingSetup& setup, FakeNodeClock& node_clock, const ResetOptions& opts)
115 : : {
116 : 0 : node_clock.set(setup.m_node.chainman->GetParams().GenesisBlock().Time());
117 : :
118 : 0 : DestroyAllBlockFilterIndexes();
119 : :
120 [ # # ]: 0 : bilingual_str error{};
121 [ # # ]: 0 : setup.m_node.block_template_manager.reset();
122 [ # # ]: 0 : setup.m_node.mempool.reset();
123 [ # # # # ]: 0 : setup.m_node.mempool = std::make_unique<CTxMemPool>(MemPoolOptionsForTest(setup.m_node), error);
124 [ # # ]: 0 : Assert(error.empty());
125 : :
126 [ # # ]: 0 : setup.m_node.chainman.reset();
127 [ # # ]: 0 : setup.m_make_chainman();
128 [ # # ]: 0 : setup.LoadVerifyActivateChainstate();
129 [ # # ]: 0 : setup.CreateBlockTemplateManager();
130 : :
131 [ # # ]: 0 : node::BlockCreateOptions options;
132 : 0 : options.coinbase_output_script = P2WSH_OP_TRUE;
133 : :
134 : 0 : std::vector<std::pair<COutPoint, CAmount>> mature_coinbase;
135 [ # # ]: 0 : for (int i = 0; i < 2 * COINBASE_MATURITY; ++i) {
136 [ # # ]: 0 : COutPoint prevout{MineBlock(setup.m_node, options)};
137 [ # # ]: 0 : if (i < COINBASE_MATURITY) {
138 [ # # ]: 0 : LOCK(cs_main);
139 [ # # # # : 0 : CAmount subsidy{setup.m_node.chainman->ActiveChainstate().CoinsTip().GetCoin(prevout)->out.nValue};
# # ]
140 [ # # ]: 0 : mature_coinbase.emplace_back(prevout, subsidy);
141 : 0 : }
142 : : }
143 : :
144 [ # # ]: 0 : if (opts.init_block_filter_basic) {
145 [ # # # # ]: 0 : Assert(InitBlockFilterIndex([&] { return interfaces::MakeChain(setup.m_node); }, BlockFilterType::BASIC, 1_MiB, /*f_memory=*/true, /*f_wipe=*/true));
146 [ # # # # ]: 0 : auto& index = *Assert(GetBlockFilterIndex(BlockFilterType::BASIC));
147 [ # # # # ]: 0 : Assert(index.Init());
148 [ # # ]: 0 : index.Sync();
149 : : // The index serves the static setup chain only. Leaving it registered
150 : : // deadlocks with ImmediateBackgroundTaskRunner when a fuzzed block
151 : : // connects under cs_main.
152 [ # # ]: 0 : StopIndex(index, setup.m_node);
153 : : }
154 : :
155 : 0 : return mature_coinbase;
156 : 0 : }
157 : :
158 : 22 : void StopIndex(BaseIndex& index, node::NodeContext& node)
159 : : {
160 [ - + ]: 22 : Assert(node.validation_signals)->SyncWithValidationInterfaceQueue();
161 : 22 : index.Stop();
162 : 22 : }
|