@@ -4840,6 +4840,20 @@ bool ChainstateManager::ActivateSnapshot(
4840
4840
return true ;
4841
4841
}
4842
4842
4843
+ static void FlushSnapshotToDisk (CCoinsViewCache& coins_cache, bool snapshot_loaded)
4844
+ {
4845
+ LogPrintf (" [snapshot] flushing %s (%.2f MB)... " , /* Continued */
4846
+ snapshot_loaded ? " snapshot chainstate to disk" : " coins cache" ,
4847
+ coins_cache.DynamicMemoryUsage () / (1000 * 1000 ));
4848
+
4849
+ const int64_t flush_now{GetTimeMillis ()};
4850
+
4851
+ // TODO: if #17487 is merged, add erase=false here if snapshot is loaded, for better performance.
4852
+ coins_cache.Flush ();
4853
+
4854
+ LogPrintf (" done (%.2fms)\n " , GetTimeMillis () - flush_now);
4855
+ }
4856
+
4843
4857
bool ChainstateManager::PopulateAndValidateSnapshot (
4844
4858
CChainState& snapshot_chainstate,
4845
4859
CAutoFile& coins_file,
@@ -4877,7 +4891,6 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
4877
4891
uint64_t coins_left = metadata.m_coins_count ;
4878
4892
4879
4893
LogPrintf (" [snapshot] loading coins from snapshot %s\n " , base_blockhash.ToString ());
4880
- int64_t flush_now{0 };
4881
4894
int64_t coins_processed{0 };
4882
4895
4883
4896
while (coins_left > 0 ) {
@@ -4921,19 +4934,14 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
4921
4934
const auto snapshot_cache_state = WITH_LOCK (::cs_main,
4922
4935
return snapshot_chainstate.GetCoinsCacheSizeState ());
4923
4936
4924
- if (snapshot_cache_state >=
4925
- CoinsCacheSizeState::CRITICAL) {
4926
- LogPrintf (" [snapshot] flushing coins cache (%.2f MB)... " , /* Continued */
4927
- coins_cache.DynamicMemoryUsage () / (1000 * 1000 ));
4928
- flush_now = GetTimeMillis ();
4929
-
4937
+ if (snapshot_cache_state >= CoinsCacheSizeState::CRITICAL) {
4930
4938
// This is a hack - we don't know what the actual best block is, but that
4931
4939
// doesn't matter for the purposes of flushing the cache here. We'll set this
4932
4940
// to its correct value (`base_blockhash`) below after the coins are loaded.
4933
4941
coins_cache.SetBestBlock (GetRandHash ());
4934
4942
4935
- coins_cache. Flush ();
4936
- LogPrintf ( " done (%.2fms) \n " , GetTimeMillis () - flush_now );
4943
+ // No need to acquire cs_main since this chainstate isn't being used yet.
4944
+ FlushSnapshotToDisk (coins_cache, /* snapshot_loaded= */ false );
4937
4945
}
4938
4946
}
4939
4947
}
@@ -4963,9 +4971,8 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
4963
4971
coins_cache.DynamicMemoryUsage () / (1000 * 1000 ),
4964
4972
base_blockhash.ToString ());
4965
4973
4966
- LogPrintf (" [snapshot] flushing snapshot chainstate to disk\n " );
4967
4974
// No need to acquire cs_main since this chainstate isn't being used yet.
4968
- coins_cache. Flush (); // TODO: if #17487 is merged, add erase=false here for better performance.
4975
+ FlushSnapshotToDisk (coins_cache, /* snapshot_loaded= */ true );
4969
4976
4970
4977
assert (coins_cache.GetBestBlock () == base_blockhash);
4971
4978
0 commit comments