Skip to content

Commit 5ca149a

Browse files
committed
Merge pull request #7053
2e29e7e Globals: Remove a bunch of Params() calls from main.cpp: (Jorge Timón)
2 parents 0e93586 + 2e29e7e commit 5ca149a

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
@@ -592,6 +592,7 @@ void CleanupBlockRevFiles()
592592

593593
void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
594594
{
595+
const CChainParams& chainparams = Params();
595596
RenameThread("bitcoin-loadblk");
596597
// -reindex
597598
if (fReindex) {
@@ -605,14 +606,14 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
605606
if (!file)
606607
break; // This error is logged in OpenBlockFile
607608
LogPrintf("Reindexing block file blk%05u.dat...\n", (unsigned int)nFile);
608-
LoadExternalBlockFile(file, &pos);
609+
LoadExternalBlockFile(chainparams, file, &pos);
609610
nFile++;
610611
}
611612
pblocktree->WriteReindexing(false);
612613
fReindex = false;
613614
LogPrintf("Reindexing finished\n");
614615
// To avoid ending up in a situation without genesis block, re-try initializing (no-op if reindexing worked):
615-
InitBlockIndex();
616+
InitBlockIndex(chainparams);
616617
}
617618

618619
// hardcoded $DATADIR/bootstrap.dat
@@ -623,7 +624,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
623624
CImportingNow imp;
624625
boost::filesystem::path pathBootstrapOld = GetDataDir() / "bootstrap.dat.old";
625626
LogPrintf("Importing bootstrap.dat...\n");
626-
LoadExternalBlockFile(file);
627+
LoadExternalBlockFile(chainparams, file);
627628
RenameOver(pathBootstrap, pathBootstrapOld);
628629
} else {
629630
LogPrintf("Warning: Could not open bootstrap file %s\n", pathBootstrap.string());
@@ -636,7 +637,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
636637
if (file) {
637638
CImportingNow imp;
638639
LogPrintf("Importing blocks file %s...\n", path.string());
639-
LoadExternalBlockFile(file);
640+
LoadExternalBlockFile(chainparams, file);
640641
} else {
641642
LogPrintf("Warning: Could not open blocks file %s\n", path.string());
642643
}
@@ -1301,7 +1302,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13011302
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
13021303

13031304
// Initialize the block index (no-op if non-empty database was already loaded)
1304-
if (!InitBlockIndex()) {
1305+
if (!InitBlockIndex(chainparams)) {
13051306
strLoadError = _("Error initializing block database");
13061307
break;
13071308
}
@@ -1336,7 +1337,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
13361337
}
13371338
}
13381339

1339-
if (!CVerifyDB().VerifyDB(pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
1340+
if (!CVerifyDB().VerifyDB(chainparams, pcoinsdbview, GetArg("-checklevel", DEFAULT_CHECKLEVEL),
13401341
GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
13411342
strLoadError = _("Corrupted block database detected");
13421343
break;
@@ -1560,7 +1561,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
15601561
uiInterface.InitMessage(_("Activating best chain..."));
15611562
// scan for better chains in the block chain database, that are not yet connected in the active best chain
15621563
CValidationState state;
1563-
if (!ActivateBestChain(state))
1564+
if (!ActivateBestChain(state, chainparams))
15641565
strErrors << "Failed to connect best block";
15651566

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

src/main.cpp

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,7 @@ enum FlushStateMode {
21392139
* or always and in all cases if we're in prune mode and are deleting files.
21402140
*/
21412141
bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
2142+
const CChainParams& chainparams = Params();
21422143
LOCK2(cs_main, cs_LastBlockFile);
21432144
static int64_t nLastWrite = 0;
21442145
static int64_t nLastFlush = 0;
@@ -2147,7 +2148,7 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
21472148
bool fFlushForPrune = false;
21482149
try {
21492150
if (fPruneMode && fCheckForPruning && !fReindex) {
2150-
FindFilesToPrune(setFilesToPrune);
2151+
FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
21512152
fCheckForPruning = false;
21522153
if (!setFilesToPrune.empty()) {
21532154
fFlushForPrune = true;
@@ -2347,8 +2348,8 @@ static int64_t nTimePostConnect = 0;
23472348
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
23482349
* corresponding to pindexNew, to bypass loading it again from disk.
23492350
*/
2350-
bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, const CBlock *pblock) {
2351-
const CChainParams& chainparams = Params();
2351+
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock)
2352+
{
23522353
assert(pindexNew->pprev == chainActive.Tip());
23532354
mempool.check(pcoinsTip);
23542355
// Read block from disk.
@@ -2480,8 +2481,8 @@ static void PruneBlockIndexCandidates() {
24802481
* Try to make some progress towards making pindexMostWork the active block.
24812482
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
24822483
*/
2483-
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, const CBlock *pblock) {
2484-
const CChainParams& chainparams = Params();
2484+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock)
2485+
{
24852486
AssertLockHeld(cs_main);
24862487
bool fInvalidFound = false;
24872488
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -2514,7 +2515,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
25142515

25152516
// Connect new blocks.
25162517
BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2517-
if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
2518+
if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
25182519
if (state.IsInvalid()) {
25192520
// The block violates a consensus rule.
25202521
if (!state.CorruptionPossible())
@@ -2555,10 +2556,10 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
25552556
* or an activated best chain. pblock is either NULL or a pointer to a block
25562557
* that is already loaded (to avoid loading it again from disk).
25572558
*/
2558-
bool ActivateBestChain(CValidationState &state, const CBlock *pblock) {
2559+
bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, const CBlock* pblock)
2560+
{
25592561
CBlockIndex *pindexNewTip = NULL;
25602562
CBlockIndex *pindexMostWork = NULL;
2561-
const CChainParams& chainparams = Params();
25622563
do {
25632564
boost::this_thread::interruption_point();
25642565

@@ -2571,7 +2572,7 @@ bool ActivateBestChain(CValidationState &state, const CBlock *pblock) {
25712572
if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
25722573
return true;
25732574

2574-
if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
2575+
if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
25752576
return false;
25762577

25772578
pindexNewTip = chainActive.Tip();
@@ -3050,9 +3051,9 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
30503051
return true;
30513052
}
30523053

3053-
bool AcceptBlock(const CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
3054+
/** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
3055+
static bool AcceptBlock(const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
30543056
{
3055-
const CChainParams& chainparams = Params();
30563057
AssertLockHeld(cs_main);
30573058

30583059
CBlockIndex *&pindex = *ppindex;
@@ -3142,7 +3143,7 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, c
31423143

31433144
// Store to disk
31443145
CBlockIndex *pindex = NULL;
3145-
bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp);
3146+
bool ret = AcceptBlock(*pblock, state, chainparams, &pindex, fRequested, dbp);
31463147
if (pindex && pfrom) {
31473148
mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
31483149
}
@@ -3151,7 +3152,7 @@ bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, c
31513152
return error("%s: AcceptBlock FAILED", __func__);
31523153
}
31533154

3154-
if (!ActivateBestChain(state, pblock))
3155+
if (!ActivateBestChain(state, chainparams, pblock))
31553156
return error("%s: ActivateBestChain failed", __func__);
31563157

31573158
return true;
@@ -3241,13 +3242,13 @@ void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
32413242
}
32423243

32433244
/* Calculate the block/rev files that should be deleted to remain under target*/
3244-
void FindFilesToPrune(std::set<int>& setFilesToPrune)
3245+
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
32453246
{
32463247
LOCK2(cs_main, cs_LastBlockFile);
32473248
if (chainActive.Tip() == NULL || nPruneTarget == 0) {
32483249
return;
32493250
}
3250-
if (chainActive.Tip()->nHeight <= Params().PruneAfterHeight()) {
3251+
if (chainActive.Tip()->nHeight <= nPruneAfterHeight) {
32513252
return;
32523253
}
32533254

@@ -3475,9 +3476,8 @@ CVerifyDB::~CVerifyDB()
34753476
uiInterface.ShowProgress("", 100);
34763477
}
34773478

3478-
bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
3479+
bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
34793480
{
3480-
const CChainParams& chainparams = Params();
34813481
LOCK(cs_main);
34823482
if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
34833483
return true;
@@ -3593,9 +3593,8 @@ bool LoadBlockIndex()
35933593
return true;
35943594
}
35953595

3596-
3597-
bool InitBlockIndex() {
3598-
const CChainParams& chainparams = Params();
3596+
bool InitBlockIndex(const CChainParams& chainparams)
3597+
{
35993598
LOCK(cs_main);
36003599

36013600
// Initialize global variables that cannot be constructed at startup.
@@ -3613,7 +3612,7 @@ bool InitBlockIndex() {
36133612
// Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
36143613
if (!fReindex) {
36153614
try {
3616-
CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
3615+
CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
36173616
// Start new block file
36183617
unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
36193618
CDiskBlockPos blockPos;
@@ -3625,7 +3624,7 @@ bool InitBlockIndex() {
36253624
CBlockIndex *pindex = AddToBlockIndex(block);
36263625
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
36273626
return error("LoadBlockIndex(): genesis block not accepted");
3628-
if (!ActivateBestChain(state, &block))
3627+
if (!ActivateBestChain(state, chainparams, &block))
36293628
return error("LoadBlockIndex(): genesis block cannot be activated");
36303629
// Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
36313630
return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
@@ -3637,11 +3636,8 @@ bool InitBlockIndex() {
36373636
return true;
36383637
}
36393638

3640-
3641-
3642-
bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
3639+
bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
36433640
{
3644-
const CChainParams& chainparams = Params();
36453641
// Map of disk positions for blocks with unknown parent (only used for reindex)
36463642
static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
36473643
int64_t nStart = GetTimeMillis();
@@ -3661,10 +3657,10 @@ bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
36613657
try {
36623658
// locate a header
36633659
unsigned char buf[MESSAGE_START_SIZE];
3664-
blkdat.FindByte(Params().MessageStart()[0]);
3660+
blkdat.FindByte(chainparams.MessageStart()[0]);
36653661
nRewind = blkdat.GetPos()+1;
36663662
blkdat >> FLATDATA(buf);
3667-
if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
3663+
if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
36683664
continue;
36693665
// read size
36703666
blkdat >> nSize;
@@ -4058,7 +4054,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
40584054
// best equivalent proof of work) than the best header chain we know about.
40594055
send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
40604056
(pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
4061-
(GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, Params().GetConsensus()) < nOneMonth);
4057+
(GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
40624058
if (!send) {
40634059
LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
40644060
}
@@ -4932,7 +4928,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
49324928
uint256 alertHash = alert.GetHash();
49334929
if (pfrom->setKnown.count(alertHash) == 0)
49344930
{
4935-
if (alert.ProcessAlert(Params().AlertKey()))
4931+
if (alert.ProcessAlert(chainparams.AlertKey()))
49364932
{
49374933
// Relay
49384934
pfrom->setKnown.insert(alertHash);

src/main.h

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

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

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

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

388385
class CBlockFileInfo
389386
{
@@ -444,7 +441,7 @@ class CVerifyDB {
444441
public:
445442
CVerifyDB();
446443
~CVerifyDB();
447-
bool VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
444+
bool VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
448445
};
449446

450447
/** 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
@@ -567,7 +567,7 @@ UniValue verifychain(const UniValue& params, bool fHelp)
567567
if (params.size() > 1)
568568
nCheckDepth = params[1].get_int();
569569

570-
return CVerifyDB().VerifyDB(pcoinsTip, nCheckLevel, nCheckDepth);
570+
return CVerifyDB().VerifyDB(Params(), pcoinsTip, nCheckLevel, nCheckDepth);
571571
}
572572

573573
/** Implementation of IsSuperMajority with better feedback */
@@ -835,7 +835,7 @@ UniValue invalidateblock(const UniValue& params, bool fHelp)
835835
}
836836

837837
if (state.IsValid()) {
838-
ActivateBestChain(state);
838+
ActivateBestChain(state, Params());
839839
}
840840

841841
if (!state.IsValid()) {
@@ -874,7 +874,7 @@ UniValue reconsiderblock(const UniValue& params, bool fHelp)
874874
}
875875

876876
if (state.IsValid()) {
877-
ActivateBestChain(state);
877+
ActivateBestChain(state, Params());
878878
}
879879

880880
if (!state.IsValid()) {

src/test/test_bitcoin.cpp

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

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

0 commit comments

Comments
 (0)