Bitcoin ABC  0.26.3
P2P Digital Currency
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
6 #ifndef BITCOIN_TXDB_H
7 #define BITCOIN_TXDB_H
9 #include <blockfileinfo.h>
10 #include <coins.h>
11 #include <dbwrapper.h>
12 #include <flatfile.h>
14 #include <memory>
15 #include <optional>
16 #include <string>
17 #include <utility>
18 #include <vector>
20 struct BlockHash;
21 class CBlockFileInfo;
22 class CBlockIndex;
24 namespace Consensus {
25 struct Params;
26 };
27 struct bilingual_str;
30 static constexpr int64_t MIN_DB_CACHE_MB = 4;
32 static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
34 static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
36 static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
38 static constexpr int64_t MAX_BLOCK_DB_CACHE_MB = 2;
40 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
41 // a meaningful difference:
42 //
43 static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
45 static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
47 static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
49 // Actually declared in validation.cpp; can't include because of circular
50 // dependency.
51 extern RecursiveMutex cs_main;
54 class CCoinsViewDB final : public CCoinsView {
55 protected:
56  std::unique_ptr<CDBWrapper> m_db;
60 public:
65  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory,
66  bool fWipe);
68  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
69  bool HaveCoin(const COutPoint &outpoint) const override;
70  BlockHash GetBestBlock() const override;
71  std::vector<BlockHash> GetHeadBlocks() const override;
72  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
73  CCoinsViewCursor *Cursor() const override;
77  bool Upgrade();
78  size_t EstimateSize() const override;
81  void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
82 };
86 public:
89  bool GetKey(COutPoint &key) const override;
90  bool GetValue(Coin &coin) const override;
91  unsigned int GetValueSize() const override;
93  bool Valid() const override;
94  void Next() override;
96 private:
97  CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
98  : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
99  std::unique_ptr<CDBIterator> pcursor;
100  std::pair<char, COutPoint> keyTmp;
102  friend class CCoinsViewDB;
103 };
106 class CBlockTreeDB : public CDBWrapper {
107 public:
108  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false,
109  bool fWipe = false);
111  bool WriteBatchSync(
112  const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
113  int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
114  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
115  bool ReadLastBlockFile(int &nFile);
116  bool WriteReindexing(bool fReindexing);
117  bool IsReindexing() const;
118  bool WriteFlag(const std::string &name, bool fValue);
119  bool ReadFlag(const std::string &name, bool &fValue);
121  const Consensus::Params &params,
122  std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex)
124  ;
128  bool Upgrade(const Consensus::Params &params);
129 };
131 std::optional<bilingual_str> CheckLegacyTxindex(CBlockTreeDB &block_tree_db);
133 #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:106
bool Upgrade(const Consensus::Params &params)
Attempt to update from an older database format.
Definition: txdb.cpp:491
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
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:268
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:190
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:198
bool IsReindexing() const
Definition: txdb.cpp:206
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:291
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:210
bool LoadBlockIndexGuts(const Consensus::Params &params, std::function< CBlockIndex *(const BlockHash &)> insertBlockIndex) EXCLUSIVE_LOCKS_REQUIRED(
Definition: txdb.h:120
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:287
Cursor for iterating over CoinsView state.
Definition: coins.h:127
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:85
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:99
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:235
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:244
Definition: txdb.h:87
bool Valid() const override
Definition: txdb.cpp:252
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:97
unsigned int GetValueSize() const override
Definition: txdb.cpp:248
void Next() override
Definition: txdb.cpp:256
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:100
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:54
BlockHash GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:102
std::vector< BlockHash > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:110
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:94
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:98
bool m_is_memory
Definition: txdb.h:58
std::unique_ptr< CDBWrapper > m_db
Definition: txdb.h:56
bool Upgrade()
Attempt to update from an older database format.
Definition: txdb.cpp:420
bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:118
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:214
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:75
fs::path m_ldb_path
Definition: txdb.h:57
void ResizeCache(size_t new_cache_size) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Dynamically alter the underlying leveldb cache size.
Definition: txdb.cpp:81
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:186
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:22
A UTXO entry.
Definition: coins.h:27
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
Definition: fs.h:33
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:124
const char * name
Definition: rest.cpp:49
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
Parameters that influence chain consensus.
Definition: params.h:76
Bilingual messages:
Definition: translation.h:17
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:43
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:32
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:38
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:30
std::optional< bilingual_str > 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:47
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:36
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:111
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:34
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:45