Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2020 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 <chain.h> # 12 : : #include <primitives/block.h> # 13 : : # 14 : : #include <memory> # 15 : : #include <string> # 16 : : #include <utility> # 17 : : #include <vector> # 18 : : # 19 : : class CBlockIndex; # 20 : : class CCoinsViewDBCursor; # 21 : : class uint256; # 22 : : # 23 : : //! -dbcache default (MiB) # 24 : : static const int64_t nDefaultDbCache = 450; # 25 : : //! -dbbatchsize default (bytes) # 26 : : static const int64_t nDefaultDbBatchSize = 16 << 20; # 27 : : //! max. -dbcache (MiB) # 28 : : static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024; # 29 : : //! min. -dbcache (MiB) # 30 : : static const int64_t nMinDbCache = 4; # 31 : : //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB) # 32 : : static const int64_t nMaxBlockDBCache = 2; # 33 : : //! Max memory allocated to block tree DB specific cache, if -txindex (MiB) # 34 : : // Unlike for the UTXO database, for the txindex scenario the leveldb cache make # 35 : : // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991 # 36 : : static const int64_t nMaxTxIndexCache = 1024; # 37 : : //! Max memory allocated to all block filter index caches combined in MiB. # 38 : : static const int64_t max_filter_index_cache = 1024; # 39 : : //! Max memory allocated to coin DB specific cache (MiB) # 40 : : static const int64_t nMaxCoinsDBCache = 8; # 41 : : # 42 : : // Actually declared in validation.cpp; can't include because of circular dependency. # 43 : : extern RecursiveMutex cs_main; # 44 : : # 45 : : /** CCoinsView backed by the coin database (chainstate/) */ # 46 : : class CCoinsViewDB final : public CCoinsView # 47 : : { # 48 : : protected: # 49 : : std::unique_ptr<CDBWrapper> m_db; # 50 : : fs::path m_ldb_path; # 51 : : bool m_is_memory; # 52 : : public: # 53 : : /** # 54 : : * @param[in] ldb_path Location in the filesystem where leveldb data will be stored. # 55 : : */ # 56 : : explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe); # 57 : : # 58 : : bool GetCoin(const COutPoint &outpoint, Coin &coin) const override; # 59 : : bool HaveCoin(const COutPoint &outpoint) const override; # 60 : : uint256 GetBestBlock() const override; # 61 : : std::vector<uint256> GetHeadBlocks() const override; # 62 : : bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override; # 63 : : CCoinsViewCursor *Cursor() const override; # 64 : : # 65 : : //! Attempt to update from an older database format. Returns whether an error occurred. # 66 : : bool Upgrade(); # 67 : : size_t EstimateSize() const override; # 68 : : # 69 : : //! Dynamically alter the underlying leveldb cache size. # 70 : : void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main); # 71 : : }; # 72 : : # 73 : : /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */ # 74 : : class CCoinsViewDBCursor: public CCoinsViewCursor # 75 : : { # 76 : : public: # 77 : 344 : ~CCoinsViewDBCursor() {} # 78 : : # 79 : : bool GetKey(COutPoint &key) const override; # 80 : : bool GetValue(Coin &coin) const override; # 81 : : unsigned int GetValueSize() const override; # 82 : : # 83 : : bool Valid() const override; # 84 : : void Next() override; # 85 : : # 86 : : private: # 87 : : CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn): # 88 : 344 : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {} # 89 : : std::unique_ptr<CDBIterator> pcursor; # 90 : : std::pair<char, COutPoint> keyTmp; # 91 : : # 92 : : friend class CCoinsViewDB; # 93 : : }; # 94 : : # 95 : : /** Access to the block database (blocks/index/) */ # 96 : : class CBlockTreeDB : public CDBWrapper # 97 : : { # 98 : : public: # 99 : : explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false); # 100 : : # 101 : : bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo); # 102 : : bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info); # 103 : : bool ReadLastBlockFile(int &nFile); # 104 : : bool WriteReindexing(bool fReindexing); # 105 : : void ReadReindexing(bool &fReindexing); # 106 : : bool WriteFlag(const std::string &name, bool fValue); # 107 : : bool ReadFlag(const std::string &name, bool &fValue); # 108 : : bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex); # 109 : : }; # 110 : : # 111 : : #endif // BITCOIN_TXDB_H