Skip to content

Commit 4868fb0

Browse files
committed
Chainparams: Explicit CChainParams arg for main:
-AcceptBlock -AcceptBlockHeader -ActivateBestChain -ConnectTip -InitBlockIndex -LoadExternalBlockFile -VerifyDB parametric constructor
1 parent afb8795 commit 4868fb0

File tree

5 files changed

+40
-45
lines changed

5 files changed

+40
-45
lines changed

src/init.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,6 +489,7 @@ void DeleteAllBlockFiles()
489489

490490
void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
491491
{
492+
const CChainParams& chainparams = Params();
492493
RenameThread("bitcoin-loadblk");
493494
// -reindex
494495
if (fReindex) {
@@ -502,14 +503,14 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
502503
if (!file)
503504
break; // This error is logged in OpenBlockFile
504505
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
505-
LoadExternalBlockFile(file, &pos);
506+
LoadExternalBlockFile(chainparams, file, &pos);
506507
nFile++;
507508
}
508509
pblocktree->WriteReindexing(false);
509510
fReindex = false;
510511
LogPrintf("Reindexing finished\n");
511512
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
512-
InitBlockIndex();
513+
InitBlockIndex(chainparams);
513514
}
514515

515516
// hardcoded $DATADIR/bootstrap.dat
@@ -520,7 +521,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
520521
CImportingNow imp;
521522
boost::filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
522523
LogPrintf("Importing bootstrap.dat...\n");
523-
LoadExternalBlockFile(file);
524+
LoadExternalBlockFile(chainparams, file);
524525
RenameOver(pathBootstrap, pathBootstrapOld);
525526
} else {
526527
LogPrintf("Warning: Could not open bootstrap file %s\n", pathBootstrap.string());
@@ -533,7 +534,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
533534
if (file) {
534535
CImportingNow imp;
535536
LogPrintf("Importing blocks file %s...\n", path.string());
536-
LoadExternalBlockFile(file);
537+
LoadExternalBlockFile(chainparams, file);
537538
} else {
538539
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
539540
}
@@ -1115,7 +1116,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11151116
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
11161117

11171118
// Initialize the block index (no-op if non-empty database was already loaded)
1118-
if (!InitBlockIndex()) {
1119+
if (!InitBlockIndex(chainparams)) {
11191120
strLoadError = _("Error initializing block database");
11201121
break;
11211122
}
@@ -1138,7 +1139,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
11381139
LogPrintf("Prune: pruned datadir may not have more than %d blocks; -checkblocks=%d may fail\n",
11391140
MIN_BLOCKS_TO_KEEP, GetArg("-checkblocks", 288));
11401141
}
1141-
if (!CVerifyDB().VerifyDB(pcoinsdbview, GetArg("-checklevel", 3),
1142+
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", 3),
11421143
GetArg("-checkblocks", 288))) {
11431144
strLoadError = _("Corrupted block database detected");
11441145
break;
@@ -1345,7 +1346,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13451346
uiInterface.InitMessage(_("Activating best chain..."));
13461347
// scan for better chains in the block chain database, that are not yet connected in the active best chain
13471348
CValidationState state;
1348-
if (!ActivateBestChain(state))
1349+
if (!ActivateBestChain(state, chainparams))
13491350
strErrors << "Failed to connect best block";
13501351

13511352
std::vector<boost::filesystem::path> vImportFiles;

src/main.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,8 +2091,8 @@ static int64_t nTimePostConnect = 0;
20912091
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
20922092
* corresponding to pindexNew, to bypass loading it again from disk.
20932093
*/
2094-
bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *pblock) {
2095-
const CChainParams& chainparams = Params();
2094+
bool static ConnectTip(CValidationState &state, const CChainParams& chainparams, CBlockIndex *pindexNew, CBlock *pblock)
2095+
{
20962096
assert(pindexNew->pprev == chainActive.Tip());
20972097
mempool.check(pcoinsTip);
20982098
// Read block from disk.
@@ -2225,8 +2225,8 @@ static void PruneBlockIndexCandidates() {
22252225
* Try to make some progress towards making pindexMostWork the active block.
22262226
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
22272227
*/
2228-
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
2229-
const CChainParams& chainparams = Params();
2228+
static bool ActivateBestChainStep(CValidationState &state, const CChainParams& chainparams, CBlockIndex *pindexMostWork, CBlock *pblock)
2229+
{
22302230
AssertLockHeld(cs_main);
22312231
bool fInvalidFound = false;
22322232
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2257,7 +2257,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
22572257

22582258
// Connect new blocks.
22592259
BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2260-
if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
2260+
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
22612261
if (state.IsInvalid()) {
22622262
// The block violates a consensus rule.
22632263
if (!state.CorruptionPossible())
@@ -2295,8 +2295,8 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
22952295
* or an activated best chain. pblock is either NULL or a pointer to a block
22962296
* that is already loaded (to avoid loading it again from disk).
22972297
*/
2298-
bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
2299-
const CChainParams& chainparams = Params();
2298+
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, CBlock *pblock)
2299+
{
23002300
CBlockIndex *pindexNewTip = NULL;
23012301
CBlockIndex *pindexMostWork = NULL;
23022302
const CChainParams& chainParams = Params();
@@ -2312,7 +2312,7 @@ bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
23122312
if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
23132313
return true;
23142314

2315-
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
2315+
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
23162316
return false;
23172317

23182318
pindexNewTip = chainActive.Tip();
@@ -2726,9 +2726,8 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
27262726
return true;
27272727
}
27282728

2729-
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
2729+
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
27302730
{
2731-
const CChainParams& chainparams = Params();
27322731
AssertLockHeld(cs_main);
27332732
// Check for duplicate
27342733
uint256 hash = block.GetHash();
@@ -2770,14 +2769,13 @@ bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBloc
27702769
return true;
27712770
}
27722771

2773-
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, CDiskBlockPos* dbp)
2772+
bool AcceptBlock(CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, CDiskBlockPos* dbp)
27742773
{
2775-
const CChainParams& chainparams = Params();
27762774
AssertLockHeld(cs_main);
27772775

27782776
CBlockIndex *&pindex = *ppindex;
27792777

2780-
if (!AcceptBlockHeader(block, state, &pindex))
2778+
if (!AcceptBlockHeader(block, state, chainparams, &pindex))
27812779
return false;
27822780

27832781
// If we're pruning, ensure that we don't allow a peer to dump a copy
@@ -2850,7 +2848,7 @@ bool ProcessNewBlock(CValidationState &state, const CChainParams& chainparams, C
28502848

28512849
// Store to disk
28522850
CBlockIndex *pindex = NULL;
2853-
bool ret = AcceptBlock(*pblock, state, &pindex, dbp);
2851+
bool ret = AcceptBlock(*pblock, state, chainparams, &pindex, dbp);
28542852
if (pindex && pfrom) {
28552853
mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
28562854
}
@@ -2859,7 +2857,7 @@ bool ProcessNewBlock(CValidationState &state, const CChainParams& chainparams, C
28592857
return error("%s: AcceptBlock FAILED", __func__);
28602858
}
28612859

2862-
if (!ActivateBestChain(state, pblock))
2860+
if (!ActivateBestChain(state, chainparams, pblock))
28632861
return error("%s: ActivateBestChain failed", __func__);
28642862

28652863
return true;
@@ -3181,9 +3179,8 @@ CVerifyDB::~CVerifyDB()
31813179
uiInterface.ShowProgress("", 100);
31823180
}
31833181

3184-
bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
3182+
bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
31853183
{
3186-
const CChainParams& chainparams = Params();
31873184
LOCK(cs_main);
31883185
if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
31893186
return true;
@@ -3298,9 +3295,8 @@ bool LoadBlockIndex()
32983295
return true;
32993296
}
33003297

3301-
3302-
bool InitBlockIndex() {
3303-
const CChainParams& chainparams = Params();
3298+
bool InitBlockIndex(const CChainParams& chainparams)
3299+
{
33043300
LOCK(cs_main);
33053301
// Check whether we're already initialized
33063302
if (chainActive.Genesis() != NULL)
@@ -3314,7 +3310,7 @@ bool InitBlockIndex() {
33143310
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
33153311
if (!fReindex) {
33163312
try {
3317-
CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
3313+
CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
33183314
// Start new block file
33193315
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
33203316
CDiskBlockPos blockPos;
@@ -3326,7 +3322,7 @@ bool InitBlockIndex() {
33263322
CBlockIndex *pindex = AddToBlockIndex(block);
33273323
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
33283324
return error("LoadBlockIndex(): genesis block not accepted");
3329-
if (!ActivateBestChain(state, &block))
3325+
if (!ActivateBestChain(state, chainparams, &block))
33303326
return error("LoadBlockIndex(): genesis block cannot be activated");
33313327
// Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
33323328
return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
@@ -3338,11 +3334,8 @@ bool InitBlockIndex() {
33383334
return true;
33393335
}
33403336

3341-
3342-
3343-
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
3337+
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
33443338
{
3345-
const CChainParams& chainparams = Params();
33463339
// Map of disk positions for blocks with unknown parent (only used for reindex)
33473340
static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
33483341
int64_t nStart = GetTimeMillis();
@@ -3362,10 +3355,10 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
33623355
try {
33633356
// locate a header
33643357
unsigned char buf[MESSAGE_START_SIZE];
3365-
blkdat.FindByte(Params().MessageStart()[0]);
3358+
blkdat.FindByte(chainparams.MessageStart()[0]);
33663359
nRewind = blkdat.GetPos()+1;
33673360
blkdat >> FLATDATA(buf);
3368-
if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
3361+
if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
33693362
continue;
33703363
// read size
33713364
blkdat >> nSize;
@@ -4368,7 +4361,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
43684361
Misbehaving(pfrom->GetId(), 20);
43694362
return error("non-continuous headers sequence");
43704363
}
4371-
if (!AcceptBlockHeader(header, state, &pindexLast)) {
4364+
if (!AcceptBlockHeader(header, state, chainparams, &pindexLast)) {
43724365
int nDoS;
43734366
if (state.IsInvalid(nDoS)) {
43744367
if (nDoS > 0)

src/main.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
176176
/** Translation to a filesystem path */
177177
boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix);
178178
/** Import blocks from an external file */
179-
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp = NULL);
179+
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp = NULL);
180180
/** Initialize a new block tree database + block data on disk */
181-
bool InitBlockIndex();
181+
bool InitBlockIndex(const CChainParams& chainparams);
182182
/** Load the block tree and coins database from disk */
183183
bool LoadBlockIndex();
184184
/** Unload database information */
@@ -201,7 +201,7 @@ std::string GetWarnings(std::string strFor);
201201
/** Retrieve a transaction (from memory pool, or from disk, if possible) */
202202
bool GetTransaction(const uint256 &hash, CTransaction &tx, const Consensus::Params& params, uint256 &hashBlock, bool fAllowSlow = false);
203203
/** Find the best known block, and make it the tip of the block chain */
204-
bool ActivateBestChain(CValidationState &state, CBlock *pblock = NULL);
204+
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, CBlock *pblock = NULL);
205205
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
206206

207207
/**
@@ -397,7 +397,7 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Co
397397
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
398398

399399
/** Store block on disk. If dbp is provided, the file is known to already reside on disk */
400-
bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, CDiskBlockPos* dbp = NULL);
400+
bool AcceptBlock(CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex **pindex, CDiskBlockPos* dbp = NULL);
401401
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex **ppindex= NULL);
402402

403403

@@ -519,7 +519,7 @@ class CVerifyDB {
519519
public:
520520
CVerifyDB();
521521
~CVerifyDB();
522-
bool VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
522+
bool VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
523523
};
524524

525525
/** Find the last common block between the parameter chain and a locator. */

src/rpcblockchain.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ Value verifychain(const Array& params, bool fHelp)
461461
if (params.size() > 1)
462462
nCheckDepth = params[1].get_int();
463463

464-
return CVerifyDB().VerifyDB(pcoinsTip, nCheckLevel, nCheckDepth);
464+
return CVerifyDB().VerifyDB(Params(), pcoinsTip, nCheckLevel, nCheckDepth);
465465
}
466466

467467
Value getblockchaininfo(const Array& params, bool fHelp)
@@ -663,7 +663,7 @@ Value invalidateblock(const Array& params, bool fHelp)
663663
}
664664

665665
if (state.IsValid()) {
666-
ActivateBestChain(state);
666+
ActivateBestChain(state, Params());
667667
}
668668

669669
if (!state.IsValid()) {
@@ -702,7 +702,7 @@ Value reconsiderblock(const Array& params, bool fHelp)
702702
}
703703

704704
if (state.IsValid()) {
705-
ActivateBestChain(state);
705+
ActivateBestChain(state, Params());
706706
}
707707

708708
if (!state.IsValid()) {

src/test/test_bitcoin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ BasicTestingSetup::~BasicTestingSetup()
4242

4343
TestingSetup::TestingSetup()
4444
{
45+
const CChainParams& chainparams = Params(CBaseChainParams::MAIN);
4546
#ifdef ENABLE_WALLET
4647
bitdb.MakeMock();
4748
#endif
@@ -52,7 +53,7 @@ TestingSetup::TestingSetup()
5253
pblocktree = new CBlockTreeDB(1 << 20, true);
5354
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
5455
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
55-
InitBlockIndex();
56+
InitBlockIndex(chainparams);
5657
#ifdef ENABLE_WALLET
5758
bool fFirstRun;
5859
pwalletMain = new CWallet("wallet.dat");

0 commit comments

Comments
 (0)