Skip to content

Commit 07ac7a2

Browse files
committed
Merge bitcoin/bitcoin#26513: Make static nLastFlush and nLastWrite Chainstate members
07dfbb5 Make static nLastFlush and nLastWrite Chainstate members (Aurèle Oulès) Pull request description: Fixes #22189. The `static std::multimap<uint256, FlatFilePos> mapBlocksUnknownParent; ` referenced in the issue was already fixed by #25571. I don't believe Chainstate references any other static variables. ACKs for top commit: jamesob: ACK 07dfbb5 ([`jamesob/ackr/26513.1.aureleoules.make_static_nlastflush_a`](https://github.com/jamesob/bitcoin/tree/ackr/26513.1.aureleoules.make_static_nlastflush_a)) theStack: Concept and code-review ACK 07dfbb5 Tree-SHA512: 0f26463c079bbc5e0e62707d4ca4c8c9bbb99edfa3391d48d4915d24e2a1190873ecd4f9f11da25b44527671cdc82c41fd8234d56a4592a246989448d34406b0
2 parents 1801d8c + 07dfbb5 commit 07ac7a2

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/validation.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,8 +2360,6 @@ bool Chainstate::FlushStateToDisk(
23602360
{
23612361
LOCK(cs_main);
23622362
assert(this->CanFlushToDisk());
2363-
static std::chrono::microseconds nLastWrite{0};
2364-
static std::chrono::microseconds nLastFlush{0};
23652363
std::set<int> setFilesToPrune;
23662364
bool full_flush_completed = false;
23672365

@@ -2415,20 +2413,20 @@ bool Chainstate::FlushStateToDisk(
24152413
}
24162414
const auto nNow = GetTime<std::chrono::microseconds>();
24172415
// Avoid writing/flushing immediately after startup.
2418-
if (nLastWrite.count() == 0) {
2419-
nLastWrite = nNow;
2416+
if (m_last_write.count() == 0) {
2417+
m_last_write = nNow;
24202418
}
2421-
if (nLastFlush.count() == 0) {
2422-
nLastFlush = nNow;
2419+
if (m_last_flush.count() == 0) {
2420+
m_last_flush = nNow;
24232421
}
24242422
// 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).
24252423
bool fCacheLarge = mode == FlushStateMode::PERIODIC && cache_state >= CoinsCacheSizeState::LARGE;
24262424
// The cache is over the limit, we have to write now.
24272425
bool fCacheCritical = mode == FlushStateMode::IF_NEEDED && cache_state >= CoinsCacheSizeState::CRITICAL;
24282426
// 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.
2429-
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > nLastWrite + DATABASE_WRITE_INTERVAL;
2427+
bool fPeriodicWrite = mode == FlushStateMode::PERIODIC && nNow > m_last_write + DATABASE_WRITE_INTERVAL;
24302428
// It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2431-
bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > nLastFlush + DATABASE_FLUSH_INTERVAL;
2429+
bool fPeriodicFlush = mode == FlushStateMode::PERIODIC && nNow > m_last_flush + DATABASE_FLUSH_INTERVAL;
24322430
// Combine all conditions that result in a full cache flush.
24332431
fDoFullFlush = (mode == FlushStateMode::ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
24342432
// Write blocks and block index to disk.
@@ -2458,7 +2456,7 @@ bool Chainstate::FlushStateToDisk(
24582456

24592457
UnlinkPrunedFiles(setFilesToPrune);
24602458
}
2461-
nLastWrite = nNow;
2459+
m_last_write = nNow;
24622460
}
24632461
// Flush best chain related state. This can only be done if the blocks / block index write was also done.
24642462
if (fDoFullFlush && !CoinsTip().GetBestBlock().IsNull()) {
@@ -2476,7 +2474,7 @@ bool Chainstate::FlushStateToDisk(
24762474
// Flush the chainstate (which may refer to block index entries).
24772475
if (!CoinsTip().Flush())
24782476
return AbortNode(state, "Failed to write to coin database");
2479-
nLastFlush = nNow;
2477+
m_last_flush = nNow;
24802478
full_flush_completed = true;
24812479
TRACE5(utxocache, flush,
24822480
(int64_t)(GetTimeMicros() - nNow.count()), // in microseconds (µs)

src/validation.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,9 @@ class Chainstate
743743
void UpdateTip(const CBlockIndex* pindexNew)
744744
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
745745

746+
std::chrono::microseconds m_last_write{0};
747+
std::chrono::microseconds m_last_flush{0};
748+
746749
friend ChainstateManager;
747750
};
748751

0 commit comments

Comments
 (0)