@@ -30,6 +30,7 @@ using node::CBlockTemplate;
30
30
namespace miner_tests {
31
31
struct MinerTestingSetup : public TestingSetup {
32
32
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);
33
34
bool TestSequenceLocks (const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
34
35
{
35
36
CCoinsViewMemPool view_mempool (&m_node.chainman ->ActiveChainstate ().CoinsTip (), *m_node.mempool );
@@ -191,60 +192,17 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
191
192
BOOST_CHECK (pblocktemplate->block .vtx [8 ]->GetHash () == hashLowFeeTx2);
192
193
}
193
194
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)
196
196
{
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;
204
197
uint256 hash;
198
+ CMutableTransaction tx;
205
199
TestMemPoolEntryHelper entry;
206
200
entry.nFee = 11 ;
207
201
entry.nHeight = 11 ;
208
202
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
-
246
203
// 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);
248
206
249
207
const CAmount BLOCKSUBSIDY = 50 *COIN;
250
208
const CAmount LOWFEE = CENT;
@@ -386,7 +344,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
386
344
tx.vin [0 ].prevout .n = 0 ;
387
345
tx.vin [0 ].scriptSig = CScript () << OP_1;
388
346
tx.vout [0 ].nValue = BLOCKSUBSIDY-LOWFEE;
389
- script = CScript () << OP_0;
347
+ CScript script = CScript () << OP_0;
390
348
tx.vout [0 ].scriptPubKey = GetScriptForDestination (ScriptHash (script));
391
349
hash = tx.GetHash ();
392
350
m_node.mempool ->addUnchecked (entry.Fee (LOWFEE).Time (GetTime ()).SpendsCoinbase (true ).FromTx (tx));
@@ -508,6 +466,55 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
508
466
509
467
BOOST_CHECK (pblocktemplate = AssemblerForTest (chainparams).CreateNewBlock (scriptPubKey));
510
468
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);
511
518
512
519
m_node.chainman ->ActiveChain ().Tip ()->nHeight --;
513
520
SetMockTime (0 );
0 commit comments