@@ -257,40 +257,56 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
257
257
m_dirty_fileinfo.insert (fileNumber);
258
258
}
259
259
260
- void BlockManager::FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
260
+ void BlockManager::FindFilesToPruneManual (
261
+ std::set<int >& setFilesToPrune,
262
+ int nManualPruneHeight,
263
+ const Chainstate& chain,
264
+ ChainstateManager& chainman)
261
265
{
262
266
assert (IsPruneMode () && nManualPruneHeight > 0 );
263
267
264
268
LOCK2 (cs_main, cs_LastBlockFile);
265
- if (chain_tip_height < 0 ) {
269
+ if (chain. m_chain . Height () < 0 ) {
266
270
return ;
267
271
}
268
272
269
- // last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
270
- unsigned int nLastBlockWeCanPrune = std::min (( unsigned )nManualPruneHeight, chain_tip_height - MIN_BLOCKS_TO_KEEP);
273
+ const auto [min_block_to_prune, last_block_can_prune] = chainman. GetPruneRange (chain, nManualPruneHeight);
274
+
271
275
int count = 0 ;
272
276
for (int fileNumber = 0 ; fileNumber < m_last_blockfile; fileNumber++) {
273
- if (m_blockfile_info[fileNumber].nSize == 0 || m_blockfile_info[fileNumber].nHeightLast > nLastBlockWeCanPrune) {
277
+ const auto & fileinfo = m_blockfile_info[fileNumber];
278
+ if (fileinfo.nSize == 0 || fileinfo.nHeightLast > (unsigned )last_block_can_prune || fileinfo.nHeightFirst < (unsigned )min_block_to_prune) {
274
279
continue ;
275
280
}
281
+
276
282
PruneOneBlockFile (fileNumber);
277
283
setFilesToPrune.insert (fileNumber);
278
284
count++;
279
285
}
280
- LogPrintf (" Prune (Manual): prune_height=%d removed %d blk/rev pairs\n " , nLastBlockWeCanPrune, count);
286
+ LogPrintf (" [%s] Prune (Manual): prune_height=%d removed %d blk/rev pairs\n " ,
287
+ chain.GetRole (), last_block_can_prune, count);
281
288
}
282
289
283
- void BlockManager::FindFilesToPrune (std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
290
+ void BlockManager::FindFilesToPrune (
291
+ std::set<int >& setFilesToPrune,
292
+ int last_prune,
293
+ const Chainstate& chain,
294
+ ChainstateManager& chainman)
284
295
{
285
296
LOCK2 (cs_main, cs_LastBlockFile);
286
- if (chain_tip_height < 0 || GetPruneTarget () == 0 ) {
297
+ // Distribute our -prune budget over all chainstates.
298
+ const auto target = std::max (
299
+ MIN_DISK_SPACE_FOR_BLOCK_FILES, GetPruneTarget () / chainman.GetAll ().size ());
300
+
301
+ if (chain.m_chain .Height () < 0 || target == 0 ) {
287
302
return ;
288
303
}
289
- if (( uint64_t )chain_tip_height <= nPruneAfterHeight ) {
304
+ if (static_cast < uint64_t >(chain. m_chain . Height ()) <= chainman. GetParams (). PruneAfterHeight () ) {
290
305
return ;
291
306
}
292
307
293
- unsigned int nLastBlockWeCanPrune{(unsigned )std::min (prune_height, chain_tip_height - static_cast <int >(MIN_BLOCKS_TO_KEEP))};
308
+ const auto [min_block_to_prune, last_block_can_prune] = chainman.GetPruneRange (chain, last_prune);
309
+
294
310
uint64_t nCurrentUsage = CalculateCurrentUsage ();
295
311
// We don't check to prune until after we've allocated new space for files
296
312
// So we should leave a buffer under our target to account for another allocation
@@ -299,29 +315,31 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
299
315
uint64_t nBytesToPrune;
300
316
int count = 0 ;
301
317
302
- if (nCurrentUsage + nBuffer >= GetPruneTarget () ) {
318
+ if (nCurrentUsage + nBuffer >= target ) {
303
319
// On a prune event, the chainstate DB is flushed.
304
320
// To avoid excessive prune events negating the benefit of high dbcache
305
321
// values, we should not prune too rapidly.
306
322
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
307
- if (is_ibd ) {
323
+ if (chainman. IsInitialBlockDownload () ) {
308
324
// Since this is only relevant during IBD, we use a fixed 10%
309
- nBuffer += GetPruneTarget () / 10 ;
325
+ nBuffer += target / 10 ;
310
326
}
311
327
312
328
for (int fileNumber = 0 ; fileNumber < m_last_blockfile; fileNumber++) {
313
- nBytesToPrune = m_blockfile_info[fileNumber].nSize + m_blockfile_info[fileNumber].nUndoSize ;
329
+ const auto & fileinfo = m_blockfile_info[fileNumber];
330
+ nBytesToPrune = fileinfo.nSize + fileinfo.nUndoSize ;
314
331
315
- if (m_blockfile_info[fileNumber] .nSize == 0 ) {
332
+ if (fileinfo .nSize == 0 ) {
316
333
continue ;
317
334
}
318
335
319
- if (nCurrentUsage + nBuffer < GetPruneTarget () ) { // are we below our target?
336
+ if (nCurrentUsage + nBuffer < target ) { // are we below our target?
320
337
break ;
321
338
}
322
339
323
- // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
324
- if (m_blockfile_info[fileNumber].nHeightLast > nLastBlockWeCanPrune) {
340
+ // don't prune files that could have a block that's not within the allowable
341
+ // prune range for the chain being pruned.
342
+ if (fileinfo.nHeightLast > (unsigned )last_block_can_prune || fileinfo.nHeightFirst < (unsigned )min_block_to_prune) {
325
343
continue ;
326
344
}
327
345
@@ -333,10 +351,10 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
333
351
}
334
352
}
335
353
336
- LogPrint (BCLog::PRUNE, " target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n " ,
337
- GetPruneTarget () / 1024 / 1024 , nCurrentUsage / 1024 / 1024 ,
338
- (int64_t (GetPruneTarget () ) - int64_t (nCurrentUsage)) / 1024 / 1024 ,
339
- nLastBlockWeCanPrune , count);
354
+ LogPrint (BCLog::PRUNE, " [%s] target=%dMiB actual=%dMiB diff=%dMiB min_height=%d max_prune_height=%d removed %d blk/rev pairs\n " ,
355
+ chain. GetRole (), target / 1024 / 1024 , nCurrentUsage / 1024 / 1024 ,
356
+ (int64_t (target ) - int64_t (nCurrentUsage)) / 1024 / 1024 ,
357
+ min_block_to_prune, last_block_can_prune , count);
340
358
}
341
359
342
360
void BlockManager::UpdatePruneLock (const std::string& name, const PruneLockInfo& lock_info) {
0 commit comments