mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-08 11:44:14 +01:00
Merge bitcoin/bitcoin#23785: refactor: Move stuff to ChainstateManager
fab6d6b2d1Move pindexBestInvalid to ChainstateManager (MarcoFalke)facd2137ecMove m_failed_blocks to ChainstateManager (MarcoFalke)fa47b5c100Move AcceptBlockHeader to ChainstateManager (MarcoFalke)fa3d62cf7bMove FindForkInGlobalIndex from BlockManager to CChainState (MarcoFalke) Pull request description: Move globals or members of the wrong class to the right class. ACKs for top commit: naumenkogs: ACKfab6d6b2d1Sjors: ACKfab6d6b2d1shaavan: ACKfab6d6b2d1Tree-SHA512: 926cbdfa22838517497bacb79ed5f521f64117c2aacf96a0176f62831b4713314a32abc0213df5ee067edf63e4a4300f752a26006d36e5aab415bb91209a271f
This commit is contained in:
@@ -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;
|
||||
@@ -155,23 +150,24 @@ CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
|
||||
return it == m_block_index.end() ? nullptr : it->second;
|
||||
}
|
||||
|
||||
CBlockIndex* BlockManager::FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
|
||||
CBlockIndex* CChainState::FindForkInGlobalIndex(const CBlockLocator& locator) const
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
|
||||
// Find the latest block common to locator and chain - we expect that
|
||||
// locator.vHave is sorted descending by height.
|
||||
for (const uint256& hash : locator.vHave) {
|
||||
CBlockIndex* pindex = LookupBlockIndex(hash);
|
||||
CBlockIndex* pindex{m_blockman.LookupBlockIndex(hash)};
|
||||
if (pindex) {
|
||||
if (chain.Contains(pindex))
|
||||
if (m_chain.Contains(pindex)) {
|
||||
return pindex;
|
||||
if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
|
||||
return chain.Tip();
|
||||
}
|
||||
if (pindex->GetAncestor(m_chain.Height()) == m_chain.Tip()) {
|
||||
return m_chain.Tip();
|
||||
}
|
||||
}
|
||||
}
|
||||
return chain.Genesis();
|
||||
return m_chain.Genesis();
|
||||
}
|
||||
|
||||
bool CheckInputScripts(const CTransaction& tx, TxValidationState& state,
|
||||
@@ -1478,7 +1474,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 {
|
||||
@@ -1489,8 +1485,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();
|
||||
}
|
||||
@@ -1512,7 +1509,7 @@ void CChainState::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSt
|
||||
{
|
||||
if (state.GetResult() != BlockValidationResult::BLOCK_MUTATED) {
|
||||
pindex->nStatus |= BLOCK_FAILED_VALID;
|
||||
m_blockman.m_failed_blocks.insert(pindex);
|
||||
m_chainman.m_failed_blocks.insert(pindex);
|
||||
setDirtyBlockIndex.insert(pindex);
|
||||
setBlockIndexCandidates.erase(pindex);
|
||||
InvalidChainFound(pindex);
|
||||
@@ -2653,8 +2650,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) {
|
||||
@@ -3065,7 +3063,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
|
||||
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
|
||||
setDirtyBlockIndex.insert(to_mark_failed);
|
||||
setBlockIndexCandidates.erase(to_mark_failed);
|
||||
m_blockman.m_failed_blocks.insert(to_mark_failed);
|
||||
m_chainman.m_failed_blocks.insert(to_mark_failed);
|
||||
|
||||
// If any new blocks somehow arrived while we were disconnecting
|
||||
// (above), then the pre-calculation of what should go into
|
||||
@@ -3106,11 +3104,11 @@ 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_blockman.m_failed_blocks.erase(it->second);
|
||||
m_chainman.m_failed_blocks.erase(it->second);
|
||||
}
|
||||
it++;
|
||||
}
|
||||
@@ -3120,7 +3118,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
|
||||
if (pindex->nStatus & BLOCK_FAILED_MASK) {
|
||||
pindex->nStatus &= ~BLOCK_FAILED_MASK;
|
||||
setDirtyBlockIndex.insert(pindex);
|
||||
m_blockman.m_failed_blocks.erase(pindex);
|
||||
m_chainman.m_failed_blocks.erase(pindex);
|
||||
}
|
||||
pindex = pindex->pprev;
|
||||
}
|
||||
@@ -3481,14 +3479,14 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
|
||||
bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
|
||||
{
|
||||
AssertLockHeld(cs_main);
|
||||
// Check for duplicate
|
||||
uint256 hash = block.GetHash();
|
||||
BlockMap::iterator miSelf = m_block_index.find(hash);
|
||||
BlockMap::iterator miSelf{m_blockman.m_block_index.find(hash)};
|
||||
if (hash != chainparams.GetConsensus().hashGenesisBlock) {
|
||||
if (miSelf != m_block_index.end()) {
|
||||
if (miSelf != m_blockman.m_block_index.end()) {
|
||||
// Block header is already known.
|
||||
CBlockIndex* pindex = miSelf->second;
|
||||
if (ppindex)
|
||||
@@ -3507,8 +3505,8 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
|
||||
|
||||
// Get prev block index
|
||||
CBlockIndex* pindexPrev = nullptr;
|
||||
BlockMap::iterator mi = m_block_index.find(block.hashPrevBlock);
|
||||
if (mi == m_block_index.end()) {
|
||||
BlockMap::iterator mi{m_blockman.m_block_index.find(block.hashPrevBlock)};
|
||||
if (mi == m_blockman.m_block_index.end()) {
|
||||
LogPrint(BCLog::VALIDATION, "%s: %s prev block not found\n", __func__, hash.ToString());
|
||||
return state.Invalid(BlockValidationResult::BLOCK_MISSING_PREV, "prev-blk-not-found");
|
||||
}
|
||||
@@ -3517,7 +3515,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
|
||||
LogPrint(BCLog::VALIDATION, "%s: %s prev block invalid\n", __func__, hash.ToString());
|
||||
return state.Invalid(BlockValidationResult::BLOCK_INVALID_PREV, "bad-prevblk");
|
||||
}
|
||||
if (!ContextualCheckBlockHeader(block, state, *this, chainparams, pindexPrev, GetAdjustedTime())) {
|
||||
if (!ContextualCheckBlockHeader(block, state, m_blockman, chainparams, pindexPrev, GetAdjustedTime())) {
|
||||
LogPrint(BCLog::VALIDATION, "%s: Consensus::ContextualCheckBlockHeader: %s, %s\n", __func__, hash.ToString(), state.ToString());
|
||||
return false;
|
||||
}
|
||||
@@ -3561,7 +3559,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
|
||||
}
|
||||
}
|
||||
}
|
||||
CBlockIndex* pindex = AddToBlockIndex(block);
|
||||
CBlockIndex* pindex{m_blockman.AddToBlockIndex(block)};
|
||||
|
||||
if (ppindex)
|
||||
*ppindex = pindex;
|
||||
@@ -3577,8 +3575,7 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>&
|
||||
LOCK(cs_main);
|
||||
for (const CBlockHeader& header : headers) {
|
||||
CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
|
||||
bool accepted = m_blockman.AcceptBlockHeader(
|
||||
header, state, chainparams, &pindex);
|
||||
bool accepted{AcceptBlockHeader(header, state, chainparams, &pindex)};
|
||||
ActiveChainstate().CheckBlockIndex();
|
||||
|
||||
if (!accepted) {
|
||||
@@ -3608,7 +3605,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
|
||||
CBlockIndex *pindexDummy = nullptr;
|
||||
CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
|
||||
|
||||
bool accepted_header = m_blockman.AcceptBlockHeader(block, state, m_params, &pindex);
|
||||
bool accepted_header{m_chainman.AcceptBlockHeader(block, state, m_params, &pindex)};
|
||||
CheckBlockIndex();
|
||||
|
||||
if (!accepted_header)
|
||||
@@ -4014,8 +4011,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)))
|
||||
@@ -4026,7 +4024,6 @@ bool BlockManager::LoadBlockIndex(
|
||||
}
|
||||
|
||||
void BlockManager::Unload() {
|
||||
m_failed_blocks.clear();
|
||||
m_blocks_unlinked.clear();
|
||||
|
||||
for (const BlockMap::value_type& entry : m_block_index) {
|
||||
@@ -4364,7 +4361,6 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman)
|
||||
{
|
||||
LOCK(cs_main);
|
||||
chainman.Unload();
|
||||
pindexBestInvalid = nullptr;
|
||||
pindexBestHeader = nullptr;
|
||||
if (mempool) mempool->clear();
|
||||
vinfoBlockFile.clear();
|
||||
@@ -5341,7 +5337,9 @@ void ChainstateManager::Unload()
|
||||
chainstate->UnloadBlockIndex();
|
||||
}
|
||||
|
||||
m_failed_blocks.clear();
|
||||
m_blockman.Unload();
|
||||
m_best_invalid = nullptr;
|
||||
}
|
||||
|
||||
void ChainstateManager::Reset()
|
||||
|
||||
Reference in New Issue
Block a user