Allow gaps while trimming headers

This commit is contained in:
Pablo Greco 2023-06-29 11:51:24 -07:00 committed by James Dorfman
parent f89728c514
commit afc16e8f84

View file

@ -2582,29 +2582,31 @@ bool CChainState::FlushStateToDisk(
(*it)->set_stored();
}
if (node::fTrimHeaders && !ShutdownRequested()) {
int trim_height = pindexBestHeader ? pindexBestHeader->nHeight - node::nMustKeepFullHeaders : 0;
if (node::fTrimHeaders && trim_height > 0 && !ShutdownRequested()) {
static int nMinTrimHeight{0};
LogPrintf("Flushing block index, trimming headers, setTrimmableBlockIndex.size(): %d\n", setTrimmableBlockIndex.size());
int trim_height = m_chain.Height() - node::nMustKeepFullHeaders;
int min_height = std::numeric_limits<int>::max();
CBlockIndex* min_index = nullptr;
for (std::set<CBlockIndex*>::iterator it = setTrimmableBlockIndex.begin(); it != setTrimmableBlockIndex.end(); it++) {
(*it)->assert_untrimmed();
if ((*it)->nHeight < trim_height) {
(*it)->trim();
if ((*it)->nHeight < min_height) {
min_height = (*it)->nHeight;
min_index = *it;
}
}
}
CBlockIndex* min_index = pindexBestHeader->GetAncestor(trim_height-1);
// Handle any remaining untrimmed blocks that were too recent for trimming last time we flushed.
if (min_index) {
min_index = min_index->pprev;
while (min_index && !min_index->trimmed()) {
min_index->trim();
int nMaxTrimHeightRound = std::max(nMinTrimHeight, min_index->nHeight + 1);
while (min_index && min_index->nHeight >= nMinTrimHeight) {
if (!min_index->trimmed()) {
// there may be gaps due to untrimmed blocks, we need to check them all
if (!min_index->trim()) {
// Header could not be trimmed, we'll need to try again next round
nMaxTrimHeightRound = min_index->nHeight;
}
}
min_index = min_index->pprev;
}
nMinTrimHeight = nMaxTrimHeightRound;
}
}
}