Bitcoin Core 31.99.0
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present 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 <future>
19#include <memory>
20#include <optional>
21#include <string>
22#include <vector>
23
24class COutPoint;
25class uint256;
26
33};
34
36class CCoinsViewDB final : public CCoinsView
37{
38protected:
43 std::unique_ptr<CDBWrapper> m_db;
44 std::shared_future<void> m_compaction;
45public:
46 explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
47 ~CCoinsViewDB() override;
48
49 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
50 std::optional<Coin> PeekCoin(const COutPoint& outpoint) const override;
51 bool HaveCoin(const COutPoint& outpoint) const override;
52 uint256 GetBestBlock() const override;
53 std::vector<uint256> GetHeadBlocks() const override;
54 void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override;
55 std::unique_ptr<CCoinsViewCursor> Cursor() const override;
56
58 bool NeedsUpgrade();
59 size_t EstimateSize() const override;
60
63
66
68 std::optional<std::string> GetDBProperty(const std::string& property);
69};
70
71#endif // BITCOIN_TXDB_H
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:37
Mutex m_db_mutex
Prevents CompactFull() from using m_db while ResizeCache() replaces it.
Definition: txdb.h:42
std::shared_future< void > CompactFullAsync() EXCLUSIVE_LOCKS_REQUIRED(cs_main
Perform a full compaction of the underlying LevelDB on a one-shot background thread.
Definition: txdb.cpp:198
std::shared_future< void > m_compaction
Definition: txdb.h:44
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:102
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:43
~CCoinsViewDB() override
Definition: txdb.cpp:63
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:58
std::optional< std::string > GetDBProperty(const std::string &property)
Return an underlying LevelDB property value, if available.
Definition: txdb.cpp:193
std::optional< Coin > PeekCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint, without caching results.
Definition: txdb.cpp:97
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:107
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:88
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:73
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state. Implementations may return nullptr.
Definition: txdb.cpp:240
CoinsViewOptions m_options
Definition: txdb.h:40
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:114
void BatchWrite(CoinsViewCacheCursor &cursor, const uint256 &block_hash) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:122
bool NeedsUpgrade()
Whether an unsupported database format is used.
Definition: txdb.cpp:37
size_t EstimateSize() const override
Estimate database size.
Definition: txdb.cpp:188
DBParams m_db_params
Definition: txdb.h:39
Pure abstract view on the open txout dataset.
Definition: coins.h:319
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
256-bit opaque blob.
Definition: uint256.h:196
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
static constexpr uint64_t DEFAULT_DB_CACHE_BATCH
Default LevelDB write batch size.
Definition: caches.h:16
Cursor for iterating over the linked list of flagged entries in CCoinsViewCache.
Definition: coins.h:272
User-controlled performance and debug options.
Definition: txdb.h:28
uint64_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:30
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:32
Application-specific storage settings.
Definition: dbwrapper.h:41
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49