Line data Source code
1 : // Copyright (c) 2019 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 <key_io.h> 10 : #include <miner.h> 11 : #include <node/context.h> 12 : #include <pow.h> 13 : #include <script/standard.h> 14 : #include <util/check.h> 15 : #include <validation.h> 16 : 17 800 : CTxIn generatetoaddress(const NodeContext& node, const std::string& address) 18 : { 19 800 : const auto dest = DecodeDestination(address); 20 800 : assert(IsValidDestination(dest)); 21 800 : const auto coinbase_script = GetScriptForDestination(dest); 22 : 23 800 : return MineBlock(node, coinbase_script); 24 800 : } 25 : 26 1000 : CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey) 27 : { 28 1000 : auto block = PrepareBlock(node, coinbase_scriptPubKey); 29 : 30 2124 : while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) { 31 1124 : ++block->nNonce; 32 1124 : assert(block->nNonce); 33 : } 34 : 35 2000 : bool processed{Assert(node.chainman)->ProcessNewBlock(Params(), block, true, nullptr)}; 36 1000 : assert(processed); 37 : 38 1000 : return CTxIn{block->vtx[0]->GetHash(), 0}; 39 1000 : } 40 : 41 1011 : std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey) 42 : { 43 1011 : auto block = std::make_shared<CBlock>( 44 2022 : BlockAssembler{*Assert(node.mempool), Params()} 45 1011 : .CreateNewBlock(coinbase_scriptPubKey) 46 1011 : ->block); 47 : 48 1011 : LOCK(cs_main); 49 1011 : block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1; 50 1011 : block->hashMerkleRoot = BlockMerkleRoot(*block); 51 : 52 : return block; 53 1011 : }