Skip to content

Commit 46b7f29

Browse files
committed
scripted-diff: Invoke CreateNewBlock with chainstate
-BEGIN VERIFY SCRIPT- find_regex='(\.|->)CreateNewBlock\(' \ && git grep -l -E "$find_regex" -- src \ | grep -v '^src/miner\.\(cpp\|h\)$' \ | xargs sed -i -E 's@'"$find_regex"'@\0::ChainstateActive(), @g' -END VERIFY SCRIPT-
1 parent d0de61b commit 46b7f29

File tree

6 files changed

+27
-27
lines changed

6 files changed

+27
-27
lines changed

src/rpc/mining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static UniValue generateBlocks(ChainstateManager& chainman, const CTxMemPool& me
150150
UniValue blockHashes(UniValue::VARR);
151151
while (nHeight < nHeightEnd && !ShutdownRequested())
152152
{
153-
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(coinbase_script));
153+
std::unique_ptr<CBlockTemplate> pblocktemplate(BlockAssembler(mempool, Params()).CreateNewBlock(::ChainstateActive(), coinbase_script));
154154
if (!pblocktemplate.get())
155155
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
156156
CBlock *pblock = &pblocktemplate->block;
@@ -358,7 +358,7 @@ static RPCHelpMan generateblock()
358358
LOCK(cs_main);
359359

360360
CTxMemPool empty_mempool;
361-
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(empty_mempool, chainparams).CreateNewBlock(coinbase_script));
361+
std::unique_ptr<CBlockTemplate> blocktemplate(BlockAssembler(empty_mempool, chainparams).CreateNewBlock(::ChainstateActive(), coinbase_script));
362362
if (!blocktemplate) {
363363
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
364364
}
@@ -747,7 +747,7 @@ static RPCHelpMan getblocktemplate()
747747

748748
// Create new block
749749
CScript scriptDummy = CScript() << OP_TRUE;
750-
pblocktemplate = BlockAssembler(mempool, Params()).CreateNewBlock(scriptDummy);
750+
pblocktemplate = BlockAssembler(mempool, Params()).CreateNewBlock(::ChainstateActive(), scriptDummy);
751751
if (!pblocktemplate)
752752
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
753753

src/test/blockfilter_index_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ CBlock BuildChainTestingSetup::CreateBlock(const CBlockIndex* prev,
6262
const CScript& scriptPubKey)
6363
{
6464
const CChainParams& chainparams = Params();
65-
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*m_node.mempool, chainparams).CreateNewBlock(scriptPubKey);
65+
std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(*m_node.mempool, chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey);
6666
CBlock& block = pblocktemplate->block;
6767
block.hashPrevBlock = prev->GetBlockHash();
6868
block.nTime = prev->nTime + 1;

src/test/miner_tests.cpp

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
122122
uint256 hashHighFeeTx = tx.GetHash();
123123
m_node.mempool->addUnchecked(entry.Fee(50000).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
124124

125-
std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
125+
std::unique_ptr<CBlockTemplate> pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey);
126126
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 4U);
127127
BOOST_CHECK(pblocktemplate->block.vtx[1]->GetHash() == hashParentTx);
128128
BOOST_CHECK(pblocktemplate->block.vtx[2]->GetHash() == hashHighFeeTx);
@@ -143,7 +143,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
143143
tx.vout[0].nValue = 5000000000LL - 1000 - 50000 - feeToUse;
144144
uint256 hashLowFeeTx = tx.GetHash();
145145
m_node.mempool->addUnchecked(entry.Fee(feeToUse).FromTx(tx));
146-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
146+
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey);
147147
// Verify that the free tx and the low fee tx didn't get selected
148148
for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
149149
BOOST_CHECK(pblocktemplate->block.vtx[i]->GetHash() != hashFreeTx);
@@ -157,7 +157,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
157157
tx.vout[0].nValue -= 2; // Now we should be just over the min relay fee
158158
hashLowFeeTx = tx.GetHash();
159159
m_node.mempool->addUnchecked(entry.Fee(feeToUse+2).FromTx(tx));
160-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
160+
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey);
161161
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 6U);
162162
BOOST_CHECK(pblocktemplate->block.vtx[4]->GetHash() == hashFreeTx);
163163
BOOST_CHECK(pblocktemplate->block.vtx[5]->GetHash() == hashLowFeeTx);
@@ -179,7 +179,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
179179
tx.vout[0].nValue = 5000000000LL - 100000000 - feeToUse;
180180
uint256 hashLowFeeTx2 = tx.GetHash();
181181
m_node.mempool->addUnchecked(entry.Fee(feeToUse).SpendsCoinbase(false).FromTx(tx));
182-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
182+
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey);
183183

184184
// Verify that this tx isn't selected.
185185
for (size_t i=0; i<pblocktemplate->block.vtx.size(); ++i) {
@@ -192,7 +192,7 @@ void MinerTestingSetup::TestPackageSelection(const CChainParams& chainparams, co
192192
tx.vin[0].prevout.n = 1;
193193
tx.vout[0].nValue = 100000000 - 10000; // 10k satoshi fee
194194
m_node.mempool->addUnchecked(entry.Fee(10000).FromTx(tx));
195-
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey);
195+
pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey);
196196
BOOST_REQUIRE_EQUAL(pblocktemplate->block.vtx.size(), 9U);
197197
BOOST_CHECK(pblocktemplate->block.vtx[8]->GetHash() == hashLowFeeTx2);
198198
}
@@ -215,7 +215,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
215215
fCheckpointsEnabled = false;
216216

217217
// Simple block creation, nothing special yet:
218-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
218+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
219219

220220
// We can't make transactions until we have inputs
221221
// Therefore, load 110 blocks :)
@@ -252,7 +252,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
252252
LOCK(m_node.mempool->cs);
253253

254254
// Just to make sure we can still make simple blocks
255-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
255+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
256256

257257
const CAmount BLOCKSUBSIDY = 50*COIN;
258258
const CAmount LOWFEE = CENT;
@@ -277,7 +277,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
277277
tx.vin[0].prevout.hash = hash;
278278
}
279279

280-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
280+
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey), std::runtime_error, HasReason("bad-blk-sigops"));
281281
m_node.mempool->clear();
282282

283283
tx.vin[0].prevout.hash = txFirst[0]->GetHash();
@@ -291,7 +291,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
291291
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).SigOpsCost(80).FromTx(tx));
292292
tx.vin[0].prevout.hash = hash;
293293
}
294-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
294+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
295295
m_node.mempool->clear();
296296

297297
// block size > limit
@@ -311,13 +311,13 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
311311
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(spendsCoinbase).FromTx(tx));
312312
tx.vin[0].prevout.hash = hash;
313313
}
314-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
314+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
315315
m_node.mempool->clear();
316316

317317
// orphan in *m_node.mempool, template creation fails
318318
hash = tx.GetHash();
319319
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).FromTx(tx));
320-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
320+
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
321321
m_node.mempool->clear();
322322

323323
// child with higher feerate than parent
@@ -334,7 +334,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
334334
tx.vout[0].nValue = tx.vout[0].nValue+BLOCKSUBSIDY-HIGHERFEE; //First txn output + fresh coinbase - new txn fee
335335
hash = tx.GetHash();
336336
m_node.mempool->addUnchecked(entry.Fee(HIGHERFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
337-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
337+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
338338
m_node.mempool->clear();
339339

340340
// coinbase in *m_node.mempool, template creation fails
@@ -346,7 +346,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
346346
// give it a fee so it'll get mined
347347
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
348348
// Should throw bad-cb-multiple
349-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
349+
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey), std::runtime_error, HasReason("bad-cb-multiple"));
350350
m_node.mempool->clear();
351351

352352
// double spend txn pair in *m_node.mempool, template creation fails
@@ -359,7 +359,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
359359
tx.vout[0].scriptPubKey = CScript() << OP_2;
360360
hash = tx.GetHash();
361361
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
362-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
362+
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey), std::runtime_error, HasReason("bad-txns-inputs-missingorspent"));
363363
m_node.mempool->clear();
364364

365365
// subsidy changing
@@ -375,7 +375,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
375375
next->BuildSkip();
376376
::ChainActive().SetTip(next);
377377
}
378-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
378+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
379379
// Extend to a 210000-long block chain.
380380
while (::ChainActive().Tip()->nHeight < 210000) {
381381
CBlockIndex* prev = ::ChainActive().Tip();
@@ -387,7 +387,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
387387
next->BuildSkip();
388388
::ChainActive().SetTip(next);
389389
}
390-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
390+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
391391

392392
// invalid p2sh txn in *m_node.mempool, template creation fails
393393
tx.vin[0].prevout.hash = txFirst[0]->GetHash();
@@ -404,7 +404,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
404404
hash = tx.GetHash();
405405
m_node.mempool->addUnchecked(entry.Fee(LOWFEE).Time(GetTime()).SpendsCoinbase(false).FromTx(tx));
406406
// Should throw block-validation-failed
407-
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
407+
BOOST_CHECK_EXCEPTION(AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey), std::runtime_error, HasReason("block-validation-failed"));
408408
m_node.mempool->clear();
409409

410410
// Delete the dummy blocks again.
@@ -492,7 +492,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
492492
tx.vin[0].nSequence = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | 1;
493493
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
494494

495-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
495+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
496496

497497
// None of the of the absolute height/time locked tx should have made
498498
// it into the template because we still check IsFinalTx in CreateNewBlock,
@@ -505,7 +505,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
505505
::ChainActive().Tip()->nHeight++;
506506
SetMockTime(::ChainActive().Tip()->GetMedianTimePast() + 1);
507507

508-
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(scriptPubKey));
508+
BOOST_CHECK(pblocktemplate = AssemblerForTest(chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey));
509509
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 5U);
510510

511511
::ChainActive().Tip()->nHeight--;

src/test/util/mining.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ std::shared_ptr<CBlock> PrepareBlock(const NodeContext& node, const CScript& coi
4242
{
4343
auto block = std::make_shared<CBlock>(
4444
BlockAssembler{*Assert(node.mempool), Params()}
45-
.CreateNewBlock(coinbase_scriptPubKey)
45+
.CreateNewBlock(::ChainstateActive(), coinbase_scriptPubKey)
4646
->block);
4747

4848
LOCK(cs_main);

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ CBlock TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransa
245245
{
246246
const CChainParams& chainparams = Params();
247247
CTxMemPool empty_pool;
248-
CBlock block = BlockAssembler(empty_pool, chainparams).CreateNewBlock(scriptPubKey)->block;
248+
CBlock block = BlockAssembler(empty_pool, chainparams).CreateNewBlock(::ChainstateActive(), scriptPubKey)->block;
249249

250250
Assert(block.vtx.size() == 1);
251251
for (const CMutableTransaction& tx : txns) {

src/test/validation_block_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
6363
static int i = 0;
6464
static uint64_t time = Params().GenesisBlock().nTime;
6565

66-
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
66+
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(::ChainstateActive(), CScript{} << i++ << OP_TRUE);
6767
auto pblock = std::make_shared<CBlock>(ptemplate->block);
6868
pblock->hashPrevBlock = prev_hash;
6969
pblock->nTime = ++time;
@@ -325,7 +325,7 @@ BOOST_AUTO_TEST_CASE(witness_commitment_index)
325325
{
326326
CScript pubKey;
327327
pubKey << 1 << OP_TRUE;
328-
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(pubKey);
328+
auto ptemplate = BlockAssembler(*m_node.mempool, Params()).CreateNewBlock(::ChainstateActive(), pubKey);
329329
CBlock pblock = ptemplate->block;
330330

331331
CTxOut witness;

0 commit comments

Comments
 (0)