Browse Source

Merge pull request #5321

34559c7 Make PruneBlockIndexCandidates safer (Pieter Wuille)
cca48f6 Reset setBlockIndexCandidates once block index db loaded (21E14)
0.10
Wladimir J. van der Laan 10 years ago
parent
commit
53a87c0355
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 25
      src/main.cpp

25
src/main.cpp

@ -2008,6 +2008,18 @@ static CBlockIndex* FindMostWorkChain() {
} while(true); } 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. // Try to make some progress towards making pindexMostWork the active block.
// pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork. // pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) { static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
@ -2055,15 +2067,7 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
return false; return false;
} }
} else { } else {
// Delete all entries in setBlockIndexCandidates that are worse than our new current block. 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 (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());
if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) { if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
// We're in a better position than we were. Return temporarily to release the lock. // We're in a better position than we were. Return temporarily to release the lock.
fContinue = false; fContinue = false;
@ -2956,6 +2960,9 @@ bool static LoadBlockIndexDB()
if (it == mapBlockIndex.end()) if (it == mapBlockIndex.end())
return true; return true;
chainActive.SetTip(it->second); chainActive.SetTip(it->second);
PruneBlockIndexCandidates();
LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n", LogPrintf("LoadBlockIndexDB(): hashBestChain=%s height=%d date=%s progress=%f\n",
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()), DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),

Loading…
Cancel
Save