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