@@ -5276,6 +5276,17 @@ bool ChainstateManager::ActivateSnapshot(
52765276 return true ;
52775277}
52785278
5279+ static void FlushSnapshotToDisk (CCoinsViewCache& coins_cache, bool snapshot_loaded)
5280+ {
5281+ LOG_TIME_MILLIS_WITH_CATEGORY_MSG_ONCE (
5282+ strprintf (" %s (%.2f MB)" ,
5283+ snapshot_loaded ? " saving snapshot chainstate" : " flushing coins cache" ,
5284+ coins_cache.DynamicMemoryUsage () / (1000 * 1000 )),
5285+ BCLog::LogFlags::ALL);
5286+
5287+ coins_cache.Flush ();
5288+ }
5289+
52795290bool ChainstateManager::PopulateAndValidateSnapshot (
52805291 CChainState& snapshot_chainstate,
52815292 CAutoFile& coins_file,
@@ -5313,7 +5324,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
53135324 uint64_t coins_left = metadata.m_coins_count ;
53145325
53155326 LogPrintf (" [snapshot] loading coins from snapshot %s\n " , base_blockhash.ToString ());
5316- int64_t flush_now{0 };
53175327 int64_t coins_processed{0 };
53185328
53195329 while (coins_left > 0 ) {
@@ -5357,19 +5367,14 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
53575367 const auto snapshot_cache_state = WITH_LOCK (::cs_main,
53585368 return snapshot_chainstate.GetCoinsCacheSizeState ());
53595369
5360- if (snapshot_cache_state >=
5361- CoinsCacheSizeState::CRITICAL) {
5362- LogPrintf (" [snapshot] flushing coins cache (%.2f MB)... " , /* Continued */
5363- coins_cache.DynamicMemoryUsage () / (1000 * 1000 ));
5364- flush_now = GetTimeMillis ();
5365-
5370+ if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
53665371 // This is a hack - we don't know what the actual best block is, but that
53675372 // doesn't matter for the purposes of flushing the cache here. We'll set this
53685373 // to its correct value (`base_blockhash`) below after the coins are loaded.
53695374 coins_cache.SetBestBlock (GetRandHash ());
53705375
5371- coins_cache. Flush ();
5372- LogPrintf ( " done (%.2fms) \n " , GetTimeMillis () - flush_now );
5376+ // No need to acquire cs_main since this chainstate isn't being used yet.
5377+ FlushSnapshotToDisk (coins_cache, /* snapshot_loaded= */ false );
53735378 }
53745379 }
53755380 }
@@ -5399,9 +5404,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
53995404 coins_cache.DynamicMemoryUsage () / (1000 * 1000 ),
54005405 base_blockhash.ToString ());
54015406
5402- LogPrintf (" [snapshot] flushing snapshot chainstate to disk\n " );
54035407 // No need to acquire cs_main since this chainstate isn't being used yet.
5404- coins_cache. Flush (); // TODO: if #17487 is merged, add erase=false here for better performance.
5408+ FlushSnapshotToDisk (coins_cache, /* snapshot_loaded= */ true );
54055409
54065410 assert (coins_cache.GetBestBlock () == base_blockhash);
54075411
0 commit comments