mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-10 20:03:34 -03:00
refactor: Reduce number of LoadChainstate return values
This commit is contained in:
parent
3b91d4b994
commit
b3e7de7ee6
5 changed files with 65 additions and 137 deletions
|
@ -90,13 +90,13 @@ int main(int argc, char* argv[])
|
||||||
cache_sizes.coins = (450 << 20) - (2 << 20) - (2 << 22);
|
cache_sizes.coins = (450 << 20) - (2 << 20) - (2 << 22);
|
||||||
node::ChainstateLoadOptions options;
|
node::ChainstateLoadOptions options;
|
||||||
options.check_interrupt = [] { return false; };
|
options.check_interrupt = [] { return false; };
|
||||||
auto rv = node::LoadChainstate(chainman, cache_sizes, options);
|
auto [status, error] = node::LoadChainstate(chainman, cache_sizes, options);
|
||||||
if (rv.has_value()) {
|
if (status != node::ChainstateLoadStatus::SUCCESS) {
|
||||||
std::cerr << "Failed to load Chain state from your datadir." << std::endl;
|
std::cerr << "Failed to load Chain state from your datadir." << std::endl;
|
||||||
goto epilogue;
|
goto epilogue;
|
||||||
} else {
|
} else {
|
||||||
auto maybe_verify_error = node::VerifyLoadedChainstate(chainman, options);
|
std::tie(status, error) = node::VerifyLoadedChainstate(chainman, options);
|
||||||
if (maybe_verify_error.has_value()) {
|
if (status != node::ChainstateLoadStatus::SUCCESS) {
|
||||||
std::cerr << "Failed to verify loaded Chain state from your datadir." << std::endl;
|
std::cerr << "Failed to verify loaded Chain state from your datadir." << std::endl;
|
||||||
goto epilogue;
|
goto epilogue;
|
||||||
}
|
}
|
||||||
|
|
83
src/init.cpp
83
src/init.cpp
|
@ -108,8 +108,6 @@ using kernel::DumpMempool;
|
||||||
|
|
||||||
using node::CacheSizes;
|
using node::CacheSizes;
|
||||||
using node::CalculateCacheSizes;
|
using node::CalculateCacheSizes;
|
||||||
using node::ChainstateLoadVerifyError;
|
|
||||||
using node::ChainstateLoadingError;
|
|
||||||
using node::DEFAULT_PERSIST_MEMPOOL;
|
using node::DEFAULT_PERSIST_MEMPOOL;
|
||||||
using node::DEFAULT_PRINTPRIORITY;
|
using node::DEFAULT_PRINTPRIORITY;
|
||||||
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
|
using node::DEFAULT_STOPAFTERBLOCKIMPORT;
|
||||||
|
@ -1452,8 +1450,6 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
|
node.chainman = std::make_unique<ChainstateManager>(chainman_opts);
|
||||||
ChainstateManager& chainman = *node.chainman;
|
ChainstateManager& chainman = *node.chainman;
|
||||||
|
|
||||||
bilingual_str strLoadError;
|
|
||||||
|
|
||||||
node::ChainstateLoadOptions options;
|
node::ChainstateLoadOptions options;
|
||||||
options.mempool = Assert(node.mempool.get());
|
options.mempool = Assert(node.mempool.get());
|
||||||
options.reindex = node::fReindex;
|
options.reindex = node::fReindex;
|
||||||
|
@ -1470,87 +1466,38 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
|
|
||||||
uiInterface.InitMessage(_("Loading block index…").translated);
|
uiInterface.InitMessage(_("Loading block index…").translated);
|
||||||
const int64_t load_block_index_start_time = GetTimeMillis();
|
const int64_t load_block_index_start_time = GetTimeMillis();
|
||||||
std::optional<ChainstateLoadingError> maybe_load_error;
|
auto catch_exceptions = [](auto&& f) {
|
||||||
try {
|
try {
|
||||||
maybe_load_error = LoadChainstate(chainman, cache_sizes, options);
|
return f();
|
||||||
} catch (const std::exception& e) {
|
} catch (const std::exception& e) {
|
||||||
LogPrintf("%s\n", e.what());
|
LogPrintf("%s\n", e.what());
|
||||||
maybe_load_error = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
|
return std::make_tuple(node::ChainstateLoadStatus::FAILURE, _("Error opening block database"));
|
||||||
}
|
}
|
||||||
if (maybe_load_error.has_value()) {
|
};
|
||||||
switch (maybe_load_error.value()) {
|
auto [status, error] = catch_exceptions([&]{ return LoadChainstate(chainman, cache_sizes, options); });
|
||||||
case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
|
if (status == node::ChainstateLoadStatus::SUCCESS) {
|
||||||
strLoadError = _("Error loading block database");
|
|
||||||
break;
|
|
||||||
case ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK:
|
|
||||||
// If the loaded chain has a wrong genesis, bail out immediately
|
|
||||||
// (we're likely using a testnet datadir, or the other way around).
|
|
||||||
return InitError(_("Incorrect or no genesis block found. Wrong datadir for network?"));
|
|
||||||
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:
|
|
||||||
return InitError(_("Unsupported chainstate database format found. "
|
|
||||||
"Please restart with -reindex-chainstate. This will "
|
|
||||||
"rebuild the chainstate database."));
|
|
||||||
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_GENERIC_BLOCKDB_OPEN_FAILED:
|
|
||||||
strLoadError = _("Error opening block database");
|
|
||||||
break;
|
|
||||||
case ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED:
|
|
||||||
strLoadError = strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
|
|
||||||
chainman.GetConsensus().SegwitHeight);
|
|
||||||
break;
|
|
||||||
case ChainstateLoadingError::SHUTDOWN_PROBED:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
std::optional<ChainstateLoadVerifyError> maybe_verify_error;
|
|
||||||
try {
|
|
||||||
uiInterface.InitMessage(_("Verifying blocks…").translated);
|
uiInterface.InitMessage(_("Verifying blocks…").translated);
|
||||||
if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
|
if (chainman.m_blockman.m_have_pruned && options.check_blocks > MIN_BLOCKS_TO_KEEP) {
|
||||||
LogPrintfCategory(BCLog::PRUNE, "pruned datadir may not have more than %d blocks; only checking available blocks\n",
|
LogPrintfCategory(BCLog::PRUNE, "pruned datadir may not have more than %d blocks; only checking available blocks\n",
|
||||||
MIN_BLOCKS_TO_KEEP);
|
MIN_BLOCKS_TO_KEEP);
|
||||||
}
|
}
|
||||||
maybe_verify_error = VerifyLoadedChainstate(chainman, options);
|
std::tie(status, error) = catch_exceptions([&]{ return VerifyLoadedChainstate(chainman, options);});
|
||||||
} catch (const std::exception& e) {
|
if (status == node::ChainstateLoadStatus::SUCCESS) {
|
||||||
LogPrintf("%s\n", e.what());
|
|
||||||
maybe_verify_error = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
|
|
||||||
}
|
|
||||||
if (maybe_verify_error.has_value()) {
|
|
||||||
switch (maybe_verify_error.value()) {
|
|
||||||
case ChainstateLoadVerifyError::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 ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB:
|
|
||||||
strLoadError = _("Corrupted block database detected");
|
|
||||||
break;
|
|
||||||
case ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE:
|
|
||||||
strLoadError = _("Error opening block database");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
fLoaded = true;
|
fLoaded = true;
|
||||||
LogPrintf(" block index %15dms\n", GetTimeMillis() - load_block_index_start_time);
|
LogPrintf(" block index %15dms\n", GetTimeMillis() - load_block_index_start_time);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (status == node::ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB) {
|
||||||
|
return InitError(error);
|
||||||
|
}
|
||||||
|
|
||||||
if (!fLoaded && !ShutdownRequested()) {
|
if (!fLoaded && !ShutdownRequested()) {
|
||||||
// first suggest a reindex
|
// first suggest a reindex
|
||||||
if (!options.reindex) {
|
if (!options.reindex) {
|
||||||
bool fRet = uiInterface.ThreadSafeQuestion(
|
bool fRet = uiInterface.ThreadSafeQuestion(
|
||||||
strLoadError + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
|
error + Untranslated(".\n\n") + _("Do you want to rebuild the block database now?"),
|
||||||
strLoadError.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
|
error.original + ".\nPlease restart with -reindex or -reindex-chainstate to recover.",
|
||||||
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
|
"", CClientUIInterface::MSG_ERROR | CClientUIInterface::BTN_ABORT);
|
||||||
if (fRet) {
|
if (fRet) {
|
||||||
fReindex = true;
|
fReindex = true;
|
||||||
|
@ -1560,7 +1507,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return InitError(strLoadError);
|
return InitError(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace node {
|
namespace node {
|
||||||
std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
|
ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
|
||||||
const ChainstateLoadOptions& options)
|
const ChainstateLoadOptions& options)
|
||||||
{
|
{
|
||||||
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
||||||
|
@ -49,26 +49,28 @@ std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.check_interrupt && options.check_interrupt()) return ChainstateLoadingError::SHUTDOWN_PROBED;
|
if (options.check_interrupt && options.check_interrupt()) return {ChainstateLoadStatus::INTERRUPTED, {}};
|
||||||
|
|
||||||
// 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 global based on the disk flag!
|
// Note that it also sets fReindex global based on the disk flag!
|
||||||
// From here on, fReindex and options.reindex values may be different!
|
// From here on, fReindex and options.reindex values may be different!
|
||||||
if (!chainman.LoadBlockIndex()) {
|
if (!chainman.LoadBlockIndex()) {
|
||||||
if (options.check_interrupt && options.check_interrupt()) return ChainstateLoadingError::SHUTDOWN_PROBED;
|
if (options.check_interrupt && options.check_interrupt()) return {ChainstateLoadStatus::INTERRUPTED, {}};
|
||||||
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
|
return {ChainstateLoadStatus::FAILURE, _("Error loading block database")};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!chainman.BlockIndex().empty() &&
|
if (!chainman.BlockIndex().empty() &&
|
||||||
!chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
|
!chainman.m_blockman.LookupBlockIndex(chainman.GetConsensus().hashGenesisBlock)) {
|
||||||
return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
|
// If the loaded chain has a wrong genesis, bail out immediately
|
||||||
|
// (we're likely using a testnet datadir, or the other way around).
|
||||||
|
return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Incorrect or no genesis block found. Wrong datadir for network?")};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 && !options.prune) {
|
if (chainman.m_blockman.m_have_pruned && !options.prune) {
|
||||||
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
|
return {ChainstateLoadStatus::FAILURE, _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain")};
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point blocktree args are consistent with what's on disk.
|
// At this point blocktree args are consistent with what's on disk.
|
||||||
|
@ -76,7 +78,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman
|
||||||
// (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()) {
|
||||||
return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED;
|
return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -95,12 +97,14 @@ std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman
|
||||||
// Refuse to load unsupported database format.
|
// Refuse to load unsupported 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().NeedsUpgrade()) {
|
if (chainstate->CoinsDB().NeedsUpgrade()) {
|
||||||
return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
|
return {ChainstateLoadStatus::FAILURE_INCOMPATIBLE_DB, _("Unsupported chainstate database format found. "
|
||||||
|
"Please restart with -reindex-chainstate. This will "
|
||||||
|
"rebuild the chainstate database.")};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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()) {
|
||||||
return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
|
return {ChainstateLoadStatus::FAILURE, _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.")};
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -110,7 +114,7 @@ std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman
|
||||||
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()) {
|
||||||
return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
|
return {ChainstateLoadStatus::FAILURE, _("Error initializing block database")};
|
||||||
}
|
}
|
||||||
assert(chainstate->m_chain.Tip() != nullptr);
|
assert(chainstate->m_chain.Tip() != nullptr);
|
||||||
}
|
}
|
||||||
|
@ -120,15 +124,15 @@ std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman
|
||||||
auto chainstates{chainman.GetAll()};
|
auto chainstates{chainman.GetAll()};
|
||||||
if (std::any_of(chainstates.begin(), chainstates.end(),
|
if (std::any_of(chainstates.begin(), chainstates.end(),
|
||||||
[](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
|
[](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
|
||||||
return ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED;
|
return {ChainstateLoadStatus::FAILURE, strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
|
||||||
}
|
chainman.GetConsensus().SegwitHeight)};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::nullopt;
|
return {ChainstateLoadStatus::SUCCESS, {}};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
|
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options)
|
||||||
const ChainstateLoadOptions& options)
|
|
||||||
{
|
{
|
||||||
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
|
||||||
return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull();
|
return options.reindex || options.reindex_chainstate || chainstate->CoinsTip().GetBestBlock().IsNull();
|
||||||
|
@ -140,18 +144,20 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
|
||||||
if (!is_coinsview_empty(chainstate)) {
|
if (!is_coinsview_empty(chainstate)) {
|
||||||
const CBlockIndex* tip = chainstate->m_chain.Tip();
|
const CBlockIndex* tip = chainstate->m_chain.Tip();
|
||||||
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
|
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
|
||||||
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
|
return {ChainstateLoadStatus::FAILURE, _("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")};
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!CVerifyDB().VerifyDB(
|
if (!CVerifyDB().VerifyDB(
|
||||||
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
|
*chainstate, chainman.GetConsensus(), chainstate->CoinsDB(),
|
||||||
options.check_level,
|
options.check_level,
|
||||||
options.check_blocks)) {
|
options.check_blocks)) {
|
||||||
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
|
return {ChainstateLoadStatus::FAILURE, _("Corrupted block database detected")};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return std::nullopt;
|
return {ChainstateLoadStatus::SUCCESS, {}};
|
||||||
}
|
}
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|
|
@ -31,18 +31,14 @@ struct ChainstateLoadOptions {
|
||||||
std::function<void()> coins_error_cb;
|
std::function<void()> coins_error_cb;
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class ChainstateLoadingError {
|
//! Chainstate load status. Simple applications can just check for the success
|
||||||
ERROR_LOADING_BLOCK_DB,
|
//! case, and treat other cases as errors. More complex applications may want to
|
||||||
ERROR_BAD_GENESIS_BLOCK,
|
//! try reindexing in the generic failure case, and pass an interrupt callback
|
||||||
ERROR_PRUNED_NEEDS_REINDEX,
|
//! and exit cleanly in the interrupted case.
|
||||||
ERROR_LOAD_GENESIS_BLOCK_FAILED,
|
enum class ChainstateLoadStatus { SUCCESS, FAILURE, FAILURE_INCOMPATIBLE_DB, INTERRUPTED };
|
||||||
ERROR_CHAINSTATE_UPGRADE_FAILED,
|
|
||||||
ERROR_REPLAYBLOCKS_FAILED,
|
//! Chainstate load status code and optional error string.
|
||||||
ERROR_LOADCHAINTIP_FAILED,
|
using ChainstateLoadResult = std::tuple<ChainstateLoadStatus, bilingual_str>;
|
||||||
ERROR_GENERIC_BLOCKDB_OPEN_FAILED,
|
|
||||||
ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED,
|
|
||||||
SHUTDOWN_PROBED,
|
|
||||||
};
|
|
||||||
|
|
||||||
/** This sequence can have 4 types of outcomes:
|
/** This sequence can have 4 types of outcomes:
|
||||||
*
|
*
|
||||||
|
@ -55,32 +51,11 @@ enum class ChainstateLoadingError {
|
||||||
* 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 std::optional<ChainstateLoadingError>
|
* LoadChainstate returns a (status code, error string) tuple.
|
||||||
* which:
|
|
||||||
*
|
|
||||||
* - if has_value()
|
|
||||||
* - Either "Soft failure", "Hard failure", or "Shutdown requested",
|
|
||||||
* differentiable by the specific enumerator.
|
|
||||||
*
|
|
||||||
* Note that a return value of SHUTDOWN_PROBED means ONLY that "during
|
|
||||||
* this sequence, when we explicitly checked shutdown_requested() at
|
|
||||||
* arbitrary points, one of those calls returned true". Therefore, a
|
|
||||||
* return value other than SHUTDOWN_PROBED does not guarantee that
|
|
||||||
* shutdown hasn't been called indirectly.
|
|
||||||
* - else
|
|
||||||
* - Success!
|
|
||||||
*/
|
*/
|
||||||
std::optional<ChainstateLoadingError> LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
|
ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSizes& cache_sizes,
|
||||||
const ChainstateLoadOptions& options);
|
|
||||||
|
|
||||||
enum class ChainstateLoadVerifyError {
|
|
||||||
ERROR_BLOCK_FROM_FUTURE,
|
|
||||||
ERROR_CORRUPTED_BLOCK_DB,
|
|
||||||
ERROR_GENERIC_FAILURE,
|
|
||||||
};
|
|
||||||
|
|
||||||
std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
|
|
||||||
const ChainstateLoadOptions& options);
|
const ChainstateLoadOptions& options);
|
||||||
|
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager& chainman, const ChainstateLoadOptions& options);
|
||||||
} // namespace node
|
} // namespace node
|
||||||
|
|
||||||
#endif // BITCOIN_NODE_CHAINSTATE_H
|
#endif // BITCOIN_NODE_CHAINSTATE_H
|
||||||
|
|
|
@ -225,11 +225,11 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
|
||||||
options.prune = node::fPruneMode;
|
options.prune = node::fPruneMode;
|
||||||
options.check_blocks = m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
|
options.check_blocks = m_args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
|
||||||
options.check_level = m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
|
options.check_level = m_args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL);
|
||||||
auto maybe_load_error = LoadChainstate(*Assert(m_node.chainman), m_cache_sizes, options);
|
auto [status, error] = LoadChainstate(*Assert(m_node.chainman), m_cache_sizes, options);
|
||||||
assert(!maybe_load_error.has_value());
|
assert(status == node::ChainstateLoadStatus::SUCCESS);
|
||||||
|
|
||||||
auto maybe_verify_error = VerifyLoadedChainstate(*Assert(m_node.chainman), options);
|
std::tie(status, error) = VerifyLoadedChainstate(*Assert(m_node.chainman), options);
|
||||||
assert(!maybe_verify_error.has_value());
|
assert(status == node::ChainstateLoadStatus::SUCCESS);
|
||||||
|
|
||||||
BlockValidationState state;
|
BlockValidationState state;
|
||||||
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {
|
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {
|
||||||
|
|
Loading…
Reference in a new issue