diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-11-26 13:25:46 +0100 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2014-11-26 13:31:03 +0100 |
commit | 53a87c0355c9698c13bc1d3732f4f7b295ab05ea (patch) | |
tree | c4f5754bd33013500e205d8786fc1bc439b901c3 /src | |
parent | 4baa9f0c9b7675c851e05eb3ac185ab88475e959 (diff) | |
parent | 34559c7c73e3ce67baea0d88ba74b0988b55142d (diff) |
Merge pull request #5321
34559c7 Make PruneBlockIndexCandidates safer (Pieter Wuille)
cca48f6 Reset setBlockIndexCandidates once block index db loaded (21E14)
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/src/main.cpp b/src/main.cpp index 88fb31980f..bda2ee7f7b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2008,6 +2008,18 @@ 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<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin(); + 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. // pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork. static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) { @@ -2055,15 +2067,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<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin(); - while (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()); + PruneBlockIndexCandidates(); if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) { // We're in a better position than we were. Return temporarily to release the lock. fContinue = false; @@ -2956,6 +2960,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()), |