Bitcoin Core  22.99.0
P2P Digital Currency
mining.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2020 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 <test/util/script.h>
15 #include <util/check.h>
16 #include <validation.h>
17 #include <versionbits.h>
18 
19 CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
20 {
21  const auto dest = DecodeDestination(address);
23  const auto coinbase_script = GetScriptForDestination(dest);
24 
25  return MineBlock(node, coinbase_script);
26 }
27 
28 std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
29 {
30  std::vector<std::shared_ptr<CBlock>> ret{total_height};
31  auto time{params.GenesisBlock().nTime};
32  for (size_t height{0}; height < total_height; ++height) {
33  CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
34 
35  CMutableTransaction coinbase_tx;
36  coinbase_tx.vin.resize(1);
37  coinbase_tx.vin[0].prevout.SetNull();
38  coinbase_tx.vout.resize(1);
39  coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
40  coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
41  coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
42  block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
43 
44  block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
45  block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
46  block.hashMerkleRoot = BlockMerkleRoot(block);
47  block.nTime = ++time;
48  block.nBits = params.GenesisBlock().nBits;
49  block.nNonce = 0;
50 
51  while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
52  ++block.nNonce;
53  assert(block.nNonce);
54  }
55  }
56  return ret;
57 }
58 
59 CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
60 {
61  auto block = PrepareBlock(node, coinbase_scriptPubKey);
62 
63  while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
64  ++block->nNonce;
65  assert(block->nNonce);
66  }
67 
68  bool processed{Assert(node.chainman)->ProcessNewBlock(Params(), block, true, nullptr)};
69  assert(processed);
70 
71  return CTxIn{block->vtx[0]->GetHash(), 0};
72 }
73 
74 std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
75 {
76  auto block = std::make_shared<CBlock>(
77  BlockAssembler{Assert(node.chainman)->ActiveChainstate(), *Assert(node.mempool), Params()}
78  .CreateNewBlock(coinbase_scriptPubKey)
79  ->block);
80 
81  LOCK(cs_main);
82  block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
83  block->hashMerkleRoot = BlockMerkleRoot(*block);
84 
85  return block;
86 }
CTxIn
An input of a transaction.
Definition: transaction.h:65
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
GetBlockSubsidy
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
Definition: validation.cpp:1068
assert
assert(!tx.IsCoinBase())
check.h
pow.h
PrepareBlock
std::shared_ptr< CBlock > PrepareBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Prepare a block to be mined.
Definition: mining.cpp:74
CBlockHeader::nBits
uint32_t nBits
Definition: block.h:28
key_io.h
GetScriptForDestination
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:351
CChainParams
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:69
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CheckProofOfWork
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:74
CChainParams::GetConsensus
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:82
BlockAssembler
Generate a new block, without valid proof-of-work.
Definition: miner.h:126
chainparams.h
MineBlock
CTxIn MineBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:59
context.h
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
validation.h
script.h
OP_0
@ OP_0
Definition: script.h:69
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:373
miner.h
standard.h
versionbits.h
CBlockHeader::nTime
uint32_t nTime
Definition: block.h:27
CChainParams::GenesisBlock
const CBlock & GenesisBlock() const
Definition: chainparams.h:95
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:261
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
merkle.h
CBlock
Definition: block.h:62
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
generatetoaddress
CTxIn generatetoaddress(const NodeContext &node, const std::string &address)
RPC-like helper function, returns the generated coin.
Definition: mining.cpp:19
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:117
LOCK
#define LOCK(cs)
Definition: sync.h:226
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:536
node
Definition: interfaces.cpp:68
VERSIONBITS_LAST_OLD_BLOCK_VERSION
static const int32_t VERSIONBITS_LAST_OLD_BLOCK_VERSION
What block version to use for new blocks (pre versionbits)
Definition: versionbits.h:14
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:39
mining.h
BlockMerkleRoot
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
P2WSH_OP_TRUE
static const CScript P2WSH_OP_TRUE
Definition: script.h:12
CreateBlockChain
std::vector< std::shared_ptr< CBlock > > CreateBlockChain(size_t total_height, const CChainParams &params)
Create a blockchain, starting from genesis.
Definition: mining.cpp:28