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