mirror of
https://github.com/Retropex/bitcoin.git
synced 2025-05-28 13:02:38 +02:00
Move prune lock checking into BlockManager
This commit is contained in:
parent
ad4f3e472f
commit
64de1480d3
@ -176,6 +176,13 @@ bool CBlockIndexHeightOnlyComparator::operator()(const CBlockIndex* pa, const CB
|
|||||||
return pa->nHeight < pb->nHeight;
|
return pa->nHeight < pb->nHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** The number of blocks to keep below the deepest prune lock.
|
||||||
|
* There is nothing special about this number. It is higher than what we
|
||||||
|
* expect to see in regular mainnet reorgs, but not so high that it would
|
||||||
|
* noticeably interfere with the pruning mechanism.
|
||||||
|
* */
|
||||||
|
static constexpr int PRUNE_LOCK_BUFFER{10};
|
||||||
|
|
||||||
std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
|
std::vector<CBlockIndex*> BlockManager::GetAllBlockIndices()
|
||||||
{
|
{
|
||||||
AssertLockHeld(cs_main);
|
AssertLockHeld(cs_main);
|
||||||
@ -269,6 +276,21 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
|
|||||||
m_dirty_fileinfo.insert(fileNumber);
|
m_dirty_fileinfo.insert(fileNumber);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool BlockManager::DoPruneLocksForbidPruning(const CBlockFileInfo& block_file_info)
|
||||||
|
{
|
||||||
|
AssertLockHeld(cs_main);
|
||||||
|
for (const auto& prune_lock : m_prune_locks) {
|
||||||
|
if (prune_lock.second.height_first == std::numeric_limits<int>::max()) continue;
|
||||||
|
// Remove the buffer and one additional block here to get actual height that is outside of the buffer
|
||||||
|
const unsigned int lock_height{(unsigned)std::max(1, prune_lock.second.height_first - PRUNE_LOCK_BUFFER - 1)};
|
||||||
|
if (block_file_info.nHeightLast > lock_height) {
|
||||||
|
LogPrint(BCLog::PRUNE, "%s limited pruning to height %d\n", prune_lock.first, lock_height);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void BlockManager::FindFilesToPruneManual(
|
void BlockManager::FindFilesToPruneManual(
|
||||||
std::set<int>& setFilesToPrune,
|
std::set<int>& setFilesToPrune,
|
||||||
int nManualPruneHeight,
|
int nManualPruneHeight,
|
||||||
@ -291,6 +313,8 @@ void BlockManager::FindFilesToPruneManual(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DoPruneLocksForbidPruning(m_blockfile_info[fileNumber])) continue;
|
||||||
|
|
||||||
PruneOneBlockFile(fileNumber);
|
PruneOneBlockFile(fileNumber);
|
||||||
setFilesToPrune.insert(fileNumber);
|
setFilesToPrune.insert(fileNumber);
|
||||||
count++;
|
count++;
|
||||||
@ -359,6 +383,8 @@ void BlockManager::FindFilesToPrune(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (DoPruneLocksForbidPruning(m_blockfile_info[fileNumber])) continue;
|
||||||
|
|
||||||
PruneOneBlockFile(fileNumber);
|
PruneOneBlockFile(fileNumber);
|
||||||
// Queue up the files for removal
|
// Queue up the files for removal
|
||||||
setFilesToPrune.insert(fileNumber);
|
setFilesToPrune.insert(fileNumber);
|
||||||
|
@ -177,6 +177,8 @@ private:
|
|||||||
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
|
bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos) const;
|
||||||
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
|
bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock) const;
|
||||||
|
|
||||||
|
bool DoPruneLocksForbidPruning(const CBlockFileInfo& block_file_info) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
|
||||||
|
|
||||||
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
|
/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
|
||||||
void FindFilesToPruneManual(
|
void FindFilesToPruneManual(
|
||||||
std::set<int>& setFilesToPrune,
|
std::set<int>& setFilesToPrune,
|
||||||
|
@ -100,12 +100,6 @@ const std::vector<std::string> CHECKLEVEL_DOC {
|
|||||||
"level 4 tries to reconnect the blocks",
|
"level 4 tries to reconnect the blocks",
|
||||||
"each level includes the checks of the previous levels",
|
"each level includes the checks of the previous levels",
|
||||||
};
|
};
|
||||||
/** The number of blocks to keep below the deepest prune lock.
|
|
||||||
* There is nothing special about this number. It is higher than what we
|
|
||||||
* expect to see in regular mainnet reorgs, but not so high that it would
|
|
||||||
* noticeably interfere with the pruning mechanism.
|
|
||||||
* */
|
|
||||||
static constexpr int PRUNE_LOCK_BUFFER{10};
|
|
||||||
|
|
||||||
GlobalMutex g_best_block_mutex;
|
GlobalMutex g_best_block_mutex;
|
||||||
std::condition_variable g_best_block_cv;
|
std::condition_variable g_best_block_cv;
|
||||||
@ -2800,21 +2794,6 @@ bool Chainstate::FlushStateToDisk(
|
|||||||
// make sure we don't prune above any of the prune locks bestblocks
|
// make sure we don't prune above any of the prune locks bestblocks
|
||||||
// pruning is height-based
|
// pruning is height-based
|
||||||
int last_prune{m_chain.Height()}; // last height we can prune
|
int last_prune{m_chain.Height()}; // last height we can prune
|
||||||
std::optional<std::string> limiting_lock; // prune lock that actually was the limiting factor, only used for logging
|
|
||||||
|
|
||||||
for (const auto& prune_lock : m_blockman.m_prune_locks) {
|
|
||||||
if (prune_lock.second.height_first == std::numeric_limits<int>::max()) continue;
|
|
||||||
// Remove the buffer and one additional block here to get actual height that is outside of the buffer
|
|
||||||
const int lock_height{prune_lock.second.height_first - PRUNE_LOCK_BUFFER - 1};
|
|
||||||
last_prune = std::max(1, std::min(last_prune, lock_height));
|
|
||||||
if (last_prune == lock_height) {
|
|
||||||
limiting_lock = prune_lock.first;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (limiting_lock) {
|
|
||||||
LogPrint(BCLog::PRUNE, "%s limited pruning to height %d\n", limiting_lock.value(), last_prune);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nManualPruneHeight > 0) {
|
if (nManualPruneHeight > 0) {
|
||||||
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
|
LOG_TIME_MILLIS_WITH_CATEGORY("find files to prune (manual)", BCLog::BENCH);
|
||||||
|
@ -70,7 +70,7 @@ class FeatureIndexPruneTest(BitcoinTestFramework):
|
|||||||
|
|
||||||
self.log.info("prune some blocks")
|
self.log.info("prune some blocks")
|
||||||
for node in self.nodes[:2]:
|
for node in self.nodes[:2]:
|
||||||
with node.assert_debug_log(['limited pruning to height 689']):
|
with node.assert_debug_log(['Prune: UnlinkPrunedFiles deleted blk/rev (00000)']):
|
||||||
pruneheight_new = node.pruneblockchain(400)
|
pruneheight_new = node.pruneblockchain(400)
|
||||||
# the prune heights used here and below are magic numbers that are determined by the
|
# the prune heights used here and below are magic numbers that are determined by the
|
||||||
# thresholds at which block files wrap, so they depend on disk serialization and default block file size.
|
# thresholds at which block files wrap, so they depend on disk serialization and default block file size.
|
||||||
@ -143,7 +143,7 @@ class FeatureIndexPruneTest(BitcoinTestFramework):
|
|||||||
self.sync_index(height=2500)
|
self.sync_index(height=2500)
|
||||||
|
|
||||||
for node in self.nodes[:2]:
|
for node in self.nodes[:2]:
|
||||||
with node.assert_debug_log(['limited pruning to height 2489']):
|
with node.assert_debug_log(['Prune: UnlinkPrunedFiles deleted blk/rev (00007)']):
|
||||||
pruneheight_new = node.pruneblockchain(2500)
|
pruneheight_new = node.pruneblockchain(2500)
|
||||||
assert_equal(pruneheight_new, 2005)
|
assert_equal(pruneheight_new, 2005)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user