Bitcoin Core  27.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-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 nDefaultDbCache = 450;
27 static const int64_t nDefaultDbBatchSize = 16 << 20;
29 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
31 static const int64_t nMinDbCache = 4;
33 static const int64_t nMaxBlockDBCache = 2;
35 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
36 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
37 static const int64_t nMaxTxIndexCache = 1024;
39 static const int64_t max_filter_index_cache = 1024;
41 static const int64_t nMaxCoinsDBCache = 8;
42 
50 };
51 
53 class CCoinsViewDB final : public CCoinsView
54 {
55 protected:
58  std::unique_ptr<CDBWrapper> m_db;
59 public:
60  explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
61 
62  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
63  bool HaveCoin(const COutPoint &outpoint) const override;
64  uint256 GetBestBlock() const override;
65  std::vector<uint256> GetHeadBlocks() const override;
66  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase = true) override;
67  std::unique_ptr<CCoinsViewCursor> Cursor() const override;
68 
70  bool NeedsUpgrade();
71  size_t EstimateSize() const override;
72 
74  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
75 
77  std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
78 };
79 
80 #endif // BITCOIN_TXDB_H
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:54
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:68
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:72
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:58
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:91
std::optional< fs::path > StoragePath()
Definition: txdb.h:77
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:49
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:76
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:180
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:54
CoinsViewOptions m_options
Definition: txdb.h:57
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:83
bool NeedsUpgrade()
Whether an unsupported database format is used.
Definition: txdb.cpp:28
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:152
DBParams m_db_params
Definition: txdb.h:56
Abstract view on the open txout dataset.
Definition: coins.h:173
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
A UTXO entry.
Definition: coins.h:32
256-bit opaque blob.
Definition: uint256.h:106
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher, std::equal_to< COutPoint >, PoolAllocator< std::pair< const COutPoint, CCoinsCacheEntry >, sizeof(std::pair< const COutPoint, CCoinsCacheEntry >)+sizeof(void *) *4 > > CCoinsMap
PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
Definition: coins.h:148
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
User-controlled performance and debug options.
Definition: txdb.h:44
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:49
size_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:46
Application-specific storage settings.
Definition: dbwrapper.h:33
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:31
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:27
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:41
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:33
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:37
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:29
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:25
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:39