Skip to content

Commit 53a87c0

Browse files
committed
Merge pull request #5321
34559c7 Make PruneBlockIndexCandidates safer (Pieter Wuille) cca48f6 Reset setBlockIndexCandidates once block index db loaded (21E14)
2 parents 4baa9f0 + 34559c7 commit 53a87c0

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/main.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,18 @@ static CBlockIndex* FindMostWorkChain() {
20082008
} while(true);
20092009
}
20102010

2011+
// Delete all entries in setBlockIndexCandidates that are worse than the current tip.
2012+
static void PruneBlockIndexCandidates() {
2013+
// Note that we can't delete the current block itself, as we may need to return to it later in case a
2014+
// reorganization to a better block fails.
2015+
std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2016+
while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2017+
setBlockIndexCandidates.erase(it++);
2018+
}
2019+
// Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2020+
assert(!setBlockIndexCandidates.empty());
2021+
}
2022+
20112023
// Try to make some progress towards making pindexMostWork the active block.
20122024
// pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
20132025
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
@@ -2055,15 +2067,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
20552067
return false;
20562068
}
20572069
} else {
2058-
// Delete all entries in setBlockIndexCandidates that are worse than our new current block.
2059-
// Note that we can't delete the current block itself, as we may need to return to it later in case a
2060-
// reorganization to a better block fails.
2061-
std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2062-
while (setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2063-
setBlockIndexCandidates.erase(it++);
2064-
}
2065-
// Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2066-
assert(!setBlockIndexCandidates.empty());
2070+
PruneBlockIndexCandidates();
20672071
if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
20682072
// We're in a better position than we were. Return temporarily to release the lock.
20692073
fContinue = false;
@@ -2956,6 +2960,9 @@ bool static LoadBlockIndexDB()
29562960
if (it == mapBlockIndex.end())
29572961
return true;
29582962
chainActive.SetTip(it->second);
2963+
2964+
PruneBlockIndexCandidates();
2965+
29592966
LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n",
29602967
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
29612968
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),

0 commit comments

Comments
 (0)