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/caches.h>
12 : #include <kernel/cs_main.h>
13 : #include <sync.h>
14 : #include <util/fs.h>
15 :
16 : #include <cstddef>
17 : #include <cstdint>
18 : #include <memory>
19 : #include <optional>
20 : #include <vector>
21 :
22 : class COutPoint;
23 : class uint256;
24 :
25 : //! User-controlled performance and debug options.
26 : struct CoinsViewOptions {
27 : //! Maximum database write batch size in bytes.
28 : size_t batch_write_bytes{DEFAULT_DB_CACHE_BATCH};
29 : //! If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
30 : int simulate_crash_ratio{0};
31 : };
32 :
33 : /** CCoinsView backed by the coin database (chainstate/) */
34 : class CCoinsViewDB final : public CCoinsView
35 : {
36 : protected:
37 : DBParams m_db_params;
38 : CoinsViewOptions m_options;
39 : std::unique_ptr<CDBWrapper> m_db;
40 : public:
41 : explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
42 :
43 : std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
44 : bool HaveCoin(const COutPoint &outpoint) const override;
45 : uint256 GetBestBlock() const override;
46 : std::vector<uint256> GetHeadBlocks() const override;
47 : bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) override;
48 : std::unique_ptr<CCoinsViewCursor> Cursor() const override;
49 :
50 : //! Whether an unsupported database format is used.
51 : bool NeedsUpgrade();
52 : size_t EstimateSize() const override;
53 :
54 : //! Dynamically alter the underlying leveldb cache size.
55 : void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
56 :
57 : //! @returns filesystem path to on-disk storage or std::nullopt if in memory.
58 8 : std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
59 : };
60 :
61 : #endif // BITCOIN_TXDB_H
|