Skip to content

Commit cca48f6

Browse files
author
21E14
committed
Reset setBlockIndexCandidates once block index db loaded
1 parent 04e988c commit cca48f6

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/main.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,6 +1949,16 @@ static CBlockIndex* FindMostWorkChain() {
19491949
} while(true);
19501950
}
19511951

1952+
// Delete all entries in setBlockIndexCandidates that are worse than the current tip.
1953+
static void PruneBlockIndexCandidates() {
1954+
// Note that we can't delete the current block itself, as we may need to return to it later in case a
1955+
// reorganization to a better block fails.
1956+
std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
1957+
while (setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
1958+
setBlockIndexCandidates.erase(it++);
1959+
}
1960+
}
1961+
19521962
// Try to make some progress towards making pindexMostWork the active block.
19531963
// pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
19541964
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
@@ -1996,13 +2006,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
19962006
return false;
19972007
}
19982008
} else {
1999-
// Delete all entries in setBlockIndexCandidates that are worse than our new current block.
2000-
// Note that we can't delete the current block itself, as we may need to return to it later in case a
2001-
// reorganization to a better block fails.
2002-
std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2003-
while (setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2004-
setBlockIndexCandidates.erase(it++);
2005-
}
2009+
PruneBlockIndexCandidates();
20062010
// Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
20072011
assert(!setBlockIndexCandidates.empty());
20082012
if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
@@ -2860,6 +2864,9 @@ bool static LoadBlockIndexDB()
28602864
if (it == mapBlockIndex.end())
28612865
return true;
28622866
chainActive.SetTip(it->second);
2867+
2868+
PruneBlockIndexCandidates();
2869+
28632870
LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n",
28642871
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
28652872
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),

0 commit comments

Comments
 (0)