Bitcoin Core  29.1.0
P2P Digital Currency
txdb.h
Go to the documentation of this file.
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 
25 static const int64_t nDefaultDbBatchSize = 16 << 20;
26 
34 };
35 
37 class CCoinsViewDB final : public CCoinsView
38 {
39 protected:
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 
54  bool NeedsUpgrade();
55  size_t EstimateSize() const override;
56 
58  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
59 
61  std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
62 };
63 
64 #endif // BITCOIN_TXDB_H
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:182
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:54
User-controlled performance and debug options.
Definition: txdb.h:28
CoinsViewOptions m_options
Definition: txdb.h:41
DBParams m_db_params
Definition: txdb.h:40
std::optional< fs::path > StoragePath()
Definition: txdb.h:61
bool NeedsUpgrade()
Whether an unsupported database format is used.
Definition: txdb.cpp:28
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:49
size_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:30
Abstract view on the open txout dataset.
Definition: coins.h:309
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:265
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:25
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:154
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:28
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:74
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:78
256-bit opaque blob.
Definition: uint256.h:201
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:37
Application-specific storage settings.
Definition: dbwrapper.h:34
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:68
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:33
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:85
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:42
bool BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:93