Skip to content

Commit 714a3e6

Browse files
committed
Only keep setBlockIndexValid entries that are possible improvements
1 parent f3330b4 commit 714a3e6

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
@@ -94,7 +94,9 @@ namespace {
9494
};
9595

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

100102
CCriticalSection cs_LastBlockFile;
@@ -2129,6 +2131,15 @@ static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMo
21292131
return false;
21302132
}
21312133
} else {
2134+
// Delete all entries in setBlockIndexValid that are worse than our new current block.
2135+
// Note that we can't delete the current block itself, as we may need to return to it later in case a
2136+
// reorganization to a better block fails.
2137+
std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexValid.begin();
2138+
while (setBlockIndexValid.value_comp()(*it, chainActive.Tip())) {
2139+
setBlockIndexValid.erase(it++);
2140+
}
2141+
// Either the current tip or a successor of it we're working towards is left in setBlockIndexValid.
2142+
assert(!setBlockIndexValid.empty());
21322143
if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
21332144
// We're in a better position than we were. Return temporarily to release the lock.
21342145
break;

0 commit comments

Comments
 (0)