Skip to content

Commit e8cb5c3

Browse files
committed
Merge pull request #4497
714a3e6 Only keep setBlockIndexValid entries that are possible improvements (Pieter Wuille)
2 parents 1de2992 + 714a3e6 commit e8cb5c3

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/main.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ namespace {
9595
};
9696

9797
CBlockIndex *pindexBestInvalid;
98-
// may contain all CBlockIndex*'s that have validness >=BLOCK_VALID_TRANSACTIONS, and must contain those who aren't failed
98+
99+
// The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS or better that are at least
100+
// as good as our current tip. Entries may be failed, though.
99101
set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexValid;
100102

101103
CCriticalSection cs_LastBlockFile;
@@ -2097,6 +2099,15 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
20972099
return false;
20982100
}
20992101
} else {
2102+
// Delete all entries in setBlockIndexValid that are worse than our new current block.
2103+
// Note that we can't delete the current block itself, as we may need to return to it later in case a
2104+
// reorganization to a better block fails.
2105+
std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexValid.begin();
2106+
while (setBlockIndexValid.value_comp()(*it, chainActive.Tip())) {
2107+
setBlockIndexValid.erase(it++);
2108+
}
2109+
// Either the current tip or a successor of it we're working towards is left in setBlockIndexValid.
2110+
assert(!setBlockIndexValid.empty());
21002111
if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
21012112
// We're in a better position than we were. Return temporarily to release the lock.
21022113
break;

0 commit comments

Comments
 (0)