Skip to content

Commit 9cb6cdc

Browse files
committed
Simplify semantics of ChainStateFlushed callback
Previously, ChainStateFlushed would fire either if a full flush completed (which can happen due to memory limits, forced flush, or on its own DATABASE_WRITE_INTERVAL timer) *or* on a ChainStateFlushed-specific DATABASE_WRITE_INTERVAL timer. This is both less clear for clients (as there are no guarantees about a flush having actually happened prior to the call), and reults in extra flushes not clearly intended by the code. We drop the second case, providing a strong guarantee without removing the periodit timer-based flushing.
1 parent 50b6533 commit 9cb6cdc

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/validation.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,13 +2066,12 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
20662066
LOCK(cs_main);
20672067
static int64_t nLastWrite = 0;
20682068
static int64_t nLastFlush = 0;
2069-
static int64_t nLastSetChain = 0;
20702069
std::set<int> setFilesToPrune;
2071-
bool fFlushForPrune = false;
2072-
bool fDoFullFlush = false;
2073-
int64_t nNow = 0;
2070+
bool full_flush_completed = false;
20742071
try {
20752072
{
2073+
bool fFlushForPrune = false;
2074+
bool fDoFullFlush = false;
20762075
LOCK(cs_LastBlockFile);
20772076
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
20782077
if (nManualPruneHeight > 0) {
@@ -2089,17 +2088,14 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
20892088
}
20902089
}
20912090
}
2092-
nNow = GetTimeMicros();
2091+
int64_t nNow = GetTimeMicros();
20932092
// Avoid writing/flushing immediately after startup.
20942093
if (nLastWrite == 0) {
20952094
nLastWrite = nNow;
20962095
}
20972096
if (nLastFlush == 0) {
20982097
nLastFlush = nNow;
20992098
}
2100-
if (nLastSetChain == 0) {
2101-
nLastSetChain = nNow;
2102-
}
21032099
int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
21042100
int64_t cacheSize = pcoinsTip->DynamicMemoryUsage();
21052101
int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
@@ -2156,12 +2152,12 @@ bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &
21562152
if (!pcoinsTip->Flush())
21572153
return AbortNode(state, "Failed to write to coin database");
21582154
nLastFlush = nNow;
2155+
full_flush_completed = true;
21592156
}
21602157
}
2161-
if (fDoFullFlush || ((mode == FlushStateMode::ALWAYS || mode == FlushStateMode::PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
2158+
if (full_flush_completed) {
21622159
// Update best block in wallet (so we can detect restored wallets).
21632160
GetMainSignals().ChainStateFlushed(chainActive.GetLocator());
2164-
nLastSetChain = nNow;
21652161
}
21662162
} catch (const std::runtime_error& e) {
21672163
return AbortNode(state, std::string("System error while flushing: ") + e.what());

src/validationinterface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ class CValidationInterface {
9999
/**
100100
* Notifies listeners of the new active block chain on-disk.
101101
*
102+
* Prior to this callback, any updates are not guaranteed to persist on disk
103+
* (ie clients need to handle shutdown/restart safety by being able to
104+
* understand when some updates were lost due to unclean shutdown).
105+
*
106+
* When this callback is invoked, the validation changes done by any prior
107+
* callback are guaranteed to exist on disk and survive a restart, including
108+
* an unclean shutdown.
109+
*
110+
* Provides a locator describing the best chain, which is likely useful for
111+
* storing current state on disk in client DBs.
112+
*
102113
* Called on a background thread.
103114
*/
104115
virtual void ChainStateFlushed(const CBlockLocator &locator) {}

0 commit comments

Comments
 (0)