Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2022 The Bitcoin Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : #ifndef BITCOIN_TXDB_H 7 : #define BITCOIN_TXDB_H 8 : 9 : #include <coins.h> 10 : #include <dbwrapper.h> 11 : #include <kernel/cs_main.h> 12 : #include <sync.h> 13 : #include <util/fs.h> 14 : 15 : #include <cstddef> 16 : #include <cstdint> 17 : #include <memory> 18 : #include <optional> 19 : #include <vector> 20 : 21 : class COutPoint; 22 : class uint256; 23 : 24 : //! -dbcache default (MiB) 25 : static const int64_t nDefaultDbCache = 450; 26 : //! -dbbatchsize default (bytes) 27 : static const int64_t nDefaultDbBatchSize = 16 << 20; 28 : //! max. -dbcache (MiB) 29 : static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024; 30 : //! min. -dbcache (MiB) 31 : static const int64_t nMinDbCache = 4; 32 : //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB) 33 : static const int64_t nMaxBlockDBCache = 2; 34 : //! Max memory allocated to block tree DB specific cache, if -txindex (MiB) 35 : // Unlike for the UTXO database, for the txindex scenario the leveldb cache make 36 : // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991 37 : static const int64_t nMaxTxIndexCache = 1024; 38 : //! Max memory allocated to all block filter index caches combined in MiB. 39 : static const int64_t max_filter_index_cache = 1024; 40 : //! Max memory allocated to coin DB specific cache (MiB) 41 : static const int64_t nMaxCoinsDBCache = 8; 42 : 43 : //! User-controlled performance and debug options. 44 : struct CoinsViewOptions { 45 : //! Maximum database write batch size in bytes. 46 : size_t batch_write_bytes = nDefaultDbBatchSize; 47 : //! If non-zero, randomly exit when the database is flushed with (1/ratio) 48 : //! probability. 49 : int simulate_crash_ratio = 0; 50 : }; 51 : 52 : /** CCoinsView backed by the coin database (chainstate/) */ 53 : class CCoinsViewDB final : public CCoinsView 54 : { 55 : protected: 56 : DBParams m_db_params; 57 : CoinsViewOptions m_options; 58 : std::unique_ptr<CDBWrapper> m_db; 59 : public: 60 : explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options); 61 : 62 : bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; 63 : bool HaveCoin(const COutPoint &outpoint) const override; 64 : uint256 GetBestBlock() const override; 65 : std::vector<uint256> GetHeadBlocks() const override; 66 : bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase = true) override; 67 : std::unique_ptr<CCoinsViewCursor> Cursor() const override; 68 : 69 : //! Whether an unsupported database format is used. 70 : bool NeedsUpgrade(); 71 : size_t EstimateSize() const override; 72 : 73 : //! Dynamically alter the underlying leveldb cache size. 74 : void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main); 75 : 76 : //! @returns filesystem path to on-disk storage or std::nullopt if in memory. 77 0 : std::optional<fs::path> StoragePath() { return m_db->StoragePath(); } 78 : }; 79 : 80 : #endif // BITCOIN_TXDB_H