22 #include <validation.h> 39 auto& pblocktree{chainman.m_blockman.m_block_tree_db};
43 pblocktree = std::make_unique<BlockTreeDB>(
DBParams{
44 .
path = chainman.m_options.datadir /
"blocks" /
"index",
45 .cache_bytes =
static_cast<size_t>(cache_sizes.block_tree_db),
46 .memory_only = options.block_tree_db_in_memory,
47 .wipe_data = options.reindex,
48 .options = chainman.m_options.block_tree_db});
50 if (options.reindex) {
51 pblocktree->WriteReindexing(
true);
54 chainman.m_blockman.CleanupBlockRevFiles();
64 if (!chainman.LoadBlockIndex()) {
69 if (!chainman.BlockIndex().empty() &&
70 !chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
78 if (chainman.m_blockman.m_have_pruned && !options.prune) {
79 return {
ChainstateLoadStatus::FAILURE,
_(
"You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain")};
86 if (!
fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
91 return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull();
94 assert(chainman.m_total_coinstip_cache > 0);
95 assert(chainman.m_total_coinsdb_cache > 0);
101 double init_cache_fraction = 0.2;
106 for (
Chainstate* chainstate : chainman.GetAll()) {
107 LogPrintf(
"Initializing chainstate %s\n", chainstate->ToString());
109 chainstate->InitCoinsDB(
110 chainman.m_total_coinsdb_cache * init_cache_fraction,
111 options.coins_db_in_memory,
112 options.reindex || options.reindex_chainstate);
114 if (options.coins_error_cb) {
115 chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
120 if (chainstate->CoinsDB().NeedsUpgrade()) {
122 "Please restart with -reindex-chainstate. This will " 123 "rebuild the chainstate database.")};
127 if (!chainstate->ReplayBlocks()) {
132 chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
133 assert(chainstate->CanFlushToDisk());
135 if (!is_coinsview_empty(chainstate)) {
137 if (!chainstate->LoadChainTip()) {
140 assert(chainstate->m_chain.Tip() !=
nullptr);
144 if (!options.reindex) {
145 auto chainstates{chainman.GetAll()};
146 if (std::any_of(chainstates.begin(), chainstates.end(),
149 chainman.GetConsensus().SegwitHeight)};
156 chainman.MaybeRebalanceCaches();
167 LogPrintf(
"Validating signatures for all blocks.\n");
174 LogPrintf(
"Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.\n");
185 chainman.InitializeChainstate(options.
mempool);
188 bool has_snapshot = chainman.DetectSnapshotChainstate();
191 LogPrintf(
"[snapshot] deleting snapshot chainstate due to reindexing\n");
192 if (!chainman.DeleteSnapshotChainstate()) {
199 return {init_status, init_error};
210 auto snapshot_completion = chainman.MaybeCompleteSnapshotValidation();
215 LogPrintf(
"[snapshot] cleaning up unneeded background chainstate, then reinitializing\n");
216 if (!chainman.ValidatedSnapshotCleanup()) {
227 chainman.InitializeChainstate(options.
mempool);
235 return {init_status, init_error};
239 "UTXO snapshot failed to validate. " 240 "Restart to resume normal initial block download, or try loading a different snapshot.")};
255 if (!is_coinsview_empty(chainstate)) {
256 const CBlockIndex* tip = chainstate->m_chain.Tip();
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")};
264 *chainstate, chainman.
GetConsensus(), chainstate->CoinsDB(),
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
const uint256 & AssumedValidBlock() const
fs::path path
Location in the filesystem where leveldb data will be stored.
static constexpr auto PRUNE_TARGET_MANUAL
std::atomic_bool fReindex
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
Provides an interface for creating and interacting with one or two chainstates: an IBD chainstate gen...
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Fatal error which should not prompt to reindex.
static ChainstateLoadResult CompleteChainstateInitialization(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options) EXCLUSIVE_LOCKS_REQUIRED(
bool require_full_verification
Setting require_full_verification to true will require all checks at check_level (below) to succeed f...
arith_uint256 UintToArith256(const uint256 &a)
bool IsSnapshotActive() const
Chainstate stores and provides an API to update our local knowledge of the current best chain...
bilingual_str _(const char *psz)
Translation function.
kernel::Notifications & GetNotifications() const
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
const Consensus::Params & GetConsensus() const
constexpr bool IsNull() const
Chainstate &InitializeChainstate(CTxMemPool *mempool) EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate.
int64_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
uint256 nMinimumChainWork
The best chain should have at least this much work.
const arith_uint256 & MinimumChainWork() const
#define EXCLUSIVE_LOCKS_REQUIRED(...)
The block chain is a tree shaped structure starting with the genesis block at the root...
std::string GetHex() const
Generic failure which reindexing may fix.
Application-specific storage settings.
std::string GetHex() const
std::tuple< ChainstateLoadStatus, bilingual_str > ChainstateLoadResult
Chainstate load status code and optional error string.
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 ...
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
This sequence can have 4 types of outcomes:
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
int64_t GetTime()
DEPRECATED, see GetTime.
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...
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...
bool IsSnapshotValidated() const EXCLUSIVE_LOCKS_REQUIRED(
Is there a snapshot in use and has it been fully validated?