mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-25 10:43:19 -03:00
[test] move mining helper functions into test library
This commit is contained in:
parent
2cb4e8bdc7
commit
f613e5dfda
7 changed files with 79 additions and 59 deletions
|
@ -10,6 +10,7 @@ EXTRA_LIBRARIES += \
|
||||||
TEST_UTIL_H = \
|
TEST_UTIL_H = \
|
||||||
test/util/blockfilter.h \
|
test/util/blockfilter.h \
|
||||||
test/util/logging.h \
|
test/util/logging.h \
|
||||||
|
test/util/mining.h \
|
||||||
test/util/setup_common.h \
|
test/util/setup_common.h \
|
||||||
test/util/str.h \
|
test/util/str.h \
|
||||||
test/util/transaction_utils.h
|
test/util/transaction_utils.h
|
||||||
|
@ -19,6 +20,7 @@ libtest_util_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
|
||||||
libtest_util_a_SOURCES = \
|
libtest_util_a_SOURCES = \
|
||||||
test/util/blockfilter.cpp \
|
test/util/blockfilter.cpp \
|
||||||
test/util/logging.cpp \
|
test/util/logging.cpp \
|
||||||
|
test/util/mining.cpp \
|
||||||
test/util/setup_common.cpp \
|
test/util/setup_common.cpp \
|
||||||
test/util/str.cpp \
|
test/util/str.cpp \
|
||||||
test/util/transaction_utils.cpp \
|
test/util/transaction_utils.cpp \
|
||||||
|
@ -28,4 +30,3 @@ LIBTEST_UTIL += $(LIBBITCOIN_SERVER)
|
||||||
LIBTEST_UTIL += $(LIBBITCOIN_COMMON)
|
LIBTEST_UTIL += $(LIBBITCOIN_COMMON)
|
||||||
LIBTEST_UTIL += $(LIBBITCOIN_UTIL)
|
LIBTEST_UTIL += $(LIBBITCOIN_UTIL)
|
||||||
LIBTEST_UTIL += $(LIBBITCOIN_CRYPTO_BASE)
|
LIBTEST_UTIL += $(LIBBITCOIN_CRYPTO_BASE)
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <consensus/validation.h>
|
#include <consensus/validation.h>
|
||||||
#include <crypto/sha256.h>
|
#include <crypto/sha256.h>
|
||||||
#include <test/util.h>
|
#include <test/util.h>
|
||||||
|
#include <test/util/mining.h>
|
||||||
#include <txmempool.h>
|
#include <txmempool.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include <node/context.h>
|
#include <node/context.h>
|
||||||
#include <optional.h>
|
#include <optional.h>
|
||||||
#include <test/util.h>
|
#include <test/util.h>
|
||||||
|
#include <test/util/mining.h>
|
||||||
#include <validationinterface.h>
|
#include <validationinterface.h>
|
||||||
#include <wallet/wallet.h>
|
#include <wallet/wallet.h>
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,9 @@
|
||||||
|
|
||||||
#include <test/util.h>
|
#include <test/util.h>
|
||||||
|
|
||||||
#include <chainparams.h>
|
|
||||||
#include <consensus/merkle.h>
|
|
||||||
#include <key_io.h>
|
#include <key_io.h>
|
||||||
#include <miner.h>
|
|
||||||
#include <outputtype.h>
|
#include <outputtype.h>
|
||||||
#include <pow.h>
|
|
||||||
#include <script/standard.h>
|
#include <script/standard.h>
|
||||||
#include <validation.h>
|
|
||||||
#include <validationinterface.h>
|
|
||||||
#ifdef ENABLE_WALLET
|
#ifdef ENABLE_WALLET
|
||||||
#include <wallet/wallet.h>
|
#include <wallet/wallet.h>
|
||||||
#endif
|
#endif
|
||||||
|
@ -44,41 +38,3 @@ void importaddress(CWallet& wallet, const std::string& address)
|
||||||
wallet.SetAddressBook(dest, /* label */ "", "receive");
|
wallet.SetAddressBook(dest, /* label */ "", "receive");
|
||||||
}
|
}
|
||||||
#endif // ENABLE_WALLET
|
#endif // ENABLE_WALLET
|
||||||
|
|
||||||
CTxIn generatetoaddress(const std::string& address)
|
|
||||||
{
|
|
||||||
const auto dest = DecodeDestination(address);
|
|
||||||
assert(IsValidDestination(dest));
|
|
||||||
const auto coinbase_script = GetScriptForDestination(dest);
|
|
||||||
|
|
||||||
return MineBlock(coinbase_script);
|
|
||||||
}
|
|
||||||
|
|
||||||
CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
|
|
||||||
{
|
|
||||||
auto block = PrepareBlock(coinbase_scriptPubKey);
|
|
||||||
|
|
||||||
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
|
|
||||||
++block->nNonce;
|
|
||||||
assert(block->nNonce);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
|
|
||||||
assert(processed);
|
|
||||||
|
|
||||||
return CTxIn{block->vtx[0]->GetHash(), 0};
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
|
|
||||||
{
|
|
||||||
auto block = std::make_shared<CBlock>(
|
|
||||||
BlockAssembler{Params()}
|
|
||||||
.CreateNewBlock(coinbase_scriptPubKey)
|
|
||||||
->block);
|
|
||||||
|
|
||||||
LOCK(cs_main);
|
|
||||||
block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1;
|
|
||||||
block->hashMerkleRoot = BlockMerkleRoot(*block);
|
|
||||||
|
|
||||||
return block;
|
|
||||||
}
|
|
||||||
|
|
|
@ -5,33 +5,19 @@
|
||||||
#ifndef BITCOIN_TEST_UTIL_H
|
#ifndef BITCOIN_TEST_UTIL_H
|
||||||
#define BITCOIN_TEST_UTIL_H
|
#define BITCOIN_TEST_UTIL_H
|
||||||
|
|
||||||
#include <memory>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class CBlock;
|
|
||||||
class CScript;
|
|
||||||
class CTxIn;
|
|
||||||
class CWallet;
|
class CWallet;
|
||||||
|
|
||||||
// Constants //
|
// Constants //
|
||||||
|
|
||||||
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
|
extern const std::string ADDRESS_BCRT1_UNSPENDABLE;
|
||||||
|
|
||||||
// Lower-level utils //
|
|
||||||
|
|
||||||
/** Returns the generated coin */
|
|
||||||
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
|
|
||||||
/** Prepare a block to be mined */
|
|
||||||
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
|
|
||||||
|
|
||||||
|
|
||||||
// RPC-like //
|
// RPC-like //
|
||||||
|
|
||||||
/** Import the address to the wallet */
|
/** Import the address to the wallet */
|
||||||
void importaddress(CWallet& wallet, const std::string& address);
|
void importaddress(CWallet& wallet, const std::string& address);
|
||||||
/** Returns a new address from the wallet */
|
/** Returns a new address from the wallet */
|
||||||
std::string getnewaddress(CWallet& w);
|
std::string getnewaddress(CWallet& w);
|
||||||
/** Returns the generated coin */
|
|
||||||
CTxIn generatetoaddress(const std::string& address);
|
|
||||||
|
|
||||||
#endif // BITCOIN_TEST_UTIL_H
|
#endif // BITCOIN_TEST_UTIL_H
|
||||||
|
|
51
src/test/util/mining.cpp
Normal file
51
src/test/util/mining.cpp
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
// 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>
|
||||||
|
#include <pow.h>
|
||||||
|
#include <script/standard.h>
|
||||||
|
#include <validation.h>
|
||||||
|
|
||||||
|
CTxIn generatetoaddress(const std::string& address)
|
||||||
|
{
|
||||||
|
const auto dest = DecodeDestination(address);
|
||||||
|
assert(IsValidDestination(dest));
|
||||||
|
const auto coinbase_script = GetScriptForDestination(dest);
|
||||||
|
|
||||||
|
return MineBlock(coinbase_script);
|
||||||
|
}
|
||||||
|
|
||||||
|
CTxIn MineBlock(const CScript& coinbase_scriptPubKey)
|
||||||
|
{
|
||||||
|
auto block = PrepareBlock(coinbase_scriptPubKey);
|
||||||
|
|
||||||
|
while (!CheckProofOfWork(block->GetHash(), block->nBits, Params().GetConsensus())) {
|
||||||
|
++block->nNonce;
|
||||||
|
assert(block->nNonce);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool processed{ProcessNewBlock(Params(), block, true, nullptr)};
|
||||||
|
assert(processed);
|
||||||
|
|
||||||
|
return CTxIn{block->vtx[0]->GetHash(), 0};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey)
|
||||||
|
{
|
||||||
|
auto block = std::make_shared<CBlock>(
|
||||||
|
BlockAssembler{Params()}
|
||||||
|
.CreateNewBlock(coinbase_scriptPubKey)
|
||||||
|
->block);
|
||||||
|
|
||||||
|
LOCK(cs_main);
|
||||||
|
block->nTime = ::ChainActive().Tip()->GetMedianTimePast() + 1;
|
||||||
|
block->hashMerkleRoot = BlockMerkleRoot(*block);
|
||||||
|
|
||||||
|
return block;
|
||||||
|
}
|
24
src/test/util/mining.h
Normal file
24
src/test/util/mining.h
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
#ifndef BITCOIN_TEST_UTIL_MINING_H
|
||||||
|
#define BITCOIN_TEST_UTIL_MINING_H
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class CBlock;
|
||||||
|
class CScript;
|
||||||
|
class CTxIn;
|
||||||
|
|
||||||
|
/** Returns the generated coin */
|
||||||
|
CTxIn MineBlock(const CScript& coinbase_scriptPubKey);
|
||||||
|
|
||||||
|
/** Prepare a block to be mined */
|
||||||
|
std::shared_ptr<CBlock> PrepareBlock(const CScript& coinbase_scriptPubKey);
|
||||||
|
|
||||||
|
/** RPC-like helper function, returns the generated coin */
|
||||||
|
CTxIn generatetoaddress(const std::string& address);
|
||||||
|
|
||||||
|
#endif // BITCOIN_TEST_UTIL_MINING_H
|
Loading…
Add table
Reference in a new issue