@@ -38,21 +38,21 @@ struct MinerTestingSetup : public TestingSetup {
38
38
CCoinsViewMemPool view_mempool (&m_node.chainman ->ActiveChainstate ().CoinsTip (), *m_node.mempool );
39
39
return CheckSequenceLocksAtTip (m_node.chainman ->ActiveChain ().Tip (), view_mempool, tx);
40
40
}
41
- BlockAssembler AssemblerForTest (const CChainParams& params );
41
+ BlockAssembler AssemblerForTest (CTxMemPool& tx_mempool );
42
42
};
43
43
} // namespace miner_tests
44
44
45
45
BOOST_FIXTURE_TEST_SUITE (miner_tests, MinerTestingSetup)
46
46
47
47
static CFeeRate blockMinFeeRate = CFeeRate(DEFAULT_BLOCK_MIN_TX_FEE);
48
48
49
- BlockAssembler MinerTestingSetup::AssemblerForTest (const CChainParams& params )
49
+ BlockAssembler MinerTestingSetup::AssemblerForTest (CTxMemPool& tx_mempool )
50
50
{
51
51
BlockAssembler::Options options;
52
52
53
53
options.nBlockMaxWeight = MAX_BLOCK_WEIGHT;
54
54
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};
56
56
}
57
57
58
58
constexpr static struct {
@@ -91,6 +91,7 @@ static CBlockIndex CreateBlockIndex(int nHeight, CBlockIndex* active_chain_tip)
91
91
// to allow reusing the blockchain created in CreateNewBlock_validity.
92
92
void MinerTestingSetup::TestPackageSelection (const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
93
93
{
94
+ CTxMemPool& tx_mempool{*m_node.mempool };
94
95
// Test the ancestor feerate transaction selection.
95
96
TestMemPoolEntryHelper entry;
96
97
@@ -119,7 +120,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
119
120
uint256 hashHighFeeTx = tx.GetHash ();
120
121
m_node.mempool ->addUnchecked (entry.Fee (50000 ).Time (GetTime ()).SpendsCoinbase (false ).FromTx (tx));
121
122
122
- std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey);
123
+ std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey);
123
124
BOOST_REQUIRE_EQUAL (pblocktemplate->block .vtx .size (), 4U );
124
125
BOOST_CHECK (pblocktemplate->block .vtx [1 ]->GetHash () == hashParentTx);
125
126
BOOST_CHECK (pblocktemplate->block .vtx [2 ]->GetHash () == hashHighFeeTx);
@@ -140,7 +141,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
140
141
tx.vout [0 ].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
141
142
uint256 hashLowFeeTx = tx.GetHash ();
142
143
m_node.mempool ->addUnchecked (entry.Fee (feeToUse).FromTx (tx));
143
- pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey);
144
+ pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey);
144
145
// Verify that the free tx and the low fee tx didn't get selected
145
146
for (size_t i=0 ; i<pblocktemplate->block .vtx .size (); ++i) {
146
147
BOOST_CHECK (pblocktemplate->block .vtx [i]->GetHash () != hashFreeTx);
@@ -154,7 +155,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
154
155
tx.vout [0 ].nValue -= 2 ; // Now we should be just over the min relay fee
155
156
hashLowFeeTx = tx.GetHash ();
156
157
m_node.mempool ->addUnchecked (entry.Fee (feeToUse+2 ).FromTx (tx));
157
- pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey);
158
+ pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey);
158
159
BOOST_REQUIRE_EQUAL (pblocktemplate->block .vtx .size (), 6U );
159
160
BOOST_CHECK (pblocktemplate->block .vtx [4 ]->GetHash () == hashFreeTx);
160
161
BOOST_CHECK (pblocktemplate->block .vtx [5 ]->GetHash () == hashLowFeeTx);
@@ -176,7 +177,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
176
177
tx.vout [0 ].nValue = 5000000000LL - 100000000 - feeToUse;
177
178
uint256 hashLowFeeTx2 = tx.GetHash ();
178
179
m_node.mempool ->addUnchecked (entry.Fee (feeToUse).SpendsCoinbase (false ).FromTx (tx));
179
- pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey);
180
+ pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey);
180
181
181
182
// Verify that this tx isn't selected.
182
183
for (size_t i=0 ; i<pblocktemplate->block .vtx .size (); ++i) {
@@ -189,21 +190,22 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
189
190
tx.vin [0 ].prevout .n = 1 ;
190
191
tx.vout [0 ].nValue = 100000000 - 10000 ; // 10k satoshi fee
191
192
m_node.mempool ->addUnchecked (entry.Fee (10000 ).FromTx (tx));
192
- pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey);
193
+ pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey);
193
194
BOOST_REQUIRE_EQUAL (pblocktemplate->block .vtx .size (), 9U );
194
195
BOOST_CHECK (pblocktemplate->block .vtx [8 ]->GetHash () == hashLowFeeTx2);
195
196
}
196
197
197
198
void MinerTestingSetup::TestBasicMining (const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst, int baseheight)
198
199
{
200
+ CTxMemPool& tx_mempool{*m_node.mempool };
199
201
uint256 hash;
200
202
CMutableTransaction tx;
201
203
TestMemPoolEntryHelper entry;
202
204
entry.nFee = 11 ;
203
205
entry.nHeight = 11 ;
204
206
205
207
// 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);
207
209
BOOST_CHECK (pblocktemplate);
208
210
209
211
const CAmount BLOCKSUBSIDY = 50 *COIN;
@@ -229,7 +231,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
229
231
tx.vin [0 ].prevout .hash = hash;
230
232
}
231
233
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" ));
233
235
m_node.mempool ->clear ();
234
236
235
237
tx.vin [0 ].prevout .hash = txFirst[0 ]->GetHash ();
@@ -243,7 +245,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
243
245
m_node.mempool ->addUnchecked (entry.Fee (LOWFEE).Time (GetTime ()).SpendsCoinbase (spendsCoinbase).SigOpsCost (80 ).FromTx (tx));
244
246
tx.vin [0 ].prevout .hash = hash;
245
247
}
246
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
248
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
247
249
m_node.mempool ->clear ();
248
250
249
251
// block size > limit
@@ -263,13 +265,13 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
263
265
m_node.mempool ->addUnchecked (entry.Fee (LOWFEE).Time (GetTime ()).SpendsCoinbase (spendsCoinbase).FromTx (tx));
264
266
tx.vin [0 ].prevout .hash = hash;
265
267
}
266
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
268
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
267
269
m_node.mempool ->clear ();
268
270
269
271
// orphan in *m_node.mempool, template creation fails
270
272
hash = tx.GetHash ();
271
273
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" ));
273
275
m_node.mempool ->clear ();
274
276
275
277
// child with higher feerate than parent
@@ -286,7 +288,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
286
288
tx.vout [0 ].nValue = tx.vout [0 ].nValue +BLOCKSUBSIDY-HIGHERFEE; // First txn output + fresh coinbase - new txn fee
287
289
hash = tx.GetHash ();
288
290
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));
290
292
m_node.mempool ->clear ();
291
293
292
294
// coinbase in *m_node.mempool, template creation fails
@@ -298,7 +300,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
298
300
// give it a fee so it'll get mined
299
301
m_node.mempool ->addUnchecked (entry.Fee (LOWFEE).Time (GetTime ()).SpendsCoinbase (false ).FromTx (tx));
300
302
// 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" ));
302
304
m_node.mempool ->clear ();
303
305
304
306
// double spend txn pair in *m_node.mempool, template creation fails
@@ -311,7 +313,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
311
313
tx.vout [0 ].scriptPubKey = CScript () << OP_2;
312
314
hash = tx.GetHash ();
313
315
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" ));
315
317
m_node.mempool ->clear ();
316
318
317
319
// subsidy changing
@@ -327,7 +329,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
327
329
next->BuildSkip ();
328
330
m_node.chainman ->ActiveChain ().SetTip (*next);
329
331
}
330
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
332
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
331
333
// Extend to a 210000-long block chain.
332
334
while (m_node.chainman ->ActiveChain ().Tip ()->nHeight < 210000 ) {
333
335
CBlockIndex* prev = m_node.chainman ->ActiveChain ().Tip ();
@@ -339,7 +341,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
339
341
next->BuildSkip ();
340
342
m_node.chainman ->ActiveChain ().SetTip (*next);
341
343
}
342
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
344
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
343
345
344
346
// invalid p2sh txn in *m_node.mempool, template creation fails
345
347
tx.vin [0 ].prevout .hash = txFirst[0 ]->GetHash ();
@@ -356,7 +358,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
356
358
hash = tx.GetHash ();
357
359
m_node.mempool ->addUnchecked (entry.Fee (LOWFEE).Time (GetTime ()).SpendsCoinbase (false ).FromTx (tx));
358
360
// 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" ));
360
362
m_node.mempool ->clear ();
361
363
362
364
// Delete the dummy blocks again.
@@ -455,7 +457,7 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
455
457
tx.vin [0 ].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1 ;
456
458
BOOST_CHECK (!TestSequenceLocks (CTransaction{tx})); // Sequence locks fail
457
459
458
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
460
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
459
461
460
462
// None of the of the absolute height/time locked tx should have made
461
463
// it into the template because we still check IsFinalTx in CreateNewBlock,
@@ -470,12 +472,14 @@ void MinerTestingSetup::TestBasicMining(const CChainParams& chainparams, const C
470
472
m_node.chainman ->ActiveChain ().Tip ()->nHeight ++;
471
473
SetMockTime (m_node.chainman ->ActiveChain ().Tip ()->GetMedianTimePast () + 1 );
472
474
473
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
475
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
474
476
BOOST_CHECK_EQUAL (pblocktemplate->block .vtx .size (), 5U );
475
477
}
476
478
477
479
void MinerTestingSetup::TestPrioritisedMining (const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst)
478
480
{
481
+ CTxMemPool& tx_mempool{*m_node.mempool };
482
+
479
483
TestMemPoolEntryHelper entry;
480
484
481
485
// Test that a tx below min fee but prioritised is included
@@ -534,7 +538,7 @@ void MinerTestingSetup::TestPrioritisedMining(const CChainParams& chainparams, c
534
538
uint256 hashFreeGrandchild = tx.GetHash ();
535
539
m_node.mempool ->addUnchecked (entry.Fee (0 ).SpendsCoinbase (false ).FromTx (tx));
536
540
537
- auto pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey);
541
+ auto pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey);
538
542
BOOST_REQUIRE_EQUAL (pblocktemplate->block .vtx .size (), 6U );
539
543
BOOST_CHECK (pblocktemplate->block .vtx [1 ]->GetHash () == hashFreeParent);
540
544
BOOST_CHECK (pblocktemplate->block .vtx [2 ]->GetHash () == hashFreePrioritisedTx);
@@ -558,8 +562,9 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
558
562
CScript scriptPubKey = CScript () << ParseHex (" 04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f" ) << OP_CHECKSIG;
559
563
std::unique_ptr<CBlockTemplate> pblocktemplate;
560
564
565
+ CTxMemPool& tx_mempool{*m_node.mempool };
561
566
// Simple block creation, nothing special yet:
562
- BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams ).CreateNewBlock (scriptPubKey));
567
+ BOOST_CHECK (pblocktemplate = AssemblerForTest (tx_mempool ).CreateNewBlock (scriptPubKey));
563
568
564
569
// We can't make transactions until we have inputs
565
570
// Therefore, load 110 blocks :)
0 commit comments