mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-12 23:18:14 +01:00
prune: scan and unlink already pruned block files on startup
This commit is contained in:
@@ -371,6 +371,23 @@ bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
|
|||||||
return true;
|
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 CBlockIndex* BlockManager::GetLastCheckpoint(const CCheckpointData& data)
|
||||||
{
|
{
|
||||||
const MapCheckpoints& checkpoints = data.mapCheckpoints;
|
const MapCheckpoints& checkpoints = data.mapCheckpoints;
|
||||||
@@ -556,11 +573,14 @@ uint64_t BlockManager::CalculateCurrentUsage()
|
|||||||
|
|
||||||
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
|
void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
|
||||||
{
|
{
|
||||||
|
std::error_code ec;
|
||||||
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
|
for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
|
||||||
FlatFilePos pos(*it, 0);
|
FlatFilePos pos(*it, 0);
|
||||||
fs::remove(BlockFileSeq().FileName(pos));
|
const bool removed_blockfile{fs::remove(BlockFileSeq().FileName(pos), ec)};
|
||||||
fs::remove(UndoFileSeq().FileName(pos));
|
const bool removed_undofile{fs::remove(UndoFileSeq().FileName(pos), ec)};
|
||||||
LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
|
if (removed_blockfile || removed_undofile) {
|
||||||
|
LogPrint(BCLog::BLOCKSTORE, "Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -158,6 +158,13 @@ public:
|
|||||||
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||||
bool LoadBlockIndexDB(const Consensus::Params& consensus_params) 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);
|
CBlockIndex* AddToBlockIndex(const CBlockHeader& block, CBlockIndex*& best_header) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
/** Create a new block index entry for a given block hash */
|
/** Create a new block index entry for a given block hash */
|
||||||
CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
CBlockIndex* InsertBlockIndex(const uint256& hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
|
||||||
|
|||||||
@@ -4260,6 +4260,8 @@ bool ChainstateManager::LoadBlockIndex()
|
|||||||
bool ret = m_blockman.LoadBlockIndexDB(GetConsensus());
|
bool ret = m_blockman.LoadBlockIndexDB(GetConsensus());
|
||||||
if (!ret) return false;
|
if (!ret) return false;
|
||||||
|
|
||||||
|
m_blockman.ScanAndUnlinkAlreadyPrunedFiles();
|
||||||
|
|
||||||
std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
|
std::vector<CBlockIndex*> vSortedByHeight{m_blockman.GetAllBlockIndices()};
|
||||||
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
|
std::sort(vSortedByHeight.begin(), vSortedByHeight.end(),
|
||||||
CBlockIndexHeightOnlyComparator());
|
CBlockIndexHeightOnlyComparator());
|
||||||
|
|||||||
Reference in New Issue
Block a user