Bitcoin Core  25.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 #include <util/result.h>
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <functional>
19 #include <memory>
20 #include <optional>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 
25 class CBlockFileInfo;
26 class CBlockIndex;
27 class COutPoint;
28 class uint256;
29 namespace Consensus {
30 struct Params;
31 };
32 
34 static const int64_t nDefaultDbCache = 450;
36 static const int64_t nDefaultDbBatchSize = 16 << 20;
38 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
40 static const int64_t nMinDbCache = 4;
42 static const int64_t nMaxBlockDBCache = 2;
44 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
45 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
46 static const int64_t nMaxTxIndexCache = 1024;
48 static const int64_t max_filter_index_cache = 1024;
50 static const int64_t nMaxCoinsDBCache = 8;
51 
59 };
60 
62 class CCoinsViewDB final : public CCoinsView
63 {
64 protected:
67  std::unique_ptr<CDBWrapper> m_db;
68 public:
69  explicit CCoinsViewDB(DBParams db_params, CoinsViewOptions options);
70 
71  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
72  bool HaveCoin(const COutPoint &outpoint) const override;
73  uint256 GetBestBlock() const override;
74  std::vector<uint256> GetHeadBlocks() const override;
75  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase = true) override;
76  std::unique_ptr<CCoinsViewCursor> Cursor() const override;
77 
79  bool NeedsUpgrade();
80  size_t EstimateSize() const override;
81 
83  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
84 
86  std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
87 };
88 
90 class CBlockTreeDB : public CDBWrapper
91 {
92 public:
94  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
95  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
96  bool ReadLastBlockFile(int &nFile);
97  bool WriteReindexing(bool fReindexing);
98  void ReadReindexing(bool &fReindexing);
99  bool WriteFlag(const std::string &name, bool fValue);
100  bool ReadFlag(const std::string &name, bool &fValue);
101  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
103 };
104 
105 [[nodiscard]] util::Result<void> CheckLegacyTxindex(CBlockTreeDB& block_tree_db);
106 
107 #endif // BITCOIN_TXDB_H
const CChainParams & Params()
Return the currently selected parameters.
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:151
Access to the block database (blocks/index/)
Definition: txdb.h:91
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:178
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:182
void ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:189
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:270
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:286
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:193
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:282
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:63
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:92
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:96
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:67
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, bool erase=true) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:115
std::optional< fs::path > StoragePath()
Definition: txdb.h:86
CCoinsViewDB(DBParams db_params, CoinsViewOptions options)
Definition: txdb.cpp:73
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:100
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:220
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:78
CoinsViewOptions m_options
Definition: txdb.h:66
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:107
bool NeedsUpgrade()
Whether an unsupported database format is used.
Definition: txdb.cpp:52
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:173
DBParams m_db_params
Definition: txdb.h:65
Abstract view on the open txout dataset.
Definition: coins.h:174
CDBWrapper(const DBParams &params)
Definition: dbwrapper.cpp:130
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:36
A UTXO entry.
Definition: coins.h:32
256-bit opaque blob.
Definition: uint256.h:105
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, alignof(void *)> > CCoinsMap
PoolAllocator's MAX_BLOCK_SIZE_BYTES parameter here uses sizeof the data, and adds the size of 4 poin...
Definition: coins.h:149
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
Transaction validation functions.
const char * name
Definition: rest.cpp:45
User-controlled performance and debug options.
Definition: txdb.h:53
int simulate_crash_ratio
If non-zero, randomly exit when the database is flushed with (1/ratio) probability.
Definition: txdb.h:58
size_t batch_write_bytes
Maximum database write batch size in bytes.
Definition: txdb.h:55
Parameters that influence chain consensus.
Definition: params.h:74
Application-specific storage settings.
Definition: dbwrapper.h:41
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:40
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:36
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:50
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:42
util::Result< void > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:34
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:46
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:38
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:34
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:48