Skip to content

Commit c9eedc8

Browse files
committed
merge bitcoin#26513: Make static nLastFlush and nLastWrite Chainstate members
1 parent 859d64e commit c9eedc8

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

src/validation.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,8 +2652,6 @@ bool CChainState::FlushStateToDisk(
26522652
{
26532653
LOCK(cs_main);
26542654
assert(this->CanFlushToDisk());
2655-
static std::chrono::microseconds nLastWrite{0};
2656-
static std::chrono::microseconds nLastFlush{0};
26572655
std::set<int> setFilesToPrune;
26582656
bool full_flush_completed = false;
26592657

@@ -2707,11 +2705,11 @@ bool CChainState::FlushStateToDisk(
27072705
}
27082706
const auto nNow = GetTime<std::chrono::microseconds>();
27092707
// Avoid writing/flushing immediately after startup.
2710-
if (nLastWrite.count() == 0) {
2711-
nLastWrite = nNow;
2708+
if (m_last_write.count() == 0) {
2709+
m_last_write = nNow;
27122710
}
2713-
if (nLastFlush.count() == 0) {
2714-
nLastFlush = nNow;
2711+
if (m_last_flush.count() == 0) {
2712+
m_last_flush = nNow;
27152713
}
27162714
// The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
27172715
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
@@ -2720,9 +2718,9 @@ bool CChainState::FlushStateToDisk(
27202718
// The evodb cache is too large
27212719
bool fEvoDbCacheCritical = mode == FlushStateMode::IF_NEEDED && m_evoDb.GetMemoryUsage() >= (64 << 20);
27222720
// It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
2723-
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > nLastWrite + DATABASE_WRITE_INTERVAL;
2721+
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > m_last_write + DATABASE_WRITE_INTERVAL;
27242722
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2725-
bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > nLastFlush + DATABASE_FLUSH_INTERVAL;
2723+
bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > m_last_flush + DATABASE_FLUSH_INTERVAL;
27262724
// Combine all conditions that result in a full cache flush.
27272725
fDoFullFlush = (mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fEvoDbCacheCritical || fPeriodicFlush || fFlushForPrune;
27282726
// Write blocks and block index to disk.
@@ -2753,7 +2751,7 @@ bool CChainState::FlushStateToDisk(
27532751

27542752
UnlinkPrunedFiles(setFilesToPrune);
27552753
}
2756-
nLastWrite = nNow;
2754+
m_last_write = nNow;
27572755
}
27582756
// Flush best chain related state. This can only be done if the blocks / block index write was also done.
27592757
if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
@@ -2779,7 +2777,7 @@ bool CChainState::FlushStateToDisk(
27792777
return AbortNode(state, "Failed to commit EvoDB");
27802778
}
27812779
}
2782-
nLastFlush = nNow;
2780+
m_last_flush = nNow;
27832781
full_flush_completed = true;
27842782
TRACE5(utxocache, flush,
27852783
(int64_t)(GetTimeMicros() - nNow.count()), // in microseconds (µs)

src/validation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <util/translation.h>
3232

3333
#include <atomic>
34+
#include <chrono>
3435
#include <map>
3536
#include <memory>
3637
#include <optional>
@@ -797,6 +798,9 @@ class CChainState
797798
void UpdateTip(const CBlockIndex* pindexNew)
798799
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
799800

801+
std::chrono::microseconds m_last_write{0};
802+
std::chrono::microseconds m_last_flush{0};
803+
800804
friend ChainstateManager;
801805
};
802806

0 commit comments

Comments
 (0)