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