Skip to content

Commit a1c0e5f

Browse files
author
MarcoFalke
committed
Merge #19088: validation: use std::chrono throughout some validation functions
789e9dd validation: use std::chrono in IsCurrentForFeeEstimation() (fanquake) 47be28c validation: use std::chrono in CChainState::FlushStateToDisk() (fanquake) Pull request description: Probably up for debate as to which type is used for the constants. Personally, swapping these to hours is more readable. ACKs for top commit: MarcoFalke: ACK 789e9dd jonatack: ACK 789e9dd Tree-SHA512: f4a25cbd00a49a54b7783a1f588be83706dd2a475cecb5c2e8b97b2d4b27c0955a7454d7486f2454e96351c44f233b300c4f4b9ca62fc7336277f10da34dd5c3
2 parents 234faba + 789e9dd commit a1c0e5f

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

src/validation.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ 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;
78-
/** Maximum age of our tip in seconds for us to be considered current for fee estimation */
79-
static const int64_t MAX_FEE_ESTIMATION_TIP_AGE = 3 * 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};
78+
/** Maximum age of our tip for us to be considered current for fee estimation */
79+
static constexpr std::chrono::hours MAX_FEE_ESTIMATION_TIP_AGE{3};
8080

8181
bool CBlockIndexWorkComparator::operator()(const CBlockIndex *pa, const CBlockIndex *pb) const {
8282
// First sort by most total work, ...
@@ -347,7 +347,7 @@ static bool IsCurrentForFeeEstimation() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
347347
AssertLockHeld(cs_main);
348348
if (::ChainstateActive().IsInitialBlockDownload())
349349
return false;
350-
if (::ChainActive().Tip()->GetBlockTime() < (GetTime() - MAX_FEE_ESTIMATION_TIP_AGE))
350+
if (::ChainActive().Tip()->GetBlockTime() < count_seconds(GetTime<std::chrono::seconds>() - MAX_FEE_ESTIMATION_TIP_AGE))
351351
return false;
352352
if (::ChainActive().Height() < pindexBestHeader->nHeight - 1)
353353
return false;
@@ -2269,8 +2269,8 @@ bool CChainState::FlushStateToDisk(
22692269
{
22702270
LOCK(cs_main);
22712271
assert(this->CanFlushToDisk());
2272-
static int64_t nLastWrite = 0;
2273-
static int64_t nLastFlush = 0;
2272+
static std::chrono::microseconds nLastWrite{0};
2273+
static std::chrono::microseconds nLastFlush{0};
22742274
std::set<int> setFilesToPrune;
22752275
bool full_flush_completed = false;
22762276

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

0 commit comments

Comments
 (0)