2019-11-24 22:44:40 -03:00
|
|
|
// Copyright (c) 2019 The Bitcoin Core developers
|
|
|
|
// 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-16 21:11:44 -03:00
|
|
|
#include <node/context.h>
|
2019-11-24 22:44:40 -03:00
|
|
|
#include <pow.h>
|
|
|
|
#include <script/standard.h>
|
2020-06-08 08:47:10 -04:00
|
|
|
#include <util/check.h>
|
2019-11-24 22:44:40 -03:00
|
|
|
#include <validation.h>
|
|
|
|
|
2019-12-16 21:11:44 -03:00
|
|
|
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
|
2019-11-24 22:44:40 -03:00
|
|
|
{
|
|
|
|
const auto dest = DecodeDestination(address);
|
|
|
|
assert(IsValidDestination(dest));
|
|
|
|
const auto coinbase_script = GetScriptForDestination(dest);
|
|
|
|
|
2019-12-16 21:11:44 -03:00
|
|
|
return MineBlock(node, coinbase_script);
|
2019-11-24 22:44:40 -03:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:11:44 -03:00
|
|
|
CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
2019-11-24 22:44:40 -03:00
|
|
|
{
|
2019-12-16 21:11:44 -03:00
|
|
|
auto block = PrepareBlock(node, coinbase_scriptPubKey);
|
2019-11-24 22:44:40 -03: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-24 22:44:40 -03:00
|
|
|
assert(processed);
|
|
|
|
|
|
|
|
return CTxIn{block->vtx[0]->GetHash(), 0};
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:11:44 -03:00
|
|
|
std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
|
2019-11-24 22:44:40 -03:00
|
|
|
{
|
|
|
|
auto block = std::make_shared<CBlock>(
|
2020-06-08 08:47:10 -04:00
|
|
|
BlockAssembler{*Assert(node.mempool), Params()}
|
2019-11-24 22:44:40 -03:00
|
|
|
.CreateNewBlock(coinbase_scriptPubKey)
|
|
|
|
->block);
|
|
|
|
|
|
|
|
LOCK(cs_main);
|
|
|
|
block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1;
|
|
|
|
block->hashMerkleRoot = BlockMerkleRoot(*block);
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|