Bitcoin Core  0.20.99
P2P Digital Currency
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <memory>
15 #include <string>
16 #include <utility>
17 #include <vector>
19 class CBlockIndex;
20 class CCoinsViewDBCursor;
21 class uint256;
24 static const int64_t nDefaultDbCache = 450;
26 static const int64_t nDefaultDbBatchSize = 16 << 20;
28 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
30 static const int64_t nMinDbCache = 4;
32 static const int64_t nMaxBlockDBCache = 2;
34 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
35 // a meaningful difference:
36 static const int64_t nMaxTxIndexCache = 1024;
38 static const int64_t max_filter_index_cache = 1024;
40 static const int64_t nMaxCoinsDBCache = 8;
43 class CCoinsViewDB final : public CCoinsView
44 {
45 protected:
47 public:
51  explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe);
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;
61  bool Upgrade();
62  size_t EstimateSize() const override;
63 };
67 {
68 public:
71  bool GetKey(COutPoint &key) const override;
72  bool GetValue(Coin &coin) const override;
73  unsigned int GetValueSize() const override;
75  bool Valid() const override;
76  void Next() override;
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;
84  friend class CCoinsViewDB;
85 };
88 class CBlockTreeDB : public CDBWrapper
89 {
90 public:
91  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
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 };
103 #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:46
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:337
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:66
static const int64_t nMaxTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:36
A UTXO entry.
Definition: coins.h:30
CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe)
Definition: txdb.cpp:42
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:81
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:30
static const int64_t max_filter_index_cache
Max memory allocated to all block filter index caches combined in MiB.
Definition: txdb.h:38
Access to the block database (blocks/index/)
Definition: txdb.h:88
Abstract view on the open txout dataset.
Definition: coins.h:180
const char * name
Definition: rest.cpp:41
CDBWrapper db
Definition: txdb.h:46
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:157
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:24
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:79
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:26
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:130
Parameters that influence chain consensus.
Definition: params.h:45
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:82
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:50
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:32
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:54
256-bit opaque blob.
Definition: uint256.h:120
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:137
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:43
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:40
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:28
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:69
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:157
Definition: txdb.h:69
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:61
Cursor for iterating over CoinsView state.
Definition: coins.h:160