12 #include <validation.h>
20 auto &pblocktree{chainman.m_blockman.m_block_tree_db};
24 pblocktree.reset(
new CBlockTreeDB(cache_sizes.block_tree_db,
25 options.block_tree_db_in_memory,
28 if (options.reindex) {
29 pblocktree->WriteReindexing(
true);
40 if (!pblocktree->Upgrade()) {
42 _(
"Error upgrading block index database")};
45 if (options.check_interrupt && options.check_interrupt()) {
53 if (!chainman.LoadBlockIndex()) {
54 if (options.check_interrupt && options.check_interrupt()) {
59 _(
"Error loading block database")};
62 if (!chainman.BlockIndex().empty() &&
63 !chainman.m_blockman.LookupBlockIndex(
64 chainman.GetConsensus().hashGenesisBlock)) {
68 _(
"Incorrect or no genesis block found. Wrong datadir for "
75 if (chainman.m_blockman.m_have_pruned && !options.prune) {
78 _(
"You need to rebuild the database using -reindex to go back to "
79 "unpruned mode. This will redownload the entire blockchain")};
86 if (!
fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
88 _(
"Error initializing block database")};
91 auto is_coinsview_empty =
93 return options.reindex || options.reindex_chainstate ||
94 chainstate->CoinsTip().GetBestBlock().IsNull();
97 assert(chainman.m_total_coinstip_cache > 0);
98 assert(chainman.m_total_coinsdb_cache > 0);
104 double init_cache_fraction = 0.2;
109 for (
Chainstate *chainstate : chainman.GetAll()) {
110 LogPrintf(
"Initializing chainstate %s\n", chainstate->ToString());
112 chainstate->InitCoinsDB(
113 chainman.m_total_coinsdb_cache *
115 options.coins_db_in_memory,
116 options.reindex || options.reindex_chainstate);
118 if (options.coins_error_cb) {
119 chainstate->CoinsErrorCatcher().AddReadErrCallback(
120 options.coins_error_cb);
126 if (!chainstate->CoinsDB().Upgrade()) {
128 _(
"Error upgrading chainstate database")};
133 if (!chainstate->ReplayBlocks()) {
135 _(
"Unable to replay blocks. You will need to rebuild the "
136 "database using -reindex-chainstate.")};
140 chainstate->InitCoinsCache(chainman.m_total_coinstip_cache *
141 init_cache_fraction);
142 assert(chainstate->CanFlushToDisk());
144 if (!is_coinsview_empty(chainstate)) {
147 if (!chainstate->LoadChainTip()) {
149 _(
"Error initializing block database")};
151 assert(chainstate->m_chain.Tip() !=
nullptr);
158 chainman.MaybeRebalanceCaches();
167 LogPrintf(
"Assuming ancestors of block %s have valid signatures.\n",
170 LogPrintf(
"Validating signatures for all blocks.\n");
172 LogPrintf(
"Setting nMinimumChainWork=%s\n",
176 LogPrintf(
"Warning: nMinimumChainWork set below default value of %s\n",
182 "Block pruning enabled. Use RPC call pruneblockchain(height) to "
183 "manually prune block and undo files.\n");
185 LogPrintf(
"Prune configured to target %u MiB on disk for block and "
195 chainman.InitializeChainstate(options.
mempool);
198 chainman.DetectSnapshotChainstate(options.
mempool);
201 auto [init_status, init_error] =
204 return {init_status, init_error};
216 auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
221 LogPrintf(
"[snapshot] cleaning up unneeded background chainstate, then "
223 if (!chainman.ValidatedSnapshotCleanup()) {
226 "Background chainstate cleanup failed unexpectedly.")};
236 chainman.InitializeChainstate(options.
mempool);
240 chainman.ActiveChainstate().UnloadBlockIndex();
242 auto [init_status, init_error] =
245 return {init_status, init_error};
249 _(
"UTXO snapshot failed to validate. "
250 "Restart to resume normal initial block download, or try "
251 "loading a different snapshot.")};
260 auto is_coinsview_empty =
263 chainstate->CoinsTip().GetBestBlock().IsNull();
269 if (!is_coinsview_empty(chainstate)) {
270 const CBlockIndex *tip = chainstate->m_chain.Tip();
273 _(
"The block database contains a block which appears "
274 "to be from the future. "
275 "This may be due to your computer's date and time "
276 "being set incorrectly. "
277 "Only rebuild the block database if you are sure "
278 "that your computer's date and time are correct")};
290 _(
"Block verification was interrupted")};
293 _(
"Corrupted block database detected")};
298 _(
"Insufficient dbcache for block verification")};
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 ...
The block chain is a tree shaped structure starting with the genesis block at the root,...
Access to the block database (blocks/index/)
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
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.
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
const arith_uint256 & MinimumChainWork() const
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
const BlockHash & AssumedValidBlock() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
bool IsSnapshotActive() const
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
const Consensus::Params & GetConsensus() const
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
std::string GetHex() const
std::string GetHex() const
static constexpr auto PRUNE_TARGET_MANUAL
uint64_t GetPruneTarget() const
Attempt to stay below this number of bytes of block files.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
@ FAILURE_INCOMPATIBLE_DB
@ FAILURE_FATAL
Fatal error which should not prompt to reindex.
@ FAILURE
Generic failure which reindexing may fix.
@ FAILURE_INSUFFICIENT_DBCACHE
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Find the first block that is not pruned.
std::tuple< ChainstateLoadStatus, bilingual_str > ChainstateLoadResult
Chainstate load status code and optional error string.
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
This sequence can have 4 types of outcomes:
static ChainstateLoadResult CompleteChainstateInitialization(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options) EXCLUSIVE_LOCKS_REQUIRED(
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
std::atomic_bool fReindex
uint256 nMinimumChainWork
bool require_full_verification
#define EXCLUSIVE_LOCKS_REQUIRED(...)
bilingual_str _(const char *psz)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.