0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-09 10:43:19 -05:00

style-only: Remove redundant scope in *Chainstate

I strongly recommend reviewing with the following git-diff flags:
  --ignore-space-change
This commit is contained in:
Carl Dong 2021-12-07 12:06:03 -05:00
parent 89bec827fd
commit 7f15eff2dd

View file

@ -26,100 +26,98 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
}; };
{ 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, block_tree_db_in_memory, fReset)); pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, block_tree_db_in_memory, 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 (shutdown_requested && shutdown_requested()) 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 (shutdown_requested && shutdown_requested()) return ChainstateLoadingError::SHUTDOWN_PROBED; if (shutdown_requested && shutdown_requested()) 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(consensus_params.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 (shutdown_requested && shutdown_requested()) return ChainstateLoadingError::SHUTDOWN_PROBED; // Check for changed -prune state. What we are concerned about is a user who has pruned blocks
return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB; // 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;
}
// 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 */ coins_db_in_memory,
/* should_wipe */ fReset || fReindexChainState);
if (coins_error_cb) {
chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb);
} }
if (!chainman.BlockIndex().empty() && // If necessary, upgrade from older database format.
!chainman.m_blockman.LookupBlockIndex(consensus_params.hashGenesisBlock)) { // This is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK; if (!chainstate->CoinsDB().Upgrade()) {
return ChainstateLoadingError::ERROR_CHAINSTATE_UPGRADE_FAILED;
} }
// Check for changed -prune state. What we are concerned about is a user who has pruned blocks // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
// in the past, but is now trying to run unpruned. if (!chainstate->ReplayBlocks()) {
if (fHavePruned && !fPruneMode) { return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
return ChainstateLoadingError::ERROR_PRUNED_NEEDS_REINDEX;
} }
// At this point blocktree args are consistent with what's on disk. // The on-disk coinsdb is now in a good state, create the cache
// If we're not mid-reindex (based on disk + args), add a genesis block on disk chainstate->InitCoinsCache(nCoinCacheUsage);
// (otherwise we use the one already on disk). assert(chainstate->CanFlushToDisk());
// This is called again in ThreadImport after the reindex completes.
if (!fReindex && !chainman.ActiveChainstate().LoadGenesisBlock()) { if (!is_coinsview_empty(chainstate)) {
return ChainstateLoadingError::ERROR_LOAD_GENESIS_BLOCK_FAILED; // LoadChainTip initializes the chain based on CoinsTip()'s best block
if (!chainstate->LoadChainTip()) {
return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
}
assert(chainstate->m_chain.Tip() != nullptr);
} }
}
// At this point we're either in reindex or we've loaded a useful if (!fReset) {
// block tree into BlockIndex()! auto chainstates{chainman.GetAll()};
if (std::any_of(chainstates.begin(), chainstates.end(),
for (CChainState* chainstate : chainman.GetAll()) { [](const CChainState* cs) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return cs->NeedsRedownload(); })) {
chainstate->InitCoinsDB( return ChainstateLoadingError::ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED;
/* cache_size_bytes */ nCoinDBCache,
/* in_memory */ coins_db_in_memory,
/* should_wipe */ fReset || fReindexChainState);
if (coins_error_cb) {
chainstate->CoinsErrorCatcher().AddReadErrCallback(coins_error_cb);
}
// 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 (!chainstate->ReplayBlocks()) {
return ChainstateLoadingError::ERROR_REPLAYBLOCKS_FAILED;
}
// The on-disk coinsdb is now in a good state, create the cache
chainstate->InitCoinsCache(nCoinCacheUsage);
assert(chainstate->CanFlushToDisk());
if (!is_coinsview_empty(chainstate)) {
// LoadChainTip initializes the chain based on CoinsTip()'s best block
if (!chainstate->LoadChainTip()) {
return ChainstateLoadingError::ERROR_LOADCHAINTIP_FAILED;
}
assert(chainstate->m_chain.Tip() != nullptr);
}
}
if (!fReset) {
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;
}
} }
} }
@ -138,22 +136,20 @@ std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManage
return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull(); return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
}; };
{ LOCK(cs_main);
LOCK(cs_main);
for (CChainState* chainstate : chainman.GetAll()) { for (CChainState* chainstate : chainman.GetAll()) {
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 > get_unix_time_seconds() + 2 * 60 * 60) { if (tip && tip->nTime > get_unix_time_seconds() + 2 * 60 * 60) {
return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE; return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
} }
if (!CVerifyDB().VerifyDB( if (!CVerifyDB().VerifyDB(
*chainstate, consensus_params, chainstate->CoinsDB(), *chainstate, consensus_params, chainstate->CoinsDB(),
check_level, check_level,
check_blocks)) { check_blocks)) {
return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB; return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
}
} }
} }
} }