Skip to content

Commit 2e2f410

Browse files
andrewtothl0rinc
andcommitted
refactor: replace m_last_write with m_next_write
Co-Authored-By: l0rinc <[email protected]>
1 parent b557fa7 commit 2e2f410

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/validation.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2876,16 +2876,12 @@ bool Chainstate::FlushStateToDisk(
28762876
}
28772877
}
28782878
const auto nNow{NodeClock::now()};
2879-
// Avoid writing/flushing immediately after startup.
2880-
if (m_last_write == decltype(m_last_write){}) {
2881-
m_last_write = nNow;
2882-
}
28832879
// 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).
28842880
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
28852881
// The cache is over the limit, we have to write now.
28862882
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
28872883
// It's been a while since we wrote the block index and chain state to disk. Do this frequently, so we don't need to redownload or reindex after a crash.
2888-
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > m_last_write + DATABASE_WRITE_INTERVAL;
2884+
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow >= m_next_write;
28892885
// Combine all conditions that result in a write to disk.
28902886
bool should_write = (mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicWrite || fFlushForPrune;
28912887
// Write blocks, block index and best chain related state to disk.
@@ -2946,7 +2942,10 @@ bool Chainstate::FlushStateToDisk(
29462942
(uint64_t)coins_mem_usage,
29472943
(bool)fFlushForPrune);
29482944
}
2949-
m_last_write = NodeClock::now();
2945+
}
2946+
2947+
if (should_write || m_next_write == NodeClock::time_point::max()) {
2948+
m_next_write = NodeClock::now() + DATABASE_WRITE_INTERVAL;
29502949
}
29512950
}
29522951
if (full_flush_completed && m_chainman.m_options.signals) {

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ class Chainstate
803803
void UpdateTip(const CBlockIndex* pindexNew)
804804
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
805805

806-
NodeClock::time_point m_last_write{};
806+
NodeClock::time_point m_next_write{NodeClock::time_point::max()};
807807

808808
/**
809809
* In case of an invalid snapshot, rename the coins leveldb directory so

0 commit comments

Comments
 (0)