22#include <validation.h>
42 if (!chainman.LoadBlockIndex()) {
47 if (!chainman.BlockIndex().empty() &&
48 !chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
56 if (chainman.m_blockman.m_have_pruned && !options.prune) {
57 return {
ChainstateLoadStatus::FAILURE,
_(
"You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain")};
64 if (chainman.m_blockman.m_blockfiles_indexed && !chainman.ActiveChainstate().LoadGenesisBlock()) {
69 return options.wipe_chainstate_db || chainstate.CoinsTip().GetBestBlock().IsNull();
72 assert(chainman.m_total_coinstip_cache > 0);
73 assert(chainman.m_total_coinsdb_cache > 0);
80 double init_cache_fraction = chainman.HistoricalChainstate() ? 0.2 : 1.0;
85 for (
const auto& chainstate : chainman.m_chainstates) {
86 LogInfo(
"Initializing chainstate %s", chainstate->ToString());
89 chainstate->InitCoinsDB(
90 chainman.m_total_coinsdb_cache * init_cache_fraction,
91 options.coins_db_in_memory,
92 options.wipe_chainstate_db);
98 if (options.coins_error_cb) {
99 chainstate->CoinsErrorCatcher().AddReadErrCallback(options.coins_error_cb);
104 if (chainstate->CoinsDB().NeedsUpgrade()) {
106 "Please restart with -reindex-chainstate. This will "
107 "rebuild the chainstate database.")};
111 if (!chainstate->ReplayBlocks()) {
116 chainstate->InitCoinsCache(chainman.m_total_coinstip_cache * init_cache_fraction);
117 assert(chainstate->CanFlushToDisk());
119 if (!is_coinsview_empty(*chainstate)) {
121 if (!chainstate->LoadChainTip()) {
124 assert(chainstate->m_chain.Tip() !=
nullptr);
131 for (
const auto& chainstate : chainman.m_chainstates) {
132 chainstate->PopulateBlockIndexCandidates();
135 const auto& chainstates{chainman.m_chainstates};
136 if (std::any_of(chainstates.begin(), chainstates.end(),
139 chainman.GetConsensus().SegwitHeight)};
145 chainman.MaybeRebalanceCaches();
156 LogInfo(
"Validating signatures for all blocks.");
163 LogInfo(
"Block pruning enabled. Use RPC call pruneblockchain(height) to manually prune block and undo files.");
165 LogInfo(
"Prune configured to target %u MiB on disk for block and undo files.",
178 Chainstate* assumeutxo_cs{chainman.LoadAssumeutxoChainstate()};
182 validated_cs.SetTargetBlock(
nullptr);
183 LogInfo(
"[snapshot] deleting snapshot chainstate due to reindexing");
184 if (!chainman.DeleteChainstate(*assumeutxo_cs)) {
187 assumeutxo_cs =
nullptr;
192 return {init_status, init_error};
203 auto snapshot_completion{assumeutxo_cs
204 ? chainman.MaybeValidateSnapshot(validated_cs, *assumeutxo_cs)
210 LogInfo(
"[snapshot] cleaning up unneeded background chainstate, then reinitializing");
211 if (!chainman.ValidatedSnapshotCleanup(validated_cs, *assumeutxo_cs)) {
218 assert(chainman.m_chainstates.empty());
220 chainman.InitializeChainstate(options.
mempool);
228 return {init_status, init_error};
232 "UTXO snapshot failed to validate. "
233 "Restart to resume normal initial block download, or try loading a different snapshot.")};
247 for (
auto& chainstate : chainman.m_chainstates) {
248 if (!is_coinsview_empty(*chainstate)) {
249 const CBlockIndex* tip = chainstate->m_chain.Tip();
252 "This may be due to your computer's date and time being set incorrectly. "
253 "Only rebuild the block database if you are sure that your computer's date and time are correct")};
257 *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.
Interface for managing multiple Chainstate objects, where each chainstate is associated with chainsta...
const uint256 & AssumedValidBlock() const
size_t m_total_coinstip_cache
The total number of bytes available for us to use across all in-memory coins caches.
kernel::Notifications & GetNotifications() const
Chainstate & ActiveChainstate() const
Alternatives to CurrentChainstate() used by older code to query latest chainstate information without...
size_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
const Consensus::Params & GetConsensus() const
const arith_uint256 & MinimumChainWork() const
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.
static ChainstateLoadResult CompleteChainstateInitialization(ChainstateManager &chainman, const ChainstateLoadOptions &options) EXCLUSIVE_LOCKS_REQUIRED(
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
uint256 nMinimumChainWork
The best chain should have at least this much work.
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(...)
consteval auto _(util::TranslatedLiteral str)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.