Bitcoin ABC  0.26.3
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 <blockfileinfo.h>
10 #include <coins.h>
11 #include <dbwrapper.h>
12 #include <flatfile.h>
13 #include <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 struct BlockHash;
26 class CBlockFileInfo;
27 class CBlockIndex;
28 class COutPoint;
29 
30 namespace Consensus {
31 struct Params;
32 };
33 
35 static constexpr int64_t MIN_DB_CACHE_MB = 4;
37 static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
39 static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
41 static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
43 static constexpr int64_t MAX_BLOCK_DB_CACHE_MB = 2;
45 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
46 // a meaningful difference:
47 // https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
48 static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
50 static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
52 static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
53 
54 // Actually declared in validation.cpp; can't include because of circular
55 // dependency.
56 extern RecursiveMutex cs_main;
57 
59 class CCoinsViewDB final : public CCoinsView {
60 protected:
61  std::unique_ptr<CDBWrapper> m_db;
64 
65 public:
70  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory,
71  bool fWipe);
72 
73  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
74  bool HaveCoin(const COutPoint &outpoint) const override;
75  BlockHash GetBestBlock() const override;
76  std::vector<BlockHash> GetHeadBlocks() const override;
77  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
78  CCoinsViewCursor *Cursor() const override;
79 
82  bool Upgrade();
83  size_t EstimateSize() const override;
84 
86  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
87 
90  std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
91 };
92 
95 public:
97 
98  bool GetKey(COutPoint &key) const override;
99  bool GetValue(Coin &coin) const override;
100  unsigned int GetValueSize() const override;
101 
102  bool Valid() const override;
103  void Next() override;
104 
105 private:
106  CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
107  : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
108  std::unique_ptr<CDBIterator> pcursor;
109  std::pair<char, COutPoint> keyTmp;
110 
111  friend class CCoinsViewDB;
112 };
113 
115 class CBlockTreeDB : public CDBWrapper {
116 public:
117  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false,
118  bool fWipe = false);
119 
120  bool WriteBatchSync(
121  const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
122  int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
123  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
124  bool ReadLastBlockFile(int &nFile);
125  bool WriteReindexing(bool fReindexing);
126  bool IsReindexing() const;
127  bool WriteFlag(const std::string &name, bool fValue);
128  bool ReadFlag(const std::string &name, bool &fValue);
130  const Consensus::Params &params,
131  std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex)
133  ;
134 
137  bool Upgrade();
138 };
139 
140 [[nodiscard]] util::Result<void>
141 CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
142 
143 #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: blockindex.h:27
Access to the block database (blocks/index/)
Definition: txdb.h:115
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:196
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * >> &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:270
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:192
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:493
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:200
bool IsReindexing() const
Definition: txdb.cpp:208
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:293
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:212
bool LoadBlockIndexGuts(const Consensus::Params &params, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex) EXCLUSIVE_LOCKS_REQUIRED(
Definition: txdb.h:129
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:289
Cursor for iterating over CoinsView state.
Definition: coins.h:127
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:94
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:108
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:237
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:246
~CCoinsViewDBCursor()
Definition: txdb.h:96
bool Valid() const override
Definition: txdb.cpp:254
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:106
unsigned int GetValueSize() const override
Definition: txdb.cpp:250
void Next() override
Definition: txdb.cpp:258
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:109
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:59
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:104
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:112
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:96
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:100
bool m_is_memory
Definition: txdb.h:63
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:61
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:422
std::optional< fs::path > StoragePath()
Definition: txdb.h:90
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:120
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:216
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:77
fs::path m_ldb_path
Definition: txdb.h:62
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:83
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:188
Abstract view on the open txout dataset.
Definition: coins.h:147
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:20
A UTXO entry.
Definition: coins.h:27
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:29
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:124
const char * name
Definition: rest.cpp:47
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:34
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
static constexpr int64_t MAX_TX_INDEX_CACHE_MB
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:48
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:37
static constexpr int64_t MAX_BLOCK_DB_CACHE_MB
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:43
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:35
util::Result< void > CheckLegacyTxindex(CBlockTreeDB &block_tree_db)
Definition: txdb.cpp:36
static constexpr int64_t MAX_COINS_DB_CACHE_MB
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:52
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:41
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:111
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:39
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:50