From 2e2f41068128c38120a5b44d24ee30f71970455a Mon Sep 17 00:00:00 2001 From: Andrew Toth Date: Sun, 8 Sep 2024 11:48:53 -0400 Subject: [PATCH] refactor: replace m_last_write with m_next_write Co-Authored-By: l0rinc --- src/validation.cpp | 11 +++++------ src/validation.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/validation.cpp b/src/validation.cpp index 12e28cbbfb..978eacc200 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2876,16 +2876,12 @@ bool Chainstate::FlushStateToDisk( } } const auto nNow{NodeClock::now()}; - // Avoid writing/flushing immediately after startup. - if (m_last_write == decltype(m_last_write){}) { - m_last_write = nNow; - } // 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). bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE; // The cache is over the limit, we have to write now. bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL; // 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. - bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > m_last_write + DATABASE_WRITE_INTERVAL; + bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow >= m_next_write; // Combine all conditions that result in a write to disk. bool should_write = (mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicWrite || fFlushForPrune; // Write blocks, block index and best chain related state to disk. @@ -2946,7 +2942,10 @@ bool Chainstate::FlushStateToDisk( (uint64_t)coins_mem_usage, (bool)fFlushForPrune); } - m_last_write = NodeClock::now(); + } + + if (should_write || m_next_write == NodeClock::time_point::max()) { + m_next_write = NodeClock::now() + DATABASE_WRITE_INTERVAL; } } if (full_flush_completed && m_chainman.m_options.signals) { diff --git a/src/validation.h b/src/validation.h index df04b4af29..5b14e466c2 100644 --- a/src/validation.h +++ b/src/validation.h @@ -803,7 +803,7 @@ private: void UpdateTip(const CBlockIndex* pindexNew) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - NodeClock::time_point m_last_write{}; + NodeClock::time_point m_next_write{NodeClock::time_point::max()}; /** * In case of an invalid snapshot, rename the coins leveldb directory so