Branch data Line data Source code
1 : : // Copyright (c) 2019-2022 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 <chainparams.h>
8 : : #include <consensus/merkle.h>
9 : : #include <consensus/validation.h>
10 : : #include <key_io.h>
11 : : #include <node/context.h>
12 : : #include <pow.h>
13 : : #include <primitives/transaction.h>
14 : : #include <test/util/script.h>
15 : : #include <util/check.h>
16 : : #include <validation.h>
17 : : #include <validationinterface.h>
18 : : #include <versionbits.h>
19 : :
20 : : using node::BlockAssembler;
21 : : using node::NodeContext;
22 : :
23 : 0 : COutPoint generatetoaddress(const NodeContext& node, const std::string& address)
24 : : {
25 : 0 : const auto dest = DecodeDestination(address);
26 [ # # # # ]: 0 : assert(IsValidDestination(dest));
27 [ # # ]: 0 : BlockAssembler::Options assembler_options;
28 [ # # ]: 0 : assembler_options.coinbase_output_script = GetScriptForDestination(dest);
29 : :
30 [ # # ]: 0 : return MineBlock(node, assembler_options);
31 : 0 : }
32 : :
33 : 0 : std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
34 : : {
35 : 0 : std::vector<std::shared_ptr<CBlock>> ret{total_height};
36 : 0 : auto time{params.GenesisBlock().nTime};
37 [ # # ]: 0 : for (size_t height{0}; height < total_height; ++height) {
38 [ # # # # ]: 0 : CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
39 : :
40 [ # # ]: 0 : CMutableTransaction coinbase_tx;
41 [ # # ]: 0 : coinbase_tx.vin.resize(1);
42 : 0 : coinbase_tx.vin[0].prevout.SetNull();
43 [ # # ]: 0 : coinbase_tx.vout.resize(1);
44 : 0 : coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
45 [ # # # # ]: 0 : coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
46 [ # # # # ]: 0 : coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
47 [ # # # # : 0 : block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
# # # # ]
48 : :
49 : 0 : block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
50 [ # # # # : 0 : block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
# # ]
51 [ # # ]: 0 : block.hashMerkleRoot = BlockMerkleRoot(block);
52 : 0 : block.nTime = ++time;
53 : 0 : block.nBits = params.GenesisBlock().nBits;
54 : 0 : block.nNonce = 0;
55 : :
56 [ # # # # : 0 : while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
# # ]
57 : 0 : ++block.nNonce;
58 [ # # ]: 0 : assert(block.nNonce);
59 : : }
60 : 0 : }
61 : 0 : return ret;
62 [ # # # # ]: 0 : }
63 : :
64 : 0 : COutPoint MineBlock(const NodeContext& node, const node::BlockAssembler::Options& assembler_options)
65 : : {
66 : 0 : auto block = PrepareBlock(node, assembler_options);
67 [ # # ]: 0 : auto valid = MineBlock(node, block);
68 [ # # ]: 0 : assert(!valid.IsNull());
69 [ # # ]: 0 : return valid;
70 : 0 : }
71 : :
72 : : struct BlockValidationStateCatcher : public CValidationInterface {
73 : : const uint256 m_hash;
74 : : std::optional<BlockValidationState> m_state;
75 : :
76 : 0 : BlockValidationStateCatcher(const uint256& hash)
77 : 0 : : m_hash{hash},
78 : 0 : m_state{} {}
79 : :
80 : : protected:
81 : 0 : void BlockChecked(const CBlock& block, const BlockValidationState& state) override
82 : : {
83 [ # # ]: 0 : if (block.GetHash() != m_hash) return;
84 : 0 : m_state = state;
85 : : }
86 : : };
87 : :
88 : 0 : COutPoint MineBlock(const NodeContext& node, std::shared_ptr<CBlock>& block)
89 : : {
90 [ # # ]: 0 : while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
91 [ # # ]: 0 : ++block->nNonce;
92 [ # # ]: 0 : assert(block->nNonce);
93 : : }
94 : :
95 : 0 : auto& chainman{*Assert(node.chainman)};
96 [ # # # # ]: 0 : const auto old_height = WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight());
97 : 0 : bool new_block;
98 [ # # ]: 0 : BlockValidationStateCatcher bvsc{block->GetHash()};
99 [ # # ]: 0 : node.validation_signals->RegisterValidationInterface(&bvsc);
100 [ # # # # : 0 : const bool processed{chainman.ProcessNewBlock(block, true, true, &new_block)};
# # ]
101 [ # # # # ]: 0 : const bool duplicate{!new_block && processed};
102 : 0 : assert(!duplicate);
103 [ # # ]: 0 : node.validation_signals->UnregisterValidationInterface(&bvsc);
104 [ # # ]: 0 : node.validation_signals->SyncWithValidationInterfaceQueue();
105 [ # # # # ]: 0 : const bool was_valid{bvsc.m_state && bvsc.m_state->IsValid()};
106 [ # # # # : 0 : assert(old_height + was_valid == WITH_LOCK(chainman.GetMutex(), return chainman.ActiveHeight()));
# # # # ]
107 : :
108 [ # # ]: 0 : if (was_valid) return {block->vtx[0]->GetHash(), 0};
109 : 0 : return {};
110 : 0 : }
111 : :
112 : 0 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node,
113 : : const BlockAssembler::Options& assembler_options)
114 : : {
115 : 0 : auto block = std::make_shared<CBlock>(
116 : 0 : BlockAssembler{Assert(node.chainman)->ActiveChainstate(), Assert(node.mempool.get()), assembler_options}
117 [ # # ]: 0 : .CreateNewBlock()
118 [ # # ]: 0 : ->block);
119 : :
120 [ # # ]: 0 : LOCK(cs_main);
121 [ # # # # : 0 : block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
# # # # ]
122 [ # # ]: 0 : block->hashMerkleRoot = BlockMerkleRoot(*block);
123 : :
124 [ # # ]: 0 : return block;
125 : 0 : }
126 : 0 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
127 : : {
128 : 0 : BlockAssembler::Options assembler_options;
129 : 0 : assembler_options.coinbase_output_script = coinbase_scriptPubKey;
130 [ # # ]: 0 : ApplyArgsManOptions(*node.args, assembler_options);
131 [ # # ]: 0 : return PrepareBlock(node, assembler_options);
132 : 0 : }
|