BSHA3  0.17.99
P2P Blockchain, based on 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 nMaxCoinsDBCache = 8;
42 
44 class CCoinsViewDB final : public CCoinsView
45 {
46 protected:
48 public:
49  explicit CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
50 
51  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
52  bool HaveCoin(const COutPoint &outpoint) const override;
53  uint256 GetBestBlock() const override;
54  std::vector<uint256> GetHeadBlocks() const override;
55  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
56  CCoinsViewCursor *Cursor() const override;
57 
59  bool Upgrade();
60  size_t EstimateSize() const override;
61 };
62 
65 {
66 public:
68 
69  bool GetKey(COutPoint &key) const override;
70  bool GetValue(Coin &coin) const override;
71  unsigned int GetValueSize() const override;
72 
73  bool Valid() const override;
74  void Next() override;
75 
76 private:
77  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
78  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
79  std::unique_ptr<CDBIterator> pcursor;
80  std::pair<char, COutPoint> keyTmp;
81 
82  friend class CCoinsViewDB;
83 };
84 
86 class CBlockTreeDB : public CDBWrapper
87 {
88 public:
89  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
90 
91  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
92  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
93  bool ReadLastBlockFile(int &nFile);
94  bool WriteReindexing(bool fReindexing);
95  void ReadReindexing(bool &fReindexing);
96  bool WriteFlag(const std::string &name, bool fValue);
97  bool ReadFlag(const std::string &name, bool &fValue);
98  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
99 };
100 
101 #endif // BITCOIN_TXDB_H
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:199
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:351
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:64
A UTXO entry.
Definition: coins.h:29
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:167
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:79
void ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:163
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:189
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:156
Access to the block database (blocks/index/)
Definition: txdb.h:86
Abstract view on the open txout dataset.
Definition: coins.h:145
const char * name
Definition: rest.cpp:37
CDBWrapper db
Definition: txdb.h:47
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:122
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:77
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:144
Parameters that influence chain consensus.
Definition: params.h:40
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:80
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:64
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:241
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:149
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo *> > &fileInfo, int nLastFile, const std::vector< const CBlockIndex *> &blockinfo)
Definition: txdb.cpp:225
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:152
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:122
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:44
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:249
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:237
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:83
void Next() override
Definition: txdb.cpp:214
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:171
bool Valid() const override
Definition: txdb.cpp:209
~CCoinsViewDBCursor()
Definition: txdb.h:67
unsigned int GetValueSize() const override
Definition: txdb.cpp:204
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