diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -1016,8 +1016,6 @@ void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main); - void InvalidChainFound(CBlockIndex *pindexNew) - EXCLUSIVE_LOCKS_REQUIRED(cs_main); CBlockIndex *FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool MarkBlockAsFinal(BlockValidationState &state, const CBlockIndex *pindex) @@ -1037,6 +1035,12 @@ CBlockIndex *pindex, bool invalidate) EXCLUSIVE_LOCKS_REQUIRED(m_cs_chainstate); + void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main); + void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + void InvalidChainFound(CBlockIndex *pindexNew) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + friend ChainstateManager; }; diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -981,27 +981,28 @@ #endif } -static void CheckForkWarningConditions() EXCLUSIVE_LOCKS_REQUIRED(cs_main) { +void CChainState::CheckForkWarningConditions() { AssertLockHeld(cs_main); + assert(std::addressof(::ChainstateActive()) == std::addressof(*this)); + // Before we get past initial download, we cannot reliably alert about forks // (we assume we don't get stuck on a fork before finishing our initial // sync) - if (::ChainstateActive().IsInitialBlockDownload()) { + if (IsInitialBlockDownload()) { return; } // If our best fork is no longer within 72 blocks (+/- 12 hours if no one // mines it) of our head, drop it if (pindexBestForkTip && - ::ChainActive().Height() - pindexBestForkTip->nHeight >= 72) { + m_chain.Height() - pindexBestForkTip->nHeight >= 72) { pindexBestForkTip = nullptr; } if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > - ::ChainActive().Tip()->nChainWork + - (GetBlockProof(*::ChainActive().Tip()) * 6))) { + m_chain.Tip()->nChainWork + (GetBlockProof(*m_chain.Tip()) * 6))) { if (!GetfLargeWorkForkFound() && pindexBestForkBase) { std::string warning = std::string("'Warning: Large-work fork detected, forking after " @@ -1032,11 +1033,13 @@ } } -static void CheckForkWarningConditionsOnNewFork(CBlockIndex *pindexNewForkTip) - EXCLUSIVE_LOCKS_REQUIRED(cs_main) { +void CChainState::CheckForkWarningConditionsOnNewFork( + CBlockIndex *pindexNewForkTip) { AssertLockHeld(cs_main); + assert(std::addressof(::ChainstateActive()) == std::addressof(*this)); + // If we are on a fork that is sufficiently large, set a warning flag. - const CBlockIndex *pfork = ::ChainActive().FindFork(pindexNewForkTip); + const CBlockIndex *pfork = m_chain.FindFork(pindexNewForkTip); // We define a condition where we should warn the user about as a fork of at // least 7 blocks with a tip within 72 blocks (+/- 12 hours if no one mines @@ -1051,7 +1054,7 @@ pindexNewForkTip->nHeight > pindexBestForkTip->nHeight) && pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) && - ::ChainActive().Height() - pindexNewForkTip->nHeight < 72) { + m_chain.Height() - pindexNewForkTip->nHeight < 72) { pindexBestForkTip = pindexNewForkTip; pindexBestForkBase = pfork; } @@ -1062,13 +1065,14 @@ // Called both upon regular invalid block discovery *and* InvalidateBlock void CChainState::InvalidChainFound(CBlockIndex *pindexNew) { AssertLockHeld(cs_main); + assert(std::addressof(::ChainstateActive()) == std::addressof(*this)); if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork) { pindexBestInvalid = pindexNew; } if (pindexBestHeader != nullptr && pindexBestHeader->GetAncestor(pindexNew->nHeight) == pindexNew) { - pindexBestHeader = ::ChainActive().Tip(); + pindexBestHeader = m_chain.Tip(); } // If the invalid chain found is supposed to be finalized, we need to move @@ -1082,11 +1086,10 @@ pindexNew->nHeight, log(pindexNew->nChainWork.getdouble()) / log(2.0), FormatISO8601DateTime(pindexNew->GetBlockTime())); - CBlockIndex *tip = ::ChainActive().Tip(); + CBlockIndex *tip = m_chain.Tip(); assert(tip); LogPrintf("%s: current best=%s height=%d log2_work=%f date=%s\n", - __func__, tip->GetBlockHash().ToString(), - ::ChainActive().Height(), + __func__, tip->GetBlockHash().ToString(), m_chain.Height(), log(tip->nChainWork.getdouble()) / log(2.0), FormatISO8601DateTime(tip->GetBlockTime())); }