Bitcoin Core  24.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-2021 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 <sync.h>
12 
13 #include <memory>
14 #include <optional>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 class CBlockFileInfo;
20 class CBlockIndex;
21 class uint256;
22 namespace Consensus {
23 struct Params;
24 };
25 struct bilingual_str;
26 
28 static const int64_t nDefaultDbCache = 450;
30 static const int64_t nDefaultDbBatchSize = 16 << 20;
32 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
34 static const int64_t nMinDbCache = 4;
36 static const int64_t nMaxBlockDBCache = 2;
38 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
39 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
40 static const int64_t nMaxTxIndexCache = 1024;
42 static const int64_t max_filter_index_cache = 1024;
44 static const int64_t nMaxCoinsDBCache = 8;
45 
46 // Actually declared in validation.cpp; can't include because of circular dependency.
47 extern RecursiveMutex cs_main;
48 
50 class CCoinsViewDB final : public CCoinsView
51 {
52 protected:
53  std::unique_ptr<CDBWrapper> m_db;
56 public:
60  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe);
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) 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 };
76 
78 class CBlockTreeDB : public CDBWrapper
79 {
80 public:
81  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
82 
83  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
84  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
85  bool ReadLastBlockFile(int &nFile);
86  bool WriteReindexing(bool fReindexing);
87  void ReadReindexing(bool &fReindexing);
88  bool WriteFlag(const std::string &name, bool fValue);
89  bool ReadFlag(const std::string &name, bool &fValue);
90  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex)
92 };
93 
94 std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB& block_tree_db);
95 
96 #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:152
Access to the block database (blocks/index/)
Definition: txdb.h:79
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:183
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:180
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:187
void ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:194
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:275
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:291
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:198
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:287
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:51
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:91
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:95
bool m_is_memory
Definition: txdb.h:55
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:53
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:73
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:99
fs::path m_ldb_path
Definition: txdb.h:54
std::unique_ptr< CCoinsViewCursor > Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:225
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:78
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:114
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:106
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:175
Abstract view on the open txout dataset.
Definition: coins.h:157
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:35
A UTXO entry.
Definition: coins.h:31
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:31
256-bit opaque blob.
Definition: uint256.h:119
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:134
Transaction validation functions.
Definition: params.h:15
const char * name
Definition: rest.cpp:46
Parameters that influence chain consensus.
Definition: params.h:73
Bilingual messages:
Definition: translation.h:18
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:49
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:34
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:30
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:44
std::optional< bilingual_str > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:34
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:36
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:40
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:32
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:28
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:121
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:42