Bitcoin Core  0.18.99
P2P Digital Currency
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 GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:60
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:352
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:66
A UTXO entry.
Definition: coins.h:29
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:81
Access to the block database (blocks/index/)
Definition: txdb.h:88
Abstract view on the open txout dataset.
Definition: coins.h:145
const char * name
Definition: rest.cpp:38
CDBWrapper db
Definition: txdb.h:49
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:122
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:79
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:144
Parameters that influence chain consensus.
Definition: params.h:49
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:64
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:56
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:68
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
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:83
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:171
~CCoinsViewDBCursor()
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:75
Cursor for iterating over CoinsView state.
Definition: coins.h:125