Branch data Line data Source code
1 : : // Copyright (c) 2019-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/mining.h>
6 : :
7 : : #include <addresstype.h>
8 : : #include <chain.h>
9 : : #include <chainparams.h>
10 : : #include <consensus/merkle.h>
11 : : #include <consensus/validation.h>
12 : : #include <key_io.h>
13 : : #include <node/block_template_manager.h>
14 : : #include <node/block_validation_state_catcher.h>
15 : : #include <node/context.h>
16 : : #include <node/miner.h>
17 : : #include <pow.h>
18 : : #include <primitives/block.h>
19 : : #include <primitives/transaction.h>
20 : : #include <script/script.h>
21 : : #include <sync.h>
22 : : #include <test/util/script.h>
23 : : #include <uint256.h>
24 : : #include <util/check.h>
25 : : #include <validation.h>
26 : : #include <validationinterface.h>
27 : : #include <versionbits.h>
28 : :
29 : : #include <cstdint>
30 : : #include <memory>
31 : : #include <optional>
32 : : #include <utility>
33 : :
34 : : using node::NodeContext;
35 : :
36 : 0 : COutPoint generatetoaddress(const NodeContext& node, const std::string& address)
37 : : {
38 : 0 : const auto dest = DecodeDestination(address);
39 [ # # # # ]: 0 : assert(IsValidDestination(dest));
40 [ # # ]: 0 : return MineBlock(node, {
41 : : .coinbase_output_script = GetScriptForDestination(dest),
42 : 0 : });
43 [ # # ]: 0 : }
44 : :
45 : 0 : std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
46 : : {
47 : 0 : std::vector<std::shared_ptr<CBlock>> ret{total_height};
48 : 0 : auto time{params.GenesisBlock().nTime};
49 : : // NOTE: here `height` does not correspond to the block height but the block height - 1.
50 [ # # ]: 0 : for (size_t height{0}; height < total_height; ++height) {
51 [ # # # # ]: 0 : CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
52 : :
53 [ # # ]: 0 : CMutableTransaction coinbase_tx;
54 : 0 : coinbase_tx.nLockTime = static_cast<uint32_t>(height);
55 : 0 : coinbase_tx.vin = {
56 : : CTxIn{
57 : 0 : COutPoint{},
58 [ # # # # ]: 0 : CScript{} << (height + 1) << OP_0, // Always include OP_0 as a dummy extraNonce.
59 : : CTxIn::MAX_SEQUENCE_NONFINAL, // Make sure timelock is enforced.
60 : 0 : },
61 [ # # # # : 0 : };
# # ]
62 : 0 : coinbase_tx.vout = {
63 [ # # ]: 0 : CTxOut{GetBlockSubsidy(height + 1, params.GetConsensus()), P2WSH_OP_TRUE},
64 [ # # # # : 0 : };
# # ]
65 [ # # # # : 0 : block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
# # # # ]
66 : :
67 : 0 : block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
68 [ # # # # : 0 : block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
# # ]
69 [ # # ]: 0 : block.hashMerkleRoot = BlockMerkleRoot(block);
70 : 0 : block.nTime = ++time;
71 : 0 : block.nBits = params.GenesisBlock().nBits;
72 : 0 : block.nNonce = 0;
73 : :
74 [ # # # # : 0 : while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
# # ]
75 : 0 : ++block.nNonce;
76 [ # # ]: 0 : assert(block.nNonce);
77 : : }
78 : 0 : }
79 : 0 : return ret;
80 [ # # # # : 0 : }
# # # # #
# # # ]
81 : :
82 : 3 : bool BuildChain(const NodeContext& node, const CBlockIndex* pindex,
83 : : const CScript& coinbase_script_pub_key,
84 : : size_t length,
85 : : std::vector<std::shared_ptr<CBlock>>& chain)
86 : : {
87 : 3 : auto& block_template_manager{*Assert(node.block_template_manager)};
88 : 3 : const Consensus::Params& consensus{Assert(node.chainman)->GetConsensus()};
89 : :
90 : 3 : chain.resize(length);
91 [ + + ]: 26 : for (auto& chain_block : chain) {
92 : 23 : auto block_template{block_template_manager.CreateNewTemplate({
93 : : .use_mempool = false,
94 : : .coinbase_output_script = coinbase_script_pub_key,
95 : 0 : })};
96 [ + - + - ]: 23 : CBlock block{Assert(block_template)->block};
97 : :
98 : : // The template is built on the active tip, so repoint it at pindex and
99 : : // redo the fields that depend on the predecessor.
100 : 23 : block.hashPrevBlock = pindex->GetBlockHash();
101 : 23 : block.nTime = pindex->nTime + 1;
102 : 23 : {
103 [ + - + - ]: 23 : CMutableTransaction tx_coinbase{*block.vtx.at(0)};
104 : 23 : tx_coinbase.nLockTime = static_cast<uint32_t>(pindex->nHeight);
105 [ + - + - ]: 23 : tx_coinbase.vin.at(0).scriptSig = CScript{} << pindex->nHeight + 1;
106 [ + - + - : 46 : block.vtx.at(0) = MakeTransactionRef(std::move(tx_coinbase));
- + ]
107 [ + - ]: 23 : block.hashMerkleRoot = BlockMerkleRoot(block);
108 : 0 : }
109 : :
110 [ + - + - : 36 : while (!CheckProofOfWork(block.GetHash(), block.nBits, consensus)) ++block.nNonce;
+ + ]
111 : :
112 [ + - - + ]: 23 : chain_block = std::make_shared<CBlock>(std::move(block));
113 : :
114 [ + - ]: 23 : BlockValidationState state;
115 [ + - + - : 23 : if (!Assert(node.chainman)->ProcessNewBlockHeaders({{*chain_block}}, true, state, &pindex)) {
- + ]
116 : 0 : return false;
117 : : }
118 : 23 : }
119 : :
120 : : return true;
121 [ + - ]: 23 : }
122 : :
123 : 0 : COutPoint MineBlock(const NodeContext& node, const node::BlockCreateOptions& assembler_options)
124 : : {
125 : 0 : auto block = PrepareBlock(node, assembler_options);
126 [ # # ]: 0 : auto valid = MineBlock(node, block);
127 [ # # ]: 0 : assert(!valid.IsNull());
128 [ # # ]: 0 : return valid;
129 : 0 : }
130 : :
131 : 0 : COutPoint MineBlock(const NodeContext& node, std::shared_ptr<CBlock>& block)
132 : : {
133 [ # # ]: 0 : while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
134 [ # # ]: 0 : ++block->nNonce;
135 [ # # ]: 0 : assert(block->nNonce);
136 : : }
137 : :
138 : 0 : return ProcessBlock(node, block);
139 : : }
140 : :
141 : 0 : COutPoint ProcessBlock(const NodeContext& node, const std::shared_ptr<CBlock>& block)
142 : : {
143 : 0 : auto& chainman{*Assert(node.chainman)};
144 [ # # # # ]: 0 : const auto old_height = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight());
145 : 0 : bool new_block;
146 [ # # ]: 0 : node::BlockValidationStateCatcher bvsc{block->GetHash()};
147 [ # # ]: 0 : node.validation_signals->RegisterValidationInterface(&bvsc);
148 [ # # # # : 0 : const bool processed{chainman.ProcessNewBlock(block, true, true, &new_block)};
# # ]
149 [ # # # # ]: 0 : const bool duplicate{!new_block && processed};
150 : 0 : assert(!duplicate);
151 [ # # ]: 0 : node.validation_signals->UnregisterValidationInterface(&bvsc);
152 [ # # ]: 0 : node.validation_signals->SyncWithValidationInterfaceQueue();
153 [ # # # # ]: 0 : const bool was_valid{bvsc.m_state && bvsc.m_state->IsValid()};
154 [ # # # # : 0 : assert(old_height + was_valid == WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight()));
# # # # ]
155 : :
156 [ # # ]: 0 : if (was_valid) return {block->vtx[0]->GetHash(), 0};
157 : 0 : return {};
158 : 0 : }
159 : :
160 : 0 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node,
161 : : const node::BlockCreateOptions& assembler_options)
162 : : {
163 : 0 : auto& block_template_manager = *Assert(node.block_template_manager);
164 : 0 : auto block_template = block_template_manager.CreateNewTemplate(assembler_options);
165 [ # # # # ]: 0 : auto block = std::make_shared<CBlock>(Assert(block_template)->block);
166 : :
167 [ # # ]: 0 : LOCK(cs_main);
168 [ # # # # : 0 : block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
# # # # ]
169 [ # # ]: 0 : block->hashMerkleRoot = BlockMerkleRoot(*block);
170 : :
171 [ # # ]: 0 : return block;
172 : 0 : }
|