Branch data Line data Source code
1 : : // Copyright (c) 2021-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <node/chainstate.h>
6 : :
7 : : #include <arith_uint256.h>
8 : : #include <chain.h>
9 : : #include <coins.h>
10 : : #include <consensus/params.h>
11 : : #include <kernel/caches.h>
12 : : #include <node/blockstorage.h>
13 : : #include <sync.h>
14 : : #include <tinyformat.h>
15 : : #include <txdb.h>
16 : : #include <uint256.h>
17 : : #include <util/fs.h>
18 : : #include <util/log.h>
19 : : #include <util/signalinterrupt.h>
20 : : #include <util/time.h>
21 : : #include <util/translation.h>
22 : : #include <validation.h>
23 : :
24 : : #include <algorithm>
25 : : #include <cassert>
26 : : #include <vector>
27 : :
28 : : using kernel::CacheSizes;
29 : :
30 : : namespace node {
31 : : // Complete initialization of chainstates after the initial call has been made
32 : : // to ChainstateManager::InitializeChainstate().
33 : 1503 : static ChainstateLoadResult CompleteChainstateInitialization(
34 : : ChainstateManager& chainman,
35 : : const ChainstateLoadOptions& options) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
36 : : {
37 [ - + - - ]: 1503 : if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
38 : :
39 : : // LoadBlockIndex will load m_have_pruned if we've ever removed a
40 : : // block file from disk.
41 : : // Note that it also sets m_blockfiles_indexed based on the disk flag!
42 [ - + ]: 1503 : if (!chainman.LoadBlockIndex()) {
43 [ # # # # ]: 0 : if (chainman.m_interrupt) return {ChainstateLoadStatus::INTERRUPTED, {}};
44 : 0 : return {ChainstateLoadStatus::FAILURE, _("Error loading block database")};
45 : : }
46 : :
47 [ - + - - ]: 1503 : if (!chainman.BlockIndex().empty() &&
48 : 0 : !chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
49 : : // If the loaded chain has a wrong genesis, bail out immediately
50 : : // (we're likely using a testnet datadir, or the other way around).
51 : 0 : return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Incorrect or no genesis block found. Wrong datadir for network?")};
52 : : }
53 : :
54 : : // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
55 : : // in the past, but is now trying to run unpruned.
56 [ - + - - ]: 1503 : if (chainman.m_blockman.m_have_pruned && !options.prune) {
57 : 0 : return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain")};
58 : : }
59 : :
60 : : // At this point blocktree args are consistent with what's on disk.
61 : : // If we're not mid-reindex (based on disk + args), add a genesis block on disk
62 : : // (otherwise we use the one already on disk).
63 : : // This is called again in ImportBlocks after the reindex completes.
64 [ + - + - ]: 1503 : if (chainman.m_blockman.m_blockfiles_indexed && !chainman.ActiveChainstate().LoadGenesisBlock()) {
65 : 0 : return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
66 : : }
67 : :
68 : 3006 : auto is_coinsview_empty = [&](Chainstate& chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
69 [ + - + - ]: 3006 : return options.wipe_chainstate_db || chainstate.CoinsTip().GetBestBlock().IsNull();
70 : 1503 : };
71 : :
72 [ - + ]: 1503 : assert(chainman.m_total_coinstip_cache > 0);
73 [ - + ]: 1503 : assert(chainman.m_total_coinsdb_cache > 0);
74 : :
75 : : // If running with multiple chainstates, limit the cache sizes with a
76 : : // discount factor. If discounted the actual cache size will be
77 : : // recalculated by `chainman.MaybeRebalanceCaches()`. The discount factor
78 : : // is conservatively chosen such that the sum of the caches does not exceed
79 : : // the allowable amount during this temporary initialization state.
80 [ + - ]: 1503 : double init_cache_fraction = chainman.HistoricalChainstate() ? 0.2 : 1.0;
81 : :
82 : : // At this point we're either in reindex or we've loaded a useful
83 : : // block tree into BlockIndex()!
84 : :
85 [ + + ]: 3006 : for (const auto& chainstate : chainman.m_chainstates) {
86 [ + - ]: 1503 : LogInfo("Initializing chainstate %s", chainstate->ToString());
87 : :
88 : 1503 : try {
89 [ + - ]: 1503 : chainstate->InitCoinsDB(
90 : 1503 : /*cache_size_bytes=*/chainman.m_total_coinsdb_cache * init_cache_fraction,
91 : 1503 : /*in_memory=*/options.coins_db_in_memory,
92 [ + - ]: 1503 : /*should_wipe=*/options.wipe_chainstate_db);
93 [ - - ]: 0 : } catch (dbwrapper_error& err) {
94 [ - - ]: 0 : LogError("%s\n", err.what());
95 [ - - ]: 0 : return {ChainstateLoadStatus::FAILURE, _("Error opening coins database")};
96 : 0 : }
97 : :
98 [ - + ]: 1503 : if (options.coins_error_cb) {
99 [ # # ]: 0 : chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
100 : : }
101 : :
102 : : // Refuse to load unsupported database format.
103 : : // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
104 [ - + ]: 1503 : if (chainstate->CoinsDB().NeedsUpgrade()) {
105 : 0 : return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Unsupported chainstate database format found. "
106 : : "Please restart with -reindex-chainstate. This will "
107 : 0 : "rebuild the chainstate database.")};
108 : : }
109 : :
110 : : // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
111 [ - + ]: 1503 : if (!chainstate->ReplayBlocks()) {
112 : 0 : return {ChainstateLoadStatus::FAILURE, _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.")};
113 : : }
114 : :
115 : : // The on-disk coinsdb is now in a good state, create the cache
116 : 1503 : chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
117 [ + - ]: 1503 : assert(chainstate->CanFlushToDisk());
118 : :
119 [ - + ]: 1503 : if (!is_coinsview_empty(*chainstate)) {
120 : : // LoadChainTip initializes the chain based on CoinsTip()'s best block
121 [ # # ]: 0 : if (!chainstate->LoadChainTip()) {
122 : 0 : return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
123 : : }
124 [ # # # # ]: 0 : assert(chainstate->m_chain.Tip() != nullptr);
125 : : }
126 : : }
127 : :
128 : : // Populate setBlockIndexCandidates in a separate loop, after all LoadChainTip()
129 : : // calls have finished modifying nSequenceId. Because nSequenceId is used in the
130 : : // set's comparator, changing it while blocks are in the set would be UB.
131 [ + + ]: 3006 : for (const auto& chainstate : chainman.m_chainstates) {
132 : 1503 : chainstate->PopulateBlockIndexCandidates();
133 : : }
134 : :
135 : 1503 : const auto& chainstates{chainman.m_chainstates};
136 [ - + ]: 1503 : if (std::any_of(chainstates.begin(), chainstates.end(),
137 : 1503 : [](const auto& cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
138 : 0 : return {ChainstateLoadStatus::FAILURE, strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
139 : 0 : chainman.GetConsensus().SegwitHeight)};
140 : 1503 : };
141 : :
142 : : // Now that chainstates are loaded and we're able to flush to
143 : : // disk, rebalance the coins caches to desired levels based
144 : : // on the condition of each chainstate.
145 : 1503 : chainman.MaybeRebalanceCaches();
146 : :
147 [ + - ]: 3006 : return {ChainstateLoadStatus::SUCCESS, {}};
148 [ - - - - : 1503 : }
+ - ]
149 : :
150 : 1503 : ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
151 : : const ChainstateLoadOptions& options)
152 : : {
153 [ + + ]: 3006 : if (!chainman.AssumedValidBlock().IsNull()) {
154 [ + - ]: 2 : LogInfo("Assuming ancestors of block %s have valid signatures.", chainman.AssumedValidBlock().GetHex());
155 : : } else {
156 : 1502 : LogInfo("Validating signatures for all blocks.");
157 : : }
158 [ + - ]: 1503 : LogInfo("Setting nMinimumChainWork=%s", chainman.MinimumChainWork().GetHex());
159 [ - + ]: 1503 : if (chainman.MinimumChainWork() < UintToArith256(chainman.GetConsensus().nMinimumChainWork)) {
160 [ # # ]: 0 : LogWarning("nMinimumChainWork set below default value of %s", chainman.GetConsensus().nMinimumChainWork.GetHex());
161 : : }
162 [ - + ]: 1503 : if (chainman.m_blockman.GetPruneTarget() == BlockManager::PRUNE_TARGET_MANUAL) {
163 : 0 : LogInfo("Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.");
164 [ - + ]: 1503 : } else if (chainman.m_blockman.GetPruneTarget()) {
165 : 0 : LogInfo("Prune configured to target %u MiB on disk for block and undo files.",
166 : : chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
167 : : }
168 : :
169 : 1503 : LOCK(cs_main);
170 : :
171 : 1503 : chainman.m_total_coinstip_cache = cache_sizes.coins;
172 : 1503 : chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
173 : :
174 : : // Load the fully validated chainstate.
175 [ + - ]: 1503 : Chainstate& validated_cs{chainman.InitializeChainstate(options.mempool)};
176 : :
177 : : // Load a chain created from a UTXO snapshot, if any exist.
178 [ + - ]: 1503 : Chainstate* assumeutxo_cs{chainman.LoadAssumeutxoChainstate()};
179 : :
180 [ - + - - ]: 1503 : if (assumeutxo_cs && options.wipe_chainstate_db) {
181 : : // Reset chainstate target to network tip instead of snapshot block.
182 [ # # ]: 0 : validated_cs.SetTargetBlock(nullptr);
183 [ # # ]: 0 : LogInfo("[snapshot] deleting snapshot chainstate due to reindexing");
184 [ # # # # ]: 0 : if (!chainman.DeleteChainstate(*assumeutxo_cs)) {
185 [ # # # # ]: 0 : return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Couldn't remove snapshot chainstate.")};
186 : : }
187 : : assumeutxo_cs = nullptr;
188 : : }
189 : :
190 [ + - - + ]: 1503 : auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
191 [ - + ]: 1503 : if (init_status != ChainstateLoadStatus::SUCCESS) {
192 [ # # ]: 0 : return {init_status, init_error};
193 : : }
194 : :
195 : : // If a snapshot chainstate was fully validated by a background chainstate during
196 : : // the last run, detect it here and clean up the now-unneeded background
197 : : // chainstate.
198 : : //
199 : : // Why is this cleanup done here (on subsequent restart) and not just when the
200 : : // snapshot is actually validated? Because this entails unusual
201 : : // filesystem operations to move leveldb data directories around, and that seems
202 : : // too risky to do in the middle of normal runtime.
203 : 1503 : auto snapshot_completion{assumeutxo_cs
204 [ - + - - ]: 1503 : ? chainman.MaybeValidateSnapshot(validated_cs, *assumeutxo_cs)
205 : : : SnapshotCompletionResult::SKIPPED};
206 : :
207 [ # # ]: 0 : if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
208 : : // do nothing; expected case
209 [ # # ]: 0 : } else if (snapshot_completion == SnapshotCompletionResult::SUCCESS) {
210 [ # # ]: 0 : LogInfo("[snapshot] cleaning up unneeded background chainstate, then reinitializing");
211 [ # # # # ]: 0 : if (!chainman.ValidatedSnapshotCleanup(validated_cs, *assumeutxo_cs)) {
212 [ # # # # ]: 0 : return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Background chainstate cleanup failed unexpectedly.")};
213 : : }
214 : :
215 : : // Because ValidatedSnapshotCleanup() has torn down chainstates with
216 : : // ChainstateManager::ResetChainstates(), reinitialize them here without
217 : : // duplicating the blockindex work above.
218 [ # # ]: 0 : assert(chainman.m_chainstates.empty());
219 : :
220 [ # # ]: 0 : chainman.InitializeChainstate(options.mempool);
221 : :
222 : : // A reload of the block index is required to recompute setBlockIndexCandidates
223 : : // for the fully validated chainstate.
224 [ # # # # ]: 0 : chainman.ActiveChainstate().ClearBlockIndexCandidates();
225 : :
226 [ # # # # ]: 0 : auto [init_status, init_error] = CompleteChainstateInitialization(chainman, options);
227 [ # # ]: 0 : if (init_status != ChainstateLoadStatus::SUCCESS) {
228 [ # # ]: 0 : return {init_status, init_error};
229 : : }
230 : 0 : } else {
231 [ # # ]: 0 : return {ChainstateLoadStatus::FAILURE_FATAL, _(
232 : : "UTXO snapshot failed to validate. "
233 : 0 : "Restart to resume normal initial block download, or try loading a different snapshot.")};
234 : : }
235 : :
236 [ + - ]: 3006 : return {ChainstateLoadStatus::SUCCESS, {}};
237 [ + - ]: 4509 : }
238 : :
239 : 1503 : ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options)
240 : : {
241 : 3006 : auto is_coinsview_empty = [&](Chainstate& chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
242 [ + - + - ]: 3006 : return options.wipe_chainstate_db || chainstate.CoinsTip().GetBestBlock().IsNull();
243 : 1503 : };
244 : :
245 : 1503 : LOCK(cs_main);
246 : :
247 [ + + ]: 3006 : for (auto& chainstate : chainman.m_chainstates) {
248 [ + - - + ]: 1503 : if (!is_coinsview_empty(*chainstate)) {
249 [ # # ]: 0 : const CBlockIndex* tip = chainstate->m_chain.Tip();
250 [ # # # # : 0 : if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
# # ]
251 [ # # ]: 0 : return {ChainstateLoadStatus::FAILURE, _("The block database contains a block which appears to be from the future. "
252 : : "This may be due to your computer's date and time being set incorrectly. "
253 : 0 : "Only rebuild the block database if you are sure that your computer's date and time are correct")};
254 : : }
255 : :
256 [ # # # # ]: 0 : VerifyDBResult result = CVerifyDB(chainman.GetNotifications()).VerifyDB(
257 [ # # # # ]: 0 : *chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
258 : 0 : options.check_level,
259 [ # # ]: 0 : options.check_blocks);
260 [ # # # # ]: 0 : switch (result) {
261 : : case VerifyDBResult::SUCCESS:
262 : : case VerifyDBResult::SKIPPED_MISSING_BLOCKS:
263 : : break;
264 : 0 : case VerifyDBResult::INTERRUPTED:
265 [ # # ]: 0 : return {ChainstateLoadStatus::INTERRUPTED, _("Block verification was interrupted")};
266 : 0 : case VerifyDBResult::CORRUPTED_BLOCK_DB:
267 [ # # ]: 0 : return {ChainstateLoadStatus::FAILURE, _("Corrupted block database detected")};
268 : 0 : case VerifyDBResult::SKIPPED_L3_CHECKS:
269 [ # # ]: 0 : if (options.require_full_verification) {
270 [ # # ]: 0 : return {ChainstateLoadStatus::FAILURE_INSUFFICIENT_DBCACHE, _("Insufficient dbcache for block verification")};
271 : : }
272 : : break;
273 : : } // no default case, so the compiler can warn about missing cases
274 : : }
275 : : }
276 : :
277 [ + - ]: 3006 : return {ChainstateLoadStatus::SUCCESS, {}};
278 [ + - ]: 3006 : }
279 : : } // namespace node
|