Skip to content

Commit 77557dd

Browse files
committed
prune: scan and unlink already pruned block files on startup
1 parent dcdfd72 commit 77557dd

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/node/blockstorage.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,23 @@ bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
371371
return true;
372372
}
373373

374+
void BlockManager::ScanAndUnlinkAlreadyPrunedFiles()
375+
{
376+
AssertLockHeld(::cs_main);
377+
if (!m_have_pruned) {
378+
return;
379+
}
380+
381+
std::set<int> block_files_to_prune;
382+
for (int file_number = 0; file_number < m_last_blockfile; file_number++) {
383+
if (m_blockfile_info[file_number].nSize == 0) {
384+
block_files_to_prune.insert(file_number);
385+
}
386+
}
387+
388+
UnlinkPrunedFiles(block_files_to_prune);
389+
}
390+
374391
const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
375392
{
376393
const MapCheckpoints& checkpoints = data.mapCheckpoints;
@@ -556,11 +573,14 @@ uint64_t BlockManager::CalculateCurrentUsage()
556573

557574
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
558575
{
576+
std::error_code ec;
559577
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
560578
FlatFilePos pos(*it, 0);
561-
fs::remove(BlockFileSeq().FileName(pos));
562-
fs::remove(UndoFileSeq().FileName(pos));
563-
LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
579+
const bool removed_blockfile{fs::remove(BlockFileSeq().FileName(pos), ec)};
580+
const bool removed_undofile{fs::remove(UndoFileSeq().FileName(pos), ec)};
581+
if (removed_blockfile || removed_undofile) {
582+
LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
583+
}
564584
}
565585
}
566586

src/node/blockstorage.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,13 @@ class BlockManager
158158
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
159159
bool LoadBlockIndexDB(const Consensus::Params& consensus_params) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
160160

161+
/**
162+
* Remove any pruned block & undo files that are still on disk.
163+
* This could happen on some systems if the file was still being read while unlinked,
164+
* or if we crash before unlinking.
165+
*/
166+
void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
167+
161168
CBlockIndex* AddToBlockIndex(const CBlockHeader& block, CBlockIndex*& best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
162169
/** Create a new block index entry for a given block hash */
163170
CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

src/validation.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4260,6 +4260,8 @@ bool ChainstateManager::LoadBlockIndex()
42604260
bool ret = m_blockman.LoadBlockIndexDB(GetConsensus());
42614261
if (!ret) return false;
42624262

4263+
m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
4264+
42634265
std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
42644266
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
42654267
CBlockIndexHeightOnlyComparator());

0 commit comments

Comments
 (0)