refactor: replace m_last_write with m_next_write

Co-Authored-By: l0rinc <pap.lorinc@gmail.com>
This commit is contained in:
Andrew Toth 2024-09-08 11:48:53 -04:00
parent b557fa7a17
commit 2e2f410681
No known key found for this signature in database
GPG Key ID: 60007AFC8938B018
2 changed files with 6 additions and 7 deletions

View File

@ -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) {

View File

@ -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