mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
test: refactor: separate CreateBlock in TestChain100Setup
This is so we can create blocks within unittests and have them be processed by specific chainstates (instead of the just the active one).
This commit is contained in:
parent
298bf5d563
commit
2705570109
3 changed files with 32 additions and 4 deletions
|
@ -237,11 +237,14 @@ void TestChain100Setup::mineBlocks(int num_blocks)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
|
CBlock TestChain100Setup::CreateBlock(
|
||||||
|
const std::vector<CMutableTransaction>& txns,
|
||||||
|
const CScript& scriptPubKey,
|
||||||
|
CChainState& chainstate)
|
||||||
{
|
{
|
||||||
const CChainParams& chainparams = Params();
|
const CChainParams& chainparams = Params();
|
||||||
CTxMemPool empty_pool;
|
CTxMemPool empty_pool;
|
||||||
CBlock block = BlockAssembler(m_node.chainman->ActiveChainstate(), empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
CBlock block = BlockAssembler(chainstate, empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
|
||||||
|
|
||||||
Assert(block.vtx.size() == 1);
|
Assert(block.vtx.size() == 1);
|
||||||
for (const CMutableTransaction& tx : txns) {
|
for (const CMutableTransaction& tx : txns) {
|
||||||
|
@ -251,6 +254,20 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
|
||||||
|
|
||||||
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
|
||||||
|
|
||||||
|
return block;
|
||||||
|
}
|
||||||
|
|
||||||
|
CBlock TestChain100Setup::CreateAndProcessBlock(
|
||||||
|
const std::vector<CMutableTransaction>& txns,
|
||||||
|
const CScript& scriptPubKey,
|
||||||
|
CChainState* chainstate)
|
||||||
|
{
|
||||||
|
if (!chainstate) {
|
||||||
|
chainstate = &Assert(m_node.chainman)->ActiveChainstate();
|
||||||
|
}
|
||||||
|
|
||||||
|
const CChainParams& chainparams = Params();
|
||||||
|
const CBlock block = this->CreateBlock(txns, scriptPubKey, *chainstate);
|
||||||
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
|
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
|
||||||
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
|
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
|
||||||
|
|
||||||
|
|
|
@ -119,9 +119,20 @@ struct TestChain100Setup : public RegTestingSetup {
|
||||||
/**
|
/**
|
||||||
* Create a new block with just given transactions, coinbase paying to
|
* Create a new block with just given transactions, coinbase paying to
|
||||||
* scriptPubKey, and try to add it to the current chain.
|
* scriptPubKey, and try to add it to the current chain.
|
||||||
|
* If no chainstate is specified, default to the active.
|
||||||
*/
|
*/
|
||||||
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
|
||||||
const CScript& scriptPubKey);
|
const CScript& scriptPubKey,
|
||||||
|
CChainState* chainstate = nullptr);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new block with just given transactions, coinbase paying to
|
||||||
|
* scriptPubKey.
|
||||||
|
*/
|
||||||
|
CBlock CreateBlock(
|
||||||
|
const std::vector<CMutableTransaction>& txns,
|
||||||
|
const CScript& scriptPubKey,
|
||||||
|
CChainState& chainstate);
|
||||||
|
|
||||||
//! Mine a series of new blocks on the active chain.
|
//! Mine a series of new blocks on the active chain.
|
||||||
void mineBlocks(int num_blocks);
|
void mineBlocks(int num_blocks);
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
#include <random.h>
|
#include <random.h>
|
||||||
#include <rpc/blockchain.h>
|
#include <rpc/blockchain.h>
|
||||||
#include <sync.h>
|
#include <sync.h>
|
||||||
#include <test/util/setup_common.h>
|
|
||||||
#include <test/util/chainstate.h>
|
#include <test/util/chainstate.h>
|
||||||
|
#include <test/util/setup_common.h>
|
||||||
#include <uint256.h>
|
#include <uint256.h>
|
||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
#include <validationinterface.h>
|
#include <validationinterface.h>
|
||||||
|
|
Loading…
Add table
Reference in a new issue