23#include <validation.h>
40 auto& pblocktree{chainman.m_blockman.m_block_tree_db};
45 pblocktree = std::make_unique<BlockTreeDB>(
DBParams{
46 .
path = chainman.m_options.datadir /
"blocks" /
"index",
47 .cache_bytes =
static_cast<size_t>(cache_sizes.block_tree_db),
48 .memory_only = options.block_tree_db_in_memory,
49 .wipe_data = options.wipe_block_tree_db,
50 .options = chainman.m_options.block_tree_db});
56 if (options.wipe_block_tree_db) {
57 pblocktree->WriteReindexing(
true);
58 chainman.m_blockman.m_blockfiles_indexed =
false;
61 chainman.m_blockman.CleanupBlockRevFiles();
70 if (!chainman.LoadBlockIndex()) {
75 if (!chainman.BlockIndex().empty() &&
76 !chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
84 if (chainman.m_blockman.m_have_pruned && !options.prune) {
85 return {
ChainstateLoadStatus::FAILURE,
_(
"You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain")};
92 if (chainman.m_blockman.m_blockfiles_indexed && !chainman.ActiveChainstate().LoadGenesisBlock()) {
97 return options.wipe_chainstate_db || chainstate->CoinsTip().GetBestBlock().IsNull();
100 assert(chainman.m_total_coinstip_cache > 0);
101 assert(chainman.m_total_coinsdb_cache > 0);
108 double init_cache_fraction = chainman.GetAll().size() > 1 ? 0.2 : 1.0;
113 for (
Chainstate* chainstate : chainman.GetAll()) {
114 LogPrintf(
"Initializing chainstate %s\n", chainstate->ToString());
117 chainstate->InitCoinsDB(
118 chainman.m_total_coinsdb_cache * init_cache_fraction,
119 options.coins_db_in_memory,
120 options.wipe_chainstate_db);
126 if (options.coins_error_cb) {
127 chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
132 if (chainstate->CoinsDB().NeedsUpgrade()) {
134 "Please restart with -reindex-chainstate. This will "
135 "rebuild the chainstate database.")};
139 if (!chainstate->ReplayBlocks()) {
144 chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
145 assert(chainstate->CanFlushToDisk());
147 if (!is_coinsview_empty(chainstate)) {
149 if (!chainstate->LoadChainTip()) {
152 assert(chainstate->m_chain.Tip() !=
nullptr);
156 if (!options.wipe_block_tree_db) {
157 auto chainstates{chainman.GetAll()};
158 if (std::any_of(chainstates.begin(), chainstates.end(),
161 chainman.GetConsensus().SegwitHeight)};
168 chainman.MaybeRebalanceCaches();
179 LogPrintf(
"Validating signatures for all blocks.\n");
186 LogPrintf(
"Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
197 chainman.InitializeChainstate(options.
mempool);
200 bool has_snapshot = chainman.DetectSnapshotChainstate();
203 LogPrintf(
"[snapshot] deleting snapshot chainstate due to reindexing\n");
204 if (!chainman.DeleteSnapshotChainstate()) {
211 return {init_status, init_error};
222 auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
227 LogPrintf(
"[snapshot] cleaning up unneeded background chainstate, then reinitializing\n");
228 if (!chainman.ValidatedSnapshotCleanup()) {
239 chainman.InitializeChainstate(options.
mempool);
247 return {init_status, init_error};
251 "UTXO snapshot failed to validate. "
252 "Restart to resume normal initial block download, or try loading a different snapshot.")};
267 if (!is_coinsview_empty(chainstate)) {
268 const CBlockIndex* tip = chainstate->m_chain.Tip();
271 "This may be due to your computer's date and time being set incorrectly. "
272 "Only rebuild the block database if you are sure that your computer's date and time are correct")};
276 *chainstate, chainman.
GetConsensus(), chainstate->CoinsDB(),
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 time before the block ...
The block chain is a tree shaped structure starting with the genesis block at the root,...
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
VerifyDBResult VerifyDB(Chainstate &chainstate, const Consensus::Params &consensus_params, 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...
const uint256 & AssumedValidBlock() const
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...
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
kernel::Notifications & GetNotifications() const
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?
bool IsSnapshotActive() const
const Consensus::Params & GetConsensus() const
const arith_uint256 & MinimumChainWork() const
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
constexpr bool IsNull() const
std::string GetHex() const
std::string GetHex() const
Hex encoding of the number (with the most significant digits first).
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
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)
uint256 nMinimumChainWork
The best chain should have at least this much work.
Application-specific storage settings.
fs::path path
Location in the filesystem where leveldb data will be stored.
bool require_full_verification
Setting require_full_verification to true will require all checks at check_level (below) to succeed f...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
bilingual_str _(ConstevalStringLiteral str)
Translation function.
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.