Bitcoin Core  22.99.0
P2P Digital Currency
mining.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019-2021 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 <node/context.h>
11 #include <node/miner.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 
20 using node::NodeContext;
21 
22 CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
23 {
24  const auto dest = DecodeDestination(address);
26  const auto coinbase_script = GetScriptForDestination(dest);
27 
28  return MineBlock(node, coinbase_script);
29 }
30 
31 std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
32 {
33  std::vector<std::shared_ptr<CBlock>> ret{total_height};
34  auto time{params.GenesisBlock().nTime};
35  for (size_t height{0}; height < total_height; ++height) {
36  CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
37 
38  CMutableTransaction coinbase_tx;
39  coinbase_tx.vin.resize(1);
40  coinbase_tx.vin[0].prevout.SetNull();
41  coinbase_tx.vout.resize(1);
42  coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
43  coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
44  coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
45  block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
46 
47  block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
48  block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
49  block.hashMerkleRoot = BlockMerkleRoot(block);
50  block.nTime = ++time;
51  block.nBits = params.GenesisBlock().nBits;
52  block.nNonce = 0;
53 
54  while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
55  ++block.nNonce;
56  assert(block.nNonce);
57  }
58  }
59  return ret;
60 }
61 
62 CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
63 {
64  auto block = PrepareBlock(node, coinbase_scriptPubKey);
65 
66  while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
67  ++block->nNonce;
68  assert(block->nNonce);
69  }
70 
71  bool processed{Assert(node.chainman)->ProcessNewBlock(Params(), block, true, nullptr)};
72  assert(processed);
73 
74  return CTxIn{block->vtx[0]->GetHash(), 0};
75 }
76 
77 std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
78 {
79  auto block = std::make_shared<CBlock>(
80  BlockAssembler{Assert(node.chainman)->ActiveChainstate(), *Assert(node.mempool), Params()}
81  .CreateNewBlock(coinbase_scriptPubKey)
82  ->block);
83 
84  LOCK(cs_main);
85  block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
86  block->hashMerkleRoot = BlockMerkleRoot(*block);
87 
88  return block;
89 }
CTxIn
An input of a transaction.
Definition: transaction.h:65
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:366
GetBlockSubsidy
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
Definition: validation.cpp:1414
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:77
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:310
node::NodeContext
NodeContext struct containing references to chain state and connection state.
Definition: context.h:40
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:407
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
chainparams.h
MineBlock
CTxIn MineBlock(const NodeContext &node, const CScript &coinbase_scriptPubKey)
Returns the generated coin.
Definition: mining.cpp:62
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:332
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
node::BlockAssembler
Generate a new block, without valid proof-of-work.
Definition: miner.h:128
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:367
generatetoaddress
CTxIn generatetoaddress(const NodeContext &node, const std::string &address)
RPC-like helper function, returns the generated coin.
Definition: mining.cpp:22
LOCK
#define LOCK(cs)
Definition: sync.h:226
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
Definition: key_io.cpp:281
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:561
node
Definition: init.h:22
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:364
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:138
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:31