88#include < coins.h>
99#include < consensus/params.h>
1010#include < node/blockstorage.h>
11+ #include < node/caches.h>
1112#include < sync.h>
1213#include < threadsafety.h>
1314#include < txdb.h>
2223#include < vector>
2324
2425namespace node {
25- std::optional<ChainstateLoadingError> LoadChainstate (bool fReset ,
26- ChainstateManager& chainman,
27- CTxMemPool* mempool,
28- bool fPruneMode ,
29- bool fReindexChainState ,
30- int64_t nBlockTreeDBCache,
31- int64_t nCoinDBCache,
32- int64_t nCoinCacheUsage,
33- bool block_tree_db_in_memory,
34- bool coins_db_in_memory,
35- std::function<bool ()> shutdown_requested,
36- std::function<void()> coins_error_cb)
26+ std::optional<ChainstateLoadingError> LoadChainstate (ChainstateManager& chainman, const CacheSizes& cache_sizes,
27+ const ChainstateLoadOptions& options)
3728{
3829 auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED (::cs_main) {
39- return fReset || fReindexChainState || chainstate->CoinsTip ().GetBestBlock ().IsNull ();
30+ return options. reindex || options. reindex_chainstate || chainstate->CoinsTip ().GetBestBlock ().IsNull ();
4031 };
4132
4233 LOCK (cs_main);
43- chainman.InitializeChainstate (mempool);
44- chainman.m_total_coinstip_cache = nCoinCacheUsage ;
45- chainman.m_total_coinsdb_cache = nCoinDBCache ;
34+ chainman.InitializeChainstate (options. mempool );
35+ chainman.m_total_coinstip_cache = cache_sizes. coins ;
36+ chainman.m_total_coinsdb_cache = cache_sizes. coins_db ;
4637
4738 auto & pblocktree{chainman.m_blockman .m_block_tree_db };
4839 // new CBlockTreeDB tries to delete the existing file, which
4940 // fails if it's still open from the previous loop. Close it first:
5041 pblocktree.reset ();
51- pblocktree.reset (new CBlockTreeDB (nBlockTreeDBCache, block_tree_db_in_memory, fReset ));
42+ pblocktree.reset (new CBlockTreeDB (cache_sizes. block_tree_db , options. block_tree_db_in_memory , options. reindex ));
5243
53- if (fReset ) {
44+ if (options. reindex ) {
5445 pblocktree->WriteReindexing (true );
5546 // If we're reindexing in prune mode, wipe away unusable block files and all undo data files
56- if (fPruneMode )
47+ if (options. prune ) {
5748 CleanupBlockRevFiles ();
49+ }
5850 }
5951
60- if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
52+ if (options. check_interrupt && options. check_interrupt ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
6153
6254 // LoadBlockIndex will load m_have_pruned if we've ever removed a
6355 // block file from disk.
64- // Note that it also sets fReindex based on the disk flag!
65- // From here on out fReindex and fReset mean something different!
56+ // Note that it also sets fReindex global based on the disk flag!
57+ // From here on, fReindex and options.reindex values may be different!
6658 if (!chainman.LoadBlockIndex ()) {
67- if (shutdown_requested && shutdown_requested ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
59+ if (options. check_interrupt && options. check_interrupt ()) return ChainstateLoadingError::SHUTDOWN_PROBED;
6860 return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
6961 }
7062
@@ -75,7 +67,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
7567
7668 // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
7769 // in the past, but is now trying to run unpruned.
78- if (chainman.m_blockman .m_have_pruned && !fPruneMode ) {
70+ if (chainman.m_blockman .m_have_pruned && !options. prune ) {
7971 return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
8072 }
8173
@@ -92,12 +84,12 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
9284
9385 for (CChainState* chainstate : chainman.GetAll ()) {
9486 chainstate->InitCoinsDB (
95- /* cache_size_bytes=*/ nCoinDBCache ,
96- /* in_memory=*/ coins_db_in_memory,
97- /* should_wipe=*/ fReset || fReindexChainState );
87+ /* cache_size_bytes=*/ cache_sizes. coins_db ,
88+ /* in_memory=*/ options. coins_db_in_memory ,
89+ /* should_wipe=*/ options. reindex || options. reindex_chainstate );
9890
99- if (coins_error_cb) {
100- chainstate->CoinsErrorCatcher ().AddReadErrCallback (coins_error_cb);
91+ if (options. coins_error_cb ) {
92+ chainstate->CoinsErrorCatcher ().AddReadErrCallback (options. coins_error_cb );
10193 }
10294
10395 // Refuse to load unsupported database format.
@@ -112,7 +104,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
112104 }
113105
114106 // The on-disk coinsdb is now in a good state, create the cache
115- chainstate->InitCoinsCache (nCoinCacheUsage );
107+ chainstate->InitCoinsCache (cache_sizes. coins );
116108 assert (chainstate->CanFlushToDisk ());
117109
118110 if (!is_coinsview_empty (chainstate)) {
@@ -124,7 +116,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
124116 }
125117 }
126118
127- if (!fReset ) {
119+ if (!options. reindex ) {
128120 auto chainstates{chainman.GetAll ()};
129121 if (std::any_of (chainstates.begin (), chainstates.end (),
130122 [](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED (cs_main) { return cs->NeedsRedownload (); })) {
@@ -136,13 +128,10 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
136128}
137129
138130std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate (ChainstateManager& chainman,
139- bool fReset ,
140- bool fReindexChainState ,
141- int check_blocks,
142- int check_level)
131+ const ChainstateLoadOptions& options)
143132{
144133 auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED (::cs_main) {
145- return fReset || fReindexChainState || chainstate->CoinsTip ().GetBestBlock ().IsNull ();
134+ return options. reindex || options. reindex_chainstate || chainstate->CoinsTip ().GetBestBlock ().IsNull ();
146135 };
147136
148137 LOCK (cs_main);
@@ -156,8 +145,8 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
156145
157146 if (!CVerifyDB ().VerifyDB (
158147 *chainstate, chainman.GetConsensus (), chainstate->CoinsDB (),
159- check_level,
160- check_blocks)) {
148+ options. check_level ,
149+ options. check_blocks )) {
161150 return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
162151 }
163152 }
0 commit comments