Bitcoin ABC  0.22.12
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 <primitives/block.h>
14 
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 struct BlockHash;
21 class CBlockIndex;
22 class CCoinsViewDBCursor;
23 
24 namespace Consensus {
25 struct Params;
26 }
27 
29 static constexpr int64_t MIN_DB_CACHE_MB = 4;
31 static constexpr int64_t MAX_DB_CACHE_MB = sizeof(void *) > 4 ? 16384 : 1024;
33 static constexpr int64_t DEFAULT_DB_CACHE_MB = 1024;
35 static constexpr int64_t DEFAULT_DB_BATCH_SIZE = 16 << 20;
37 static constexpr int64_t MAX_BLOCK_DB_CACHE_MB = 2;
39 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
40 // a meaningful difference:
41 // https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
42 static constexpr int64_t MAX_TX_INDEX_CACHE_MB = 1024;
44 static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB = 1024;
46 static constexpr int64_t MAX_COINS_DB_CACHE_MB = 8;
47 
49 class CCoinsViewDB final : public CCoinsView {
50 protected:
52 
53 public:
58  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory,
59  bool fWipe);
60 
61  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
62  bool HaveCoin(const COutPoint &outpoint) const override;
63  BlockHash GetBestBlock() const override;
64  std::vector<BlockHash> GetHeadBlocks() const override;
65  bool BatchWrite(CCoinsMap &mapCoins, const BlockHash &hashBlock) override;
66  CCoinsViewCursor *Cursor() const override;
67 
70  bool Upgrade();
71  size_t EstimateSize() const override;
72 };
73 
76 public:
78 
79  bool GetKey(COutPoint &key) const override;
80  bool GetValue(Coin &coin) const override;
81  unsigned int GetValueSize() const override;
82 
83  bool Valid() const override;
84  void Next() override;
85 
86 private:
87  CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
88  : CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
89  std::unique_ptr<CDBIterator> pcursor;
90  std::pair<char, COutPoint> keyTmp;
91 
92  friend class CCoinsViewDB;
93 };
94 
96 class CBlockTreeDB : public CDBWrapper {
97 public:
98  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false,
99  bool fWipe = false);
100 
101  bool WriteBatchSync(
102  const std::vector<std::pair<int, const CBlockFileInfo *>> &fileInfo,
103  int nLastFile, const std::vector<const CBlockIndex *> &blockinfo);
104  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
105  bool ReadLastBlockFile(int &nFile);
106  bool WriteReindexing(bool fReindexing);
107  bool IsReindexing() const;
108  bool WriteFlag(const std::string &name, bool fValue);
109  bool ReadFlag(const std::string &name, bool &fValue);
110  bool LoadBlockIndexGuts(
111  const Consensus::Params &params,
112  std::function<CBlockIndex *(const BlockHash &)> insertBlockIndex);
113 
116  bool Upgrade(const Consensus::Params &params);
117 };
118 
119 #endif // BITCOIN_TXDB_H
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:75
A UTXO entry.
Definition: coins.h:27
static constexpr int64_t MIN_DB_CACHE_MB
min. -dbcache (MiB)
Definition: txdb.h:29
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:89
CCoinsViewDBCursor(CDBIterator *pcursorIn, const BlockHash &hashBlockIn)
Definition: txdb.h:87
static constexpr int64_t MAX_TX_INDEX_CACHE_MB
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:42
static constexpr int64_t MAX_DB_CACHE_MB
max. -dbcache (MiB)
Definition: txdb.h:31
static constexpr int64_t DEFAULT_DB_BATCH_SIZE
-dbbatchsize default (bytes)
Definition: txdb.h:35
Access to the block database (blocks/index/)
Definition: txdb.h:96
Abstract view on the open txout dataset.
Definition: coins.h:175
const char * name
Definition: rest.cpp:43
CDBWrapper db
Definition: txdb.h:51
static constexpr int64_t DEFAULT_DB_CACHE_MB
-dbcache default (MiB)
Definition: txdb.h:33
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:152
Parameters that influence chain consensus.
Definition: params.h:59
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:90
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:37
static constexpr int64_t MAX_FILTER_INDEX_CACHE_MB
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:44
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: blockindex.h:23
const CChainParams & Params()
Return the currently selected parameters.
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:49
static constexpr int64_t MAX_COINS_DB_CACHE_MB
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:46
~CCoinsViewDBCursor()
Definition: txdb.h:77
Cursor for iterating over CoinsView state.
Definition: coins.h:155