Skip to content

Commit 47be28c

Browse files
committed
validation: use std::chrono in CChainState::FlushStateToDisk()
1 parent 55b4c65 commit 47be28c

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/validation.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000;
7171
static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
7272
/** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
7373
static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
74-
/** Time to wait (in seconds) between writing blocks/block index to disk. */
75-
static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60;
76-
/** Time to wait (in seconds) between flushing chainstate to disk. */
77-
static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
74+
/** Time to wait between writing blocks/block index to disk. */
75+
static constexpr std::chrono::hours DATABASE_WRITE_INTERVAL{1};
76+
/** Time to wait between flushing chainstate to disk. */
77+
static constexpr std::chrono::hours DATABASE_FLUSH_INTERVAL{24};
7878
/** Maximum age of our tip in seconds for us to be considered current for fee estimation */
7979
static const int64_t MAX_FEE_ESTIMATION_TIP_AGE = 3 * 60 * 60;
8080

@@ -2264,8 +2264,8 @@ bool CChainState::FlushStateToDisk(
22642264
{
22652265
LOCK(cs_main);
22662266
assert(this->CanFlushToDisk());
2267-
static int64_t nLastWrite = 0;
2268-
static int64_t nLastFlush = 0;
2267+
static std::chrono::microseconds nLastWrite{0};
2268+
static std::chrono::microseconds nLastFlush{0};
22692269
std::set<int> setFilesToPrune;
22702270
bool full_flush_completed = false;
22712271

@@ -2297,22 +2297,22 @@ bool CChainState::FlushStateToDisk(
22972297
}
22982298
}
22992299
}
2300-
int64_t nNow = GetTimeMicros();
2300+
const auto nNow = GetTime<std::chrono::microseconds>();
23012301
// Avoid writing/flushing immediately after startup.
2302-
if (nLastWrite == 0) {
2302+
if (nLastWrite.count() == 0) {
23032303
nLastWrite = nNow;
23042304
}
2305-
if (nLastFlush == 0) {
2305+
if (nLastFlush.count() == 0) {
23062306
nLastFlush = nNow;
23072307
}
23082308
// 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).
23092309
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
23102310
// The cache is over the limit, we have to write now.
23112311
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
23122312
// 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.
2313-
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
2313+
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > nLastWrite + DATABASE_WRITE_INTERVAL;
23142314
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2315-
bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
2315+
bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > nLastFlush + DATABASE_FLUSH_INTERVAL;
23162316
// Combine all conditions that result in a full cache flush.
23172317
fDoFullFlush = (mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
23182318
// Write blocks and block index to disk.

0 commit comments

Comments
 (0)