|
28 | 28 | #include <vector>
|
29 | 29 |
|
30 | 30 | namespace node {
|
31 |
| -ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes, |
32 |
| - const ChainstateLoadOptions& options) |
| 31 | +// Complete initialization of chainstates after the initial call has been made |
| 32 | +// to ChainstateManager::InitializeChainstate(). |
| 33 | +static ChainstateLoadResult CompleteChainstateInitialization( |
| 34 | + ChainstateManager& chainman, |
| 35 | + const CacheSizes& cache_sizes, |
| 36 | + const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) |
33 | 37 | {
|
34 |
| - auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { |
35 |
| - return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull(); |
36 |
| - }; |
37 |
| - |
38 |
| - if (!chainman.AssumedValidBlock().IsNull()) { |
39 |
| - LogPrintf("Assuming ancestors of block %s have valid signatures.\n", chainman.AssumedValidBlock().GetHex()); |
40 |
| - } else { |
41 |
| - LogPrintf("Validating signatures for all blocks.\n"); |
42 |
| - } |
43 |
| - LogPrintf("Setting nMinimumChainWork=%s\n", chainman.MinimumChainWork().GetHex()); |
44 |
| - if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) { |
45 |
| - LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainman.GetConsensus().nMinimumChainWork.GetHex()); |
46 |
| - } |
47 |
| - if (chainman.m_blockman.GetPruneTarget() == std::numeric_limits<uint64_t>::max()) { |
48 |
| - LogPrintf("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n"); |
49 |
| - } else if (chainman.m_blockman.GetPruneTarget()) { |
50 |
| - LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", chainman.m_blockman.GetPruneTarget() / 1024 / 1024); |
51 |
| - } |
52 |
| - |
53 |
| - LOCK(cs_main); |
54 |
| - chainman.m_total_coinstip_cache = cache_sizes.coins; |
55 |
| - chainman.m_total_coinsdb_cache = cache_sizes.coins_db; |
56 |
| - |
57 |
| - // Load the fully validated chainstate. |
58 |
| - chainman.InitializeChainstate(options.mempool); |
59 |
| - |
60 |
| - // Load a chain created from a UTXO snapshot, if any exist. |
61 |
| - chainman.DetectSnapshotChainstate(options.mempool); |
62 |
| - |
63 | 38 | auto& pblocktree{chainman.m_blockman.m_block_tree_db};
|
64 | 39 | // new CBlockTreeDB tries to delete the existing file, which
|
65 | 40 | // fails if it's still open from the previous loop. Close it first:
|
@@ -106,6 +81,10 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
|
106 | 81 | return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
|
107 | 82 | }
|
108 | 83 |
|
| 84 | + auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { |
| 85 | + return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull(); |
| 86 | + }; |
| 87 | + |
109 | 88 | // Conservative value which is arbitrarily chosen, as it will ultimately be changed
|
110 | 89 | // by a call to `chainman.MaybeRebalanceCaches()`. We just need to make sure
|
111 | 90 | // that the sum of the two caches (40%) does not exceed the allowable amount
|
@@ -170,6 +149,43 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
|
170 | 149 | return {ChainstateLoadStatus::SUCCESS, {}};
|
171 | 150 | }
|
172 | 151 |
|
| 152 | +ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes, |
| 153 | + const ChainstateLoadOptions& options) |
| 154 | +{ |
| 155 | + if (!chainman.AssumedValidBlock().IsNull()) { |
| 156 | + LogPrintf("Assuming ancestors of block %s have valid signatures.\n", chainman.AssumedValidBlock().GetHex()); |
| 157 | + } else { |
| 158 | + LogPrintf("Validating signatures for all blocks.\n"); |
| 159 | + } |
| 160 | + LogPrintf("Setting nMinimumChainWork=%s\n", chainman.MinimumChainWork().GetHex()); |
| 161 | + if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) { |
| 162 | + LogPrintf("Warning: nMinimumChainWork set below default value of %s\n", chainman.GetConsensus().nMinimumChainWork.GetHex()); |
| 163 | + } |
| 164 | + if (chainman.m_blockman.GetPruneTarget() == std::numeric_limits<uint64_t>::max()) { |
| 165 | + LogPrintf("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n"); |
| 166 | + } else if (chainman.m_blockman.GetPruneTarget()) { |
| 167 | + LogPrintf("Prune configured to target %u MiB on disk for block and undo files.\n", chainman.m_blockman.GetPruneTarget() / 1024 / 1024); |
| 168 | + } |
| 169 | + |
| 170 | + LOCK(cs_main); |
| 171 | + |
| 172 | + chainman.m_total_coinstip_cache = cache_sizes.coins; |
| 173 | + chainman.m_total_coinsdb_cache = cache_sizes.coins_db; |
| 174 | + |
| 175 | + // Load the fully validated chainstate. |
| 176 | + chainman.InitializeChainstate(options.mempool); |
| 177 | + |
| 178 | + // Load a chain created from a UTXO snapshot, if any exist. |
| 179 | + chainman.DetectSnapshotChainstate(options.mempool); |
| 180 | + |
| 181 | + auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options); |
| 182 | + if (init_status != ChainstateLoadStatus::SUCCESS) { |
| 183 | + return {init_status, init_error}; |
| 184 | + } |
| 185 | + |
| 186 | + return {ChainstateLoadStatus::SUCCESS, {}}; |
| 187 | +} |
| 188 | + |
173 | 189 | ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options)
|
174 | 190 | {
|
175 | 191 | auto is_coinsview_empty = [&](Chainstate* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
|
0 commit comments