diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -2488,7 +2488,7 @@ // Note that it also sets fReindex based on the disk flag! // From here on out fReindex and fReset mean something // different! - if (!LoadBlockIndex(params)) { + if (!chainman.LoadBlockIndex(params)) { if (ShutdownRequested()) { break; } diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -331,13 +331,6 @@ */ bool LoadGenesisBlock(const CChainParams &chainparams); -/** - * Load the block tree and coins database from disk, initializing state if we're - * running with -reindex. - */ -bool LoadBlockIndex(const Consensus::Params ¶ms) - EXCLUSIVE_LOCKS_REQUIRED(cs_main); - /** * Unload database information. */ @@ -1223,6 +1216,11 @@ CChain &ValidatedChain() const { return ValidatedChainstate().m_chain; } CBlockIndex *ValidatedTip() const { return ValidatedChain().Tip(); } + //! Load the block tree and coins database from disk, initializing state if + //! we're running with -reindex + bool LoadBlockIndex(const Consensus::Params ¶ms) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + //! Unload block index and chain data before shutdown. void Unload() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4613,9 +4613,10 @@ m_block_index.clear(); } -static bool LoadBlockIndexDB(const Consensus::Params ¶ms) +static bool LoadBlockIndexDB(ChainstateManager &chainman, + const Consensus::Params ¶ms) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { - if (!g_chainman.m_blockman.LoadBlockIndex( + if (!chainman.m_blockman.LoadBlockIndex( params, *pblocktree, ::ChainstateActive().setBlockIndexCandidates)) { return false; @@ -4628,10 +4629,8 @@ for (int nFile = 0; nFile <= nLastBlockFile; nFile++) { pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]); } - LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString()); - for (int nFile = nLastBlockFile + 1; true; nFile++) { CBlockFileInfo info; if (pblocktree->ReadBlockFileInfo(nFile, info)) { @@ -4645,7 +4644,7 @@ LogPrintf("Checking all blk files are present...\n"); std::set setBlkDataFiles; for (const std::pair &item : - g_chainman.BlockIndex()) { + chainman.BlockIndex()) { CBlockIndex *pindex = item.second; if (pindex->nStatus.hasData()) { setBlkDataFiles.insert(pindex->nFile); @@ -5036,16 +5035,17 @@ fHavePruned = false; } -bool LoadBlockIndex(const Consensus::Params ¶ms) { +bool ChainstateManager::LoadBlockIndex(const Consensus::Params ¶ms) { + AssertLockHeld(cs_main); // Load block index from databases bool needs_init = fReindex; if (!fReindex) { - bool ret = LoadBlockIndexDB(params); + bool ret = LoadBlockIndexDB(*this, params); if (!ret) { return false; } - needs_init = g_chainman.m_blockman.m_block_index.empty(); + needs_init = m_blockman.m_block_index.empty(); } if (needs_init) {