txdb.h raw
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 The Limenka 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 LIMENKA_TXDB_H
7 #define LIMENKA_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 = 64 << 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 * Cursor requires FlushStateToDisk for consistency.
38 */
39 class CCoinsViewDB final : public CCoinsView
40 {
41 protected:
42 DBParams m_db_params;
43 CoinsViewOptions m_options;
44 std::unique_ptr<CDBWrapper> m_db;
45 public:
46 explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
47
48 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
49 bool HaveCoin(const COutPoint &outpoint) const override;
50 uint256 GetBestBlock() const override;
51 std::vector<uint256> GetHeadBlocks() const override;
52 bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256 &hashBlock) override;
53 std::unique_ptr<CCoinsViewCursor> Cursor() const override;
54
55 //! Whether an unsupported database format is used.
56 bool NeedsUpgrade();
57 size_t EstimateSize() const override;
58
59 //! Dynamically alter the underlying leveldb cache size.
60 void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
61
62 //! @returns filesystem path to on-disk storage or std::nullopt if in memory.
63 std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
64 };
65
66 #endif // LIMENKA_TXDB_H
67