node/chainstate: Remove do/while loop

I strongly recommend reviewing with the following git-diff flags:
  --ignore-space-change
pull/826/head
Carl Dong 3 years ago
parent 975235ca0a
commit adf4912d77

@ -28,139 +28,138 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
}; };
do { try {
try { LOCK(cs_main);
LOCK(cs_main); chainman.InitializeChainstate(mempool);
chainman.InitializeChainstate(mempool); chainman.m_total_coinstip_cache = nCoinCacheUsage;
chainman.m_total_coinstip_cache = nCoinCacheUsage; chainman.m_total_coinsdb_cache = nCoinDBCache;
chainman.m_total_coinsdb_cache = nCoinDBCache;
UnloadBlockIndex(mempool, chainman);
UnloadBlockIndex(mempool, chainman);
auto& pblocktree{chainman.m_blockman.m_block_tree_db};
auto& pblocktree{chainman.m_blockman.m_block_tree_db}; // new CBlockTreeDB tries to delete the existing file, which
// new CBlockTreeDB tries to delete the existing file, which // fails if it's still open from the previous loop. Close it first:
// fails if it's still open from the previous loop. Close it first: pblocktree.reset();
pblocktree.reset(); pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset));
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset));
if (fReset) {
if (fReset) { pblocktree->WriteReindexing(true);
pblocktree->WriteReindexing(true); //If we're reindexing in prune mode, wipe away unusable block files and all undo data files
//If we're reindexing in prune mode, wipe away unusable block files and all undo data files if (fPruneMode)
if (fPruneMode) CleanupBlockRevFiles();
CleanupBlockRevFiles(); }
}
if (ShutdownRequested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
// 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()) {
if (ShutdownRequested()) return ChainstateLoadingError::SHUTDOWN_PROBED; if (ShutdownRequested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
}
// LoadBlockIndex will load fHavePruned if we've ever removed a if (!chainman.BlockIndex().empty() &&
// block file from disk. !chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
// Note that it also sets fReindex based on the disk flag! return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
// From here on out fReindex and fReset mean something different! }
if (!chainman.LoadBlockIndex()) {
if (ShutdownRequested()) return ChainstateLoadingError::SHUTDOWN_PROBED;
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
}
if (!chainman.BlockIndex().empty() && // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
!chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) { // in the past, but is now trying to run unpruned.
return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK; 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;
}
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks // At this point we're either in reindex or we've loaded a useful
// in the past, but is now trying to run unpruned. // block tree into BlockIndex()!
if (fHavePruned && !fPruneMode) {
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX; for (CChainState* chainstate : chainman.GetAll()) {
chainstate->InitCoinsDB(
/* cache_size_bytes */ nCoinDBCache,
/* in_memory */ false,
/* should_wipe */ fReset || fReindexChainState);
chainstate->CoinsErrorCatcher().AddReadErrCallback([]() {
uiInterface.ThreadSafeMessageBox(
_("Error reading from database, shutting down."),
"", CClientUIInterface::MSG_ERROR);
});
// 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;
} }
// At this point blocktree args are consistent with what's on disk. // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
// If we're not mid-reindex (based on disk + args), add a genesis block on disk if (!chainstate->ReplayBlocks()) {
// (otherwise we use the one already on disk). return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
// This is called again in ThreadImport after the reindex completes.
if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) {
return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED;
} }
// At this point we're either in reindex or we've loaded a useful // The on-disk coinsdb is now in a good state, create the cache
// block tree into BlockIndex()! chainstate->InitCoinsCache(nCoinCacheUsage);
assert(chainstate->CanFlushToDisk());
for (CChainState* chainstate : chainman.GetAll()) {
chainstate->InitCoinsDB(
/* cache_size_bytes */ nCoinDBCache,
/* in_memory */ false,
/* should_wipe */ fReset || fReindexChainState);
chainstate->CoinsErrorCatcher().AddReadErrCallback([]() {
uiInterface.ThreadSafeMessageBox(
_("Error reading from database, shutting down."),
"", CClientUIInterface::MSG_ERROR);
});
// 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;
}
// ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate if (!is_coinsview_empty(chainstate)) {
if (!chainstate->ReplayBlocks()) { // LoadChainTip initializes the chain based on CoinsTip()'s best block
return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED; if (!chainstate->LoadChainTip()) {
return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
} }
assert(chainstate->m_chain.Tip() != nullptr);
}
}
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
}
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;
}
}
// The on-disk coinsdb is now in a good state, create the cache try {
chainstate->InitCoinsCache(nCoinCacheUsage); LOCK(cs_main);
assert(chainstate->CanFlushToDisk());
if (!is_coinsview_empty(chainstate)) { for (CChainState* chainstate : chainman.GetAll()) {
// LoadChainTip initializes the chain based on CoinsTip()'s best block if (!is_coinsview_empty(chainstate)) {
if (!chainstate->LoadChainTip()) { uiInterface.InitMessage(_("Verifying blocks…").translated);
return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED; if (fHavePruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
} LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks\n",
assert(chainstate->m_chain.Tip() != nullptr); MIN_BLOCKS_TO_KEEP);
} }
}
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
}
if (!fReset) { const CBlockIndex* tip = chainstate->m_chain.Tip();
LOCK(cs_main); RPCNotifyBlockChange(tip);
auto chainstates{chainman.GetAll()}; if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
if (std::any_of(chainstates.begin(), chainstates.end(), return ChainstateLoadingError::ERROR_BLOCK_FROM_FUTURE;
[](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) { }
return ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED;
}
}
try { if (!CVerifyDB().VerifyDB(
LOCK(cs_main); *chainstate, chainparams, chainstate->CoinsDB(),
check_level,
for (CChainState* chainstate : chainman.GetAll()) { check_blocks)) {
if (!is_coinsview_empty(chainstate)) { return ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB;
uiInterface.InitMessage(_("Verifying blocks…").translated);
if (fHavePruned && check_blocks > MIN_BLOCKS_TO_KEEP) {
LogPrintf("Prune: pruned datadir may not have more than %d blocks; only checking available blocks\n",
MIN_BLOCKS_TO_KEEP);
}
const CBlockIndex* tip = chainstate->m_chain.Tip();
RPCNotifyBlockChange(tip);
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
return ChainstateLoadingError::ERROR_BLOCK_FROM_FUTURE;
}
if (!CVerifyDB().VerifyDB(
*chainstate, chainparams, chainstate->CoinsDB(),
check_level,
check_blocks)) {
return ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB;
}
} }
} }
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
} }
} while(false); } catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
return ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
}
return std::nullopt; return std::nullopt;
} }

Loading…
Cancel
Save