Skip to content

Commit 5e11226

Browse files
committed
Avoid pruning below the blockfilterindex sync height
1 parent 00d57ff commit 5e11226

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/validation.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <cuckoocache.h>
1818
#include <flatfile.h>
1919
#include <hash.h>
20+
#include <index/blockfilterindex.h>
2021
#include <index/txindex.h>
2122
#include <logging.h>
2223
#include <logging/timer.h>
@@ -2249,17 +2250,25 @@ bool CChainState::FlushStateToDisk(
22492250
{
22502251
bool fFlushForPrune = false;
22512252
bool fDoFullFlush = false;
2253+
22522254
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(&m_mempool);
22532255
LOCK(cs_LastBlockFile);
22542256
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
2257+
// make sure we don't prune above the blockfilterindexes bestblocks
2258+
// pruning is height-based
2259+
int last_prune = m_chain.Height(); // last height we can prune
2260+
ForEachBlockFilterIndex([&](BlockFilterIndex& index) {
2261+
last_prune = std::max(1, std::min(last_prune, index.GetSummary().best_block_height));
2262+
});
2263+
22552264
if (nManualPruneHeight > 0) {
22562265
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
22572266

2258-
m_blockman.FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight, m_chain.Height());
2267+
m_blockman.FindFilesToPruneManual(setFilesToPrune, std::min(last_prune, nManualPruneHeight), m_chain.Height());
22592268
} else {
22602269
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune", BCLog::BENCH);
22612270

2262-
m_blockman.FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight(), m_chain.Height(), IsInitialBlockDownload());
2271+
m_blockman.FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight(), m_chain.Height(), last_prune, IsInitialBlockDownload());
22632272
fCheckForPruning = false;
22642273
}
22652274
if (!setFilesToPrune.empty()) {
@@ -3934,7 +3943,7 @@ void PruneBlockFilesManual(int nManualPruneHeight)
39343943
}
39353944
}
39363945

3937-
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, bool is_ibd)
3946+
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
39383947
{
39393948
LOCK2(cs_main, cs_LastBlockFile);
39403949
if (chain_tip_height < 0 || nPruneTarget == 0) {
@@ -3944,7 +3953,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
39443953
return;
39453954
}
39463955

3947-
unsigned int nLastBlockWeCanPrune = chain_tip_height - MIN_BLOCKS_TO_KEEP;
3956+
unsigned int nLastBlockWeCanPrune = std::min(prune_height, chain_tip_height - static_cast<int>(MIN_BLOCKS_TO_KEEP));
39483957
uint64_t nCurrentUsage = CalculateCurrentUsage();
39493958
// We don't check to prune until after we've allocated new space for files
39503959
// So we should leave a buffer under our target to account for another allocation

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class BlockManager
361361
*
362362
* @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
363363
*/
364-
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, bool is_ibd);
364+
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd);
365365

366366
public:
367367
BlockMap m_block_index GUARDED_BY(cs_main);

0 commit comments

Comments
 (0)