Bitcoin
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <chain.h>
12 #include <primitives/block.h>
13 
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 class CBlockIndex;
21 class CCoinsViewDBCursor;
22 class uint256;
23 
25 static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
27 static const int64_t nDefaultDbCache = 450;
29 static const int64_t nDefaultDbBatchSize = 16 << 20;
31 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
33 static const int64_t nMinDbCache = 4;
35 static const int64_t nMaxBlockDBCache = 2;
37 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
38 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
39 static const int64_t nMaxTxIndexCache = 1024;
41 static const int64_t max_filter_index_cache = 1024;
43 static const int64_t nMaxCoinsDBCache = 8;
44 
46 class CCoinsViewDB final : public CCoinsView
47 {
48 protected:
50 public:
51  explicit CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
52 
53  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
54  bool HaveCoin(const COutPoint &outpoint) const override;
55  uint256 GetBestBlock() const override;
56  std::vector<uint256> GetHeadBlocks() const override;
57  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
58  CCoinsViewCursor *Cursor() const override;
59 
61  bool Upgrade();
62  size_t EstimateSize() const override;
63 };
64 
67 {
68 public:
70 
71  bool GetKey(COutPoint &key) const override;
72  bool GetValue(Coin &coin) const override;
73  unsigned int GetValueSize() const override;
74 
75  bool Valid() const override;
76  void Next() override;
77 
78 private:
79  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
80  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
81  std::unique_ptr<CDBIterator> pcursor;
82  std::pair<char, COutPoint> keyTmp;
83 
84  friend class CCoinsViewDB;
85 };
86 
88 class CBlockTreeDB : public CDBWrapper
89 {
90 public:
91  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
92 
93  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
94  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
95  bool ReadLastBlockFile(int &nFile);
96  bool WriteReindexing(bool fReindexing);
97  void ReadReindexing(bool &fReindexing);
98  bool WriteFlag(const std::string &name, bool fValue);
99  bool ReadFlag(const std::string &name, bool &fValue);
100  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
101 };
102 
103 #endif // BITCOIN_TXDB_H
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:62
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:150
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:39
Definition: dbwrapper.h:117
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:147
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:66
CDBWrapper db
Definition: txdb.h:49
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:82
Definition: params.h:49
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:33
Definition: txdb.h:88
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:187
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:41
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:235
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:79
Definition: coins.h:125
static constexpr int MAX_BLOCK_COINSDB_USAGE
No need to periodic flush if at least this much space still available.
Definition: txdb.h:25
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:350
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:142
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:169
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:223
void Next() override
Definition: txdb.cpp:212
Definition: transaction.h:18
void ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:161
const char * name
Definition: rest.cpp:39
Definition: coins.h:29
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:122
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:247
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:154
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:197
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:27
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:29
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:54
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:35
Definition: coins.h:145
Definition: uint256.h:121
std::vector< uint256 > GetHeadBlocks() const override
Definition: txdb.cpp:73
Definition: dbwrapper.h:176
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Definition: txdb.cpp:58
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:43
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:31
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:165
unsigned int GetValueSize() const override
Definition: txdb.cpp:202
Definition: chain.h:139
signed long long int64_t
Definition: stdint.h:18
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:239
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:81
bool Valid() const override
Definition: txdb.cpp:207
~CCoinsViewDBCursor()
Definition: txdb.h:69
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Definition: txdb.cpp:81
Definition: txdb.h:66
Definition: chain.h:40
key
Definition: extract_strings_qt.py:80
Definition: txdb.h:46