Skip to content

Commit 2e29e7e

Browse files
committed
Globals: Remove a bunch of Params() calls from main.cpp:
1) Chainparams: Explicit CChainParams arg for main: -AcceptBlock -AcceptBlockHeader -ActivateBestChain -ConnectTip -InitBlockIndex -LoadExternalBlockFile -VerifyDB parametric constructor 2) Also pickup more Params()\. in main.cpp 3) Pass nPruneAfterHeight explicitly to new FindFilesToPrune() in main.cpp
1 parent eac53ec commit 2e29e7e

File tree

5 files changed

+44
-49
lines changed

5 files changed

+44
-49
lines changed

src/init.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ void CleanupBlockRevFiles()
588588

589589
void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
590590
{
591+
const CChainParams& chainparams = Params();
591592
RenameThread("bitcoin-loadblk");
592593
// -reindex
593594
if (fReindex) {
@@ -601,14 +602,14 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
601602
if (!file)
602603
break; // This error is logged in OpenBlockFile
603604
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
604-
LoadExternalBlockFile(file, &pos);
605+
LoadExternalBlockFile(chainparams, file, &pos);
605606
nFile++;
606607
}
607608
pblocktree->WriteReindexing(false);
608609
fReindex = false;
609610
LogPrintf("Reindexing finished\n");
610611
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
611-
InitBlockIndex();
612+
InitBlockIndex(chainparams);
612613
}
613614

614615
// hardcoded $DATADIR/bootstrap.dat
@@ -619,7 +620,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
619620
CImportingNow imp;
620621
boost::filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
621622
LogPrintf("Importing bootstrap.dat...\n");
622-
LoadExternalBlockFile(file);
623+
LoadExternalBlockFile(chainparams, file);
623624
RenameOver(pathBootstrap, pathBootstrapOld);
624625
} else {
625626
LogPrintf("Warning: Could not open bootstrap file %s\n", pathBootstrap.string());
@@ -632,7 +633,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
632633
if (file) {
633634
CImportingNow imp;
634635
LogPrintf("Importing blocks file %s...\n", path.string());
635-
LoadExternalBlockFile(file);
636+
LoadExternalBlockFile(chainparams, file);
636637
} else {
637638
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
638639
}
@@ -1297,7 +1298,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
12971298
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
12981299

12991300
// Initialize the block index (no-op if non-empty database was already loaded)
1300-
if (!InitBlockIndex()) {
1301+
if (!InitBlockIndex(chainparams)) {
13011302
strLoadError = _("Error initializing block database");
13021303
break;
13031304
}
@@ -1332,7 +1333,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13321333
}
13331334
}
13341335

1335-
if (!CVerifyDB().VerifyDB(pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1336+
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
13361337
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
13371338
strLoadError = _("Corrupted block database detected");
13381339
break;
@@ -1556,7 +1557,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
15561557
uiInterface.InitMessage(_("Activating best chain..."));
15571558
// scan for better chains in the block chain database, that are not yet connected in the active best chain
15581559
CValidationState state;
1559-
if (!ActivateBestChain(state))
1560+
if (!ActivateBestChain(state, chainparams))
15601561
strErrors << "Failed to connect best block";
15611562

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

src/main.cpp

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,7 @@ enum FlushStateMode {
19391939
* or always and in all cases if we're in prune mode and are deleting files.
19401940
*/
19411941
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
1942+
const CChainParams& chainparams = Params();
19421943
LOCK2(cs_main, cs_LastBlockFile);
19431944
static int64_t nLastWrite = 0;
19441945
static int64_t nLastFlush = 0;
@@ -1947,7 +1948,7 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
19471948
bool fFlushForPrune = false;
19481949
try {
19491950
if (fPruneMode && fCheckForPruning && !fReindex) {
1950-
FindFilesToPrune(setFilesToPrune);
1951+
FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
19511952
fCheckForPruning = false;
19521953
if (!setFilesToPrune.empty()) {
19531954
fFlushForPrune = true;
@@ -2147,8 +2148,8 @@ static int64_t nTimePostConnect = 0;
21472148
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
21482149
* corresponding to pindexNew, to bypass loading it again from disk.
21492150
*/
2150-
bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, const CBlock *pblock) {
2151-
const CChainParams& chainparams = Params();
2151+
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock)
2152+
{
21522153
assert(pindexNew->pprev == chainActive.Tip());
21532154
mempool.check(pcoinsTip);
21542155
// Read block from disk.
@@ -2280,8 +2281,8 @@ static void PruneBlockIndexCandidates() {
22802281
* Try to make some progress towards making pindexMostWork the active block.
22812282
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
22822283
*/
2283-
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, const CBlock *pblock) {
2284-
const CChainParams& chainparams = Params();
2284+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock)
2285+
{
22852286
AssertLockHeld(cs_main);
22862287
bool fInvalidFound = false;
22872288
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2314,7 +2315,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
23142315

23152316
// Connect new blocks.
23162317
BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2317-
if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
2318+
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
23182319
if (state.IsInvalid()) {
23192320
// The block violates a consensus rule.
23202321
if (!state.CorruptionPossible())
@@ -2355,10 +2356,10 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
23552356
* or an activated best chain. pblock is either NULL or a pointer to a block
23562357
* that is already loaded (to avoid loading it again from disk).
23572358
*/
2358-
bool ActivateBestChain(CValidationState &state, const CBlock *pblock) {
2359+
bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, const CBlock* pblock)
2360+
{
23592361
CBlockIndex *pindexNewTip = NULL;
23602362
CBlockIndex *pindexMostWork = NULL;
2361-
const CChainParams& chainparams = Params();
23622363
do {
23632364
boost::this_thread::interruption_point();
23642365

@@ -2371,7 +2372,7 @@ bool ActivateBestChain(CValidationState &state, const CBlock *pblock) {
23712372
if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
23722373
return true;
23732374

2374-
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
2375+
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
23752376
return false;
23762377

23772378
pindexNewTip = chainActive.Tip();
@@ -2850,9 +2851,9 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
28502851
return true;
28512852
}
28522853

2853-
bool AcceptBlock(const CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
2854+
/** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
2855+
static bool AcceptBlock(const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
28542856
{
2855-
const CChainParams& chainparams = Params();
28562857
AssertLockHeld(cs_main);
28572858

28582859
CBlockIndex *&pindex = *ppindex;
@@ -2942,7 +2943,7 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, c
29422943

29432944
// Store to disk
29442945
CBlockIndex *pindex = NULL;
2945-
bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp);
2946+
bool ret = AcceptBlock(*pblock, state, chainparams, &pindex, fRequested, dbp);
29462947
if (pindex && pfrom) {
29472948
mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
29482949
}
@@ -2951,7 +2952,7 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, c
29512952
return error("%s: AcceptBlock FAILED", __func__);
29522953
}
29532954

2954-
if (!ActivateBestChain(state, pblock))
2955+
if (!ActivateBestChain(state, chainparams, pblock))
29552956
return error("%s: ActivateBestChain failed", __func__);
29562957

29572958
return true;
@@ -3041,13 +3042,13 @@ void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
30413042
}
30423043

30433044
/* Calculate the block/rev files that should be deleted to remain under target*/
3044-
void FindFilesToPrune(std::set<int>& setFilesToPrune)
3045+
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
30453046
{
30463047
LOCK2(cs_main, cs_LastBlockFile);
30473048
if (chainActive.Tip() == NULL || nPruneTarget == 0) {
30483049
return;
30493050
}
3050-
if (chainActive.Tip()->nHeight <= Params().PruneAfterHeight()) {
3051+
if (chainActive.Tip()->nHeight <= nPruneAfterHeight) {
30513052
return;
30523053
}
30533054

@@ -3275,9 +3276,8 @@ CVerifyDB::~CVerifyDB()
32753276
uiInterface.ShowProgress("", 100);
32763277
}
32773278

3278-
bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
3279+
bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
32793280
{
3280-
const CChainParams& chainparams = Params();
32813281
LOCK(cs_main);
32823282
if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
32833283
return true;
@@ -3393,9 +3393,8 @@ bool LoadBlockIndex()
33933393
return true;
33943394
}
33953395

3396-
3397-
bool InitBlockIndex() {
3398-
const CChainParams& chainparams = Params();
3396+
bool InitBlockIndex(const CChainParams& chainparams)
3397+
{
33993398
LOCK(cs_main);
34003399

34013400
// Initialize global variables that cannot be constructed at startup.
@@ -3413,7 +3412,7 @@ bool InitBlockIndex() {
34133412
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
34143413
if (!fReindex) {
34153414
try {
3416-
CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
3415+
CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
34173416
// Start new block file
34183417
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
34193418
CDiskBlockPos blockPos;
@@ -3425,7 +3424,7 @@ bool InitBlockIndex() {
34253424
CBlockIndex *pindex = AddToBlockIndex(block);
34263425
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
34273426
return error("LoadBlockIndex(): genesis block not accepted");
3428-
if (!ActivateBestChain(state, &block))
3427+
if (!ActivateBestChain(state, chainparams, &block))
34293428
return error("LoadBlockIndex(): genesis block cannot be activated");
34303429
// Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
34313430
return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
@@ -3437,11 +3436,8 @@ bool InitBlockIndex() {
34373436
return true;
34383437
}
34393438

3440-
3441-
3442-
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
3439+
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
34433440
{
3444-
const CChainParams& chainparams = Params();
34453441
// Map of disk positions for blocks with unknown parent (only used for reindex)
34463442
static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
34473443
int64_t nStart = GetTimeMillis();
@@ -3461,10 +3457,10 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
34613457
try {
34623458
// locate a header
34633459
unsigned char buf[MESSAGE_START_SIZE];
3464-
blkdat.FindByte(Params().MessageStart()[0]);
3460+
blkdat.FindByte(chainparams.MessageStart()[0]);
34653461
nRewind = blkdat.GetPos()+1;
34663462
blkdat >> FLATDATA(buf);
3467-
if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
3463+
if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
34683464
continue;
34693465
// read size
34703466
blkdat >> nSize;
@@ -3858,7 +3854,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
38583854
// best equivalent proof of work) than the best header chain we know about.
38593855
send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
38603856
(pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
3861-
(GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, Params().GetConsensus()) < nOneMonth);
3857+
(GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
38623858
if (!send) {
38633859
LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
38643860
}
@@ -4701,7 +4697,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
47014697
uint256 alertHash = alert.GetHash();
47024698
if (pfrom->setKnown.count(alertHash) == 0)
47034699
{
4704-
if (alert.ProcessAlert(Params().AlertKey()))
4700+
if (alert.ProcessAlert(chainparams.AlertKey()))
47054701
{
47064702
// Relay
47074703
pfrom->setKnown.insert(alertHash);

src/main.h

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

205205
/**
@@ -217,7 +217,7 @@ CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
217217
*
218218
* @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
219219
*/
220-
void FindFilesToPrune(std::set<int>& setFilesToPrune);
220+
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
221221

222222
/**
223223
* Actually unlink the specified files
@@ -383,9 +383,6 @@ bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIn
383383
/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
384384
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
385385

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

390387
class CBlockFileInfo
391388
{
@@ -446,7 +443,7 @@ class CVerifyDB {
446443
public:
447444
CVerifyDB();
448445
~CVerifyDB();
449-
bool VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
446+
bool VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
450447
};
451448

452449
/** 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
@@ -562,7 +562,7 @@ UniValue verifychain(const UniValue& params, bool fHelp)
562562
if (params.size() > 1)
563563
nCheckDepth = params[1].get_int();
564564

565-
return CVerifyDB().VerifyDB(pcoinsTip, nCheckLevel, nCheckDepth);
565+
return CVerifyDB().VerifyDB(Params(), pcoinsTip, nCheckLevel, nCheckDepth);
566566
}
567567

568568
/** Implementation of IsSuperMajority with better feedback */
@@ -828,7 +828,7 @@ UniValue invalidateblock(const UniValue& params, bool fHelp)
828828
}
829829

830830
if (state.IsValid()) {
831-
ActivateBestChain(state);
831+
ActivateBestChain(state, Params());
832832
}
833833

834834
if (!state.IsValid()) {
@@ -867,7 +867,7 @@ UniValue reconsiderblock(const UniValue& params, bool fHelp)
867867
}
868868

869869
if (state.IsValid()) {
870-
ActivateBestChain(state);
870+
ActivateBestChain(state, Params());
871871
}
872872

873873
if (!state.IsValid()) {

src/test/test_bitcoin.cpp

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

5151
TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
5252
{
53+
const CChainParams& chainparams = Params();
5354
#ifdef ENABLE_WALLET
5455
bitdb.MakeMock();
5556
#endif
@@ -60,7 +61,7 @@ TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(cha
6061
pblocktree = new CBlockTreeDB(1 << 20, true);
6162
pcoinsdbview = new CCoinsViewDB(1 << 23, true);
6263
pcoinsTip = new CCoinsViewCache(pcoinsdbview);
63-
InitBlockIndex();
64+
InitBlockIndex(chainparams);
6465
#ifdef ENABLE_WALLET
6566
bool fFirstRun;
6667
pwalletMain = new CWallet("wallet.dat");

0 commit comments

Comments
 (0)