2021-09-20 17:12:18 -03:00
|
|
|
// Copyright (c) 2021 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <node/chainstate.h>
|
|
|
|
|
2021-09-20 15:02:07 -03:00
|
|
|
#include <consensus/params.h> // for Consensus::Params
|
2021-09-20 17:12:18 -03:00
|
|
|
#include <node/blockstorage.h> // for CleanupBlockRevFiles, fHavePruned, fReindex
|
|
|
|
#include <validation.h> // for a lot of things
|
|
|
|
|
2021-08-17 18:07:14 -04:00
|
|
|
std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
|
|
|
|
ChainstateManager& chainman,
|
2021-09-20 17:44:21 -03:00
|
|
|
CTxMemPool* mempool,
|
2021-08-17 18:07:14 -04:00
|
|
|
bool fPruneMode,
|
2021-09-20 15:02:07 -03:00
|
|
|
const Consensus::Params& consensus_params,
|
2021-08-17 18:07:14 -04:00
|
|
|
bool fReindexChainState,
|
|
|
|
int64_t nBlockTreeDBCache,
|
|
|
|
int64_t nCoinDBCache,
|
2021-08-18 13:39:34 -04:00
|
|
|
int64_t nCoinCacheUsage,
|
2021-09-22 16:36:24 -03:00
|
|
|
std::function<bool()> shutdown_requested,
|
2021-08-18 13:39:34 -04:00
|
|
|
std::function<void()> coins_error_cb)
|
2021-08-17 18:07:14 -04:00
|
|
|
{
|
2021-09-20 17:12:18 -03:00
|
|
|
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
|
|
|
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
|
|
|
|
};
|
|
|
|
|
2021-08-18 13:54:08 -04:00
|
|
|
{
|
2021-09-20 18:46:06 -03:00
|
|
|
LOCK(cs_main);
|
|
|
|
chainman.InitializeChainstate(mempool);
|
|
|
|
chainman.m_total_coinstip_cache = nCoinCacheUsage;
|
|
|
|
chainman.m_total_coinsdb_cache = nCoinDBCache;
|
|
|
|
|
|
|
|
UnloadBlockIndex(mempool, chainman);
|
|
|
|
|
|
|
|
auto& pblocktree{chainman.m_blockman.m_block_tree_db};
|
|
|
|
// new CBlockTreeDB tries to delete the existing file, which
|
|
|
|
// fails if it's still open from the previous loop. Close it first:
|
|
|
|
pblocktree.reset();
|
|
|
|
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset));
|
|
|
|
|
|
|
|
if (fReset) {
|
|
|
|
pblocktree->WriteReindexing(true);
|
|
|
|
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files
|
|
|
|
if (fPruneMode)
|
|
|
|
CleanupBlockRevFiles();
|
|
|
|
}
|
|
|
|
|
2021-09-22 16:36:24 -03:00
|
|
|
if (shutdown_requested && shutdown_requested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
// LoadBlockIndex will load fHavePruned if we've ever removed a
|
|
|
|
// block file from disk.
|
|
|
|
// Note that it also sets fReindex based on the disk flag!
|
|
|
|
// From here on out fReindex and fReset mean something different!
|
|
|
|
if (!chainman.LoadBlockIndex()) {
|
2021-09-22 16:36:24 -03:00
|
|
|
if (shutdown_requested && shutdown_requested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
|
2021-09-20 18:46:06 -03:00
|
|
|
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
|
|
|
|
}
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
if (!chainman.BlockIndex().empty() &&
|
2021-09-20 15:02:07 -03:00
|
|
|
!chainman.m_blockman.LookupBlockIndex(consensus_params.hashGenesisBlock)) {
|
2021-09-20 18:46:06 -03:00
|
|
|
return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
|
|
|
|
}
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks
|
|
|
|
// in the past, but is now trying to run unpruned.
|
|
|
|
if (fHavePruned && !fPruneMode) {
|
|
|
|
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point blocktree args are consistent with what's on disk.
|
|
|
|
// If we're not mid-reindex (based on disk + args), add a genesis block on disk
|
|
|
|
// (otherwise we use the one already on disk).
|
|
|
|
// This is called again in ThreadImport after the reindex completes.
|
|
|
|
if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
|
|
|
|
return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED;
|
|
|
|
}
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
// At this point we're either in reindex or we've loaded a useful
|
|
|
|
// block tree into BlockIndex()!
|
|
|
|
|
|
|
|
for (CChainState* chainstate : chainman.GetAll()) {
|
|
|
|
chainstate->InitCoinsDB(
|
|
|
|
/* cache_size_bytes */ nCoinDBCache,
|
|
|
|
/* in_memory */ false,
|
|
|
|
/* should_wipe */ fReset || fReindexChainState);
|
|
|
|
|
2021-08-18 13:39:34 -04:00
|
|
|
if (coins_error_cb) {
|
|
|
|
chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb);
|
|
|
|
}
|
2021-09-20 18:46:06 -03:00
|
|
|
|
|
|
|
// If necessary, upgrade from older database format.
|
|
|
|
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
|
|
|
|
if (!chainstate->CoinsDB().Upgrade()) {
|
|
|
|
return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
|
2021-09-20 17:12:18 -03:00
|
|
|
}
|
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
|
|
|
|
if (!chainstate->ReplayBlocks()) {
|
|
|
|
return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
|
2021-09-20 17:12:18 -03:00
|
|
|
}
|
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
// The on-disk coinsdb is now in a good state, create the cache
|
|
|
|
chainstate->InitCoinsCache(nCoinCacheUsage);
|
|
|
|
assert(chainstate->CanFlushToDisk());
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
if (!is_coinsview_empty(chainstate)) {
|
|
|
|
// LoadChainTip initializes the chain based on CoinsTip()'s best block
|
|
|
|
if (!chainstate->LoadChainTip()) {
|
|
|
|
return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
|
2021-09-20 17:12:18 -03:00
|
|
|
}
|
2021-09-20 18:46:06 -03:00
|
|
|
assert(chainstate->m_chain.Tip() != nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fReset) {
|
|
|
|
LOCK(cs_main);
|
|
|
|
auto chainstates{chainman.GetAll()};
|
|
|
|
if (std::any_of(chainstates.begin(), chainstates.end(),
|
|
|
|
[](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
|
|
|
|
return ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED;
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-11-10 17:57:14 -03:00
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
|
|
|
|
bool fReset,
|
|
|
|
bool fReindexChainState,
|
2021-09-20 15:02:07 -03:00
|
|
|
const Consensus::Params& consensus_params,
|
2021-11-10 17:57:14 -03:00
|
|
|
unsigned int check_blocks,
|
2021-09-22 16:36:10 -03:00
|
|
|
unsigned int check_level,
|
|
|
|
std::function<int64_t()> get_unix_time_seconds)
|
2021-11-10 17:57:14 -03:00
|
|
|
{
|
|
|
|
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
|
|
|
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
|
|
|
|
};
|
|
|
|
|
2021-08-18 13:54:08 -04:00
|
|
|
{
|
2021-09-20 18:46:06 -03:00
|
|
|
LOCK(cs_main);
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
for (CChainState* chainstate : chainman.GetAll()) {
|
|
|
|
if (!is_coinsview_empty(chainstate)) {
|
|
|
|
const CBlockIndex* tip = chainstate->m_chain.Tip();
|
2021-09-22 16:36:10 -03:00
|
|
|
if (tip && tip->nTime > get_unix_time_seconds() + 2 * 60 * 60) {
|
2021-11-10 17:57:14 -03:00
|
|
|
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
|
2021-09-20 18:46:06 -03:00
|
|
|
}
|
2021-09-20 17:12:18 -03:00
|
|
|
|
2021-09-20 18:46:06 -03:00
|
|
|
if (!CVerifyDB().VerifyDB(
|
2021-09-20 15:02:07 -03:00
|
|
|
*chainstate, consensus_params, chainstate->CoinsDB(),
|
2021-09-20 18:46:06 -03:00
|
|
|
check_level,
|
|
|
|
check_blocks)) {
|
2021-11-10 17:57:14 -03:00
|
|
|
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
|
2021-09-20 17:12:18 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-09-20 18:46:06 -03:00
|
|
|
}
|
|
|
|
|
2021-08-17 18:07:14 -04:00
|
|
|
return std::nullopt;
|
2021-09-20 17:12:18 -03:00
|
|
|
}
|