Skip to content

Commit d298ff8

Browse files
committed
During IBD, prune as much as possible until we get close to where we will eventually keep blocks
1 parent 96ec3b6 commit d298ff8

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/node/blockstorage.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ void BlockManager::FindFilesToPrune(
298298
// Distribute our -prune budget over all chainstates.
299299
const auto target = std::max(
300300
MIN_DISK_SPACE_FOR_BLOCK_FILES, GetPruneTarget() / chainman.GetAll().size());
301+
const uint64_t target_sync_height = chainman.m_best_header->nHeight;
301302

302303
if (chain.m_chain.Height() < 0 || target == 0) {
303304
return;
@@ -320,10 +321,13 @@ void BlockManager::FindFilesToPrune(
320321
// On a prune event, the chainstate DB is flushed.
321322
// To avoid excessive prune events negating the benefit of high dbcache
322323
// values, we should not prune too rapidly.
323-
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
324-
if (chainman.IsInitialBlockDownload()) {
325-
// Since this is only relevant during IBD, we use a fixed 10%
326-
nBuffer += target / 10;
324+
// So when pruning in IBD, increase the buffer to avoid a re-prune too soon.
325+
const auto chain_tip_height = chain.m_chain.Height();
326+
if (chainman.IsInitialBlockDownload() && target_sync_height > (uint64_t)chain_tip_height) {
327+
// Since this is only relevant during IBD, we assume blocks are at least 1 MB on average
328+
static constexpr uint64_t average_block_size = 1000000; /* 1 MB */
329+
const uint64_t remaining_blocks = target_sync_height - chain_tip_height;
330+
nBuffer += average_block_size * remaining_blocks;
327331
}
328332

329333
for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {

0 commit comments

Comments
 (0)