17
17
#include < cuckoocache.h>
18
18
#include < flatfile.h>
19
19
#include < hash.h>
20
+ #include < index/blockfilterindex.h>
20
21
#include < index/txindex.h>
21
22
#include < logging.h>
22
23
#include < logging/timer.h>
@@ -2249,17 +2250,25 @@ bool CChainState::FlushStateToDisk(
2249
2250
{
2250
2251
bool fFlushForPrune = false ;
2251
2252
bool fDoFullFlush = false ;
2253
+
2252
2254
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&m_mempool);
2253
2255
LOCK (cs_LastBlockFile);
2254
2256
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
+
2255
2264
if (nManualPruneHeight > 0 ) {
2256
2265
LOG_TIME_MILLIS_WITH_CATEGORY (" find files to prune (manual)" , BCLog::BENCH);
2257
2266
2258
- m_blockman.FindFilesToPruneManual (setFilesToPrune, nManualPruneHeight, m_chain.Height ());
2267
+ m_blockman.FindFilesToPruneManual (setFilesToPrune, std::min (last_prune, nManualPruneHeight) , m_chain.Height ());
2259
2268
} else {
2260
2269
LOG_TIME_MILLIS_WITH_CATEGORY (" find files to prune" , BCLog::BENCH);
2261
2270
2262
- m_blockman.FindFilesToPrune (setFilesToPrune, chainparams.PruneAfterHeight (), m_chain.Height (), IsInitialBlockDownload ());
2271
+ m_blockman.FindFilesToPrune (setFilesToPrune, chainparams.PruneAfterHeight (), m_chain.Height (), last_prune, IsInitialBlockDownload ());
2263
2272
fCheckForPruning = false ;
2264
2273
}
2265
2274
if (!setFilesToPrune.empty ()) {
@@ -3934,7 +3943,7 @@ void PruneBlockFilesManual(int nManualPruneHeight)
3934
3943
}
3935
3944
}
3936
3945
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)
3938
3947
{
3939
3948
LOCK2 (cs_main, cs_LastBlockFile);
3940
3949
if (chain_tip_height < 0 || nPruneTarget == 0 ) {
@@ -3944,7 +3953,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
3944
3953
return ;
3945
3954
}
3946
3955
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)) ;
3948
3957
uint64_t nCurrentUsage = CalculateCurrentUsage ();
3949
3958
// We don't check to prune until after we've allocated new space for files
3950
3959
// So we should leave a buffer under our target to account for another allocation
0 commit comments