@@ -2008,6 +2008,18 @@ static CBlockIndex* FindMostWorkChain() {
2008
2008
} while (true );
2009
2009
}
2010
2010
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
+
2011
2023
// Try to make some progress towards making pindexMostWork the active block.
2012
2024
// pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2013
2025
static bool ActivateBestChainStep (CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
@@ -2055,15 +2067,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
2055
2067
return false ;
2056
2068
}
2057
2069
} 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 ();
2067
2071
if (!pindexOldTip || chainActive.Tip ()->nChainWork > pindexOldTip->nChainWork ) {
2068
2072
// We're in a better position than we were. Return temporarily to release the lock.
2069
2073
fContinue = false ;
@@ -2956,6 +2960,9 @@ bool static LoadBlockIndexDB()
2956
2960
if (it == mapBlockIndex.end ())
2957
2961
return true ;
2958
2962
chainActive.SetTip (it->second );
2963
+
2964
+ PruneBlockIndexCandidates ();
2965
+
2959
2966
LogPrintf (" LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n " ,
2960
2967
chainActive.Tip ()->GetBlockHash ().ToString (), chainActive.Height (),
2961
2968
DateTimeStrFormat (" %Y-%m-%d %H:%M:%S" , chainActive.Tip ()->GetBlockTime ()),
0 commit comments