Merge bitcoin/bitcoin#20827: During IBD, prune as much as possible until we get close to where we will eventually keep blocks

d298ff8b62 During IBD, prune as much as possible until we get close to where we will eventually keep blocks (Luke Dashjr)

Pull request description:

  This should reduce pruning flushes even more, speeding up IBD with pruning on systems that have a sufficient dbcache.

  Assumes 1 MB per block between tip and best header chain. Simply adds this to the buffer pruning is trying to leave available, which results in pruning almost everything up until we get close to where we need to be keeping blocks.

ACKs for top commit:
  andrewtoth:
    ACK d298ff8b62
  fjahr:
    utACK d298ff8b62
  achow101:
    ACK d298ff8b62

Tree-SHA512: 2a482376bfb177e2ba7c2f0bb0b58b02efdb38b34755a18d1fc3e869df5959c85b6f1009e1386fa8b89c4f90d520383e36bd3e21dec221042315134efb1a455b
This commit is contained in:
Ava Chow 2024-01-25 15:07:45 -05:00
commit 36720994a4
No known key found for this signature in database
GPG key ID: 17565732E08E5E41

View file

@ -307,6 +307,7 @@ void BlockManager::FindFilesToPrune(
// Distribute our -prune budget over all chainstates.
const auto target = std::max(
MIN_DISK_SPACE_FOR_BLOCK_FILES, GetPruneTarget() / chainman.GetAll().size());
const uint64_t target_sync_height = chainman.m_best_header->nHeight;
if (chain.m_chain.Height() < 0 || target == 0) {
return;
@ -329,10 +330,13 @@ void BlockManager::FindFilesToPrune(
// On a prune event, the chainstate DB is flushed.
// To avoid excessive prune events negating the benefit of high dbcache
// values, we should not prune too rapidly.
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
if (chainman.IsInitialBlockDownload()) {
// Since this is only relevant during IBD, we use a fixed 10%
nBuffer += target / 10;
// So when pruning in IBD, increase the buffer to avoid a re-prune too soon.
const auto chain_tip_height = chain.m_chain.Height();
if (chainman.IsInitialBlockDownload() && target_sync_height > (uint64_t)chain_tip_height) {
// Since this is only relevant during IBD, we assume blocks are at least 1 MB on average
static constexpr uint64_t average_block_size = 1000000; /* 1 MB */
const uint64_t remaining_blocks = target_sync_height - chain_tip_height;
nBuffer += average_block_size * remaining_blocks;
}
for (int fileNumber = 0; fileNumber < this->MaxBlockfileNum(); fileNumber++) {