Skip to content

Commit 87ee0e2

Browse files
committed
Merge pull request #6986
6bc9e40 Chainparams: Explicit CChainParams arg for miner: (Jorge Timón) 598e494 Chainparams: Explicit CChainParams arg for main (pre miner): (Jorge Timón)
2 parents 972bf9c + 6bc9e40 commit 87ee0e2

File tree

8 files changed

+41
-41
lines changed

8 files changed

+41
-41
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1603,7 +1603,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
16031603
scheduler.scheduleEvery(f, nPowTargetSpacing);
16041604

16051605
// Generate coins in the background
1606-
GenerateBitcoins(GetBoolArg("-gen", false), GetArg("-genproclimit", DEFAULT_GENERATE_THREADS), Params());
1606+
GenerateBitcoins(GetBoolArg("-gen", false), GetArg("-genproclimit", DEFAULT_GENERATE_THREADS), chainparams);
16071607

16081608
// ********************************************************* Step 12: finished
16091609

src/main.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2927,9 +2927,8 @@ static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned
29272927
}
29282928

29292929

2930-
bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
2930+
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos* dbp)
29312931
{
2932-
const CChainParams& chainparams = Params();
29332932
// Preliminary checks
29342933
bool checked = CheckBlock(*pblock, state);
29352934

@@ -2958,9 +2957,8 @@ bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock*
29582957
return true;
29592958
}
29602959

2961-
bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
2960+
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
29622961
{
2963-
const CChainParams& chainparams = Params();
29642962
AssertLockHeld(cs_main);
29652963
assert(pindexPrev && pindexPrev == chainActive.Tip());
29662964
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
@@ -3500,7 +3498,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
35003498
// process in case the block isn't known yet
35013499
if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
35023500
CValidationState state;
3503-
if (ProcessNewBlock(state, NULL, &block, true, dbp))
3501+
if (ProcessNewBlock(state, chainparams, NULL, &block, true, dbp))
35043502
nLoaded++;
35053503
if (state.IsError())
35063504
break;
@@ -3522,7 +3520,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
35223520
LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
35233521
head.ToString());
35243522
CValidationState dummy;
3525-
if (ProcessNewBlock(dummy, NULL, &block, true, &it->second))
3523+
if (ProcessNewBlock(dummy, chainparams, NULL, &block, true, &it->second))
35263524
{
35273525
nLoaded++;
35283526
queue.push_back(block.GetHash());
@@ -4562,7 +4560,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
45624560
// Such an unrequested block may still be processed, subject to the
45634561
// conditions in AcceptBlock().
45644562
bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
4565-
ProcessNewBlock(state, pfrom, &block, forceProcessing, NULL);
4563+
ProcessNewBlock(state, chainparams, pfrom, &block, forceProcessing, NULL);
45664564
int nDoS;
45674565
if (state.IsInvalid(nDoS)) {
45684566
assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes

src/main.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
class CBlockIndex;
3232
class CBlockTreeDB;
3333
class CBloomFilter;
34+
class CChainParams;
3435
class CInv;
3536
class CScriptCheck;
3637
class CTxMemPool;
@@ -161,7 +162,7 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals);
161162
* @param[out] dbp If pblock is stored to disk (or already there), this will be set to its location.
162163
* @return True if state.IsValid()
163164
*/
164-
bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp);
165+
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos* dbp);
165166
/** Check whether enough disk space is available for an incoming block */
166167
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
167168
/** Open a block file (blk?????.dat) */
@@ -380,7 +381,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
380381
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev);
381382

382383
/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
383-
bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex *pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
384+
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
384385

385386
/** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
386387
bool AcceptBlock(const CBlock& block, CValidationState& state, CBlockIndex **pindex, bool fRequested, CDiskBlockPos* dbp);

src/miner.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,8 @@ int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParam
9999
return nNewTime - nOldTime;
100100
}
101101

102-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
102+
CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn)
103103
{
104-
const CChainParams& chainparams = Params();
105104
// Create new block
106105
auto_ptr<CBlockTemplate> pblocktemplate(new CBlockTemplate());
107106
if(!pblocktemplate.get())
@@ -110,7 +109,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
110109

111110
// -regtest only: allow overriding block.nVersion with
112111
// -blockversion=N to test forking scenarios
113-
if (Params().MineBlocksOnDemand())
112+
if (chainparams.MineBlocksOnDemand())
114113
pblock->nVersion = GetArg("-blockversion", pblock->nVersion);
115114

116115
// Create coinbase tx
@@ -345,13 +344,13 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
345344

346345
// Fill in header
347346
pblock->hashPrevBlock = pindexPrev->GetBlockHash();
348-
UpdateTime(pblock, Params().GetConsensus(), pindexPrev);
349-
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, Params().GetConsensus());
347+
UpdateTime(pblock, chainparams.GetConsensus(), pindexPrev);
348+
pblock->nBits = GetNextWorkRequired(pindexPrev, pblock, chainparams.GetConsensus());
350349
pblock->nNonce = 0;
351350
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
352351

353352
CValidationState state;
354-
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false))
353+
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false))
355354
throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
356355
}
357356

@@ -432,7 +431,7 @@ static bool ProcessBlockFound(const CBlock* pblock, const CChainParams& chainpar
432431

433432
// Process this block the same as if we had received it from another node
434433
CValidationState state;
435-
if (!ProcessNewBlock(state, NULL, pblock, true, NULL))
434+
if (!ProcessNewBlock(state, chainparams, NULL, pblock, true, NULL))
436435
return error("BitcoinMiner: ProcessNewBlock, block not accepted");
437436

438437
return true;
@@ -478,7 +477,7 @@ void static BitcoinMiner(const CChainParams& chainparams)
478477
unsigned int nTransactionsUpdatedLast = mempool.GetTransactionsUpdated();
479478
CBlockIndex* pindexPrev = chainActive.Tip();
480479

481-
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript->reserveScript));
480+
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(chainparams, coinbaseScript->reserveScript));
482481
if (!pblocktemplate.get())
483482
{
484483
LogPrintf("Error in BitcoinMiner: Keypool ran out, please call keypoolrefill before restarting the mining thread\n");

src/miner.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct CBlockTemplate
2929
/** Run the miner threads */
3030
void GenerateBitcoins(bool fGenerate, int nThreads, const CChainParams& chainparams);
3131
/** Generate a new block, without valid proof-of-work */
32-
CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn);
32+
CBlockTemplate* CreateNewBlock(const CChainParams& chainparams, const CScript& scriptPubKeyIn);
3333
/** Modify the extranonce in a block */
3434
void IncrementExtraNonce(CBlock* pblock, const CBlockIndex* pindexPrev, unsigned int& nExtraNonce);
3535
int64_t UpdateTime(CBlockHeader* pblock, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev);

src/rpcmining.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ UniValue generate(const UniValue& params, bool fHelp)
157157
UniValue blockHashes(UniValue::VARR);
158158
while (nHeight < nHeightEnd)
159159
{
160-
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(coinbaseScript->reserveScript));
160+
auto_ptr<CBlockTemplate> pblocktemplate(CreateNewBlock(Params(), coinbaseScript->reserveScript));
161161
if (!pblocktemplate.get())
162162
throw JSONRPCError(RPC_INTERNAL_ERROR, "Couldn't create new block");
163163
CBlock *pblock = &pblocktemplate->block;
@@ -171,7 +171,7 @@ UniValue generate(const UniValue& params, bool fHelp)
171171
++pblock->nNonce;
172172
}
173173
CValidationState state;
174-
if (!ProcessNewBlock(state, NULL, pblock, true, NULL))
174+
if (!ProcessNewBlock(state, Params(), NULL, pblock, true, NULL))
175175
throw JSONRPCError(RPC_INTERNAL_ERROR, "ProcessNewBlock, block not accepted");
176176
++nHeight;
177177
blockHashes.push_back(pblock->GetHash().GetHex());
@@ -426,7 +426,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
426426
if (block.hashPrevBlock != pindexPrev->GetBlockHash())
427427
return "inconclusive-not-best-prevblk";
428428
CValidationState state;
429-
TestBlockValidity(state, block, pindexPrev, false, true);
429+
TestBlockValidity(state, Params(), block, pindexPrev, false, true);
430430
return BIP22ValidationResult(state);
431431
}
432432
}
@@ -510,7 +510,7 @@ UniValue getblocktemplate(const UniValue& params, bool fHelp)
510510
pblocktemplate = NULL;
511511
}
512512
CScript scriptDummy = CScript() << OP_TRUE;
513-
pblocktemplate = CreateNewBlock(scriptDummy);
513+
pblocktemplate = CreateNewBlock(Params(), scriptDummy);
514514
if (!pblocktemplate)
515515
throw JSONRPCError(RPC_OUT_OF_MEMORY, "Out of memory");
516516

@@ -652,7 +652,7 @@ UniValue submitblock(const UniValue& params, bool fHelp)
652652
CValidationState state;
653653
submitblock_StateCatcher sc(block.GetHash());
654654
RegisterValidationInterface(&sc);
655-
bool fAccepted = ProcessNewBlock(state, NULL, &block, true, NULL);
655+
bool fAccepted = ProcessNewBlock(state, Params(), NULL, &block, true, NULL);
656656
UnregisterValidationInterface(&sc);
657657
if (fBlockPresent)
658658
{

src/test/miner_tests.cpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ struct {
5959
// NOTE: These tests rely on CreateNewBlock doing its own self-validation!
6060
BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
6161
{
62+
const CChainParams& chainparams = Params(CBaseChainParams::MAIN);
6263
CScript scriptPubKey = CScript() << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f") << OP_CHECKSIG;
6364
CBlockTemplate *pblocktemplate;
6465
CMutableTransaction tx,tx2;
@@ -69,7 +70,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
6970
fCheckpointsEnabled = false;
7071

7172
// Simple block creation, nothing special yet:
72-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
73+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
7374

7475
// We can't make transactions until we have inputs
7576
// Therefore, load 100 blocks :)
@@ -91,14 +92,14 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
9192
pblock->hashMerkleRoot = pblock->ComputeMerkleRoot();
9293
pblock->nNonce = blockinfo[i].nonce;
9394
CValidationState state;
94-
BOOST_CHECK(ProcessNewBlock(state, NULL, pblock, true, NULL));
95+
BOOST_CHECK(ProcessNewBlock(state, chainparams, NULL, pblock, true, NULL));
9596
BOOST_CHECK(state.IsValid());
9697
pblock->hashPrevBlock = pblock->GetHash();
9798
}
9899
delete pblocktemplate;
99100

100101
// Just to make sure we can still make simple blocks
101-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
102+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
102103
delete pblocktemplate;
103104

104105
// block sigops > limit: 1000 CHECKMULTISIG + 1
@@ -116,7 +117,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
116117
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
117118
tx.vin[0].prevout.hash = hash;
118119
}
119-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
120+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
120121
delete pblocktemplate;
121122
mempool.clear();
122123

@@ -136,14 +137,14 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
136137
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
137138
tx.vin[0].prevout.hash = hash;
138139
}
139-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
140+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
140141
delete pblocktemplate;
141142
mempool.clear();
142143

143144
// orphan in mempool
144145
hash = tx.GetHash();
145146
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
146-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
147+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
147148
delete pblocktemplate;
148149
mempool.clear();
149150

@@ -161,7 +162,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
161162
tx.vout[0].nValue = 5900000000LL;
162163
hash = tx.GetHash();
163164
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
164-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
165+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
165166
delete pblocktemplate;
166167
mempool.clear();
167168

@@ -172,7 +173,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
172173
tx.vout[0].nValue = 0;
173174
hash = tx.GetHash();
174175
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
175-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
176+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
176177
delete pblocktemplate;
177178
mempool.clear();
178179

@@ -190,7 +191,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
190191
tx.vout[0].nValue -= 1000000;
191192
hash = tx.GetHash();
192193
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
193-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
194+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
194195
delete pblocktemplate;
195196
mempool.clear();
196197

@@ -204,17 +205,17 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
204205
tx.vout[0].scriptPubKey = CScript() << OP_2;
205206
hash = tx.GetHash();
206207
mempool.addUnchecked(hash, CTxMemPoolEntry(tx, 11, GetTime(), 111.0, 11));
207-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
208+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
208209
delete pblocktemplate;
209210
mempool.clear();
210211

211212
// subsidy changing
212213
int nHeight = chainActive.Height();
213214
chainActive.Tip()->nHeight = 209999;
214-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
215+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
215216
delete pblocktemplate;
216217
chainActive.Tip()->nHeight = 210000;
217-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
218+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
218219
delete pblocktemplate;
219220
chainActive.Tip()->nHeight = nHeight;
220221

@@ -246,7 +247,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
246247
mempool.addUnchecked(hash, CTxMemPoolEntry(tx2, 11, GetTime(), 111.0, 11));
247248
BOOST_CHECK(!CheckFinalTx(tx2, LOCKTIME_MEDIAN_TIME_PAST));
248249

249-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
250+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
250251

251252
// Neither tx should have make it into the template.
252253
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 1);
@@ -261,7 +262,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
261262
//BOOST_CHECK(CheckFinalTx(tx));
262263
//BOOST_CHECK(CheckFinalTx(tx2));
263264

264-
BOOST_CHECK(pblocktemplate = CreateNewBlock(scriptPubKey));
265+
BOOST_CHECK(pblocktemplate = CreateNewBlock(chainparams, scriptPubKey));
265266
BOOST_CHECK_EQUAL(pblocktemplate->block.vtx.size(), 2);
266267
delete pblocktemplate;
267268

src/test/test_bitcoin.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
114114
CBlock
115115
TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
116116
{
117-
CBlockTemplate *pblocktemplate = CreateNewBlock(scriptPubKey);
117+
const CChainParams& chainparams = Params();
118+
CBlockTemplate *pblocktemplate = CreateNewBlock(chainparams, scriptPubKey);
118119
CBlock& block = pblocktemplate->block;
119120

120121
// Replace mempool-selected txns with just coinbase plus passed-in txns:
@@ -125,10 +126,10 @@ TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>&
125126
unsigned int extraNonce = 0;
126127
IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
127128

128-
while (!CheckProofOfWork(block.GetHash(), block.nBits, Params(CBaseChainParams::REGTEST).GetConsensus())) ++block.nNonce;
129+
while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
129130

130131
CValidationState state;
131-
ProcessNewBlock(state, NULL, &block, true, NULL);
132+
ProcessNewBlock(state, chainparams, NULL, &block, true, NULL);
132133

133134
CBlock result = block;
134135
delete pblocktemplate;

0 commit comments

Comments
 (0)