From cca48f69b04462c5c9bfefd34443e0b8401dbd6c Mon Sep 17 00:00:00 2001 From: 21E14 <21xe14@gmail.com> Date: Sat, 1 Nov 2014 17:42:12 -0400 Subject: [PATCH 1/2] Reset setBlockIndexCandidates once block index db loaded --- src/main.cpp | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 008a05910..1ba17614d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1949,6 +1949,16 @@ static CBlockIndex* FindMostWorkChain() { } while(true); } +// Delete all entries in setBlockIndexCandidates that are worse than the current tip. +static void PruneBlockIndexCandidates() { + // Note that we can't delete the current block itself, as we may need to return to it later in case a + // reorganization to a better block fails. + std::set::iterator it = setBlockIndexCandidates.begin(); + while (setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) { + setBlockIndexCandidates.erase(it++); + } +} + // Try to make some progress towards making pindexMostWork the active block. // pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork. static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) { @@ -1996,13 +2006,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo return false; } } else { - // Delete all entries in setBlockIndexCandidates that are worse than our new current block. - // Note that we can't delete the current block itself, as we may need to return to it later in case a - // reorganization to a better block fails. - std::set::iterator it = setBlockIndexCandidates.begin(); - while (setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) { - setBlockIndexCandidates.erase(it++); - } + PruneBlockIndexCandidates(); // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates. assert(!setBlockIndexCandidates.empty()); if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) { @@ -2860,6 +2864,9 @@ bool static LoadBlockIndexDB() if (it == mapBlockIndex.end()) return true; chainActive.SetTip(it->second); + + PruneBlockIndexCandidates(); + LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n", chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()), From 34559c7c73e3ce67baea0d88ba74b0988b55142d Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 20 Nov 2014 12:43:50 +0100 Subject: [PATCH 2/2] Make PruneBlockIndexCandidates safer --- src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1ba17614d..ac65a4ac2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1954,9 +1954,11 @@ static void PruneBlockIndexCandidates() { // Note that we can't delete the current block itself, as we may need to return to it later in case a // reorganization to a better block fails. std::set::iterator it = setBlockIndexCandidates.begin(); - while (setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) { + while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) { setBlockIndexCandidates.erase(it++); } + // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates. + assert(!setBlockIndexCandidates.empty()); } // Try to make some progress towards making pindexMostWork the active block. @@ -2007,8 +2009,6 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo } } else { PruneBlockIndexCandidates(); - // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates. - assert(!setBlockIndexCandidates.empty()); if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) { // We're in a better position than we were. Return temporarily to release the lock. fContinue = false;