Skip to content

Commit 2705570

Browse files
committed
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).
1 parent 298bf5d commit 2705570

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/test/util/setup_common.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,14 @@ void TestChain100Setup::mineBlocks(int num_blocks)
237237
}
238238
}
239239

240-
CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
240+
CBlock TestChain100Setup::CreateBlock(
241+
const std::vector<CMutableTransaction>& txns,
242+
const CScript& scriptPubKey,
243+
CChainState& chainstate)
241244
{
242245
const CChainParams& chainparams = Params();
243246
CTxMemPool empty_pool;
244-
CBlock block = BlockAssembler(m_node.chainman->ActiveChainstate(), empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
247+
CBlock block = BlockAssembler(chainstate, empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
245248

246249
Assert(block.vtx.size() == 1);
247250
for (const CMutableTransaction& tx : txns) {
@@ -251,6 +254,20 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
251254

252255
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
253256

257+
return block;
258+
}
259+
260+
CBlock TestChain100Setup::CreateAndProcessBlock(
261+
const std::vector<CMutableTransaction>& txns,
262+
const CScript& scriptPubKey,
263+
CChainState* chainstate)
264+
{
265+
if (!chainstate) {
266+
chainstate = &Assert(m_node.chainman)->ActiveChainstate();
267+
}
268+
269+
const CChainParams& chainparams = Params();
270+
const CBlock block = this->CreateBlock(txns, scriptPubKey, *chainstate);
254271
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
255272
Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
256273

src/test/util/setup_common.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,20 @@ struct TestChain100Setup : public RegTestingSetup {
119119
/**
120120
* Create a new block with just given transactions, coinbase paying to
121121
* scriptPubKey, and try to add it to the current chain.
122+
* If no chainstate is specified, default to the active.
122123
*/
123124
CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
124-
const CScript& scriptPubKey);
125+
const CScript& scriptPubKey,
126+
CChainState* chainstate = nullptr);
127+
128+
/**
129+
* Create a new block with just given transactions, coinbase paying to
130+
* scriptPubKey.
131+
*/
132+
CBlock CreateBlock(
133+
const std::vector<CMutableTransaction>& txns,
134+
const CScript& scriptPubKey,
135+
CChainState& chainstate);
125136

126137
//! Mine a series of new blocks on the active chain.
127138
void mineBlocks(int num_blocks);

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
#include <random.h>
99
#include <rpc/blockchain.h>
1010
#include <sync.h>
11-
#include <test/util/setup_common.h>
1211
#include <test/util/chainstate.h>
12+
#include <test/util/setup_common.h>
1313
#include <uint256.h>
1414
#include <validation.h>
1515
#include <validationinterface.h>

0 commit comments

Comments
 (0)