src/validation.cpp
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
{
    LOCK2(cs_main, cs_LastBlockFile);
    if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
        return;
    }
    if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
        return;
    }

    unsigned int nLastBlockWeMustKeep = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
    uint64_t nCurrentUsage = CalculateCurrentUsage();
    // We don't check to prune until after we've allocated new space for files
    // So we should leave a buffer under our target to account for another allocation
    // before the next pruning.
    uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
    uint64_t nBytesToPrune;
    int count=0;

    if (nCurrentUsage + nBuffer >= nPruneTarget) {
        // 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 (IsInitialBlockDownload()) {
            // Since this is only relevant during IBD, we use a fixed 10%
            nBuffer += nPruneTarget / 10;
        }

        for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
            nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;

            if (vinfoBlockFile[fileNumber].nSize == 0)
                continue;

            if (nCurrentUsage + nBuffer < nPruneTarget)  // are we below our target?
                break;

            // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
            if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeMustKeep)
                continue;

            PruneOneBlockFile(fileNumber);
            // Queue up the files for removal
            setFilesToPrune.insert(fileNumber);
            nCurrentUsage -= nBytesToPrune;
            count++;
        }
    }

    LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB min_must_keep=%d removed %d blk/rev pairs\n",
           nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
           ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
           nLastBlockWeMustKeep, count);
}
/* Calculate the block/rev files that should be deleted to remain under target*/
Calculate the block/rev files that should be deleted to remain under target
Calculate the block / rev files that should be deleted to remain under target
void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
{
    LOCK2(cs_main, cs_LastBlockFile);
    if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
        return;
    }
    if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
        return;
    }
    unsigned int nLastBlockWeMustKeep = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
    uint64_t nCurrentUsage = CalculateCurrentUsage();
    uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
    uint64_t nBytesToPrune;
    int count=0;
    if (nCurrentUsage + nBuffer >= nPruneTarget) {
        if (IsInitialBlockDownload()) {
            nBuffer += nPruneTarget / 10;
        }
        for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
            nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
            if (vinfoBlockFile[fileNumber].nSize == 0)
                continue;
            if (nCurrentUsage + nBuffer < nPruneTarget)  
                break;
            if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeMustKeep)
                continue;
            PruneOneBlockFile(fileNumber);
            setFilesToPrune.insert(fileNumber);
            nCurrentUsage -= nBytesToPrune;
            count++;
        }
    }
    LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB min_must_keep=%d removed %d blk/rev pairs\n",
           nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
           ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
           nLastBlockWeMustKeep, count);
}
void FindFilesToPrune ( std :: set < int > & setFilesToPrune , uint64_t nPruneAfterHeight ) { LOCK2 ( cs_main , cs_LastBlockFile ) ; if ( chainActive . Tip ( ) == nullptr || nPruneTarget == 0 ) { return ; } if ( ( uint64_t ) chainActive . Tip ( ) -> nHeight <= nPruneAfterHeight ) { return ; } unsigned int nLastBlockWeMustKeep = chainActive . Tip ( ) -> nHeight - MIN_BLOCKS_TO_KEEP ; uint64_t nCurrentUsage = CalculateCurrentUsage ( ) ; uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE ; uint64_t nBytesToPrune ; int count = 0 ; if ( nCurrentUsage + nBuffer >= nPruneTarget ) { if ( IsInitialBlockDownload ( ) ) { nBuffer += nPruneTarget / 10 ; } for ( int fileNumber = 0 ; fileNumber < nLastBlockFile ; fileNumber ++ ) { nBytesToPrune = vinfoBlockFile [ fileNumber ] . nSize + vinfoBlockFile [ fileNumber ] . nUndoSize ; if ( vinfoBlockFile [ fileNumber ] . nSize == 0 ) continue ; if ( nCurrentUsage + nBuffer < nPruneTarget ) break ; if ( vinfoBlockFile [ fileNumber ] . nHeightLast > nLastBlockWeMustKeep ) continue ; PruneOneBlockFile ( fileNumber ) ; setFilesToPrune . insert ( fileNumber ) ; nCurrentUsage -= nBytesToPrune ; count ++ ; } } LogPrint ( " " , " \n " , nPruneTarget / 1024 / 1024 , nCurrentUsage / 1024 / 1024 , ( ( int64_t ) nPruneTarget - ( int64_t ) nCurrentUsage ) / 1024 / 1024 , nLastBlockWeMustKeep , count ) ; }
Calculate the block/rev files that should be deleted to remain under target
Calculate the block / rev files that should be deleted to remain under target
// We don't check to prune until after we've allocated new space for files // So we should leave a buffer under our target to account for another allocation // before the next pruning. // 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. // Since this is only relevant during IBD, we use a fixed 10% // are we below our target? // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning // Queue up the files for removal