aboutsummaryrefslogtreecommitdiff
path: root/src/node
diff options
context:
space:
mode:
Diffstat (limited to 'src/node')
-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
4 files changed, 297 insertions, 0 deletions
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