Bitcoin Core  0.19.99
P2P Digital Currency
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
6 #ifndef BITCOIN_TXDB_H
7 #define BITCOIN_TXDB_H
9 #include <coins.h>
10 #include <dbwrapper.h>
11 #include <chain.h>
12 #include <primitives/block.h>
14 #include <map>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 #include <vector>
20 class CBlockIndex;
21 class CCoinsViewDBCursor;
22 class uint256;
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:
39 static const int64_t nMaxTxIndexCache = 1024;
41 static const int64_t max_filter_index_cache = 1024;
43 static const int64_t nMaxCoinsDBCache = 8;
46 class CCoinsViewDB final : public CCoinsView
47 {
48 protected:
50 public:
54  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe);
56  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
57  bool HaveCoin(const COutPoint &outpoint) const override;
58  uint256 GetBestBlock() const override;
59  std::vector<uint256> GetHeadBlocks() const override;
60  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
61  CCoinsViewCursor *Cursor() const override;
64  bool Upgrade();
65  size_t EstimateSize() const override;
66 };
70 {
71 public:
74  bool GetKey(COutPoint &key) const override;
75  bool GetValue(Coin &coin) const override;
76  unsigned int GetValueSize() const override;
78  bool Valid() const override;
79  void Next() override;
81 private:
82  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
83  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
84  std::unique_ptr<CDBIterator> pcursor;
85  std::pair<char, COutPoint> keyTmp;
87  friend class CCoinsViewDB;
88 };
91 class CBlockTreeDB : public CDBWrapper
92 {
93 public:
94  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
96  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
97  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
98  bool ReadLastBlockFile(int &nFile);
99  bool WriteReindexing(bool fReindexing);
100  void ReadReindexing(bool &fReindexing);
101  bool WriteFlag(const std::string &name, bool fValue);
102  bool ReadFlag(const std::string &name, bool &fValue);
103  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
104 };
106 #endif // BITCOIN_TXDB_H
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:59
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:351
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:69
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:39
A UTXO entry.
Definition: coins.h:30
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:55
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:84
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:33
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:41
static constexpr int MAX_BLOCK_COINSDB_USAGE
No need to periodic flush if at least this much space still available.
Definition: txdb.h:25
Access to the block database (blocks/index/)
Definition: txdb.h:91
Abstract view on the open txout dataset.
Definition: coins.h:154
const char * name
Definition: rest.cpp:39
CDBWrapper db
Definition: txdb.h:49
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:131
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:27
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:82
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:29
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:143
Parameters that influence chain consensus.
Definition: params.h:47
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:85
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:63
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:35
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:67
256-bit opaque blob.
Definition: uint256.h:121
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:139
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:46
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 BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:82
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:170
Definition: txdb.h:72
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:74
Cursor for iterating over CoinsView state.
Definition: coins.h:134