node/chainstate: Decouple from stringy errors

This commit is contained in:
Kittywhiskers Van Gogh 2024-10-03 08:58:02 +00:00
parent 9ab08c42e4
commit d7419e42d6
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
3 changed files with 152 additions and 109 deletions

View File

@ -1864,11 +1864,8 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
bilingual_str strLoadError; bilingual_str strLoadError;
uiInterface.InitMessage(_("Loading block index…").translated); uiInterface.InitMessage(_("Loading block index…").translated);
const auto load_block_index_start_time{SteadyClock::now()}; const auto load_block_index_start_time{SteadyClock::now()};
bool rv = LoadChainstate(fLoaded, auto rv = LoadChainstate(fReset,
strLoadError,
fReset,
chainman, chainman,
node, node,
fPruneMode, fPruneMode,
@ -1879,8 +1876,69 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
nBlockTreeDBCache, nBlockTreeDBCache,
nCoinDBCache, nCoinDBCache,
nCoinCacheUsage); nCoinCacheUsage);
if (!rv) return false; if (rv.has_value()) {
if (fLoaded) { switch (rv.value()) {
case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
strLoadError = _("Error loading block database");
break;
case ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK:
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
case ChainstateLoadingError::ERROR_BAD_DEVNET_GENESIS_BLOCK:
return InitError(_("Incorrect or no devnet genesis block found. Wrong datadir for devnet specified?"));
case ChainstateLoadingError::ERROR_TXINDEX_DISABLED_WHEN_GOV_ENABLED:
return InitError(_("Transaction index can't be disabled with governance validation enabled. Either start with -disablegovernance command line switch or enable transaction index."));
case ChainstateLoadingError::ERROR_ADDRIDX_NEEDS_REINDEX:
strLoadError = _("You need to rebuild the database using -reindex to enable -addressindex");
break;
case ChainstateLoadingError::ERROR_SPENTIDX_NEEDS_REINDEX:
strLoadError = _("You need to rebuild the database using -reindex to enable -spentindex");
break;
case ChainstateLoadingError::ERROR_TIMEIDX_NEEDS_REINDEX:
strLoadError = _("You need to rebuild the database using -reindex to enable -timestampindex");
break;
case ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX:
strLoadError = _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain");
break;
case ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED:
strLoadError = _("Error initializing block database");
break;
case ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED:
strLoadError = _("Error upgrading chainstate database");
break;
case ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED:
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
break;
case ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED:
strLoadError = _("Error initializing block database");
break;
case ChainstateLoadingError::ERROR_EVO_DB_SANITY_FAILED:
strLoadError = _("Error initializing block database");
break;
case ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED:
strLoadError = _("Error opening block database");
break;
case ChainstateLoadingError::ERROR_BLOCK_FROM_FUTURE:
strLoadError = _("The block database contains a block which appears to be from the future. "
"This may be due to your computer's date and time being set incorrectly. "
"Only rebuild the block database if you are sure that your computer's date and time are correct");
break;
case ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB:
strLoadError = _("Corrupted block database detected");
break;
case ChainstateLoadingError::ERROR_COMMITING_EVO_DB:
strLoadError = _("Failed to commit Evo database");
break;
case ChainstateLoadingError::ERROR_UPGRADING_EVO_DB:
strLoadError = _("Error upgrading Evo database");
break;
case ChainstateLoadingError::ERROR_UPGRADING_SIGNALS_DB:
strLoadError = _("Error upgrading evo database for EHF");
break;
case ChainstateLoadingError::SHUTDOWN_PROBED:
break;
}
} else {
fLoaded = true;
LogPrintf(" block index %15dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - load_block_index_start_time)); LogPrintf(" block index %15dms\n", Ticks<std::chrono::milliseconds>(SteadyClock::now() - load_block_index_start_time));
} }

View File

@ -8,7 +8,6 @@
#include <deploymentstatus.h> // for DeploymentActiveAfter #include <deploymentstatus.h> // for DeploymentActiveAfter
#include <rpc/blockchain.h> // for RPCNotifyBlockChange #include <rpc/blockchain.h> // for RPCNotifyBlockChange
#include <util/time.h> // for GetTime #include <util/time.h> // for GetTime
#include <util/translation.h> // for bilingual_str
#include <node/blockstorage.h> // for CleanupBlockRevFiles, fHavePruned, fReindex #include <node/blockstorage.h> // for CleanupBlockRevFiles, fHavePruned, fReindex
#include <node/context.h> // for NodeContext #include <node/context.h> // for NodeContext
#include <node/ui_interface.h> // for InitError, uiInterface, and CClientUIInterface member access #include <node/ui_interface.h> // for InitError, uiInterface, and CClientUIInterface member access
@ -25,19 +24,18 @@
#include <llmq/instantsend.h> // for llmq::quorumInstantSendManager #include <llmq/instantsend.h> // for llmq::quorumInstantSendManager
#include <llmq/snapshot.h> // for llmq::quorumSnapshotManager #include <llmq/snapshot.h> // for llmq::quorumSnapshotManager
bool LoadChainstate(bool& fLoaded, std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
bilingual_str& strLoadError, ChainstateManager& chainman,
bool fReset, NodeContext& node,
ChainstateManager& chainman, bool fPruneMode,
NodeContext& node, bool is_governance_enabled,
bool fPruneMode, const CChainParams& chainparams,
bool is_governance_enabled, const ArgsManager& args,
const CChainParams& chainparams, bool fReindexChainState,
const ArgsManager& args, int64_t nBlockTreeDBCache,
bool fReindexChainState, int64_t nCoinDBCache,
int64_t nBlockTreeDBCache, int64_t nCoinCacheUsage)
int64_t nCoinDBCache, {
int64_t nCoinCacheUsage) {
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
}; };
@ -95,51 +93,49 @@ bool LoadChainstate(bool& fLoaded,
CleanupBlockRevFiles(); CleanupBlockRevFiles();
} }
if (ShutdownRequested()) break; if (ShutdownRequested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
// LoadBlockIndex will load m_have_pruned if we've ever removed a // LoadBlockIndex will load m_have_pruned if we've ever removed a
// block file from disk. // block file from disk.
// Note that it also sets fReindex based on the disk flag! // Note that it also sets fReindex based on the disk flag!
// From here on out fReindex and fReset mean something different! // From here on out fReindex and fReset mean something different!
if (!chainman.LoadBlockIndex()) { if (!chainman.LoadBlockIndex()) {
if (ShutdownRequested()) break; if (ShutdownRequested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
strLoadError = _("Error loading block database"); return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
break;
} }
if (is_governance_enabled && !args.GetBoolArg("-txindex", DEFAULT_TXINDEX) && chainparams.NetworkIDString() != CBaseChainParams::REGTEST) { // TODO remove this when pruning is fixed. See https://github.com/dashpay/dash/pull/1817 and https://github.com/dashpay/dash/pull/1743 // TODO: Remove this when pruning is fixed.
return InitError(_("Transaction index can't be disabled with governance validation enabled. Either start with -disablegovernance command line switch or enable transaction index.")); // See https://github.com/dashpay/dash/pull/1817 and https://github.com/dashpay/dash/pull/1743
if (is_governance_enabled && !args.GetBoolArg("-txindex", DEFAULT_TXINDEX) && chainparams.NetworkIDString() != CBaseChainParams::REGTEST) {
return ChainstateLoadingError::ERROR_TXINDEX_DISABLED_WHEN_GOV_ENABLED;
} }
// If the loaded chain has a wrong genesis, bail out immediately // If the loaded chain has a wrong genesis, bail out immediately
// (we're likely using a testnet datadir, or the other way around). // (we're likely using a testnet datadir, or the other way around).
if (!chainman.BlockIndex().empty() && if (!chainman.BlockIndex().empty() &&
!chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) { !chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?")); return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
} }
if (!chainparams.GetConsensus().hashDevnetGenesisBlock.IsNull() && !chainman.BlockIndex().empty() && if (!chainparams.GetConsensus().hashDevnetGenesisBlock.IsNull() && !chainman.BlockIndex().empty() &&
!chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashDevnetGenesisBlock)) { !chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashDevnetGenesisBlock)) {
return InitError(_("Incorrect or no devnet genesis block found. Wrong datadir for devnet specified?")); return ChainstateLoadingError::ERROR_BAD_DEVNET_GENESIS_BLOCK;
} }
if (!fReset && !fReindexChainState) { if (!fReset && !fReindexChainState) {
// Check for changed -addressindex state // Check for changed -addressindex state
if (!fAddressIndex && fAddressIndex != args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX)) { if (!fAddressIndex && fAddressIndex != args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex to enable -addressindex"); return ChainstateLoadingError::ERROR_ADDRIDX_NEEDS_REINDEX;
break;
} }
// Check for changed -timestampindex state // Check for changed -timestampindex state
if (!fTimestampIndex && fTimestampIndex != args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX)) { if (!fTimestampIndex && fTimestampIndex != args.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex to enable -timestampindex"); return ChainstateLoadingError::ERROR_TIMEIDX_NEEDS_REINDEX;
break;
} }
// Check for changed -spentindex state // Check for changed -spentindex state
if (!fSpentIndex && fSpentIndex != args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)) { if (!fSpentIndex && fSpentIndex != args.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX)) {
strLoadError = _("You need to rebuild the database using -reindex to enable -spentindex"); return ChainstateLoadingError::ERROR_SPENTIDX_NEEDS_REINDEX;
break;
} }
} }
@ -152,8 +148,7 @@ bool LoadChainstate(bool& fLoaded,
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks // 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. // in the past, but is now trying to run unpruned.
if (chainman.m_blockman.m_have_pruned && !fPruneMode) { if (chainman.m_blockman.m_have_pruned && !fPruneMode) {
strLoadError = _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain"); return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
break;
} }
// At this point blocktree args are consistent with what's on disk. // At this point blocktree args are consistent with what's on disk.
@ -161,15 +156,12 @@ bool LoadChainstate(bool& fLoaded,
// (otherwise we use the one already on disk). // (otherwise we use the one already on disk).
// This is called again in ThreadImport after the reindex completes. // This is called again in ThreadImport after the reindex completes.
if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) { if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
strLoadError = _("Error initializing block database"); return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED;
break;
} }
// At this point we're either in reindex or we've loaded a useful // At this point we're either in reindex or we've loaded a useful
// block tree into BlockIndex()! // block tree into BlockIndex()!
bool failed_chainstate_init = false;
for (CChainState* chainstate : chainman.GetAll()) { for (CChainState* chainstate : chainman.GetAll()) {
chainstate->InitCoinsDB( chainstate->InitCoinsDB(
/* cache_size_bytes */ nCoinDBCache, /* cache_size_bytes */ nCoinDBCache,
@ -185,16 +177,12 @@ bool LoadChainstate(bool& fLoaded,
// If necessary, upgrade from older database format. // If necessary, upgrade from older database format.
// This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
if (!chainstate->CoinsDB().Upgrade()) { if (!chainstate->CoinsDB().Upgrade()) {
strLoadError = _("Error upgrading chainstate database"); return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
failed_chainstate_init = true;
break;
} }
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
if (!chainstate->ReplayBlocks()) { if (!chainstate->ReplayBlocks()) {
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate."); return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
failed_chainstate_init = true;
break;
} }
// The on-disk coinsdb is now in a good state, create the cache // The on-disk coinsdb is now in a good state, create the cache
@ -206,46 +194,29 @@ bool LoadChainstate(bool& fLoaded,
// (for multiple chainstates to actually work in parallel) // (for multiple chainstates to actually work in parallel)
// and not a global // and not a global
if (&chainman.ActiveChainstate() == chainstate && !node.evodb->CommitRootTransaction()) { if (&chainman.ActiveChainstate() == chainstate && !node.evodb->CommitRootTransaction()) {
strLoadError = _("Failed to commit EvoDB"); return ChainstateLoadingError::ERROR_COMMITING_EVO_DB;
failed_chainstate_init = true;
break;
} }
if (!is_coinsview_empty(chainstate)) { if (!is_coinsview_empty(chainstate)) {
// LoadChainTip initializes the chain based on CoinsTip()'s best block // LoadChainTip initializes the chain based on CoinsTip()'s best block
if (!chainstate->LoadChainTip()) { if (!chainstate->LoadChainTip()) {
strLoadError = _("Error initializing block database"); return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
failed_chainstate_init = true;
break; // out of the per-chainstate loop
} }
assert(chainstate->m_chain.Tip() != nullptr); assert(chainstate->m_chain.Tip() != nullptr);
} }
} }
if (failed_chainstate_init) { if (!node.dmnman->MigrateDBIfNeeded() || !node.dmnman->MigrateDBIfNeeded2()) {
break; // out of the chainstate activation do-while return ChainstateLoadingError::ERROR_UPGRADING_EVO_DB;
}
if (!node.dmnman->MigrateDBIfNeeded()) {
strLoadError = _("Error upgrading evo database");
break;
}
if (!node.dmnman->MigrateDBIfNeeded2()) {
strLoadError = _("Error upgrading evo database");
break;
} }
if (!node.mnhf_manager->ForceSignalDBUpdate()) { if (!node.mnhf_manager->ForceSignalDBUpdate()) {
strLoadError = _("Error upgrading evo database for EHF"); return ChainstateLoadingError::ERROR_UPGRADING_SIGNALS_DB;
break;
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogPrintf("%s\n", e.what()); LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database"); return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
break;
} }
bool failed_verification = false;
try { try {
LOCK(cs_main); LOCK(cs_main);
@ -260,11 +231,7 @@ bool LoadChainstate(bool& fLoaded,
const CBlockIndex* tip = chainstate->m_chain.Tip(); const CBlockIndex* tip = chainstate->m_chain.Tip();
RPCNotifyBlockChange(tip); RPCNotifyBlockChange(tip);
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) { if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
strLoadError = _("The block database contains a block which appears to be from the future. " return ChainstateLoadingError::ERROR_BLOCK_FROM_FUTURE;
"This may be due to your computer's date and time being set incorrectly. "
"Only rebuild the block database if you are sure that your computer's date and time are correct");
failed_verification = true;
break;
} }
const bool v19active{DeploymentActiveAfter(tip, chainparams.GetConsensus(), Consensus::DEPLOYMENT_V19)}; const bool v19active{DeploymentActiveAfter(tip, chainparams.GetConsensus(), Consensus::DEPLOYMENT_V19)};
if (v19active) { if (v19active) {
@ -277,9 +244,7 @@ bool LoadChainstate(bool& fLoaded,
*node.evodb, *node.evodb,
args.GetArg("-checklevel", DEFAULT_CHECKLEVEL), args.GetArg("-checklevel", DEFAULT_CHECKLEVEL),
args.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) { args.GetArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
strLoadError = _("Corrupted block database detected"); return ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB;
failed_verification = true;
break;
} }
// VerifyDB() disconnects blocks which might result in us switching back to legacy. // VerifyDB() disconnects blocks which might result in us switching back to legacy.
@ -299,22 +264,14 @@ bool LoadChainstate(bool& fLoaded,
// and not a global // and not a global
if (&chainman.ActiveChainstate() == chainstate && !node.evodb->IsEmpty()) { if (&chainman.ActiveChainstate() == chainstate && !node.evodb->IsEmpty()) {
// EvoDB processed some blocks earlier but we have no blocks anymore, something is wrong // EvoDB processed some blocks earlier but we have no blocks anymore, something is wrong
strLoadError = _("Error initializing block database"); return ChainstateLoadingError::ERROR_EVO_DB_SANITY_FAILED;
failed_verification = true;
break;
} }
} }
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
LogPrintf("%s\n", e.what()); LogPrintf("%s\n", e.what());
strLoadError = _("Error opening block database"); return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
failed_verification = true;
break;
}
if (!failed_verification) {
fLoaded = true;
} }
} while(false); } while(false);
return true; return std::nullopt;
} }

View File

@ -6,13 +6,36 @@
#define BITCOIN_NODE_CHAINSTATE_H #define BITCOIN_NODE_CHAINSTATE_H
#include <cstdint> // for int64_t #include <cstdint> // for int64_t
#include <optional> // for std::optional
class ArgsManager; class ArgsManager;
struct bilingual_str;
class CChainParams; class CChainParams;
class ChainstateManager; class ChainstateManager;
struct NodeContext; struct NodeContext;
enum class ChainstateLoadingError {
ERROR_LOADING_BLOCK_DB,
ERROR_BAD_GENESIS_BLOCK,
ERROR_BAD_DEVNET_GENESIS_BLOCK,
ERROR_TXINDEX_DISABLED_WHEN_GOV_ENABLED,
ERROR_ADDRIDX_NEEDS_REINDEX,
ERROR_SPENTIDX_NEEDS_REINDEX,
ERROR_TIMEIDX_NEEDS_REINDEX,
ERROR_PRUNED_NEEDS_REINDEX,
ERROR_LOAD_GENESIS_BLOCK_FAILED,
ERROR_CHAINSTATE_UPGRADE_FAILED,
ERROR_REPLAYBLOCKS_FAILED,
ERROR_LOADCHAINTIP_FAILED,
ERROR_EVO_DB_SANITY_FAILED,
ERROR_GENERIC_BLOCKDB_OPEN_FAILED,
ERROR_BLOCK_FROM_FUTURE,
ERROR_CORRUPTED_BLOCK_DB,
ERROR_COMMITING_EVO_DB,
ERROR_UPGRADING_EVO_DB,
ERROR_UPGRADING_SIGNALS_DB,
SHUTDOWN_PROBED,
};
/** This sequence can have 4 types of outcomes: /** This sequence can have 4 types of outcomes:
* *
* 1. Success * 1. Success
@ -24,26 +47,31 @@ struct NodeContext;
* 4. Hard failure * 4. Hard failure
* - a failure that definitively cannot be recovered from with a reindex * - a failure that definitively cannot be recovered from with a reindex
* *
* Currently, LoadChainstate returns a bool which: * Currently, LoadChainstate returns a std::optional<ChainstateLoadingError>
* - if false * which:
* - Definitely a "Hard failure" *
* - if true * - if has_value()
* - if fLoaded -> "Success" * - Either "Soft failure", "Hard failure", or "Shutdown requested",
* - if ShutdownRequested() -> "Shutdown requested" * differentiable by the specific enumerator.
* - else -> "Soft failure" *
* Note that a return value of SHUTDOWN_PROBED means ONLY that "during
* this sequence, when we explicitly checked ShutdownRequested() at
* arbitrary points, one of those calls returned true". Therefore, a
* return value other than SHUTDOWN_PROBED does not guarantee that
* ShutdownRequested() hasn't been called indirectly.
* - else
* - Success!
*/ */
bool LoadChainstate(bool& fLoaded, std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
bilingual_str& strLoadError, ChainstateManager& chainman,
bool fReset, NodeContext& node,
ChainstateManager& chainman, bool fPruneMode,
NodeContext& node, bool is_governance_enabled,
bool fPruneMode, const CChainParams& chainparams,
bool is_governance_enabled, const ArgsManager& args,
const CChainParams& chainparams, bool fReindexChainState,
const ArgsManager& args, int64_t nBlockTreeDBCache,
bool fReindexChainState, int64_t nCoinDBCache,
int64_t nBlockTreeDBCache, int64_t nCoinCacheUsage);
int64_t nCoinDBCache,
int64_t nCoinCacheUsage);
#endif // BITCOIN_NODE_CHAINSTATE_H #endif // BITCOIN_NODE_CHAINSTATE_H