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 <kernel/cs_main.h>
15 #include <util/result.h>
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <functional>
20 #include <memory>
21 #include <optional>
22 #include <string>
23 #include <utility>
24 #include <vector>
25 
26 struct BlockHash;
27 class CBlockFileInfo;
28 class CBlockIndex;
29 class COutPoint;
30 
31 namespace Consensus {
32 struct Params;
33 };
34 
36 static constexpr int64_t MIN_DB_CACHE_MB = 4;
38 static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
40 static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
42 static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
44 static constexpr int64_t MAX_BLOCK_DB_CACHE_MB = 2;
46 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
47 // a meaningful difference:
48 // https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
49 static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
51 static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
53 static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
54 
56 class CCoinsViewDB final : public CCoinsView {
57 protected:
58  std::unique_ptr<CDBWrapper> m_db;
61 
62 public:
67  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory,
68  bool fWipe);
69 
70  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
71  bool HaveCoin(const COutPoint &outpoint) const override;
72  BlockHash GetBestBlock() const override;
73  std::vector<BlockHash> GetHeadBlocks() const override;
74  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
75  CCoinsViewCursor *Cursor() const override;
76 
79  bool Upgrade();
80  size_t EstimateSize() const override;
81 
83  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
84 
87  std::optional<fs::path> StoragePath() { return m_db->StoragePath(); }
88 };
89 
92 public:
94 
95  bool GetKey(COutPoint &key) const override;
96  bool GetValue(Coin &coin) const override;
97  unsigned int GetValueSize() const override;
98 
99  bool Valid() const override;
100  void Next() override;
101 
102 private:
103  CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
104  : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
105  std::unique_ptr<CDBIterator> pcursor;
106  std::pair<char, COutPoint> keyTmp;
107 
108  friend class CCoinsViewDB;
109 };
110 
112 class CBlockTreeDB : public CDBWrapper {
113 public:
114  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false,
115  bool fWipe = false);
116 
117  bool WriteBatchSync(
118  const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
119  int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
120  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
121  bool ReadLastBlockFile(int &nFile);
122  bool WriteReindexing(bool fReindexing);
123  bool IsReindexing() const;
124  bool WriteFlag(const std::string &name, bool fValue);
125  bool ReadFlag(const std::string &name, bool &fValue);
127  const Consensus::Params &params,
128  std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex)
130  ;
131 
134  bool Upgrade();
135 };
136 
137 [[nodiscard]] util::Result<void>
138 CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
139 
140 #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:26
Access to the block database (blocks/index/)
Definition: txdb.h:112
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:494
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:294
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:126
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:91
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:105
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:237
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:246
~CCoinsViewDBCursor()
Definition: txdb.h:93
bool Valid() const override
Definition: txdb.cpp:254
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:103
unsigned int GetValueSize() const override
Definition: txdb.cpp:250
void Next() override
Definition: txdb.cpp:258
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:106
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:56
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:60
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:58
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:423
std::optional< fs::path > StoragePath()
Definition: txdb.h:87
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:59
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:30
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:124
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:7
const char * name
Definition: rest.cpp:48
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:49
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:38
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:44
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:36
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:53
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:42
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:40
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:51