diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -127,7 +127,7 @@ std::set m_failed_blocks; public: - CChain chainActive; + CChain m_chain; BlockMap mapBlockIndex GUARDED_BY(cs_main); std::multimap mapBlocksUnlinked; CBlockIndex *pindexBestInvalid = nullptr; @@ -250,7 +250,7 @@ RecursiveMutex cs_main; BlockMap &mapBlockIndex = g_chainstate.mapBlockIndex; -CChain &chainActive = g_chainstate.chainActive; +CChain &chainActive = g_chainstate.m_chain; CBlockIndex *pindexBestHeader = nullptr; Mutex g_best_block_mutex; std::condition_variable g_best_block_cv; @@ -2320,7 +2320,7 @@ } /** - * Disconnect chainActive's tip. + * Disconnect m_chain's tip. * After calling, the mempool will be in an inconsistent state, with * transactions from disconnected blocks being added to disconnectpool. You * should make the mempool consistent again by calling updateMempoolForReorg. @@ -2333,7 +2333,7 @@ bool CChainState::DisconnectTip(const Config &config, CValidationState &state, DisconnectedBlockTransactions *disconnectpool) { AssertLockHeld(cs_main); - CBlockIndex *pindexDelete = chainActive.Tip(); + CBlockIndex *pindexDelete = m_chain.Tip(); const Consensus::Params &consensusParams = config.GetChainParams().GetConsensus(); @@ -2392,7 +2392,7 @@ pindexFinalized = pindexDelete->pprev; } - chainActive.SetTip(pindexDelete->pprev); + m_chain.SetTip(pindexDelete->pprev); // Update chainActive and related variables. UpdateTip(config, pindexDelete->pprev); @@ -2558,7 +2558,7 @@ } /** - * Connect a new block to chainActive. pblock is either nullptr or a pointer to + * Connect a new block to m_chain. pblock is either nullptr or a pointer to * a CBlock corresponding to pindexNew, to bypass loading it again from disk. * * The block is always added to connectTrace (either after loading from disk or @@ -2575,7 +2575,7 @@ const CChainParams ¶ms = config.GetChainParams(); const Consensus::Params &consensusParams = params.GetConsensus(); - assert(pindexNew->pprev == chainActive.Tip()); + assert(pindexNew->pprev == m_chain.Tip()); // Read block from disk. int64_t nTime1 = GetTimeMicros(); std::shared_ptr pthisBlock; @@ -2667,8 +2667,8 @@ disconnectpool.importMempool(g_mempool); } - // Update chainActive & related variables. - chainActive.SetTip(pindexNew); + // Update m_chain & related variables. + m_chain.SetTip(pindexNew); UpdateTip(config, pindexNew); int64_t nTime6 = GetTimeMicros(); @@ -2716,7 +2716,7 @@ InvalidChainFound(pindexNew); } - const CBlockIndex *pindexFork = chainActive.FindFork(pindexNew); + const CBlockIndex *pindexFork = m_chain.FindFork(pindexNew); // Check whether all blocks on the path between the currently active // chain and the candidate are valid. Just going until the active chain @@ -2730,7 +2730,7 @@ // state. bool fParkedChain = pindexTest->nStatus.isOnParkedChain(); if (fParkedChain && gArgs.GetBoolArg("-automaticunparking", true)) { - const CBlockIndex *pindexTip = chainActive.Tip(); + const CBlockIndex *pindexTip = m_chain.Tip(); // During initialization, pindexTip and/or pindexFork may be // null. In this case, we just ignore the fact that the chain is @@ -2852,7 +2852,7 @@ // return to it later in case a reorganization to a better block fails. auto it = setBlockIndexCandidates.begin(); while (it != setBlockIndexCandidates.end() && - setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) { + setBlockIndexCandidates.value_comp()(*it, m_chain.Tip())) { setBlockIndexCandidates.erase(it++); } @@ -2872,13 +2872,13 @@ ConnectTrace &connectTrace) { AssertLockHeld(cs_main); - const CBlockIndex *pindexOldTip = chainActive.Tip(); - const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork); + const CBlockIndex *pindexOldTip = m_chain.Tip(); + const CBlockIndex *pindexFork = m_chain.FindFork(pindexMostWork); // Disconnect active blocks which are no longer in the best chain. bool fBlocksDisconnected = false; DisconnectedBlockTransactions disconnectpool; - while (chainActive.Tip() && chainActive.Tip() != pindexFork) { + while (m_chain.Tip() && m_chain.Tip() != pindexFork) { if (!DisconnectTip(config, state, &disconnectpool)) { // This is likely a fatal error, but keep the mempool consistent, // just in case. Only remove from the mempool in this case. @@ -2934,7 +2934,7 @@ } else { PruneBlockIndexCandidates(); if (!pindexOldTip || - chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) { + m_chain.Tip()->nChainWork > pindexOldTip->nChainWork) { // We're in a better position than we were. Return // temporarily to release the lock. fContinue = false; @@ -3034,7 +3034,7 @@ { LOCK(cs_main); - CBlockIndex *starting_tip = chainActive.Tip(); + CBlockIndex *starting_tip = m_chain.Tip(); bool blocks_connected = false; do { // We absolutely may not unlock cs_main until we've made forward @@ -3050,7 +3050,7 @@ // Whether we have anything to do at all. if (pindexMostWork == nullptr || - pindexMostWork == chainActive.Tip()) { + pindexMostWork == m_chain.Tip()) { break; } @@ -3072,16 +3072,16 @@ pindexMostWork = nullptr; } - pindexNewTip = chainActive.Tip(); + pindexNewTip = m_chain.Tip(); for (const PerBlockConnectTrace &trace : connectTrace.GetBlocksConnected()) { assert(trace.pblock && trace.pindex); GetMainSignals().BlockConnected(trace.pblock, trace.pindex, trace.conflictedTxs); } - } while (!chainActive.Tip() || + } while (!m_chain.Tip() || (starting_tip && CBlockIndexWorkComparator()( - chainActive.Tip(), starting_tip))); + m_chain.Tip(), starting_tip))); // Check the index once we're done with the above loop, since // we're going to release cs_main soon. If the index is in a bad @@ -3093,7 +3093,7 @@ return true; } - const CBlockIndex *pindexFork = chainActive.FindFork(starting_tip); + const CBlockIndex *pindexFork = m_chain.FindFork(starting_tip); bool fInitialDownload = IsInitialBlockDownload(); // Notify external listeners about the new tip. @@ -3143,18 +3143,18 @@ CBlockIndex *pindex) { { LOCK(cs_main); - if (pindex->nChainWork < chainActive.Tip()->nChainWork) { + if (pindex->nChainWork < m_chain.Tip()->nChainWork) { // Nothing to do, this block is not at the tip. return true; } - if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) { + if (m_chain.Tip()->nChainWork > nLastPreciousChainwork) { // The chain has been extended since the last call, reset the // counter. nBlockReverseSequenceId = -1; } - nLastPreciousChainwork = chainActive.Tip()->nChainWork; + nLastPreciousChainwork = m_chain.Tip()->nChainWork; setBlockIndexCandidates.erase(pindex); pindex->nSequenceId = nBlockReverseSequenceId; if (nBlockReverseSequenceId > std::numeric_limits::min()) { @@ -3196,14 +3196,14 @@ LOCK(cs_main); - if (!chainActive.Contains(pindex)) { + if (!m_chain.Contains(pindex)) { break; } pindex_was_in_chain = true; - CBlockIndex *invalid_walk_tip = chainActive.Tip(); + CBlockIndex *invalid_walk_tip = m_chain.Tip(); - // ActivateBestChain considers blocks already in chainActive + // ActivateBestChain considers blocks already in m_chain // unconditionally valid already, so force disconnect away from it. DisconnectedBlockTransactions disconnectpool; @@ -3222,7 +3222,7 @@ return false; } - assert(invalid_walk_tip->pprev == chainActive.Tip()); + assert(invalid_walk_tip->pprev == m_chain.Tip()); // We immediately mark the disconnected blocks as invalid. // This prevents a case where pruned nodes may fail to invalidateblock @@ -3261,7 +3261,7 @@ { LOCK(cs_main); - if (chainActive.Contains(to_mark_failed_or_parked)) { + if (m_chain.Contains(to_mark_failed_or_parked)) { // If the to-be-marked invalid block is in the active chain, // something is interfering and we can't proceed. return false; @@ -3284,7 +3284,7 @@ CBlockIndex *i = it.second; if (i->IsValid(BlockValidity::TRANSACTIONS) && i->HaveTxsDownloaded() && - !setBlockIndexCandidates.value_comp()(i, chainActive.Tip())) { + !setBlockIndexCandidates.value_comp()(i, m_chain.Tip())) { setBlockIndexCandidates.insert(i); } } @@ -3531,9 +3531,8 @@ pindex->nSequenceId = nBlockSequenceId++; } - if (chainActive.Tip() == nullptr || - !setBlockIndexCandidates.value_comp()(pindex, - chainActive.Tip())) { + if (m_chain.Tip() == nullptr || + !setBlockIndexCandidates.value_comp()(pindex, m_chain.Tip())) { setBlockIndexCandidates.insert(pindex); } @@ -4176,24 +4175,22 @@ // tie-breaker, attempting to pick the more honestly-mined block. int64_t newBlockTimeDiff = std::llabs(pindex->GetReceivedTimeDiff()); int64_t chainTipTimeDiff = - chainActive.Tip() ? std::llabs(chainActive.Tip()->GetReceivedTimeDiff()) - : 0; + m_chain.Tip() ? std::llabs(m_chain.Tip()->GetReceivedTimeDiff()) : 0; - bool isSameHeight = chainActive.Tip() && - (pindex->nChainWork == chainActive.Tip()->nChainWork); + bool isSameHeight = + m_chain.Tip() && (pindex->nChainWork == m_chain.Tip()->nChainWork); if (isSameHeight) { LogPrintf("Chain tip timestamp-to-received-time difference: hash=%s, " "diff=%d\n", - chainActive.Tip()->GetBlockHash().ToString(), - chainTipTimeDiff); + m_chain.Tip()->GetBlockHash().ToString(), chainTipTimeDiff); LogPrintf("New block timestamp-to-received-time difference: hash=%s, " "diff=%d\n", pindex->GetBlockHash().ToString(), newBlockTimeDiff); } bool fHasMoreOrSameWork = - (chainActive.Tip() ? pindex->nChainWork >= chainActive.Tip()->nChainWork - : true); + (m_chain.Tip() ? pindex->nChainWork >= m_chain.Tip()->nChainWork + : true); // Blocks that are too out-of-order needlessly limit the effectiveness of // pruning, because pruning will not delete block files that contain any @@ -4201,7 +4198,7 @@ // regardless of whether pruning is enabled; it should generally be safe to // not process unrequested blocks. bool fTooFarAhead = - (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP)); + (pindex->nHeight > int(m_chain.Height() + MIN_BLOCKS_TO_KEEP)); // TODO: Decouple this function from the block download logic by removing // fRequested @@ -4257,8 +4254,8 @@ // last minute so we can make sure everything is ready to be reorged if // needed. if (gArgs.GetBoolArg("-parkdeepreorg", true)) { - const CBlockIndex *pindexFork = chainActive.FindFork(pindex); - if (pindexFork && pindexFork->nHeight + 1 < chainActive.Height()) { + const CBlockIndex *pindexFork = m_chain.FindFork(pindex); + if (pindexFork && pindexFork->nHeight + 1 < m_chain.Height()) { LogPrintf("Park block %s as it would cause a deep reorg.\n", pindex->GetBlockHash().ToString()); pindex->nStatus = pindex->nStatus.withParked(); @@ -4269,7 +4266,7 @@ // Header is valid/has work and the merkle tree is good. // Relay now, but if it does not build on our best tip, let the // SendMessages loop relay it. - if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev) { + if (!IsInitialBlockDownload() && m_chain.Tip() == pindex->pprev) { GetMainSignals().NewPoWValidBlock(pindex, pblock); } @@ -5165,7 +5162,7 @@ LOCK(cs_main); // Check whether we're already initialized by checking for genesis in - // mapBlockIndex. Note that we can't use chainActive here, since it is + // 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 // thing loaded at this point. if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash())) { @@ -5365,8 +5362,8 @@ // 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 chainActive has been initialized.) - if (chainActive.Height() < 0) { + // require that m_chain has been initialized.) + if (m_chain.Height() < 0) { assert(mapBlockIndex.size() <= 1); return; } @@ -5450,7 +5447,7 @@ // Genesis block's hash must match. assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // The current active chain's genesis block must be this block. - assert(pindex == chainActive.Genesis()); + assert(pindex == m_chain.Genesis()); } if (!pindex->HaveTxsDownloaded()) { // nSequenceId can't be set positive for blocks that aren't linked @@ -5519,7 +5516,7 @@ // (i.e., hasParkedParent only if an ancestor is properly parked). assert(!pindex->nStatus.isOnParkedChain()); } - if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && + if (!CBlockIndexWorkComparator()(pindex, m_chain.Tip()) && pindexFirstNeverProcessed == nullptr) { if (pindexFirstInvalid == nullptr) { // If this block sorts at least as good as the current tip and @@ -5529,9 +5526,9 @@ assert(pindex->nStatus.isOnParkedChain() || setBlockIndexCandidates.count(pindex)); } - // chainActive.Tip() must also be there even if some data has + // m_chain.Tip() must also be there even if some data has // been pruned. - if (pindex == chainActive.Tip()) { + if (pindex == m_chain.Tip()) { assert(setBlockIndexCandidates.count(pindex)); } // If some parent is missing, then it could be that this block @@ -5586,12 +5583,12 @@ // - it has a descendant that at some point had more work than the // tip, and // - we tried switching to that descendant but were missing - // data for some intermediate block between chainActive and the + // data for some intermediate block between m_chain and the // tip. - // So if this block is itself better than chainActive.Tip() and it + // So if this block is itself better than m_chain.Tip() and it // wasn't in // setBlockIndexCandidates, then it must be in mapBlocksUnlinked. - if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && + if (!CBlockIndexWorkComparator()(pindex, m_chain.Tip()) && setBlockIndexCandidates.count(pindex) == 0) { if (pindexFirstInvalid == nullptr) { assert(foundInUnlinked);