diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h index 8f4365e9d..d6a89769f 100644 --- a/src/index/blockfilterindex.h +++ b/src/index/blockfilterindex.h @@ -1,110 +1,112 @@ // Copyright (c) 2018 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_INDEX_BLOCKFILTERINDEX_H #define BITCOIN_INDEX_BLOCKFILTERINDEX_H #include #include #include #include #include +static const char *const DEFAULT_BLOCKFILTERINDEX = "0"; + /** Interval between compact filter checkpoints. See BIP 157. */ static constexpr int CFCHECKPT_INTERVAL = 1000; /** * BlockFilterIndex is used to store and retrieve block filters, hashes, and * headers for a range of blocks by height. An index is constructed for each * supported filter type with its own database (ie. filter data for different * types are stored in separate databases). * * This index is used to serve BIP 157 net requests. */ class BlockFilterIndex final : public BaseIndex { private: BlockFilterType m_filter_type; std::string m_name; std::unique_ptr m_db; FlatFilePos m_next_filter_pos; std::unique_ptr m_filter_fileseq; bool ReadFilterFromDisk(const FlatFilePos &pos, BlockFilter &filter) const; size_t WriteFilterToDisk(FlatFilePos &pos, const BlockFilter &filter); Mutex m_cs_headers_cache; /** * Cache of block hash to filter header, to avoid disk access when * responding to getcfcheckpt. */ std::unordered_map m_headers_cache GUARDED_BY(m_cs_headers_cache); protected: bool Init() override; bool CommitInternal(CDBBatch &batch) override; bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override; bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override; BaseIndex::DB &GetDB() const override { return *m_db; } const char *GetName() const override { return m_name.c_str(); } public: /** Constructs the index, which becomes available to be queried. */ explicit BlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size, bool f_memory = false, bool f_wipe = false); BlockFilterType GetFilterType() const { return m_filter_type; } /** Get a single filter by block. */ bool LookupFilter(const CBlockIndex *block_index, BlockFilter &filter_out) const; /** Get a single filter header by block. */ bool LookupFilterHeader(const CBlockIndex *block_index, uint256 &header_out) EXCLUSIVE_LOCKS_REQUIRED(!m_cs_headers_cache); /** Get a range of filters between two heights on a chain. */ bool LookupFilterRange(int start_height, const CBlockIndex *stop_index, std::vector &filters_out) const; /** Get a range of filter hashes between two heights on a chain. */ bool LookupFilterHashRange(int start_height, const CBlockIndex *stop_index, std::vector &hashes_out) const; }; /** * Get a block filter index by type. Returns nullptr if index has not been * initialized or was already destroyed. */ BlockFilterIndex *GetBlockFilterIndex(BlockFilterType filter_type); /** Iterate over all running block filter indexes, invoking fn on each. */ void ForEachBlockFilterIndex(std::function fn); /** * Initialize a block filter index for the given type if one does not already * exist. Returns true if a new index is created and false if one has already * been initialized. */ bool InitBlockFilterIndex(BlockFilterType filter_type, size_t n_cache_size, bool f_memory = false, bool f_wipe = false); /** * Destroy the block filter index with the given type. Returns false if no such * index exists. This just releases the allocated memory and closes the database * connection, it does not delete the index data. */ bool DestroyBlockFilterIndex(BlockFilterType filter_type); /** Destroy all open block filter indexes. */ void DestroyAllBlockFilterIndexes(); #endif // BITCOIN_INDEX_BLOCKFILTERINDEX_H diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h index 6ec0f6589..0ad1c8d25 100644 --- a/src/index/coinstatsindex.h +++ b/src/index/coinstatsindex.h @@ -1,67 +1,69 @@ // Copyright (c) 2020-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_INDEX_COINSTATSINDEX_H #define BITCOIN_INDEX_COINSTATSINDEX_H #include #include #include #include #include struct Amount; +static constexpr bool DEFAULT_COINSTATSINDEX{false}; + /** * CoinStatsIndex maintains statistics on the UTXO set. */ class CoinStatsIndex final : public BaseIndex { private: std::string m_name; std::unique_ptr m_db; MuHash3072 m_muhash; uint64_t m_transaction_output_count{0}; uint64_t m_bogo_size{0}; Amount m_total_amount{Amount::zero()}; Amount m_total_subsidy{Amount::zero()}; Amount m_total_unspendable_amount{Amount::zero()}; Amount m_total_prevout_spent_amount{Amount::zero()}; Amount m_total_new_outputs_ex_coinbase_amount{Amount::zero()}; Amount m_total_coinbase_amount{Amount::zero()}; Amount m_total_unspendables_genesis_block{Amount::zero()}; Amount m_total_unspendables_bip30{Amount::zero()}; Amount m_total_unspendables_scripts{Amount::zero()}; Amount m_total_unspendables_unclaimed_rewards{Amount::zero()}; bool ReverseBlock(const CBlock &block, const CBlockIndex *pindex); protected: bool Init() override; bool CommitInternal(CDBBatch &batch) override; bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override; bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override; BaseIndex::DB &GetDB() const override { return *m_db; } const char *GetName() const override { return "coinstatsindex"; } public: // Constructs the index, which becomes available to be queried. explicit CoinStatsIndex(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); // Look up stats for a specific block using CBlockIndex std::optional LookUpStats(const CBlockIndex *block_index) const; }; /// The global UTXO set hash object. extern std::unique_ptr g_coin_stats_index; #endif // BITCOIN_INDEX_COINSTATSINDEX_H diff --git a/src/index/txindex.h b/src/index/txindex.h index 5e2357052..017a0d599 100644 --- a/src/index/txindex.h +++ b/src/index/txindex.h @@ -1,57 +1,59 @@ // Copyright (c) 2017-2018 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_INDEX_TXINDEX_H #define BITCOIN_INDEX_TXINDEX_H #include #include struct BlockHash; struct TxId; +static constexpr bool DEFAULT_TXINDEX{false}; + /** * TxIndex is used to look up transactions included in the blockchain by ID. * The index is written to a LevelDB database and records the filesystem * location of each transaction by transaction ID. */ class TxIndex final : public BaseIndex { protected: class DB; private: const std::unique_ptr m_db; protected: bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override; BaseIndex::DB &GetDB() const override; const char *GetName() const override { return "txindex"; } public: /// Constructs the index, which becomes available to be queried. explicit TxIndex(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); // Destructor is declared because this class contains a unique_ptr to an // incomplete type. virtual ~TxIndex() override; /// Look up a transaction by identifier. /// /// @param[in] txid The ID of the transaction to be returned. /// @param[out] block_hash The hash of the block the transaction is found /// in. /// @param[out] tx The transaction itself. /// @return true if transaction is found, false otherwise bool FindTx(const TxId &txid, BlockHash &block_hash, CTransactionRef &tx) const; }; /// The global transaction index, used in GetTransaction. May be null. extern std::unique_ptr g_txindex; #endif // BITCOIN_INDEX_TXINDEX_H diff --git a/src/node/caches.cpp b/src/node/caches.cpp index 7e87711d5..79de6533c 100644 --- a/src/node/caches.cpp +++ b/src/node/caches.cpp @@ -1,48 +1,48 @@ // 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 +#include #include #include -#include namespace node { CacheSizes CalculateCacheSizes(const ArgsManager &args, size_t n_indexes) { int64_t nTotalCache = (args.GetIntArg("-dbcache", DEFAULT_DB_CACHE_MB) << 20); // total cache cannot be less than MIN_DB_CACHE_MB nTotalCache = std::max(nTotalCache, MIN_DB_CACHE_MB << 20); // total cache cannot be greater than MAX_DB_CACHE_MB nTotalCache = std::min(nTotalCache, MAX_DB_CACHE_MB << 20); CacheSizes sizes; sizes.block_tree_db = std::min(nTotalCache / 8, MAX_BLOCK_DB_CACHE_MB << 20); nTotalCache -= sizes.block_tree_db; sizes.tx_index = std::min(nTotalCache / 8, args.GetBoolArg("-txindex", DEFAULT_TXINDEX) ? MAX_TX_INDEX_CACHE_MB << 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_MB << 20); sizes.filter_index = max_cache / n_indexes; nTotalCache -= sizes.filter_index * n_indexes; } // use 25%-50% of the remainder for disk cache sizes.coins_db = std::min(nTotalCache / 2, (nTotalCache / 4) + (1 << 23)); // cap total coins db cache sizes.coins_db = std::min(sizes.coins_db, MAX_COINS_DB_CACHE_MB << 20); nTotalCache -= sizes.coins_db; // the rest goes to in-memory cache sizes.coins = nTotalCache; return sizes; } } // namespace node diff --git a/src/validation.h b/src/validation.h index 3cf895279..5f5459870 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1,1508 +1,1505 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Copyright (c) 2017-2020 The Bitcoin developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_VALIDATION_H #define BITCOIN_VALIDATION_H #if defined(HAVE_CONFIG_H) #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include