move-mostly: Make pindexBestHeader a ChainMan member

[META] In the next commit, we move the clearing of pindexBestHeader to
       ChainstateManager::Unload()
This commit is contained in:
Carl Dong
2020-12-24 16:18:46 -05:00
parent 5d670173a3
commit 0d567daf23
8 changed files with 45 additions and 42 deletions

View File

@@ -120,7 +120,6 @@ const std::vector<std::string> CHECKLEVEL_DOC {
*/
RecursiveMutex cs_main;
CBlockIndex *pindexBestHeader = nullptr;
Mutex g_best_block_mutex;
std::condition_variable g_best_block_cv;
uint256 g_best_block;
@@ -280,8 +279,9 @@ static bool IsCurrentForFeeEstimation(CChainState& active_chainstate) EXCLUSIVE_
return false;
if (active_chainstate.m_chain.Tip()->GetBlockTime() < count_seconds(GetTime<std::chrono::seconds>() - MAX_FEE_ESTIMATION_TIP_AGE))
return false;
if (active_chainstate.m_chain.Height() < pindexBestHeader->nHeight - 1)
if (active_chainstate.m_chain.Height() < active_chainstate.m_chainman.pindexBestHeader->nHeight - 1) {
return false;
}
return true;
}
@@ -1602,8 +1602,8 @@ void CChainState::InvalidChainFound(CBlockIndex* 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();
if (m_chainman.pindexBestHeader != nullptr && m_chainman.pindexBestHeader->GetAncestor(pindexNew->nHeight) == pindexNew) {
m_chainman.pindexBestHeader = m_chain.Tip();
}
LogPrintf("%s: invalid block=%s height=%d log2_work=%f date=%s\n", __func__,
@@ -2029,8 +2029,8 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
BlockMap::const_iterator it = m_blockman.m_block_index.find(hashAssumeValid);
if (it != m_blockman.m_block_index.end()) {
if (it->second.GetAncestor(pindex->nHeight) == pindex &&
pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
pindexBestHeader->nChainWork >= nMinimumChainWork) {
m_chainman.pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
m_chainman.pindexBestHeader->nChainWork >= nMinimumChainWork) {
// This block is a member of the assumed verified chain and an ancestor of the best header.
// Script verification is skipped when connecting blocks under the
// assumevalid block. Assuming the assumevalid block is valid this
@@ -2045,7 +2045,7 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
// artificially set the default assumed verified block further back.
// The test against nMinimumChainWork prevents the skipping when denied access to any chain at
// least as good as the expected chain.
fScriptChecks = (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
fScriptChecks = (GetBlockProofEquivalentTime(*m_chainman.pindexBestHeader, *pindex, *m_chainman.pindexBestHeader, m_params.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
}
}
}
@@ -2904,7 +2904,7 @@ static bool NotifyHeaderTip(CChainState& chainstate) LOCKS_EXCLUDED(cs_main) {
CBlockIndex* pindexHeader = nullptr;
{
LOCK(cs_main);
pindexHeader = pindexBestHeader;
pindexHeader = chainstate.m_chainman.pindexBestHeader;
if (pindexHeader != pindexHeaderOld) {
fNotify = true;
@@ -3621,7 +3621,7 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
}
}
}
CBlockIndex* pindex{m_blockman.AddToBlockIndex(block)};
CBlockIndex* pindex{m_blockman.AddToBlockIndex(block, pindexBestHeader)};
if (ppindex)
*ppindex = pindex;
@@ -4122,7 +4122,7 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman)
{
AssertLockHeld(::cs_main);
chainman.Unload();
pindexBestHeader = nullptr;
chainman.pindexBestHeader = nullptr;
if (mempool) mempool->clear();
g_versionbitscache.Clear();
for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
@@ -4239,7 +4239,7 @@ bool CChainState::LoadGenesisBlock()
if (blockPos.IsNull()) {
return error("%s: writing genesis block to disk failed", __func__);
}
CBlockIndex *pindex = m_blockman.AddToBlockIndex(block);
CBlockIndex* pindex = m_blockman.AddToBlockIndex(block, m_chainman.pindexBestHeader);
ReceivedBlockTransactions(block, pindex, blockPos);
} catch (const std::runtime_error& e) {
return error("%s: failed to write genesis block: %s", __func__, e.what());