@@ -133,11 +133,6 @@ arith_uint256 nMinimumChainWork;
133
133
134
134
CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
135
135
136
- // Internal stuff
137
- namespace {
138
- CBlockIndex* pindexBestInvalid = nullptr ;
139
- } // namespace
140
-
141
136
// Internal stuff from blockstorage ...
142
137
extern RecursiveMutex cs_LastBlockFile;
143
138
extern std::vector<CBlockFileInfo> vinfoBlockFile;
@@ -155,23 +150,24 @@ CBlockIndex* BlockManager::LookupBlockIndex(const uint256& hash) const
155
150
return it == m_block_index.end () ? nullptr : it->second ;
156
151
}
157
152
158
- CBlockIndex* BlockManager ::FindForkInGlobalIndex (const CChain& chain, const CBlockLocator& locator)
153
+ CBlockIndex* CChainState ::FindForkInGlobalIndex (const CBlockLocator& locator) const
159
154
{
160
155
AssertLockHeld (cs_main);
161
156
162
157
// Find the latest block common to locator and chain - we expect that
163
158
// locator.vHave is sorted descending by height.
164
159
for (const uint256& hash : locator.vHave ) {
165
- CBlockIndex* pindex = LookupBlockIndex (hash);
160
+ CBlockIndex* pindex{m_blockman. LookupBlockIndex (hash)} ;
166
161
if (pindex) {
167
- if (chain .Contains (pindex))
162
+ if (m_chain .Contains (pindex)) {
168
163
return pindex;
169
- if (pindex->GetAncestor (chain.Height ()) == chain.Tip ()) {
170
- return chain.Tip ();
164
+ }
165
+ if (pindex->GetAncestor (m_chain.Height ()) == m_chain.Tip ()) {
166
+ return m_chain.Tip ();
171
167
}
172
168
}
173
169
}
174
- return chain .Genesis ();
170
+ return m_chain .Genesis ();
175
171
}
176
172
177
173
bool CheckInputScripts (const CTransaction& tx, TxValidationState& state,
@@ -1478,7 +1474,7 @@ void CChainState::CheckForkWarningConditions()
1478
1474
return ;
1479
1475
}
1480
1476
1481
- if (pindexBestInvalid && pindexBestInvalid ->nChainWork > m_chain.Tip ()->nChainWork + (GetBlockProof (*m_chain.Tip ()) * 6 )) {
1477
+ if (m_chainman. m_best_invalid && m_chainman. m_best_invalid ->nChainWork > m_chain.Tip ()->nChainWork + (GetBlockProof (*m_chain.Tip ()) * 6 )) {
1482
1478
LogPrintf (" %s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\n Chain state database corruption likely.\n " , __func__);
1483
1479
SetfLargeWorkInvalidChainFound (true );
1484
1480
} else {
@@ -1489,8 +1485,9 @@ void CChainState::CheckForkWarningConditions()
1489
1485
// Called both upon regular invalid block discovery *and* InvalidateBlock
1490
1486
void CChainState::InvalidChainFound (CBlockIndex* pindexNew)
1491
1487
{
1492
- if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork )
1493
- pindexBestInvalid = pindexNew;
1488
+ if (!m_chainman.m_best_invalid || pindexNew->nChainWork > m_chainman.m_best_invalid ->nChainWork ) {
1489
+ m_chainman.m_best_invalid = pindexNew;
1490
+ }
1494
1491
if (pindexBestHeader != nullptr && pindexBestHeader->GetAncestor (pindexNew->nHeight ) == pindexNew) {
1495
1492
pindexBestHeader = m_chain.Tip ();
1496
1493
}
@@ -1512,7 +1509,7 @@ void CChainState::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSt
1512
1509
{
1513
1510
if (state.GetResult () != BlockValidationResult::BLOCK_MUTATED) {
1514
1511
pindex->nStatus |= BLOCK_FAILED_VALID;
1515
- m_blockman .m_failed_blocks .insert (pindex);
1512
+ m_chainman .m_failed_blocks .insert (pindex);
1516
1513
setDirtyBlockIndex.insert (pindex);
1517
1514
setBlockIndexCandidates.erase (pindex);
1518
1515
InvalidChainFound (pindex);
@@ -2653,8 +2650,9 @@ CBlockIndex* CChainState::FindMostWorkChain() {
2653
2650
bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2654
2651
if (fFailedChain || fMissingData ) {
2655
2652
// Candidate chain is not usable (either invalid or missing data)
2656
- if (fFailedChain && (pindexBestInvalid == nullptr || pindexNew->nChainWork > pindexBestInvalid->nChainWork ))
2657
- pindexBestInvalid = pindexNew;
2653
+ if (fFailedChain && (m_chainman.m_best_invalid == nullptr || pindexNew->nChainWork > m_chainman.m_best_invalid ->nChainWork )) {
2654
+ m_chainman.m_best_invalid = pindexNew;
2655
+ }
2658
2656
CBlockIndex *pindexFailed = pindexNew;
2659
2657
// Remove the entire chain from the set.
2660
2658
while (pindexTest != pindexFailed) {
@@ -3065,7 +3063,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
3065
3063
to_mark_failed->nStatus |= BLOCK_FAILED_VALID;
3066
3064
setDirtyBlockIndex.insert (to_mark_failed);
3067
3065
setBlockIndexCandidates.erase (to_mark_failed);
3068
- m_blockman .m_failed_blocks .insert (to_mark_failed);
3066
+ m_chainman .m_failed_blocks .insert (to_mark_failed);
3069
3067
3070
3068
// If any new blocks somehow arrived while we were disconnecting
3071
3069
// (above), then the pre-calculation of what should go into
@@ -3106,11 +3104,11 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
3106
3104
if (it->second ->IsValid (BLOCK_VALID_TRANSACTIONS) && it->second ->HaveTxsDownloaded () && setBlockIndexCandidates.value_comp ()(m_chain.Tip (), it->second )) {
3107
3105
setBlockIndexCandidates.insert (it->second );
3108
3106
}
3109
- if (it->second == pindexBestInvalid ) {
3107
+ if (it->second == m_chainman. m_best_invalid ) {
3110
3108
// Reset invalid block marker if it was pointing to one of those.
3111
- pindexBestInvalid = nullptr ;
3109
+ m_chainman. m_best_invalid = nullptr ;
3112
3110
}
3113
- m_blockman .m_failed_blocks .erase (it->second );
3111
+ m_chainman .m_failed_blocks .erase (it->second );
3114
3112
}
3115
3113
it++;
3116
3114
}
@@ -3120,7 +3118,7 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
3120
3118
if (pindex->nStatus & BLOCK_FAILED_MASK) {
3121
3119
pindex->nStatus &= ~BLOCK_FAILED_MASK;
3122
3120
setDirtyBlockIndex.insert (pindex);
3123
- m_blockman .m_failed_blocks .erase (pindex);
3121
+ m_chainman .m_failed_blocks .erase (pindex);
3124
3122
}
3125
3123
pindex = pindex->pprev ;
3126
3124
}
@@ -3481,14 +3479,14 @@ static bool ContextualCheckBlock(const CBlock& block, BlockValidationState& stat
3481
3479
return true ;
3482
3480
}
3483
3481
3484
- bool BlockManager ::AcceptBlockHeader (const CBlockHeader& block, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
3482
+ bool ChainstateManager ::AcceptBlockHeader (const CBlockHeader& block, BlockValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
3485
3483
{
3486
3484
AssertLockHeld (cs_main);
3487
3485
// Check for duplicate
3488
3486
uint256 hash = block.GetHash ();
3489
- BlockMap::iterator miSelf = m_block_index.find (hash);
3487
+ BlockMap::iterator miSelf{m_blockman. m_block_index .find (hash)} ;
3490
3488
if (hash != chainparams.GetConsensus ().hashGenesisBlock ) {
3491
- if (miSelf != m_block_index.end ()) {
3489
+ if (miSelf != m_blockman. m_block_index .end ()) {
3492
3490
// Block header is already known.
3493
3491
CBlockIndex* pindex = miSelf->second ;
3494
3492
if (ppindex)
@@ -3507,8 +3505,8 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
3507
3505
3508
3506
// Get prev block index
3509
3507
CBlockIndex* pindexPrev = nullptr ;
3510
- BlockMap::iterator mi = m_block_index.find (block.hashPrevBlock );
3511
- if (mi == m_block_index.end ()) {
3508
+ BlockMap::iterator mi{m_blockman. m_block_index .find (block.hashPrevBlock )} ;
3509
+ if (mi == m_blockman. m_block_index .end ()) {
3512
3510
LogPrint (BCLog::VALIDATION, " %s: %s prev block not found\n " , __func__, hash.ToString ());
3513
3511
return state.Invalid (BlockValidationResult::BLOCK_MISSING_PREV, " prev-blk-not-found" );
3514
3512
}
@@ -3517,7 +3515,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
3517
3515
LogPrint (BCLog::VALIDATION, " %s: %s prev block invalid\n " , __func__, hash.ToString ());
3518
3516
return state.Invalid (BlockValidationResult::BLOCK_INVALID_PREV, " bad-prevblk" );
3519
3517
}
3520
- if (!ContextualCheckBlockHeader (block, state, * this , chainparams, pindexPrev, GetAdjustedTime ())) {
3518
+ if (!ContextualCheckBlockHeader (block, state, m_blockman , chainparams, pindexPrev, GetAdjustedTime ())) {
3521
3519
LogPrint (BCLog::VALIDATION, " %s: Consensus::ContextualCheckBlockHeader: %s, %s\n " , __func__, hash.ToString (), state.ToString ());
3522
3520
return false ;
3523
3521
}
@@ -3561,7 +3559,7 @@ bool BlockManager::AcceptBlockHeader(const CBlockHeader& block, BlockValidationS
3561
3559
}
3562
3560
}
3563
3561
}
3564
- CBlockIndex* pindex = AddToBlockIndex (block);
3562
+ CBlockIndex* pindex{m_blockman. AddToBlockIndex (block)} ;
3565
3563
3566
3564
if (ppindex)
3567
3565
*ppindex = pindex;
@@ -3577,8 +3575,7 @@ bool ChainstateManager::ProcessNewBlockHeaders(const std::vector<CBlockHeader>&
3577
3575
LOCK (cs_main);
3578
3576
for (const CBlockHeader& header : headers) {
3579
3577
CBlockIndex *pindex = nullptr ; // Use a temp pindex instead of ppindex to avoid a const_cast
3580
- bool accepted = m_blockman.AcceptBlockHeader (
3581
- header, state, chainparams, &pindex);
3578
+ bool accepted{AcceptBlockHeader (header, state, chainparams, &pindex)};
3582
3579
ActiveChainstate ().CheckBlockIndex ();
3583
3580
3584
3581
if (!accepted) {
@@ -3608,7 +3605,7 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, Block
3608
3605
CBlockIndex *pindexDummy = nullptr ;
3609
3606
CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
3610
3607
3611
- bool accepted_header = m_blockman .AcceptBlockHeader (block, state, m_params, &pindex);
3608
+ bool accepted_header{m_chainman .AcceptBlockHeader (block, state, m_params, &pindex)} ;
3612
3609
CheckBlockIndex ();
3613
3610
3614
3611
if (!accepted_header)
@@ -4014,8 +4011,9 @@ bool BlockManager::LoadBlockIndex(
4014
4011
}
4015
4012
}
4016
4013
}
4017
- if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork ))
4018
- pindexBestInvalid = pindex;
4014
+ if (pindex->nStatus & BLOCK_FAILED_MASK && (!chainman.m_best_invalid || pindex->nChainWork > chainman.m_best_invalid ->nChainWork )) {
4015
+ chainman.m_best_invalid = pindex;
4016
+ }
4019
4017
if (pindex->pprev )
4020
4018
pindex->BuildSkip ();
4021
4019
if (pindex->IsValid (BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator ()(pindexBestHeader, pindex)))
@@ -4026,7 +4024,6 @@ bool BlockManager::LoadBlockIndex(
4026
4024
}
4027
4025
4028
4026
void BlockManager::Unload () {
4029
- m_failed_blocks.clear ();
4030
4027
m_blocks_unlinked.clear ();
4031
4028
4032
4029
for (const BlockMap::value_type& entry : m_block_index) {
@@ -4364,7 +4361,6 @@ void UnloadBlockIndex(CTxMemPool* mempool, ChainstateManager& chainman)
4364
4361
{
4365
4362
LOCK (cs_main);
4366
4363
chainman.Unload ();
4367
- pindexBestInvalid = nullptr ;
4368
4364
pindexBestHeader = nullptr ;
4369
4365
if (mempool) mempool->clear ();
4370
4366
vinfoBlockFile.clear ();
@@ -5341,7 +5337,9 @@ void ChainstateManager::Unload()
5341
5337
chainstate->UnloadBlockIndex ();
5342
5338
}
5343
5339
5340
+ m_failed_blocks.clear ();
5344
5341
m_blockman.Unload ();
5342
+ m_best_invalid = nullptr ;
5345
5343
}
5346
5344
5347
5345
void ChainstateManager::Reset ()
0 commit comments