@@ -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 ) {
0 commit comments