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