10 #include <validation.h>
13 std::optional<ChainstateLoadingError>
16 bool fReindexChainState, int64_t nBlockTreeDBCache,
17 int64_t nCoinDBCache, int64_t nCoinCacheUsage,
18 bool block_tree_db_in_memory,
bool coins_db_in_memory,
19 std::function<
bool()> shutdown_requested,
20 std::function<
void()> coins_error_cb) {
21 auto is_coinsview_empty =
23 return fReset || fReindexChainState ||
24 chainstate->CoinsTip().GetBestBlock().IsNull();
28 chainman.InitializeChainstate(mempool);
32 auto &pblocktree{chainman.
m_blockman.m_block_tree_db};
37 new CBlockTreeDB(nBlockTreeDBCache, block_tree_db_in_memory, fReset));
40 pblocktree->WriteReindexing(
true);
51 if (!pblocktree->Upgrade(consensus_params)) {
55 if (shutdown_requested && shutdown_requested()) {
64 if (shutdown_requested && shutdown_requested()) {
95 chainstate->InitCoinsDB(
98 fReset || fReindexChainState);
100 if (coins_error_cb) {
101 chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb);
107 if (!chainstate->CoinsDB().Upgrade()) {
113 if (!chainstate->ReplayBlocks()) {
118 chainstate->InitCoinsCache(nCoinCacheUsage);
119 assert(chainstate->CanFlushToDisk());
121 if (!is_coinsview_empty(chainstate)) {
124 if (!chainstate->LoadChainTip()) {
127 assert(chainstate->m_chain.Tip() !=
nullptr);
134 std::optional<ChainstateLoadVerifyError>
136 bool fReindexChainState,
const Config &config,
137 unsigned int check_blocks,
unsigned int check_level,
138 std::function<int64_t()> get_unix_time_seconds) {
139 auto is_coinsview_empty =
141 return fReset || fReindexChainState ||
142 chainstate->CoinsTip().GetBestBlock().IsNull();
148 if (!is_coinsview_empty(chainstate)) {
149 const CBlockIndex *tip = chainstate->m_chain.Tip();
155 if (!
CVerifyDB().VerifyDB(*chainstate, config,
156 chainstate->CoinsDB(), check_level,
RecursiveMutex cs_main
Global state.
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/)
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RAII wrapper for VerifyDB: Verify consistency of the block and coin databases.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
bool LoadGenesisBlock()
Ensures we have a genesis block in the block tree, possibly writing one to disk.
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.
int64_t m_total_coinsdb_cache
The total number of bytes available for us to use across all leveldb coins databases.
node::BlockMap & BlockIndex() EXCLUSIVE_LOCKS_REQUIRED(
Chainstate & ActiveChainstate() const
The most-work chain.
bool LoadBlockIndex() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Load the block tree and coins database from disk, initializing state if we're running with -reindex.
Chainstate &InitializeChainstate(CTxMemPool *mempool, const std::optional< BlockHash > &snapshot_blockhash=std::nullopt) LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(std::vector< Chainstate * GetAll)()
Instantiate a new chainstate and assign it based upon whether it is from a snapshot.
node::BlockManager m_blockman
A single BlockManager instance is shared across each constructed chainstate to avoid duplicating bloc...
CBlockIndex * LookupBlockIndex(const BlockHash &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool m_have_pruned
True if any block files have ever been pruned.
const CBlockIndex *GetFirstStoredBlock(const CBlockIndex *start_block) EXCLUSIVE_LOCKS_REQUIRED(voi CleanupBlockRevFiles)()
Find the first block that is not pruned.
@ ERROR_BLOCK_FROM_FUTURE
@ ERROR_CORRUPTED_BLOCK_DB
std::optional< ChainstateLoadVerifyError > VerifyLoadedChainstate(ChainstateManager &chainman, bool fReset, bool fReindexChainState, const Config &config, unsigned int check_blocks, unsigned int check_level, std::function< int64_t()> get_unix_time_seconds)
std::optional< ChainstateLoadingError > LoadChainstate(bool fReset, ChainstateManager &chainman, CTxMemPool *mempool, bool fPruneMode_, const Consensus::Params &consensus_params, bool fReindexChainState, int64_t nBlockTreeDBCache, int64_t nCoinDBCache, int64_t nCoinCacheUsage, bool block_tree_db_in_memory, bool coins_db_in_memory, std::function< bool()> shutdown_requested, std::function< void()> coins_error_cb)
This sequence can have 4 types of outcomes:
std::atomic_bool fReindex
@ ERROR_CHAINSTATE_UPGRADE_FAILED
@ ERROR_UPGRADING_BLOCK_DB
@ ERROR_REPLAYBLOCKS_FAILED
@ ERROR_BAD_GENESIS_BLOCK
@ ERROR_PRUNED_NEEDS_REINDEX
@ ERROR_LOADCHAINTIP_FAILED
@ ERROR_LOAD_GENESIS_BLOCK_FAILED
Parameters that influence chain consensus.
BlockHash hashGenesisBlock
#define EXCLUSIVE_LOCKS_REQUIRED(...)