aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-11-23 19:49:48 +0100
committerMarcoFalke <falke.marco@gmail.com>2021-12-15 17:46:39 +0100
commitfab6d6b2d154893ab422dda87f3535d42c3e06f4 (patch)
treebcf3841bfff98cdb318d25ee05c4e580b05045e2 /src
parentfacd2137eceacb95e1f71c87ddc704d752b37272 (diff)
Move pindexBestInvalid to ChainstateManager
A private member is better than a global.
Diffstat (limited to 'src')
-rw-r--r--src/validation.cpp28
-rw-r--r--src/validation.h3
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<CBlockFileInfo> 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,