Skip to content

Commit fa29218

Browse files
author
MacroFake
committed
test: Pass mempool reference to AssemblerForTest
1 parent b2e6d37 commit fa29218

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

src/test/miner_tests.cpp

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ struct MinerTestingSetup : public TestingSetup {
3838
CCoinsViewMemPool view_mempool(&m_node.chainman->ActiveChainstate().CoinsTip(), *m_node.mempool);
3939
return CheckSequenceLocksAtTip(m_node.chainman->ActiveChain().Tip(), view_mempool, tx);
4040
}
41-
BlockAssembler AssemblerForTest(const CChainParams& params);
41+
BlockAssembler AssemblerForTest(CTxMemPool& tx_mempool);
4242
};
4343
} // namespace miner_tests
4444

4545
BOOST_FIXTURE_TEST_SUITE(miner_tests, MinerTestingSetup)
4646

4747
static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
4848

49-
BlockAssembler MinerTestingSetup::AssemblerForTest(const CChainParams& params)
49+
BlockAssembler MinerTestingSetup::AssemblerForTest(CTxMemPool& tx_mempool)
5050
{
5151
BlockAssembler::Options options;
5252

5353
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
5454
options.blockMinFeeRate = blockMinFeeRate;
55-
return BlockAssembler{m_node.chainman->ActiveChainstate(), m_node.mempool.get(), options};
55+
return BlockAssembler{m_node.chainman->ActiveChainstate(), &tx_mempool, options};
5656
}
5757

5858
constexpr static struct {
@@ -91,6 +91,7 @@ static CBlockIndex CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip)
9191
// to allow reusing the blockchain created in CreateNewBlock_validity.
9292
void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
9393
{
94+
CTxMemPool& tx_mempool{*m_node.mempool};
9495
// Test the ancestor feerate transaction selection.
9596
TestMemPoolEntryHelper entry;
9697

@@ -119,7 +120,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
119120
uint256 hashHighFeeTx = tx.GetHash();
120121
m_node.mempool->addUnchecked(entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
121122

122-
std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
123+
std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
123124
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
124125
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
125126
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
@@ -140,7 +141,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
140141
tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
141142
uint256 hashLowFeeTx = tx.GetHash();
142143
m_node.mempool->addUnchecked(entry.Fee(feeToUse).FromTx(tx));
143-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
144+
pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
144145
// Verify that the free tx and the low fee tx didn't get selected
145146
for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
146147
BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
@@ -154,7 +155,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
154155
tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
155156
hashLowFeeTx = tx.GetHash();
156157
m_node.mempool->addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
157-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
158+
pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
158159
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
159160
BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
160161
BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
@@ -176,7 +177,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
176177
tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
177178
uint256 hashLowFeeTx2 = tx.GetHash();
178179
m_node.mempool->addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
179-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
180+
pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
180181

181182
// Verify that this tx isn't selected.
182183
for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
@@ -189,21 +190,22 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
189190
tx.vin[0].prevout.n = 1;
190191
tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
191192
m_node.mempool->addUnchecked(entry.Fee(10000).FromTx(tx));
192-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
193+
pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
193194
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
194195
BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
195196
}
196197

197198
void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
198199
{
200+
CTxMemPool& tx_mempool{*m_node.mempool};
199201
uint256 hash;
200202
CMutableTransaction tx;
201203
TestMemPoolEntryHelper entry;
202204
entry.nFee = 11;
203205
entry.nHeight = 11;
204206

205207
// Just to make sure we can still make simple blocks
206-
auto pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
208+
auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
207209
BOOST_CHECK(pblocktemplate);
208210

209211
const CAmount BLOCKSUBSIDY = 50*COIN;
@@ -229,7 +231,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
229231
tx.vin[0].prevout.hash = hash;
230232
}
231233

232-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
234+
BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
233235
m_node.mempool->clear();
234236

235237
tx.vin[0].prevout.hash = txFirst[0]->GetHash();
@@ -243,7 +245,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
243245
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
244246
tx.vin[0].prevout.hash = hash;
245247
}
246-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
248+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
247249
m_node.mempool->clear();
248250

249251
// block size > limit
@@ -263,13 +265,13 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
263265
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
264266
tx.vin[0].prevout.hash = hash;
265267
}
266-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
268+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
267269
m_node.mempool->clear();
268270

269271
// orphan in *m_node.mempool, template creation fails
270272
hash = tx.GetHash();
271273
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx));
272-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
274+
BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
273275
m_node.mempool->clear();
274276

275277
// child with higher feerate than parent
@@ -286,7 +288,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
286288
tx.vout[0].nValue = tx.vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE; //First txn output + fresh coinbase - new txn fee
287289
hash = tx.GetHash();
288290
m_node.mempool->addUnchecked(entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
289-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
291+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
290292
m_node.mempool->clear();
291293

292294
// coinbase in *m_node.mempool, template creation fails
@@ -298,7 +300,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
298300
// give it a fee so it'll get mined
299301
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
300302
// Should throw bad-cb-multiple
301-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
303+
BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
302304
m_node.mempool->clear();
303305

304306
// double spend txn pair in *m_node.mempool, template creation fails
@@ -311,7 +313,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
311313
tx.vout[0].scriptPubKey = CScript() << OP_2;
312314
hash = tx.GetHash();
313315
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
314-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
316+
BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
315317
m_node.mempool->clear();
316318

317319
// subsidy changing
@@ -327,7 +329,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
327329
next->BuildSkip();
328330
m_node.chainman->ActiveChain().SetTip(*next);
329331
}
330-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
332+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
331333
// Extend to a 210000-long block chain.
332334
while (m_node.chainman->ActiveChain().Tip()->nHeight < 210000) {
333335
CBlockIndex* prev = m_node.chainman->ActiveChain().Tip();
@@ -339,7 +341,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
339341
next->BuildSkip();
340342
m_node.chainman->ActiveChain().SetTip(*next);
341343
}
342-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
344+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
343345

344346
// invalid p2sh txn in *m_node.mempool, template creation fails
345347
tx.vin[0].prevout.hash = txFirst[0]->GetHash();
@@ -356,7 +358,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
356358
hash = tx.GetHash();
357359
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
358360
// Should throw block-validation-failed
359-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
361+
BOOST_CHECK_EXCEPTION(AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
360362
m_node.mempool->clear();
361363

362364
// Delete the dummy blocks again.
@@ -455,7 +457,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
455457
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
456458
BOOST_CHECK(!TestSequenceLocks(CTransaction{tx})); // Sequence locks fail
457459

458-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
460+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
459461

460462
// None of the of the absolute height/time locked tx should have made
461463
// it into the template because we still check IsFinalTx in CreateNewBlock,
@@ -470,12 +472,14 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
470472
m_node.chainman->ActiveChain().Tip()->nHeight++;
471473
SetMockTime(m_node.chainman->ActiveChain().Tip()->GetMedianTimePast() + 1);
472474

473-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
475+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
474476
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
475477
}
476478

477479
void MinerTestingSetup::TestPrioritisedMining(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
478480
{
481+
CTxMemPool& tx_mempool{*m_node.mempool};
482+
479483
TestMemPoolEntryHelper entry;
480484

481485
// Test that a tx below min fee but prioritised is included
@@ -534,7 +538,7 @@ void MinerTestingSetup::TestPrioritisedMining(const CChainParams& chainparams, c
534538
uint256 hashFreeGrandchild = tx.GetHash();
535539
m_node.mempool->addUnchecked(entry.Fee(0).SpendsCoinbase(false).FromTx(tx));
536540

537-
auto pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
541+
auto pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey);
538542
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
539543
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashFreeParent);
540544
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashFreePrioritisedTx);
@@ -558,8 +562,9 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
558562
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
559563
std::unique_ptr<CBlockTemplate> pblocktemplate;
560564

565+
CTxMemPool& tx_mempool{*m_node.mempool};
561566
// Simple block creation, nothing special yet:
562-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
567+
BOOST_CHECK(pblocktemplate = AssemblerForTest(tx_mempool).CreateNewBlock(scriptPubKey));
563568

564569
// We can't make transactions until we have inputs
565570
// Therefore, load 110 blocks :)

0 commit comments

Comments
 (0)