Skip to content

Commit 0f9a444

Browse files
committed
MOVEONLY: group miner tests into MinerTestingSetup functions
No behavior changes. Recommend using --color-moved=dimmed_zebra.
1 parent 28bdaa3 commit 0f9a444

File tree

1 file changed

+55
-48
lines changed

1 file changed

+55
-48
lines changed

src/test/miner_tests.cpp

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using node::CBlockTemplate;
3030
namespace miner_tests {
3131
struct MinerTestingSetup : public TestingSetup {
3232
void TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
33+
void TestBasicMining(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
3334
bool TestSequenceLocks(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
3435
{
3536
CCoinsViewMemPool view_mempool(&m_node.chainman->ActiveChainstate().CoinsTip(), *m_node.mempool);
@@ -191,60 +192,17 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
191192
BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
192193
}
193194

194-
// NOTE: These tests rely on CreateNewBlock doing its own self-validation!
195-
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
195+
void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
196196
{
197-
// Note that by default, these tests run with size accounting enabled.
198-
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
199-
const CChainParams& chainparams = *chainParams;
200-
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
201-
std::unique_ptr<CBlockTemplate> pblocktemplate;
202-
CMutableTransaction tx;
203-
CScript script;
204197
uint256 hash;
198+
CMutableTransaction tx;
205199
TestMemPoolEntryHelper entry;
206200
entry.nFee = 11;
207201
entry.nHeight = 11;
208202

209-
fCheckpointsEnabled = false;
210-
211-
// Simple block creation, nothing special yet:
212-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
213-
214-
// We can't make transactions until we have inputs
215-
// Therefore, load 110 blocks :)
216-
static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
217-
int baseheight = 0;
218-
std::vector<CTransactionRef> txFirst;
219-
for (const auto& bi : BLOCKINFO) {
220-
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
221-
{
222-
LOCK(cs_main);
223-
pblock->nVersion = VERSIONBITS_TOP_BITS;
224-
pblock->nTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1;
225-
CMutableTransaction txCoinbase(*pblock->vtx[0]);
226-
txCoinbase.nVersion = 1;
227-
txCoinbase.vin[0].scriptSig = CScript{} << (m_node.chainman->ActiveChain().Height() + 1) << bi.extranonce;
228-
txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
229-
txCoinbase.vout[0].scriptPubKey = CScript();
230-
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
231-
if (txFirst.size() == 0)
232-
baseheight = m_node.chainman->ActiveChain().Height();
233-
if (txFirst.size() < 4)
234-
txFirst.push_back(pblock->vtx[0]);
235-
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
236-
pblock->nNonce = bi.nonce;
237-
}
238-
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
239-
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
240-
pblock->hashPrevBlock = pblock->GetHash();
241-
}
242-
243-
LOCK(cs_main);
244-
LOCK(m_node.mempool->cs);
245-
246203
// Just to make sure we can still make simple blocks
247-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
204+
auto pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
205+
BOOST_CHECK(pblocktemplate);
248206

249207
const CAmount BLOCKSUBSIDY = 50*COIN;
250208
const CAmount LOWFEE = CENT;
@@ -386,7 +344,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
386344
tx.vin[0].prevout.n = 0;
387345
tx.vin[0].scriptSig = CScript() << OP_1;
388346
tx.vout[0].nValue = BLOCKSUBSIDY-LOWFEE;
389-
script = CScript() << OP_0;
347+
CScript script = CScript() << OP_0;
390348
tx.vout[0].scriptPubKey = GetScriptForDestination(ScriptHash(script));
391349
hash = tx.GetHash();
392350
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
@@ -508,6 +466,55 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
508466

509467
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
510468
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
469+
}
470+
471+
// NOTE: These tests rely on CreateNewBlock doing its own self-validation!
472+
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
473+
{
474+
// Note that by default, these tests run with size accounting enabled.
475+
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
476+
const CChainParams& chainparams = *chainParams;
477+
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
478+
std::unique_ptr<CBlockTemplate> pblocktemplate;
479+
480+
fCheckpointsEnabled = false;
481+
482+
// Simple block creation, nothing special yet:
483+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
484+
485+
// We can't make transactions until we have inputs
486+
// Therefore, load 110 blocks :)
487+
static_assert(std::size(BLOCKINFO) == 110, "Should have 110 blocks to import");
488+
int baseheight = 0;
489+
std::vector<CTransactionRef> txFirst;
490+
for (const auto& bi : BLOCKINFO) {
491+
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
492+
{
493+
LOCK(cs_main);
494+
pblock->nVersion = VERSIONBITS_TOP_BITS;
495+
pblock->nTime = m_node.chainman->ActiveChain().Tip()->GetMedianTimePast()+1;
496+
CMutableTransaction txCoinbase(*pblock->vtx[0]);
497+
txCoinbase.nVersion = 1;
498+
txCoinbase.vin[0].scriptSig = CScript{} << (m_node.chainman->ActiveChain().Height() + 1) << bi.extranonce;
499+
txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
500+
txCoinbase.vout[0].scriptPubKey = CScript();
501+
pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
502+
if (txFirst.size() == 0)
503+
baseheight = m_node.chainman->ActiveChain().Height();
504+
if (txFirst.size() < 4)
505+
txFirst.push_back(pblock->vtx[0]);
506+
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
507+
pblock->nNonce = bi.nonce;
508+
}
509+
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
510+
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));
511+
pblock->hashPrevBlock = pblock->GetHash();
512+
}
513+
514+
LOCK(cs_main);
515+
LOCK(m_node.mempool->cs);
516+
517+
TestBasicMining(chainparams, scriptPubKey, txFirst, baseheight);
511518

512519
m_node.chainman->ActiveChain().Tip()->nHeight--;
513520
SetMockTime(0);

0 commit comments

Comments
 (0)