aboutsummaryrefslogtreecommitdiff
path: root/src/test/util/mining.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/util/mining.cpp')
-rw-r--r--src/test/util/mining.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/test/util/mining.cpp b/src/test/util/mining.cpp
index 3fc3329da2..1204873828 100644
--- a/src/test/util/mining.cpp
+++ b/src/test/util/mining.cpp
@@ -11,8 +11,10 @@
#include <node/context.h>
#include <pow.h>
#include <script/standard.h>
+#include <test/util/script.h>
#include <util/check.h>
#include <validation.h>
+#include <versionbits.h>
CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
{
@@ -23,6 +25,37 @@ CTxIn generatetoaddress(const NodeContext& node, const std::string& address)
return MineBlock(node, coinbase_script);
}
+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;
+}
+
CTxIn MineBlock(const NodeContext& node, const CScript& coinbase_scriptPubKey)
{
auto block = PrepareBlock(node, coinbase_scriptPubKey);