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 @@ -9,6 +9,7 @@ #include #include // For PeerLogicValidation #include +#include #include diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -250,15 +250,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,9 @@ #include -#include #include #include +#include #include diff --git a/src/init.cpp b/src/init.cpp --- a/src/init.cpp +++ b/src/init.cpp @@ -792,7 +792,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(), @@ -2435,7 +2435,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?") @@ -2467,7 +2467,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)); @@ -2693,7 +2693,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 @@ -1504,8 +1504,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() @@ -1515,7 +1515,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); @@ -1770,11 +1770,11 @@ CBlockIndex *pblockindex; { LOCK(cs_main); - if (mapBlockIndex.count(hash) == 0) { + if (::BlockIndex().count(hash) == 0) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); } - pblockindex = mapBlockIndex[hash]; + pblockindex = ::BlockIndex()[hash]; } BlockValidationState state; ParkBlock(config, state, pblockindex); @@ -1850,11 +1850,11 @@ { LOCK(cs_main); - if (mapBlockIndex.count(hash) == 0) { + if (::BlockIndex().count(hash) == 0) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); } - CBlockIndex *pblockindex = mapBlockIndex[hash]; + CBlockIndex *pblockindex = ::BlockIndex()[hash]; UnparkBlockAndChildren(pblockindex); } diff --git a/src/txdb.cpp b/src/txdb.cpp --- a/src/txdb.cpp +++ b/src/txdb.cpp @@ -271,7 +271,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; @@ -683,6 +684,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) @@ -756,7 +760,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) @@ -988,6 +992,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 @@ -85,7 +85,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; @@ -144,6 +143,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); @@ -821,6 +826,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(); std::string strCmd = gArgs.GetArg("-alertnotify", ""); @@ -3564,7 +3573,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 " @@ -4406,9 +4415,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)); } @@ -4511,7 +4520,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); @@ -4801,21 +4810,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); } @@ -4917,7 +4926,7 @@ return false; } - needs_init = mapBlockIndex.empty(); + needs_init = g_blockman.m_block_index.empty(); } if (needs_init) { @@ -4936,10 +4945,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; } @@ -5134,21 +5143,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> @@ -5265,7 +5274,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 @@ -5667,10 +5676,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 @@ -304,7 +304,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;