Skip to content

Commit 598e494

Browse files
committed
Chainparams: Explicit CChainParams arg for main (pre miner):
-ProcessNewBlock -TestBlockValidity
1 parent 3ac7060 commit 598e494

File tree

6 files changed

+18
-17
lines changed

6 files changed

+18
-17
lines changed

src/main.cpp

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

29172917

2918-
bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
2918+
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos* dbp)
29192919
{
2920-
const CChainParams& chainparams = Params();
29212920
// Preliminary checks
29222921
bool checked = CheckBlock(*pblock, state);
29232922

@@ -2946,9 +2945,8 @@ bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock*
29462945
return true;
29472946
}
29482947

2949-
bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
2948+
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
29502949
{
2951-
const CChainParams& chainparams = Params();
29522950
AssertLockHeld(cs_main);
29532951
assert(pindexPrev && pindexPrev == chainActive.Tip());
29542952
if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
@@ -3488,7 +3486,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
34883486
// process in case the block isn't known yet
34893487
if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
34903488
CValidationState state;
3491-
if (ProcessNewBlock(state, NULL, &block, true, dbp))
3489+
if (ProcessNewBlock(state, chainparams, NULL, &block, true, dbp))
34923490
nLoaded++;
34933491
if (state.IsError())
34943492
break;
@@ -3510,7 +3508,7 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
35103508
LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
35113509
head.ToString());
35123510
CValidationState dummy;
3513-
if (ProcessNewBlock(dummy, NULL, &block, true, &it->second))
3511+
if (ProcessNewBlock(dummy, chainparams, NULL, &block, true, &it->second))
35143512
{
35153513
nLoaded++;
35163514
queue.push_back(block.GetHash());
@@ -4547,7 +4545,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
45474545
// Such an unrequested block may still be processed, subject to the
45484546
// conditions in AcceptBlock().
45494547
bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
4550-
ProcessNewBlock(state, pfrom, &block, forceProcessing, NULL);
4548+
ProcessNewBlock(state, chainparams, pfrom, &block, forceProcessing, NULL);
45514549
int nDoS;
45524550
if (state.IsInvalid(nDoS)) {
45534551
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;
@@ -159,7 +160,7 @@ void UnregisterNodeSignals(CNodeSignals& nodeSignals);
159160
* @param[out] dbp If pblock is stored to disk (or already there), this will be set to its location.
160161
* @return True if state.IsValid()
161162
*/
162-
bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp);
163+
bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos* dbp);
163164
/** Check whether enough disk space is available for an incoming block */
164165
bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
165166
/** Open a block file (blk?????.dat) */
@@ -378,7 +379,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
378379
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev);
379380

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

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

src/miner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ CBlockTemplate* CreateNewBlock(const CScript& scriptPubKeyIn)
351351
pblocktemplate->vTxSigOps[0] = GetLegacySigOpCount(pblock->vtx[0]);
352352

353353
CValidationState state;
354-
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false))
354+
if (!TestBlockValidity(state, chainparams, *pblock, pindexPrev, false, false))
355355
throw std::runtime_error("CreateNewBlock(): TestBlockValidity failed");
356356
}
357357

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

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

438438
return true;

src/rpcmining.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
@@ -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: 2 additions & 1 deletion
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;
@@ -91,7 +92,7 @@ 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
}

src/test/test_bitcoin.cpp

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

@@ -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)