2020-12-31 09:48:25 +01:00
|
|
|
// Copyright (c) 2019-2020 The Bitcoin Core developers
|
2019-11-25 02:44:40 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <test/util/mining.h>
|
|
|
|
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <consensus/merkle.h>
|
|
|
|
#include <key_io.h>
|
|
|
|
#include <miner.h>
|
2019-12-17 07:11:44 +07:00
|
|
|
#include <node/context.h>
|
2019-11-25 02:44:40 +01:00
|
|
|
#include <pow.h>
|
|
|
|
#include <script/standard.h>
|
2021-04-03 13:39:42 +02:00
|
|
|
#include <test/util/script.h>
|
2020-06-08 08:47:10 -04:00
|
|
|
#include <util/check.h>
|
2019-11-25 02:44:40 +01:00
|
|
|
#include <validation.h>
|
2021-04-03 13:39:42 +02:00
|
|
|
#include <versionbits.h>
|
2019-11-25 02:44:40 +01:00
|
|
|
|
2019-12-17 07:11:44 +07:00
|
|
|
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
|
2019-11-25 02:44:40 +01:00
|
|
|
{
|
|
|
|
const auto dest = DecodeDestination(address);
|
|
|
|
assert(IsValidDestination(dest));
|
|
|
|
const auto coinbase_script = GetScriptForDestination(dest);
|
|
|
|
|
2019-12-17 07:11:44 +07:00
|
|
|
return MineBlock(node, coinbase_script);
|
2019-11-25 02:44:40 +01:00
|
|
|
}
|
|
|
|
|
2021-04-03 13:39:42 +02:00
|
|
|
std::vector<std::shared_ptr<CBlock>> CreateBlockChain(size_t total_height, const CChainParams& params)
|
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<CBlock>> ret{total_height};
|
|
|
|
auto time{params.GenesisBlock().nTime};
|
|
|
|
for (size_t height{0}; height < total_height; ++height) {
|
|
|
|
CBlock& block{*(ret.at(height) = std::make_shared<CBlock>())};
|
|
|
|
|
|
|
|
CMutableTransaction coinbase_tx;
|
|
|
|
coinbase_tx.vin.resize(1);
|
|
|
|
coinbase_tx.vin[0].prevout.SetNull();
|
|
|
|
coinbase_tx.vout.resize(1);
|
|
|
|
coinbase_tx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
|
|
|
|
coinbase_tx.vout[0].nValue = GetBlockSubsidy(height + 1, params.GetConsensus());
|
|
|
|
coinbase_tx.vin[0].scriptSig = CScript() << (height + 1) << OP_0;
|
|
|
|
block.vtx = {MakeTransactionRef(std::move(coinbase_tx))};
|
|
|
|
|
|
|
|
block.nVersion = VERSIONBITS_LAST_OLD_BLOCK_VERSION;
|
|
|
|
block.hashPrevBlock = (height >= 1 ? *ret.at(height - 1) : params.GenesisBlock()).GetHash();
|
|
|
|
block.hashMerkleRoot = BlockMerkleRoot(block);
|
|
|
|
block.nTime = ++time;
|
|
|
|
block.nBits = params.GenesisBlock().nBits;
|
|
|
|
block.nNonce = 0;
|
|
|
|
|
|
|
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, params.GetConsensus())) {
|
|
|
|
++block.nNonce;
|
|
|
|
assert(block.nNonce);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:11:44 +07:00
|
|
|
CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
2019-11-25 02:44:40 +01:00
|
|
|
{
|
2019-12-17 07:11:44 +07:00
|
|
|
auto block = PrepareBlock(node, coinbase_scriptPubKey);
|
2019-11-25 02:44:40 +01:00
|
|
|
|
|
|
|
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
|
|
|
|
++block->nNonce;
|
|
|
|
assert(block->nNonce);
|
|
|
|
}
|
|
|
|
|
2020-06-14 14:44:35 -04:00
|
|
|
bool processed{Assert(node.chainman)->ProcessNewBlock(Params(), block, true, nullptr)};
|
2019-11-25 02:44:40 +01:00
|
|
|
assert(processed);
|
|
|
|
|
|
|
|
return CTxIn{block->vtx[0]->GetHash(), 0};
|
|
|
|
}
|
|
|
|
|
2019-12-17 07:11:44 +07:00
|
|
|
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
2019-11-25 02:44:40 +01:00
|
|
|
{
|
|
|
|
auto block = std::make_shared<CBlock>(
|
2020-10-14 17:00:00 -04:00
|
|
|
BlockAssembler{Assert(node.chainman)->ActiveChainstate(), *Assert(node.mempool), Params()}
|
2021-03-16 19:32:10 -04:00
|
|
|
.CreateNewBlock(coinbase_scriptPubKey)
|
2019-11-25 02:44:40 +01:00
|
|
|
->block);
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
2020-10-14 17:00:00 -04:00
|
|
|
block->nTime = Assert(node.chainman)->ActiveChain().Tip()->GetMedianTimePast() + 1;
|
2019-11-25 02:44:40 +01:00
|
|
|
block->hashMerkleRoot = BlockMerkleRoot(*block);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|