Calculate the block/rev files that should be deleted to remain under target*/
| 5263 | |
| 5264 | /* Calculate the block/rev files that should be deleted to remain under target*/ |
| 5265 | void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight) |
| 5266 | { |
| 5267 | LOCK2(cs_main, cs_LastBlockFile); |
| 5268 | if (chainActive.Tip() == NULL || nPruneTarget == 0) { |
| 5269 | return; |
| 5270 | } |
| 5271 | if (chainActive.Height() <= Params().PruneAfterHeight()) { |
| 5272 | return; |
| 5273 | } |
| 5274 | |
| 5275 | unsigned int nLastBlockWeMustKeep = chainActive.Height() - MIN_BLOCKS_TO_KEEP; |
| 5276 | uint64_t nCurrentUsage = CalculateCurrentUsage(); |
| 5277 | // We don't check to prune until after we've allocated new space for files |
| 5278 | // So we should leave a buffer under our target to account for another allocation |
| 5279 | // before the next pruning. |
| 5280 | uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE; |
| 5281 | uint64_t nBytesToPrune; |
| 5282 | int count=0; |
| 5283 | |
| 5284 | if (nCurrentUsage + nBuffer >= nPruneTarget) { |
| 5285 | for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) { |
| 5286 | nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize; |
| 5287 | |
| 5288 | if (vinfoBlockFile[fileNumber].nSize == 0) |
| 5289 | continue; |
| 5290 | |
| 5291 | if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target? |
| 5292 | break; |
| 5293 | |
| 5294 | // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip |
| 5295 | if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeMustKeep) |
| 5296 | break; |
| 5297 | |
| 5298 | PruneOneBlockFile(fileNumber); |
| 5299 | // Queue up the files for removal |
| 5300 | setFilesToPrune.insert(fileNumber); |
| 5301 | nCurrentUsage -= nBytesToPrune; |
| 5302 | count++; |
| 5303 | } |
| 5304 | } |
| 5305 | |
| 5306 | LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB min_must_keep=%d removed %d blk/rev pairs\n", |
| 5307 | nPruneTarget/1024/1024, nCurrentUsage/1024/1024, |
| 5308 | ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024, |
| 5309 | nLastBlockWeMustKeep, count); |
| 5310 | } |
| 5311 | |
| 5312 | bool CheckDiskSpace(uint64_t nAdditionalBytes) |
| 5313 | { |
no test coverage detected