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