From fab6d6b2d154893ab422dda87f3535d42c3e06f4 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Tue, 23 Nov 2021 19:49:48 +0100 Subject: [PATCH] Move pindexBestInvalid to ChainstateManager A private member is better than a global. --- src/validation.cpp | 28 +++++++++++++--------------- src/validation.h | 3 +++ 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index ba6b9e853a..27af75245f 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -133,11 +133,6 @@ arith_uint256 nMinimumChainWork; CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE); -// Internal stuff -namespace { - CBlockIndex* pindexBestInvalid = nullptr; -} // namespace - // Internal stuff from blockstorage ... extern RecursiveMutex cs_LastBlockFile; extern std::vector vinfoBlockFile; @@ -1257,7 +1252,7 @@ void CChainState::CheckForkWarningConditions() return; } - if (pindexBestInvalid && pindexBestInvalid->nChainWork > m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6)) { + if (m_chainman.m_best_invalid && m_chainman.m_best_invalid->nChainWork > m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6)) { LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__); SetfLargeWorkInvalidChainFound(true); } else { @@ -1268,8 +1263,9 @@ void CChainState::CheckForkWarningConditions() // Called both upon regular invalid block discovery *and* InvalidateBlock void CChainState::InvalidChainFound(CBlockIndex* pindexNew) { - if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork) - pindexBestInvalid = pindexNew; + if (!m_chainman.m_best_invalid || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork) { + m_chainman.m_best_invalid = pindexNew; + } if (pindexBestHeader != nullptr && pindexBestHeader->GetAncestor(pindexNew->nHeight) == pindexNew) { pindexBestHeader = m_chain.Tip(); } @@ -2432,8 +2428,9 @@ CBlockIndex* CChainState::FindMostWorkChain() { bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA); if (fFailedChain || fMissingData) { // Candidate chain is not usable (either invalid or missing data) - if (fFailedChain && (pindexBestInvalid == nullptr || pindexNew->nChainWork > pindexBestInvalid->nChainWork)) - pindexBestInvalid = pindexNew; + if (fFailedChain && (m_chainman.m_best_invalid == nullptr || pindexNew->nChainWork > m_chainman.m_best_invalid->nChainWork)) { + m_chainman.m_best_invalid = pindexNew; + } CBlockIndex *pindexFailed = pindexNew; // Remove the entire chain from the set. while (pindexTest != pindexFailed) { @@ -2885,9 +2882,9 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) { if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), it->second)) { setBlockIndexCandidates.insert(it->second); } - if (it->second == pindexBestInvalid) { + if (it->second == m_chainman.m_best_invalid) { // Reset invalid block marker if it was pointing to one of those. - pindexBestInvalid = nullptr; + m_chainman.m_best_invalid = nullptr; } m_chainman.m_failed_blocks.erase(it->second); } @@ -3792,8 +3789,9 @@ bool BlockManager::LoadBlockIndex( } } } - if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork)) - pindexBestInvalid = pindex; + if (pindex->nStatus & BLOCK_FAILED_MASK && (!chainman.m_best_invalid || pindex->nChainWork > chainman.m_best_invalid->nChainWork)) { + chainman.m_best_invalid = pindex; + } if (pindex->pprev) pindex->BuildSkip(); if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex))) @@ -4141,7 +4139,6 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman) { LOCK(cs_main); chainman.Unload(); - pindexBestInvalid = nullptr; pindexBestHeader = nullptr; if (mempool) mempool->clear(); vinfoBlockFile.clear(); @@ -5120,6 +5117,7 @@ void ChainstateManager::Unload() m_failed_blocks.clear(); m_blockman.Unload(); + m_best_invalid = nullptr; } void ChainstateManager::Reset() diff --git a/src/validation.h b/src/validation.h index 3a702b4fa6..784ff0b3bc 100644 --- a/src/validation.h +++ b/src/validation.h @@ -866,6 +866,9 @@ private: //! by the background validation chainstate. bool m_snapshot_validated{false}; + CBlockIndex* m_best_invalid; + friend bool BlockManager::LoadBlockIndex(const Consensus::Params&, ChainstateManager&); + //! Internal helper for ActivateSnapshot(). [[nodiscard]] bool PopulateAndValidateSnapshot( CChainState& snapshot_chainstate,