Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 2,037 Lines • ▼ Show 20 Lines | |||||
*/ | */ | ||||
static bool FlushStateToDisk(const CChainParams &chainparams, | static bool FlushStateToDisk(const CChainParams &chainparams, | ||||
CValidationState &state, FlushStateMode mode, | CValidationState &state, FlushStateMode mode, | ||||
int nManualPruneHeight) { | int nManualPruneHeight) { | ||||
int64_t nMempoolUsage = g_mempool.DynamicMemoryUsage(); | int64_t nMempoolUsage = g_mempool.DynamicMemoryUsage(); | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
static int64_t nLastWrite = 0; | static int64_t nLastWrite = 0; | ||||
static int64_t nLastFlush = 0; | static int64_t nLastFlush = 0; | ||||
static int64_t nLastSetChain = 0; | |||||
std::set<int> setFilesToPrune; | std::set<int> setFilesToPrune; | ||||
bool fFlushForPrune = false; | bool full_flush_completed = false; | ||||
bool fDoFullFlush = false; | |||||
int64_t nNow = 0; | |||||
try { | try { | ||||
{ | { | ||||
bool fFlushForPrune = false; | |||||
bool fDoFullFlush = false; | |||||
LOCK(cs_LastBlockFile); | LOCK(cs_LastBlockFile); | ||||
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && | if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && | ||||
!fReindex) { | !fReindex) { | ||||
if (nManualPruneHeight > 0) { | if (nManualPruneHeight > 0) { | ||||
FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight); | FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight); | ||||
} else { | } else { | ||||
FindFilesToPrune(setFilesToPrune, | FindFilesToPrune(setFilesToPrune, | ||||
chainparams.PruneAfterHeight()); | chainparams.PruneAfterHeight()); | ||||
fCheckForPruning = false; | fCheckForPruning = false; | ||||
} | } | ||||
if (!setFilesToPrune.empty()) { | if (!setFilesToPrune.empty()) { | ||||
fFlushForPrune = true; | fFlushForPrune = true; | ||||
if (!fHavePruned) { | if (!fHavePruned) { | ||||
pblocktree->WriteFlag("prunedblockfiles", true); | pblocktree->WriteFlag("prunedblockfiles", true); | ||||
fHavePruned = true; | fHavePruned = true; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
nNow = GetTimeMicros(); | int64_t nNow = GetTimeMicros(); | ||||
// Avoid writing/flushing immediately after startup. | // Avoid writing/flushing immediately after startup. | ||||
if (nLastWrite == 0) { | if (nLastWrite == 0) { | ||||
nLastWrite = nNow; | nLastWrite = nNow; | ||||
} | } | ||||
if (nLastFlush == 0) { | if (nLastFlush == 0) { | ||||
nLastFlush = nNow; | nLastFlush = nNow; | ||||
} | } | ||||
if (nLastSetChain == 0) { | |||||
nLastSetChain = nNow; | |||||
} | |||||
int64_t nMempoolSizeMax = | int64_t nMempoolSizeMax = | ||||
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; | gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000; | ||||
int64_t cacheSize = pcoinsTip->DynamicMemoryUsage(); | int64_t cacheSize = pcoinsTip->DynamicMemoryUsage(); | ||||
int64_t nTotalSpace = | int64_t nTotalSpace = | ||||
nCoinCacheUsage + | nCoinCacheUsage + | ||||
std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0); | std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0); | ||||
// The cache is large and we're within 10% and 10 MiB of the limit, | // 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). | // but we have time now (not in the middle of a block processing). | ||||
▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | try { | ||||
} | } | ||||
// Flush the chainstate (which may refer to block index | // Flush the chainstate (which may refer to block index | ||||
// entries). | // entries). | ||||
if (!pcoinsTip->Flush()) { | if (!pcoinsTip->Flush()) { | ||||
return AbortNode(state, "Failed to write to coin database"); | return AbortNode(state, "Failed to write to coin database"); | ||||
} | } | ||||
nLastFlush = nNow; | nLastFlush = nNow; | ||||
full_flush_completed = true; | |||||
} | } | ||||
} | } | ||||
if (fDoFullFlush || | if (full_flush_completed) { | ||||
((mode == FlushStateMode::ALWAYS || | |||||
mode == FlushStateMode::PERIODIC) && | |||||
nNow > | |||||
nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) { | |||||
// Update best block in wallet (so we can detect restored wallets). | // Update best block in wallet (so we can detect restored wallets). | ||||
GetMainSignals().ChainStateFlushed(chainActive.GetLocator()); | GetMainSignals().ChainStateFlushed(chainActive.GetLocator()); | ||||
nLastSetChain = nNow; | |||||
} | } | ||||
} catch (const std::runtime_error &e) { | } catch (const std::runtime_error &e) { | ||||
return AbortNode(state, std::string("System error while flushing: ") + | return AbortNode(state, std::string("System error while flushing: ") + | ||||
e.what()); | e.what()); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 3,545 Lines • Show Last 20 Lines |