aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Toth <andrewstoth@gmail.com>2022-12-20 12:25:36 -0500
committerAndrew Toth <andrewstoth@gmail.com>2022-12-20 12:25:36 -0500
commit77557dda4a123515d0fa2a545ee21d7c43a66988 (patch)
treef652cebf93d4c5547b6721aca729b627ac681af7 /src
parentdcdfd72861c09a7945b9facc3726177a2d06fa64 (diff)
prune: scan and unlink already pruned block files on startup
Diffstat (limited to 'src')
-rw-r--r--src/node/blockstorage.cpp26
-rw-r--r--src/node/blockstorage.h7
-rw-r--r--src/validation.cpp2
3 files changed, 32 insertions, 3 deletions
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index 04d46f4361..714f0e7f25 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -371,6 +371,23 @@ bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
return true;
}
+void BlockManager::ScanAndUnlinkAlreadyPrunedFiles()
+{
+ AssertLockHeld(::cs_main);
+ if (!m_have_pruned) {
+ return;
+ }
+
+ std::set<int> block_files_to_prune;
+ for (int file_number = 0; file_number < m_last_blockfile; file_number++) {
+ if (m_blockfile_info[file_number].nSize == 0) {
+ block_files_to_prune.insert(file_number);
+ }
+ }
+
+ UnlinkPrunedFiles(block_files_to_prune);
+}
+
const CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
{
const MapCheckpoints& checkpoints = data.mapCheckpoints;
@@ -556,11 +573,14 @@ uint64_t BlockManager::CalculateCurrentUsage()
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
{
+ std::error_code ec;
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
FlatFilePos pos(*it, 0);
- fs::remove(BlockFileSeq().FileName(pos));
- fs::remove(UndoFileSeq().FileName(pos));
- LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
+ const bool removed_blockfile{fs::remove(BlockFileSeq().FileName(pos), ec)};
+ const bool removed_undofile{fs::remove(UndoFileSeq().FileName(pos), ec)};
+ if (removed_blockfile || removed_undofile) {
+ LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
+ }
}
}
diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h
index 29501c1959..c080867010 100644
--- a/src/node/blockstorage.h
+++ b/src/node/blockstorage.h
@@ -158,6 +158,13 @@ public:
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool LoadBlockIndexDB(const Consensus::Params& consensus_params) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+ /**
+ * Remove any pruned block & undo files that are still on disk.
+ * This could happen on some systems if the file was still being read while unlinked,
+ * or if we crash before unlinking.
+ */
+ void ScanAndUnlinkAlreadyPrunedFiles() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+
CBlockIndex* AddToBlockIndex(const CBlockHeader& block, CBlockIndex*& best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
/** Create a new block index entry for a given block hash */
CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
diff --git a/src/validation.cpp b/src/validation.cpp
index 76bea97341..486c67a74e 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -4260,6 +4260,8 @@ bool ChainstateManager::LoadBlockIndex()
bool ret = m_blockman.LoadBlockIndexDB(GetConsensus());
if (!ret) return false;
+ m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
+
std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
CBlockIndexHeightOnlyComparator());