aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/Makefile.am4
-rw-r--r--src/init.cpp291
-rw-r--r--src/node/caches.cpp32
-rw-r--r--src/node/caches.h22
-rw-r--r--src/node/chainstate.cpp158
-rw-r--r--src/node/chainstate.h85
-rw-r--r--src/rpc/blockchain.cpp2
-rw-r--r--src/test/util/setup_common.cpp28
-rw-r--r--src/test/util/setup_common.h2
-rw-r--r--src/validation.cpp8
-rw-r--r--src/validation.h2
11 files changed, 432 insertions, 202 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 654b322d33..4199f7e89d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -174,6 +174,8 @@ BITCOIN_CORE_H = \
netbase.h \
netmessagemaker.h \
node/blockstorage.h \
+ node/caches.h \
+ node/chainstate.h \
node/coin.h \
node/coinstats.h \
node/context.h \
@@ -339,6 +341,8 @@ libbitcoin_server_a_SOURCES = \
net.cpp \
net_processing.cpp \
node/blockstorage.cpp \
+ node/caches.cpp \
+ node/chainstate.cpp \
node/coin.cpp \
node/coinstats.cpp \
node/context.cpp \
diff --git a/src/init.cpp b/src/init.cpp
index 22c0d928b5..1f4537a975 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -34,6 +34,8 @@
#include <net_processing.h>
#include <netbase.h>
#include <node/blockstorage.h>
+#include <node/caches.h>
+#include <node/chainstate.h>
#include <node/context.h>
#include <node/miner.h>
#include <node/ui_interface.h>
@@ -1380,217 +1382,124 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
bool fReindexChainState = args.GetBoolArg("-reindex-chainstate", false);
// cache size calculations
- int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
- nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
- nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
- int64_t nBlockTreeDBCache = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
- nTotalCache -= nBlockTreeDBCache;
- int64_t nTxIndexCache = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
- nTotalCache -= nTxIndexCache;
- int64_t filter_index_cache = 0;
- if (!g_enabled_filter_types.empty()) {
- size_t n_indexes = g_enabled_filter_types.size();
- int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
- filter_index_cache = max_cache / n_indexes;
- nTotalCache -= filter_index_cache * n_indexes;
- }
- int64_t nCoinDBCache = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
- nCoinDBCache = std::min(nCoinDBCache, nMaxCoinsDBCache << 20); // cap total coins db cache
- nTotalCache -= nCoinDBCache;
- int64_t nCoinCacheUsage = nTotalCache; // the rest goes to in-memory cache
+ CacheSizes cache_sizes = CalculateCacheSizes(args, g_enabled_filter_types.size());
+
int64_t nMempoolSizeMax = args.GetIntArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
LogPrintf("Cache configuration:\n");
- LogPrintf("* Using %.1f MiB for block index database\n", nBlockTreeDBCache * (1.0 / 1024 / 1024));
+ LogPrintf("* Using %.1f MiB for block index database\n", cache_sizes.block_tree_db * (1.0 / 1024 / 1024));
if (args.GetBoolArg("-txindex", DEFAULT_TXINDEX)) {
- LogPrintf("* Using %.1f MiB for transaction index database\n", nTxIndexCache * (1.0 / 1024 / 1024));
+ LogPrintf("* Using %.1f MiB for transaction index database\n", cache_sizes.tx_index * (1.0 / 1024 / 1024));
}
for (BlockFilterType filter_type : g_enabled_filter_types) {
LogPrintf("* Using %.1f MiB for %s block filter index database\n",
- filter_index_cache * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
+ cache_sizes.filter_index * (1.0 / 1024 / 1024), BlockFilterTypeName(filter_type));
}
- LogPrintf("* Using %.1f MiB for chain state database\n", nCoinDBCache * (1.0 / 1024 / 1024));
- LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", nCoinCacheUsage * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
+ LogPrintf("* Using %.1f MiB for chain state database\n", cache_sizes.coins_db * (1.0 / 1024 / 1024));
+ LogPrintf("* Using %.1f MiB for in-memory UTXO set (plus up to %.1f MiB of unused mempool space)\n", cache_sizes.coins * (1.0 / 1024 / 1024), nMempoolSizeMax * (1.0 / 1024 / 1024));
bool fLoaded = false;
while (!fLoaded && !ShutdownRequested()) {
const bool fReset = fReindex;
- auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
- return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
- };
bilingual_str strLoadError;
uiInterface.InitMessage(_("Loading block index…").translated);
-
- do {
- const int64_t load_block_index_start_time = GetTimeMillis();
- try {
- LOCK(cs_main);
- chainman.InitializeChainstate(Assert(node.mempool.get()));
- chainman.m_total_coinstip_cache = nCoinCacheUsage;
- chainman.m_total_coinsdb_cache = nCoinDBCache;
-
- UnloadBlockIndex(node.mempool.get(), 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();
- }
-
- if (ShutdownRequested()) break;
-
- // 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()) break;
- strLoadError = _("Error loading block database");
- break;
- }
-
+ const int64_t load_block_index_start_time = GetTimeMillis();
+ std::optional<ChainstateLoadingError> rv;
+ try {
+ rv = LoadChainstate(fReset,
+ chainman,
+ Assert(node.mempool.get()),
+ fPruneMode,
+ chainparams.GetConsensus(),
+ fReindexChainState,
+ cache_sizes.block_tree_db,
+ cache_sizes.coins_db,
+ cache_sizes.coins,
+ false,
+ false,
+ ShutdownRequested,
+ []() {
+ uiInterface.ThreadSafeMessageBox(
+ _("Error reading from database, shutting down."),
+ "", CClientUIInterface::MSG_ERROR);
+ });
+ } catch (const std::exception& e) {
+ LogPrintf("%s\n", e.what());
+ rv = ChainstateLoadingError::ERROR_GENERIC_BLOCKDB_OPEN_FAILED;
+ }
+ if (rv.has_value()) {
+ switch (rv.value()) {
+ case ChainstateLoadingError::ERROR_LOADING_BLOCK_DB:
+ 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).
- if (!chainman.BlockIndex().empty() &&
- !chainman.m_blockman.LookupBlockIndex(chainparams.GetConsensus().hashGenesisBlock)) {
- return InitError(_("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
- // in the past, but is now trying to run unpruned.
- if (fHavePruned && !fPruneMode) {
- strLoadError = _("You need to rebuild the database using -reindex to go back to unpruned mode. This will redownload the entire blockchain");
- break;
- }
-
- // 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()) {
- strLoadError = _("Error initializing block database");
- break;
- }
-
- // At this point we're either in reindex or we've loaded a useful
- // block tree into BlockIndex()!
-
- bool failed_chainstate_init = false;
-
- 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()) {
- strLoadError = _("Error upgrading chainstate database");
- failed_chainstate_init = true;
- break;
- }
-
- // ReplayBlocks is a no-op if we cleared the coinsviewdb with -reindex or -reindex-chainstate
- if (!chainstate->ReplayBlocks()) {
- strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
- failed_chainstate_init = true;
- break;
- }
-
- // 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()) {
- strLoadError = _("Error initializing block database");
- failed_chainstate_init = true;
- break; // out of the per-chainstate loop
- }
- assert(chainstate->m_chain.Tip() != nullptr);
- }
- }
-
- if (failed_chainstate_init) {
- break; // out of the chainstate activation do-while
- }
- } catch (const std::exception& e) {
- LogPrintf("%s\n", e.what());
+ 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:
+ 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_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."),
+ chainparams.GetConsensus().SegwitHeight);
+ break;
+ case ChainstateLoadingError::SHUTDOWN_PROBED:
+ break;
}
-
- 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(); })) {
- strLoadError = strprintf(_("Witness data for blocks after height %d requires validation. Please restart with -reindex."),
- chainparams.GetConsensus().SegwitHeight);
- break;
- }
- }
-
- bool failed_verification = false;
-
+ } else {
+ std::optional<ChainstateLoadVerifyError> rv2;
try {
- LOCK(cs_main);
-
- for (CChainState* chainstate : chainman.GetAll()) {
- if (!is_coinsview_empty(chainstate)) {
- uiInterface.InitMessage(_("Verifying blocks…").translated);
- if (fHavePruned && args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS) > 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) {
- 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");
- failed_verification = true;
- break;
- }
-
- if (!CVerifyDB().VerifyDB(
- *chainstate, chainparams, chainstate->CoinsDB(),
- args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
- args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS))) {
- strLoadError = _("Corrupted block database detected");
- failed_verification = true;
- break;
- }
- }
+ uiInterface.InitMessage(_("Verifying blocks…").translated);
+ auto check_blocks = args.GetIntArg("-checkblocks", DEFAULT_CHECKBLOCKS);
+ 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);
}
+ rv2 = VerifyLoadedChainstate(chainman,
+ fReset,
+ fReindexChainState,
+ chainparams.GetConsensus(),
+ check_blocks,
+ args.GetIntArg("-checklevel", DEFAULT_CHECKLEVEL),
+ static_cast<int64_t(*)()>(GetTime));
} catch (const std::exception& e) {
LogPrintf("%s\n", e.what());
- strLoadError = _("Error opening block database");
- failed_verification = true;
- break;
+ rv2 = ChainstateLoadVerifyError::ERROR_GENERIC_FAILURE;
}
-
- if (!failed_verification) {
+ if (rv2.has_value()) {
+ switch (rv2.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;
LogPrintf(" block index %15dms\n", GetTimeMillis() - load_block_index_start_time);
}
- } while(false);
+ }
if (!fLoaded && !ShutdownRequested()) {
// first suggest a reindex
@@ -1626,14 +1535,14 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
return InitError(*error);
}
- g_txindex = std::make_unique<TxIndex>(nTxIndexCache, false, fReindex);
+ g_txindex = std::make_unique<TxIndex>(cache_sizes.tx_index, false, fReindex);
if (!g_txindex->Start(chainman.ActiveChainstate())) {
return false;
}
}
for (const auto& filter_type : g_enabled_filter_types) {
- InitBlockFilterIndex(filter_type, filter_index_cache, false, fReindex);
+ InitBlockFilterIndex(filter_type, cache_sizes.filter_index, false, fReindex);
if (!GetBlockFilterIndex(filter_type)->Start(chainman.ActiveChainstate())) {
return false;
}
@@ -1857,7 +1766,17 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
// ********************************************************* Step 13: finished
+ // At this point, the RPC is "started", but still in warmup, which means it
+ // cannot yet be called. Before we make it callable, we need to make sure
+ // that the RPC's view of the best block is valid and consistent with
+ // ChainstateManager's ActiveTip.
+ //
+ // If we do not do this, RPC's view of the best block will be height=0 and
+ // hash=0x0. This will lead to erroroneous responses for things like
+ // waitforblockheight.
+ RPCNotifyBlockChange(chainman.ActiveTip());
SetRPCWarmupFinished();
+
uiInterface.InitMessage(_("Done loading").translated);
for (const auto& client : node.chain_clients) {
diff --git a/src/node/caches.cpp b/src/node/caches.cpp
new file mode 100644
index 0000000000..36254dc714
--- /dev/null
+++ b/src/node/caches.cpp
@@ -0,0 +1,32 @@
+// 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/caches.h>
+
+#include <txdb.h>
+#include <util/system.h>
+#include <validation.h>
+
+CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes)
+{
+ int64_t nTotalCache = (args.GetIntArg("-dbcache", nDefaultDbCache) << 20);
+ nTotalCache = std::max(nTotalCache, nMinDbCache << 20); // total cache cannot be less than nMinDbCache
+ nTotalCache = std::min(nTotalCache, nMaxDbCache << 20); // total cache cannot be greater than nMaxDbcache
+ CacheSizes sizes;
+ sizes.block_tree_db = std::min(nTotalCache / 8, nMaxBlockDBCache << 20);
+ nTotalCache -= sizes.block_tree_db;
+ sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? nMaxTxIndexCache << 20 : 0);
+ nTotalCache -= sizes.tx_index;
+ sizes.filter_index = 0;
+ if (n_indexes > 0) {
+ int64_t max_cache = std::min(nTotalCache / 8, max_filter_index_cache << 20);
+ sizes.filter_index = max_cache / n_indexes;
+ nTotalCache -= sizes.filter_index * n_indexes;
+ }
+ sizes.coins_db = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // use 25%-50% of the remainder for disk cache
+ sizes.coins_db = std::min(sizes.coins_db, nMaxCoinsDBCache << 20); // cap total coins db cache
+ nTotalCache -= sizes.coins_db;
+ sizes.coins = nTotalCache; // the rest goes to in-memory cache
+ return sizes;
+}
diff --git a/src/node/caches.h b/src/node/caches.h
new file mode 100644
index 0000000000..200f0b85b8
--- /dev/null
+++ b/src/node/caches.h
@@ -0,0 +1,22 @@
+// 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.
+
+#ifndef BITCOIN_NODE_CACHES_H
+#define BITCOIN_NODE_CACHES_H
+
+#include <cstddef>
+#include <cstdint>
+
+class ArgsManager;
+
+struct CacheSizes {
+ int64_t block_tree_db;
+ int64_t coins_db;
+ int64_t coins;
+ int64_t tx_index;
+ int64_t filter_index;
+};
+CacheSizes CalculateCacheSizes(const ArgsManager& args, size_t n_indexes = 0);
+
+#endif // BITCOIN_NODE_CACHES_H
diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp
new file mode 100644
index 0000000000..abeebad1a6
--- /dev/null
+++ b/src/node/chainstate.cpp
@@ -0,0 +1,158 @@
+// 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>
+
+#include <consensus/params.h>
+#include <node/blockstorage.h>
+#include <validation.h>
+
+std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
+ ChainstateManager& chainman,
+ CTxMemPool* mempool,
+ bool fPruneMode,
+ const Consensus::Params& consensus_params,
+ bool fReindexChainState,
+ int64_t nBlockTreeDBCache,
+ int64_t nCoinDBCache,
+ int64_t nCoinCacheUsage,
+ bool block_tree_db_in_memory,
+ bool coins_db_in_memory,
+ std::function<bool()> shutdown_requested,
+ std::function<void()> coins_error_cb)
+{
+ auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
+ return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
+ };
+
+ 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, block_tree_db_in_memory, 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();
+ }
+
+ 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;
+ return ChainstateLoadingError::ERROR_LOADING_BLOCK_DB;
+ }
+
+ if (!chainman.BlockIndex().empty() &&
+ !chainman.m_blockman.LookupBlockIndex(consensus_params.hashGenesisBlock)) {
+ return ChainstateLoadingError::ERROR_BAD_GENESIS_BLOCK;
+ }
+
+ // 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;
+ }
+
+ // 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 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;
+ }
+ }
+
+ return std::nullopt;
+}
+
+std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
+ bool fReset,
+ bool fReindexChainState,
+ const Consensus::Params& consensus_params,
+ unsigned int check_blocks,
+ unsigned int check_level,
+ std::function<int64_t()> get_unix_time_seconds)
+{
+ auto is_coinsview_empty = [&](CChainState* chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main) {
+ return fReset || fReindexChainState || chainstate->CoinsTip().GetBestBlock().IsNull();
+ };
+
+ LOCK(cs_main);
+
+ for (CChainState* chainstate : chainman.GetAll()) {
+ if (!is_coinsview_empty(chainstate)) {
+ const CBlockIndex* tip = chainstate->m_chain.Tip();
+ if (tip && tip->nTime > get_unix_time_seconds() + 2 * 60 * 60) {
+ return ChainstateLoadVerifyError::ERROR_BLOCK_FROM_FUTURE;
+ }
+
+ if (!CVerifyDB().VerifyDB(
+ *chainstate, consensus_params, chainstate->CoinsDB(),
+ check_level,
+ check_blocks)) {
+ return ChainstateLoadVerifyError::ERROR_CORRUPTED_BLOCK_DB;
+ }
+ }
+ }
+
+ return std::nullopt;
+}
diff --git a/src/node/chainstate.h b/src/node/chainstate.h
new file mode 100644
index 0000000000..11278a0991
--- /dev/null
+++ b/src/node/chainstate.h
@@ -0,0 +1,85 @@
+// 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.
+
+#ifndef BITCOIN_NODE_CHAINSTATE_H
+#define BITCOIN_NODE_CHAINSTATE_H
+
+#include <cstdint>
+#include <functional>
+#include <optional>
+
+class ChainstateManager;
+namespace Consensus {
+ struct Params;
+}
+class CTxMemPool;
+
+enum class ChainstateLoadingError {
+ ERROR_LOADING_BLOCK_DB,
+ ERROR_BAD_GENESIS_BLOCK,
+ ERROR_PRUNED_NEEDS_REINDEX,
+ ERROR_LOAD_GENESIS_BLOCK_FAILED,
+ ERROR_CHAINSTATE_UPGRADE_FAILED,
+ ERROR_REPLAYBLOCKS_FAILED,
+ ERROR_LOADCHAINTIP_FAILED,
+ ERROR_GENERIC_BLOCKDB_OPEN_FAILED,
+ ERROR_BLOCKS_WITNESS_INSUFFICIENTLY_VALIDATED,
+ SHUTDOWN_PROBED,
+};
+
+/** This sequence can have 4 types of outcomes:
+ *
+ * 1. Success
+ * 2. Shutdown requested
+ * - nothing failed but a shutdown was triggered in the middle of the
+ * sequence
+ * 3. Soft failure
+ * - a failure that might be recovered from with a reindex
+ * 4. Hard failure
+ * - a failure that definitively cannot be recovered from with a reindex
+ *
+ * Currently, LoadChainstate returns a std::optional<ChainstateLoadingError>
+ * 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_requested() hasn't been called indirectly.
+ * - else
+ * - Success!
+ */
+std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
+ ChainstateManager& chainman,
+ CTxMemPool* mempool,
+ bool fPruneMode,
+ const Consensus::Params& consensus_params,
+ bool fReindexChainState,
+ int64_t nBlockTreeDBCache,
+ int64_t nCoinDBCache,
+ int64_t nCoinCacheUsage,
+ bool block_tree_db_in_memory,
+ bool coins_db_in_memory,
+ std::function<bool()> shutdown_requested = nullptr,
+ std::function<void()> coins_error_cb = nullptr);
+
+enum class ChainstateLoadVerifyError {
+ ERROR_BLOCK_FROM_FUTURE,
+ ERROR_CORRUPTED_BLOCK_DB,
+ ERROR_GENERIC_FAILURE,
+};
+
+std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
+ bool fReset,
+ bool fReindexChainState,
+ const Consensus::Params& consensus_params,
+ unsigned int check_blocks,
+ unsigned int check_level,
+ std::function<int64_t()> get_unix_time_seconds);
+
+#endif // BITCOIN_NODE_CHAINSTATE_H
diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
index 827b3a7da3..7b37635db0 100644
--- a/src/rpc/blockchain.cpp
+++ b/src/rpc/blockchain.cpp
@@ -1387,7 +1387,7 @@ static RPCHelpMan verifychain()
CChainState& active_chainstate = chainman.ActiveChainstate();
return CVerifyDB().VerifyDB(
- active_chainstate, Params(), active_chainstate.CoinsTip(), check_level, check_depth);
+ active_chainstate, Params().GetConsensus(), active_chainstate.CoinsTip(), check_level, check_depth);
},
};
}
diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp
index f5cc88f4ce..d0be9fe73a 100644
--- a/src/test/util/setup_common.cpp
+++ b/src/test/util/setup_common.cpp
@@ -17,6 +17,8 @@
#include <net_processing.h>
#include <node/miner.h>
#include <noui.h>
+#include <node/blockstorage.h>
+#include <node/chainstate.h>
#include <policy/fees.h>
#include <pow.h>
#include <rpc/blockchain.h>
@@ -24,6 +26,7 @@
#include <rpc/server.h>
#include <scheduler.h>
#include <script/sigcache.h>
+#include <shutdown.h>
#include <streams.h>
#include <txdb.h>
#include <util/strencodings.h>
@@ -143,8 +146,10 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
m_node.fee_estimator = std::make_unique<CBlockPolicyEstimator>();
m_node.mempool = std::make_unique<CTxMemPool>(m_node.fee_estimator.get(), 1);
+ m_cache_sizes = CalculateCacheSizes(m_args);
+
m_node.chainman = std::make_unique<ChainstateManager>();
- m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(1 << 20, true);
+ m_node.chainman->m_blockman.m_block_tree_db = std::make_unique<CBlockTreeDB>(m_cache_sizes.block_tree_db, true);
// Start script-checking threads. Set g_parallel_script_checks to true so they are used.
constexpr int script_check_threads = 2;
@@ -177,15 +182,18 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
// instead of unit tests, but for now we need these here.
RegisterAllCoreRPCCommands(tableRPC);
- m_node.chainman->InitializeChainstate(m_node.mempool.get());
- m_node.chainman->ActiveChainstate().InitCoinsDB(
- /*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
- assert(!m_node.chainman->ActiveChainstate().CanFlushToDisk());
- m_node.chainman->ActiveChainstate().InitCoinsCache(1 << 23);
- assert(m_node.chainman->ActiveChainstate().CanFlushToDisk());
- if (!m_node.chainman->ActiveChainstate().LoadGenesisBlock()) {
- throw std::runtime_error("LoadGenesisBlock failed.");
- }
+ auto rv = LoadChainstate(fReindex.load(),
+ *Assert(m_node.chainman.get()),
+ Assert(m_node.mempool.get()),
+ fPruneMode,
+ chainparams.GetConsensus(),
+ m_args.GetBoolArg("-reindex-chainstate", false),
+ m_cache_sizes.block_tree_db,
+ m_cache_sizes.coins_db,
+ m_cache_sizes.coins,
+ true,
+ true);
+ assert(!rv.has_value());
BlockValidationState state;
if (!m_node.chainman->ActiveChainstate().ActivateBestChain(state)) {
diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h
index 4f2ccb6ebb..df2c8952bc 100644
--- a/src/test/util/setup_common.h
+++ b/src/test/util/setup_common.h
@@ -9,6 +9,7 @@
#include <fs.h>
#include <key.h>
#include <util/system.h>
+#include <node/caches.h>
#include <node/context.h>
#include <pubkey.h>
#include <random.h>
@@ -89,6 +90,7 @@ struct BasicTestingSetup {
* initialization behaviour.
*/
struct ChainTestingSetup : public BasicTestingSetup {
+ CacheSizes m_cache_sizes{};
explicit ChainTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
~ChainTestingSetup();
diff --git a/src/validation.cpp b/src/validation.cpp
index 7efd176e00..d4203de56a 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3864,7 +3864,7 @@ CVerifyDB::~CVerifyDB()
bool CVerifyDB::VerifyDB(
CChainState& chainstate,
- const CChainParams& chainparams,
+ const Consensus::Params& consensus_params,
CCoinsView& coinsview,
int nCheckLevel, int nCheckDepth)
{
@@ -3906,10 +3906,10 @@ bool CVerifyDB::VerifyDB(
}
CBlock block;
// check level 0: read from disk
- if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
+ if (!ReadBlockFromDisk(block, pindex, consensus_params))
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
// check level 1: verify block validity
- if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
+ if (nCheckLevel >= 1 && !CheckBlock(block, state, consensus_params))
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
// check level 2: verify undo validity
@@ -3957,7 +3957,7 @@ bool CVerifyDB::VerifyDB(
uiInterface.ShowProgress(_("Verifying blocks…").translated, percentageDone, false);
pindex = chainstate.m_chain.Next(pindex);
CBlock block;
- if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
+ if (!ReadBlockFromDisk(block, pindex, consensus_params))
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
if (!chainstate.ConnectBlock(block, state, pindex, coins)) {
return error("VerifyDB(): *** found unconnectable block at %d, hash=%s (%s)", pindex->nHeight, pindex->GetBlockHash().ToString(), state.ToString());
diff --git a/src/validation.h b/src/validation.h
index 3bb26084f1..7457ca5239 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -342,7 +342,7 @@ public:
~CVerifyDB();
bool VerifyDB(
CChainState& chainstate,
- const CChainParams& chainparams,
+ const Consensus::Params& consensus_params,
CCoinsView& coinsview,
int nCheckLevel,
int nCheckDepth) EXCLUSIVE_LOCKS_REQUIRED(cs_main);