diff --git a/src/avalanche/test/processor_tests.cpp b/src/avalanche/test/processor_tests.cpp --- a/src/avalanche/test/processor_tests.cpp +++ b/src/avalanche/test/processor_tests.cpp @@ -10,6 +10,9 @@ #include #include // For PeerLogicValidation #include +// D6970 moved LookupBlockIndex from chain.h to validation.h TODO: remove this +// when LookupBlockIndex is refactored out of validation +#include #include diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -56,15 +56,6 @@ }; extern RecursiveMutex cs_main; -typedef std::unordered_map BlockMap; -extern BlockMap &mapBlockIndex GUARDED_BY(cs_main); - -inline CBlockIndex *LookupBlockIndex(const BlockHash &hash) - EXCLUSIVE_LOCKS_REQUIRED(cs_main) { - AssertLockHeld(cs_main); - BlockMap::const_iterator it = mapBlockIndex.find(hash); - return it == mapBlockIndex.end() ? nullptr : it->second; -} arith_uint256 GetBlockProof(const CBlockIndex &block); diff --git a/src/checkpoints.cpp b/src/checkpoints.cpp --- a/src/checkpoints.cpp +++ b/src/checkpoints.cpp @@ -4,9 +4,11 @@ #include -#include #include #include +// D6970 moved LookupBlockIndex from chain.h to validation.h TODO: remove this +// when LookupBlockIndex is refactored out of validation +#include #include diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -798,7 +798,7 @@ OptionsCategory::DEBUG_TEST); gArgs.AddArg( "-checkblockindex", - strprintf("Do a full consistency check for mapBlockIndex, " + strprintf("Do a full consistency check for the block tree, " "setBlockIndexCandidates, ::ChainActive() and " "mapBlocksUnlinked occasionally. (default: %u, regtest: %u)", defaultChainParams->DefaultConsistencyChecks(), @@ -2444,7 +2444,7 @@ // If the loaded chain has a wrong genesis, bail out immediately // (we're likely using a testnet datadir, or the other way // around). - if (!mapBlockIndex.empty() && + if (!::BlockIndex().empty() && !LookupBlockIndex(params.hashGenesisBlock)) { return InitError(_("Incorrect or no genesis block found. " "Wrong datadir for network?") @@ -2476,7 +2476,7 @@ } // At this point we're either in reindex or we've loaded a - // useful block tree into mapBlockIndex! + // useful block tree into BlockIndex()! pcoinsdbview.reset(new CCoinsViewDB( nCoinDBCache, false, fReset || fReindexChainState)); @@ -2706,7 +2706,7 @@ //// debug print { LOCK(cs_main); - LogPrintf("mapBlockIndex.size() = %u\n", mapBlockIndex.size()); + LogPrintf("block tree size = %u\n", ::BlockIndex().size()); chain_active_height = ::ChainActive().Height(); } LogPrintf("nBestHeight = %d\n", chain_active_height); diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1509,8 +1509,8 @@ /** * Idea: the set of chain tips is ::ChainActive().tip, plus orphan blocks * which do not have another orphan building off of them. Algorithm: - * - Make one pass through mapBlockIndex, picking out the orphan blocks, - * and also storing a set of the orphan block's pprev pointers. + * - Make one pass through g_blockman.m_block_index, picking out the orphan + * blocks, and also storing a set of the orphan block's pprev pointers. * - Iterate through the orphan blocks. If the block isn't pointed to by * another orphan, it is a chain tip. * - add ::ChainActive().Tip() @@ -1520,7 +1520,7 @@ std::set setPrevs; for (const std::pair &item : - mapBlockIndex) { + ::BlockIndex()) { if (!::ChainActive().Contains(item.second)) { setOrphans.insert(item.second); setPrevs.insert(item.second->pprev); diff --git a/src/txdb.cpp b/src/txdb.cpp --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -272,7 +272,7 @@ pcursor->Seek(std::make_pair(DB_BLOCK_INDEX, uint256())); - // Load mapBlockIndex + // Load m_block_index while (pcursor->Valid()) { boost::this_thread::interruption_point(); if (ShutdownRequested()) { diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -171,6 +171,7 @@ extern CScript COINBASE_FLAGS; extern RecursiveMutex cs_main; extern CTxMemPool g_mempool; +typedef std::unordered_map BlockMap; extern Mutex g_best_block_mutex; extern std::condition_variable g_best_block_cv; extern uint256 g_best_block; @@ -682,6 +683,9 @@ /** Replay blocks that aren't fully applied to the database. */ bool ReplayBlocks(const Consensus::Params ¶ms, CCoinsView *view); +CBlockIndex *LookupBlockIndex(const BlockHash &hash) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + /** Find the last common block between the parameter chain and a locator. */ CBlockIndex *FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator) @@ -755,7 +759,7 @@ /** * If a block header hasn't already been seen, call CheckBlockHeader on it, * ensure that it doesn't descend from an invalid block, and then add it to - * mapBlockIndex. + * m_block_index. */ bool AcceptBlockHeader(const Config &config, const CBlockHeader &block, BlockValidationState &state, CBlockIndex **ppindex) @@ -990,6 +994,9 @@ /** @returns the most-work chain. */ CChain &ChainActive(); +/** @returns the global block index map. */ +BlockMap &BlockIndex(); + /** * Global variable that points to the coins database (protected by cs_main) */ diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -86,7 +86,6 @@ */ RecursiveMutex cs_main; -BlockMap &mapBlockIndex = g_blockman.m_block_index; CBlockIndex *pindexBestHeader = nullptr; Mutex g_best_block_mutex; std::condition_variable g_best_block_cv; @@ -143,6 +142,12 @@ : excessiveBlockSize(config.GetMaxBlockSize()), checkPoW(true), checkMerkleRoot(true) {} +CBlockIndex *LookupBlockIndex(const BlockHash &hash) { + AssertLockHeld(cs_main); + BlockMap::const_iterator it = g_blockman.m_block_index.find(hash); + return it == g_blockman.m_block_index.end() ? nullptr : it->second; +} + CBlockIndex *FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator) { AssertLockHeld(cs_main); @@ -820,6 +825,10 @@ static CBlockIndex const *pindexBestForkTip = nullptr; static CBlockIndex const *pindexBestForkBase = nullptr; +BlockMap &BlockIndex() { + return g_blockman.m_block_index; +} + static void AlertNotify(const std::string &strMessage) { uiInterface.NotifyAlertChanged(); #if defined(HAVE_SYSTEM) @@ -3666,7 +3675,7 @@ // Don't accept any forks from the main chain prior to last checkpoint. // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's - // in our MapBlockIndex. + // in our g_blockman.m_block_index. CBlockIndex *pcheckpoint = Checkpoints::GetLastCheckpoint(checkpoints); if (pcheckpoint && nHeight < pcheckpoint->nHeight) { LogPrintf("ERROR: %s: forked chain older than last checkpoint " @@ -4509,9 +4518,9 @@ // Calculate nChainWork std::vector> vSortedByHeight; - vSortedByHeight.reserve(mapBlockIndex.size()); + vSortedByHeight.reserve(m_block_index.size()); for (const std::pair &item : - mapBlockIndex) { + m_block_index) { CBlockIndex *pindex = item.second; vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex)); } @@ -4614,7 +4623,7 @@ LogPrintf("Checking all blk files are present...\n"); std::set setBlkDataFiles; for (const std::pair &item : - mapBlockIndex) { + g_blockman.m_block_index) { CBlockIndex *pindex = item.second; if (pindex->nStatus.hasData()) { setBlkDataFiles.insert(pindex->nFile); @@ -4904,21 +4913,21 @@ // Latest block common to both the old and the new tip. const CBlockIndex *pindexFork = nullptr; - if (mapBlockIndex.count(hashHeads[0]) == 0) { + if (m_blockman.m_block_index.count(hashHeads[0]) == 0) { return error( "ReplayBlocks(): reorganization to unknown block requested"); } - pindexNew = mapBlockIndex[hashHeads[0]]; + pindexNew = m_blockman.m_block_index[hashHeads[0]]; if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush. - if (mapBlockIndex.count(hashHeads[1]) == 0) { + if (m_blockman.m_block_index.count(hashHeads[1]) == 0) { return error( "ReplayBlocks(): reorganization from unknown block requested"); } - pindexOld = mapBlockIndex[hashHeads[1]]; + pindexOld = m_blockman.m_block_index[hashHeads[1]]; pindexFork = LastCommonAncestor(pindexOld, pindexNew); assert(pindexFork != nullptr); } @@ -5021,7 +5030,7 @@ return false; } - needs_init = mapBlockIndex.empty(); + needs_init = g_blockman.m_block_index.empty(); } if (needs_init) { @@ -5040,10 +5049,10 @@ LOCK(cs_main); // Check whether we're already initialized by checking for genesis in - // mapBlockIndex. Note that we can't use m_chain here, since it is - // set based on the coins db, not the block index db, which is the only + // m_blockman.m_block_index. Note that we can't use m_chain here, since it + // is set based on the coins db, not the block index db, which is the only // thing loaded at this point. - if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash())) { + if (m_blockman.m_block_index.count(chainparams.GenesisBlock().GetHash())) { return true; } @@ -5238,21 +5247,21 @@ LOCK(cs_main); // During a reindex, we read the genesis block and call CheckBlockIndex - // before ActivateBestChain, so we have the genesis block in mapBlockIndex - // but no active chain. (A few of the tests when iterating the block tree - // require that m_chain has been initialized.) + // before ActivateBestChain, so we have the genesis block in + // m_blockman.m_block_index but no active chain. (A few of the tests when + // iterating the block tree require that m_chain has been initialized.) if (m_chain.Height() < 0) { - assert(mapBlockIndex.size() <= 1); + assert(m_blockman.m_block_index.size() <= 1); return; } // Build forward-pointing map of the entire block tree. std::multimap forward; - for (const auto &entry : mapBlockIndex) { + for (const auto &entry : m_blockman.m_block_index) { forward.emplace(entry.second->pprev, entry.second); } - assert(forward.size() == mapBlockIndex.size()); + assert(forward.size() == m_blockman.m_block_index.size()); std::pair::iterator, std::multimap::iterator> @@ -5369,7 +5378,7 @@ // The pskip pointer must point back for all but the first 2 blocks. assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); - // All mapBlockIndex entries must at least be TREE valid + // All m_blockman.m_block_index entries must at least be TREE valid assert(pindexFirstNotTreeValid == nullptr); if (pindex->nStatus.getValidity() >= BlockValidity::TREE) { // TREE valid implies all parents are TREE valid @@ -5771,10 +5780,10 @@ ~CMainCleanup() { // block headers for (const std::pair &it : - mapBlockIndex) { + g_blockman.m_block_index) { delete it.second; } - mapBlockIndex.clear(); + g_blockman.m_block_index.clear(); } }; static CMainCleanup instance_of_cmaincleanup; diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -333,7 +333,7 @@ auto locked_chain = wallet.chain().lock(); LockAssertion lock(::cs_main); auto inserted = - mapBlockIndex.emplace(BlockHash(GetRandHash()), new CBlockIndex); + ::BlockIndex().emplace(BlockHash(GetRandHash()), new CBlockIndex); assert(inserted.second); const BlockHash &hash = inserted.first->first; block = inserted.first->second; diff --git a/test/lint/lint-circular-dependencies.sh b/test/lint/lint-circular-dependencies.sh --- a/test/lint/lint-circular-dependencies.sh +++ b/test/lint/lint-circular-dependencies.sh @@ -33,6 +33,7 @@ "seeder/bitcoin -> seeder/db -> seeder/bitcoin" "chainparams -> protocol -> config -> chainparams" "wallet/scriptpubkeyman -> wallet/wallet -> wallet/scriptpubkeyman" + "checkpoints -> validation -> checkpoints" ) EXIT_CODE=0