Skip to content

Commit 4ed55df

Browse files
committed
refactoring: add block_index_candidates arg to LoadBlockIndex
Prevents BlockManager from having to reference ChainstateActive() within one of its methods which improves encapsulation and makes testing easier.
1 parent 613c46f commit 4ed55df

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/validation.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3705,7 +3705,10 @@ CBlockIndex * BlockManager::InsertBlockIndex(const uint256& hash)
37053705
return pindexNew;
37063706
}
37073707

3708-
bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params, CBlockTreeDB& blocktree)
3708+
bool BlockManager::LoadBlockIndex(
3709+
const Consensus::Params& consensus_params,
3710+
CBlockTreeDB& blocktree,
3711+
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
37093712
{
37103713
if (!blocktree.LoadBlockIndexGuts(consensus_params, [this](const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return this->InsertBlockIndex(hash); }))
37113714
return false;
@@ -3743,8 +3746,9 @@ bool BlockManager::LoadBlockIndex(const Consensus::Params& consensus_params, CBl
37433746
pindex->nStatus |= BLOCK_FAILED_CHILD;
37443747
setDirtyBlockIndex.insert(pindex);
37453748
}
3746-
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr))
3747-
::ChainstateActive().setBlockIndexCandidates.insert(pindex);
3749+
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->HaveTxsDownloaded() || pindex->pprev == nullptr)) {
3750+
block_index_candidates.insert(pindex);
3751+
}
37483752
if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
37493753
pindexBestInvalid = pindex;
37503754
if (pindex->pprev)
@@ -3769,7 +3773,8 @@ void BlockManager::Unload() {
37693773

37703774
bool static LoadBlockIndexDB(const CChainParams& chainparams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
37713775
{
3772-
if (!g_blockman.LoadBlockIndex(chainparams.GetConsensus(), *pblocktree))
3776+
if (!g_blockman.LoadBlockIndex(
3777+
chainparams.GetConsensus(), *pblocktree, ::ChainstateActive().setBlockIndexCandidates))
37733778
return false;
37743779

37753780
// Load block file info

src/validation.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,19 @@ class BlockManager {
475475
*/
476476
std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
477477

478+
/**
479+
* Load the blocktree off disk and into memory. Populate certain metadata
480+
* per index entry (nStatus, nChainWork, nTimeMax, etc.) as well as peripheral
481+
* collections like setDirtyBlockIndex.
482+
*
483+
* @param[out] block_index_candidates Fill this set with any valid blocks for
484+
* which we've downloaded all transactions.
485+
*/
478486
bool LoadBlockIndex(
479487
const Consensus::Params& consensus_params,
480-
CBlockTreeDB& blocktree) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
488+
CBlockTreeDB& blocktree,
489+
std::set<CBlockIndex*, CBlockIndexWorkComparator>& block_index_candidates)
490+
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
481491

482492
/** Clear all data members. */
483493
void Unload() EXCLUSIVE_LOCKS_REQUIRED(cs_main);

0 commit comments

Comments
 (0)