Merge cbfbf46e57 into merged_master (Bitcoin PR bitcoin/bitcoin#25781)

This commit is contained in:
Byron Hambly 2025-04-10 08:56:13 +02:00
commit 59a4dcb178
17 changed files with 142 additions and 66 deletions

View file

@ -27,10 +27,9 @@
#include <unordered_map>
namespace node {
std::atomic_bool fImporting(false);
std::atomic_bool fReindex(false);
bool fPruneMode = false;
uint64_t nPruneTarget = 0;
// ELEMENTS
bool fTrimHeaders = false;
uint64_t nMustKeepFullHeaders = std::numeric_limits<uint64_t>::max();
int64_t nHeaderDownloadBuffer = std::numeric_limits<int64_t>::max();
@ -158,7 +157,7 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
void BlockManager::FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight, int chain_tip_height)
{
assert(fPruneMode && nManualPruneHeight > 0);
assert(IsPruneMode() && nManualPruneHeight > 0);
LOCK2(cs_main, cs_LastBlockFile);
if (chain_tip_height < 0) {
@ -182,7 +181,7 @@ void BlockManager::FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nM
void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight, int chain_tip_height, int prune_height, bool is_ibd)
{
LOCK2(cs_main, cs_LastBlockFile);
if (chain_tip_height < 0 || nPruneTarget == 0) {
if (chain_tip_height < 0 || GetPruneTarget() == 0) {
return;
}
if ((uint64_t)chain_tip_height <= nPruneAfterHeight) {
@ -198,14 +197,14 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
uint64_t nBytesToPrune;
int count = 0;
if (nCurrentUsage + nBuffer >= nPruneTarget) {
if (nCurrentUsage + nBuffer >= GetPruneTarget()) {
// On a prune event, the chainstate DB is flushed.
// To avoid excessive prune events negating the benefit of high dbcache
// values, we should not prune too rapidly.
// So when pruning in IBD, increase the buffer a bit to avoid a re-prune too soon.
if (is_ibd) {
// Since this is only relevant during IBD, we use a fixed 10%
nBuffer += nPruneTarget / 10;
nBuffer += GetPruneTarget() / 10;
}
for (int fileNumber = 0; fileNumber < m_last_blockfile; fileNumber++) {
@ -215,7 +214,7 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
continue;
}
if (nCurrentUsage + nBuffer < nPruneTarget) { // are we below our target?
if (nCurrentUsage + nBuffer < GetPruneTarget()) { // are we below our target?
break;
}
@ -233,9 +232,9 @@ void BlockManager::FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPr
}
LogPrint(BCLog::PRUNE, "target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
nLastBlockWeCanPrune, count);
GetPruneTarget() / 1024 / 1024, nCurrentUsage / 1024 / 1024,
(int64_t(GetPruneTarget()) - int64_t(nCurrentUsage)) / 1024 / 1024,
nLastBlockWeCanPrune, count);
}
void BlockManager::UpdatePruneLock(const std::string& name, const PruneLockInfo& lock_info) {
@ -668,7 +667,7 @@ bool BlockManager::FindBlockPos(FlatFilePos& pos, unsigned int nAddSize, unsigne
if (out_of_space) {
return AbortNode("Disk space is too low!", _("Disk space is too low!"));
}
if (bytes_allocated != 0 && fPruneMode) {
if (bytes_allocated != 0 && IsPruneMode()) {
m_check_for_pruning = true;
}
}
@ -692,7 +691,7 @@ bool BlockManager::FindUndoPos(BlockValidationState& state, int nFile, FlatFileP
if (out_of_space) {
return AbortNode(state, "Disk space is too low!", _("Disk space is too low!"));
}
if (bytes_allocated != 0 && fPruneMode) {
if (bytes_allocated != 0 && IsPruneMode()) {
m_check_for_pruning = true;
}
@ -857,17 +856,20 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight, CCha
return blockPos;
}
struct CImportingNow {
CImportingNow()
{
assert(fImporting == false);
fImporting = true;
}
class ImportingNow
{
std::atomic<bool>& m_importing;
~CImportingNow()
public:
ImportingNow(std::atomic<bool>& importing) : m_importing{importing}
{
assert(fImporting == true);
fImporting = false;
assert(m_importing == false);
m_importing = true;
}
~ImportingNow()
{
assert(m_importing == true);
m_importing = false;
}
};
@ -877,7 +879,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
ScheduleBatchPriority();
{
CImportingNow imp;
ImportingNow imp{chainman.m_blockman.m_importing};
// -reindex
if (fReindex) {
@ -943,7 +945,7 @@ void ThreadImport(ChainstateManager& chainman, std::vector<fs::path> vImportFile
StartShutdown();
return;
}
} // End scope of CImportingNow
} // End scope of ImportingNow
chainman.ActiveChainstate().LoadMempool(mempool_path);
}
} // namespace node