| 137 | } |
| 138 | |
| 139 | void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd) |
| 140 | { |
| 141 | LOCK2(cs_main, cs_LastBlockFile); |
| 142 | if (chain_tip_height < 0 || nPruneTarget == 0) { |
| 143 | return; |
| 144 | } |
| 145 | if ((uint64_t)chain_tip_height <= nPruneAfterHeight) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | unsigned int nLastBlockWeCanPrune{(unsigned)std::min(prune_height, chain_tip_height - static_cast<int>(MIN_BLOCKS_TO_KEEP))}; |
| 150 | uint64_t nCurrentUsage = CalculateCurrentUsage(); |
| 151 | // We don't check to prune until after we've allocated new space for files |
| 152 | // So we should leave a buffer under our target to account for another allocation |
| 153 | // before the next pruning. |
| 154 | uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE; |
| 155 | uint64_t nBytesToPrune; |
| 156 | int count = 0; |
| 157 | |
| 158 | if (nCurrentUsage + nBuffer >= nPruneTarget) { |
| 159 | // On a prune event, the chainstate DB is flushed. |
| 160 | // To avoid excessive prune events negating the benefit of high dbcache |
| 161 | // values, we should not prune too rapidly. |
| 162 | // So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon. |
| 163 | if (is_ibd) { |
| 164 | // Since this is only relevant during IBD, we use a fixed 10% |
| 165 | nBuffer += nPruneTarget / 10; |
| 166 | } |
| 167 | |
| 168 | for (int fileNumber = 0; fileNumber < m_last_blockfile; fileNumber++) { |
| 169 | nBytesToPrune = m_blockfile_info[fileNumber].nSize + m_blockfile_info[fileNumber].nUndoSize; |
| 170 | |
| 171 | if (m_blockfile_info[fileNumber].nSize == 0) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | if (nCurrentUsage + nBuffer < nPruneTarget) { // are we below our target? |
| 176 | break; |
| 177 | } |
| 178 | |
| 179 | // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning |
| 180 | if (m_blockfile_info[fileNumber].nHeightLast > nLastBlockWeCanPrune) { |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | PruneOneBlockFile(fileNumber); |
| 185 | // Queue up the files for removal |
| 186 | setFilesToPrune.insert(fileNumber); |
| 187 | nCurrentUsage -= nBytesToPrune; |
| 188 | count++; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | LogPrint(BCLog::PRUNE, "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n", |
| 193 | nPruneTarget/1024/1024, nCurrentUsage/1024/1024, |
| 194 | ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024, |
| 195 | nLastBlockWeCanPrune, count); |
| 196 | } |
no test coverage detected