Bitcoin ABC 0.32.4
P2P Digital Currency
chainstate.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 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 <chainparams.h>
8#include <config.h>
9#include <consensus/params.h>
10#include <kernel/caches.h>
11#include <node/blockstorage.h>
12#include <util/fs.h>
13#include <validation.h>
14
16
17namespace node {
18// Complete initialization of chainstates after the initial call has been made
19// to ChainstateManager::InitializeChainstate().
21 ChainstateManager &chainman, const CacheSizes &cache_sizes,
23 auto &pblocktree{chainman.m_blockman.m_block_tree_db};
24 // new CBlockTreeDB tries to delete the existing file, which
25 // fails if it's still open from the previous loop. Close it first:
26 pblocktree.reset();
27 pblocktree = std::make_unique<CBlockTreeDB>(
28 DBParams{.path = chainman.m_options.datadir / "blocks" / "index",
29 .cache_bytes = cache_sizes.block_tree_db,
30 .memory_only = options.block_tree_db_in_memory,
31 .wipe_data = options.reindex,
32 .options = chainman.m_options.block_tree_db});
33
34 if (options.reindex) {
35 pblocktree->WriteReindexing(true);
36 // If we're reindexing in prune mode, wipe away unusable block
37 // files and all undo data files
38 if (options.prune) {
39 chainman.m_blockman.CleanupBlockRevFiles();
40 }
41 }
42
43 // If necessary, upgrade from older database format.
44 // This is a no-op if we cleared the block tree db with -reindex
45 // or -reindex-chainstate
46 if (!pblocktree->Upgrade()) {
48 _("Error upgrading block index database")};
49 }
50
51 if (options.check_interrupt && options.check_interrupt()) {
53 }
54
55 // LoadBlockIndex will load m_have_pruned if we've ever removed a
56 // block file from disk.
57 // Note that it also sets fReindex global based on the disk flag!
58 // From here on, fReindex and options.reindex values may be different!
59 if (!chainman.LoadBlockIndex()) {
60 if (options.check_interrupt && options.check_interrupt()) {
62 }
63
65 _("Error loading block database")};
66 }
67
68 if (!chainman.BlockIndex().empty() &&
69 !chainman.m_blockman.LookupBlockIndex(
70 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).
74 _("Incorrect or no genesis block found. Wrong datadir for "
75 "network?")};
76 }
77
78 // Check for changed -prune state. What we are concerned about is a
79 // user who has pruned blocks in the past, but is now trying to run
80 // unpruned.
81 if (chainman.m_blockman.m_have_pruned && !options.prune) {
82 return {
84 _("You need to rebuild the database using -reindex to go back to "
85 "unpruned mode. This will redownload the entire blockchain")};
86 }
87
88 // At this point blocktree args are consistent with what's on disk.
89 // If we're not mid-reindex (based on disk + args), add a genesis
90 // block on disk (otherwise we use the one already on disk). This is
91 // called again in ImportBlocks after the reindex completes.
92 if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
94 _("Error initializing block database")};
95 }
96
97 auto is_coinsview_empty =
99 return options.reindex || options.reindex_chainstate ||
100 chainstate->CoinsTip().GetBestBlock().IsNull();
101 };
102
103 assert(chainman.m_total_coinstip_cache > 0);
104 assert(chainman.m_total_coinsdb_cache > 0);
105
106 // Conservative value which is arbitrarily chosen, as it will ultimately be
107 // changed by a call to `chainman.MaybeRebalanceCaches()`. We just need to
108 // make sure that the sum of the two caches (40%) does not exceed the
109 // allowable amount during this temporary initialization state.
110 double init_cache_fraction = 0.2;
111
112 // At this point we're either in reindex or we've loaded a useful
113 // block tree into BlockIndex()!
114
115 for (Chainstate *chainstate : chainman.GetAll()) {
116 LogPrintf("Initializing chainstate %s\n", chainstate->ToString());
117
118 chainstate->InitCoinsDB(
119 /* cache_size_bytes */ chainman.m_total_coinsdb_cache *
120 init_cache_fraction,
121 /* in_memory */ options.coins_db_in_memory,
122 /* should_wipe */ options.reindex || options.reindex_chainstate);
123
124 if (options.coins_error_cb) {
125 chainstate->CoinsErrorCatcher().AddReadErrCallback(
126 options.coins_error_cb);
127 }
128
129 // Refuse to load unsupported database format.
130 // This is a no-op if we cleared the coinsviewdb with -reindex
131 // or -reindex-chainstate
132 if (chainstate->CoinsDB().NeedsUpgrade()) {
134 _("Unsupported chainstate database format found. "
135 "Please restart with -reindex-chainstate. This will "
136 "rebuild the chainstate database.")};
137 }
138
139 // ReplayBlocks is a no-op if we cleared the coinsviewdb with
140 // -reindex or -reindex-chainstate
141 if (!chainstate->ReplayBlocks()) {
143 _("Unable to replay blocks. You will need to rebuild the "
144 "database using -reindex-chainstate.")};
145 }
146
147 // The on-disk coinsdb is now in a good state, create the cache
148 chainstate->InitCoinsCache(chainman.m_total_coinstip_cache *
149 init_cache_fraction);
150 assert(chainstate->CanFlushToDisk());
151
152 if (!is_coinsview_empty(chainstate)) {
153 // LoadChainTip initializes the chain based on CoinsTip()'s
154 // best block
155 if (!chainstate->LoadChainTip()) {
157 _("Error initializing block database")};
158 }
159 assert(chainstate->m_chain.Tip() != nullptr);
160 }
161 }
162
163 // Now that chainstates are loaded and we're able to flush to
164 // disk, rebalance the coins caches to desired levels based
165 // on the condition of each chainstate.
166 chainman.MaybeRebalanceCaches();
167
169}
170
172 const CacheSizes &cache_sizes,
173 const ChainstateLoadOptions &options) {
174 if (!chainman.AssumedValidBlock().IsNull()) {
175 LogPrintf("Assuming ancestors of block %s have valid signatures.\n",
176 chainman.AssumedValidBlock().GetHex());
177 } else {
178 LogPrintf("Validating signatures for all blocks.\n");
179 }
180 LogPrintf("Setting nMinimumChainWork=%s\n",
181 chainman.MinimumChainWork().GetHex());
182 if (chainman.MinimumChainWork() <
184 LogPrintf("Warning: nMinimumChainWork set below default value of %s\n",
186 }
187 if (chainman.m_blockman.GetPruneTarget() ==
189 LogPrintf(
190 "Block pruning enabled. Use RPC call pruneblockchain(height) to "
191 "manually prune block and undo files.\n");
192 } else if (chainman.m_blockman.GetPruneTarget()) {
193 LogPrintf("Prune configured to target %u MiB on disk for block and "
194 "undo files.\n",
195 chainman.m_blockman.GetPruneTarget() / 1024 / 1024);
196 }
197
198 LOCK(cs_main);
199 chainman.m_total_coinstip_cache = cache_sizes.coins;
200 chainman.m_total_coinsdb_cache = cache_sizes.coins_db;
201
202 // Load the fully validated chainstate.
203 chainman.InitializeChainstate(options.mempool);
204
205 // Load a chain created from a UTXO snapshot, if any exist.
206 bool has_snapshot = chainman.DetectSnapshotChainstate(options.mempool);
207
208 if (has_snapshot && (options.reindex || options.reindex_chainstate)) {
209 LogPrintf(
210 "[snapshot] deleting snapshot chainstate due to reindexing\n");
211 if (!chainman.DeleteSnapshotChainstate()) {
213 Untranslated("Couldn't remove snapshot chainstate.")};
214 }
215 }
216
217 {
218 auto [init_status, init_error] =
219 CompleteChainstateInitialization(chainman, cache_sizes, options);
220 if (init_status != ChainstateLoadStatus::SUCCESS) {
221 return {init_status, init_error};
222 }
223 }
224
225 // If a snapshot chainstate was fully validated by a background chainstate
226 // during the last run, detect it here and clean up the now-unneeded
227 // background chainstate.
228 //
229 // Why is this cleanup done here (on subsequent restart) and not just when
230 // the snapshot is actually validated? Because this entails unusual
231 // filesystem operations to move leveldb data directories around, and that
232 // seems too risky to do in the middle of normal runtime.
233 auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
234
235 if (snapshot_completion == SnapshotCompletionResult::SKIPPED) {
236 // do nothing; expected case
237 } else if (snapshot_completion == SnapshotCompletionResult::SUCCESS) {
238 LogPrintf("[snapshot] cleaning up unneeded background chainstate, then "
239 "reinitializing\n");
240 if (!chainman.ValidatedSnapshotCleanup()) {
243 "Background chainstate cleanup failed unexpectedly.")};
244 }
245
246 // Because ValidatedSnapshotCleanup() has torn down chainstates with
247 // ChainstateManager::ResetChainstates(), reinitialize them here without
248 // duplicating the blockindex work above.
249 assert(chainman.GetAll().empty());
250 assert(!chainman.IsSnapshotActive());
251 assert(!chainman.IsSnapshotValidated());
252
253 chainman.InitializeChainstate(options.mempool);
254
255 // A reload of the block index is required to recompute
256 // setBlockIndexCandidates for the fully validated chainstate.
257 chainman.ActiveChainstate().ClearBlockIndexCandidates();
258
259 auto [init_status, init_error] =
260 CompleteChainstateInitialization(chainman, cache_sizes, options);
261 if (init_status != ChainstateLoadStatus::SUCCESS) {
262 return {init_status, init_error};
263 }
264 } else {
266 _("UTXO snapshot failed to validate. "
267 "Restart to resume normal initial block download, or try "
268 "loading a different snapshot.")};
269 }
270
272}
273
276 const ChainstateLoadOptions &options) {
277 auto is_coinsview_empty =
279 return options.reindex || options.reindex_chainstate ||
280 chainstate->CoinsTip().GetBestBlock().IsNull();
281 };
282
283 LOCK(cs_main);
284
285 for (Chainstate *chainstate : chainman.GetAll()) {
286 if (!is_coinsview_empty(chainstate)) {
287 const CBlockIndex *tip = chainstate->m_chain.Tip();
288 if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
290 _("The block database contains a block which appears "
291 "to be from the future. "
292 "This may be due to your computer's date and time "
293 "being set incorrectly. "
294 "Only rebuild the block database if you are sure "
295 "that your computer's date and time are correct")};
296 }
297
298 VerifyDBResult result =
299 CVerifyDB(chainman.GetNotifications())
300 .VerifyDB(*chainstate, chainstate->CoinsDB(),
301 options.check_level, options.check_blocks);
302 switch (result) {
305 break;
308 _("Block verification was interrupted")};
311 _("Corrupted block database detected")};
313 if (options.require_full_verification) {
314 return {
316 _("Insufficient dbcache for block verification")};
317 }
318 break;
319 } // no default case, so the compiler can warn about missing cases
320 }
321 }
322
324}
325} // namespace node
arith_uint256 UintToArith256(const uint256 &a)
static constexpr int64_t MAX_FUTURE_BLOCK_TIME
Maximum amount of time that a block timestamp is allowed to exceed the current network-adjusted time ...
Definition: chain.h:28
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: blockindex.h:25
uint32_t nTime
Definition: blockindex.h:76
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Definition: validation.h:655
VerifyDBResult VerifyDB(Chainstate &chainstate, CCoinsView &coinsview, int nCheckLevel, int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Chainstate stores and provides an API to update our local knowledge of the current best chain.
Definition: validation.h:734
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
Definition: validation.h:1186
SnapshotCompletionResult MaybeCompleteSnapshotValidation() EXCLUSIVE_LOCKS_REQUIRED(const CBlockIndex *GetSnapshotBaseBlock() const EXCLUSIVE_LOCKS_REQUIRED(Chainstate ActiveChainstate)() const
Once the background validation chainstate has reached the height which is the base of the UTXO snapsh...
Definition: validation.h:1437
size_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
Definition: validation.h:1389
kernel::Notifications & GetNotifications() const
Definition: validation.h:1294
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
Definition: validation.h:1475
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
Definition: validation.h:1393
bool IsSnapshotActive() const
const Consensus::Params & GetConsensus() const
Definition: validation.h:1282
const arith_uint256 & MinimumChainWork() const
Definition: validation.h:1288
const BlockHash & AssumedValidBlock() const
Definition: validation.h:1291
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
Definition: validation.h:1403
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
Definition: validation.h:1327
bool IsNull() const
Definition: uint256.h:32
std::string GetHex() const
Definition: uint256.cpp:16
std::string GetHex() const
static constexpr auto PRUNE_TARGET_MANUAL
Definition: blockstorage.h:365
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
Definition: blockstorage.h:362
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
#define LogPrintf(...)
Definition: logging.h:424
Definition: init.h:31
@ FAILURE_FATAL
Fatal error which should not prompt to reindex.
@ FAILURE
Generic failure which reindexing may fix.
std::tuple< ChainstateLoadStatus, bilingual_str > ChainstateLoadResult
Chainstate load status code and optional error string.
Definition: chainstate.h:58
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:171
static ChainstateLoadResult CompleteChainstateInitialization(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options) EXCLUSIVE_LOCKS_REQUIRED(
Definition: chainstate.cpp:20
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:275
std::atomic_bool fReindex
uint256 nMinimumChainWork
Definition: params.h:88
Application-specific storage settings.
Definition: dbwrapper.h:32
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:34
#define LOCK(cs)
Definition: sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:105
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
assert(!tx.IsCoinBase())
VerifyDBResult
Definition: validation.h:643