Dogecoin Core  1.14.2
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 
13 #include <map>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
18 #include <boost/function.hpp>
19 
20 class CBlockIndex;
21 class CCoinsViewDBCursor;
22 class uint256;
23 
25 static constexpr int DB_PEAK_USAGE_FACTOR = 2;
27 static constexpr int MAX_BLOCK_COINSDB_USAGE = 200 * DB_PEAK_USAGE_FACTOR;
29 static constexpr int MIN_BLOCK_COINSDB_USAGE = 50 * DB_PEAK_USAGE_FACTOR;
31 static const int64_t nDefaultDbCache = 450;
33 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
35 static const int64_t nMinDbCache = 4;
37 static const int64_t nMaxBlockDBCache = 2;
39 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
40 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
41 static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
43 static const int64_t nMaxCoinsDBCache = 8;
44 
45 struct CDiskTxPos : public CDiskBlockPos
46 {
47  unsigned int nTxOffset; // after header
48 
50 
51  template <typename Stream, typename Operation>
52  inline void SerializationOp(Stream& s, Operation ser_action) {
53  READWRITE(*(CDiskBlockPos*)this);
55  }
56 
57  CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
58  }
59 
61  SetNull();
62  }
63 
64  void SetNull() {
66  nTxOffset = 0;
67  }
68 };
69 
71 class CCoinsViewDB : public CCoinsView
72 {
73 protected:
75 public:
76  CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
77 
78  bool GetCoins(const uint256 &txid, CCoins &coins) const;
79  bool HaveCoins(const uint256 &txid) const;
80  uint256 GetBestBlock() const;
81  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
82  CCoinsViewCursor *Cursor() const;
83 };
84 
87 {
88 public:
90 
91  bool GetKey(uint256 &key) const;
92  bool GetValue(CCoins &coins) const;
93  unsigned int GetValueSize() const;
94 
95  bool Valid() const;
96  void Next();
97 
98 private:
99  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
100  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
101  std::unique_ptr<CDBIterator> pcursor;
102  std::pair<char, uint256> keyTmp;
103 
104  friend class CCoinsViewDB;
105 };
106 
108 class CBlockTreeDB : public CDBWrapper
109 {
110 public:
111  CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
112 private:
114  void operator=(const CBlockTreeDB&);
115 public:
116  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
117  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
118  bool ReadLastBlockFile(int &nFile);
119  bool WriteReindexing(bool fReindex);
120  bool ReadReindexing(bool &fReindex);
121  bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
122  bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
123  bool WriteFlag(const std::string &name, bool fValue);
124  bool ReadFlag(const std::string &name, bool &fValue);
125  bool LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex);
126 };
127 
128 #endif // BITCOIN_TXDB_H
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:158
Access to the block database (blocks/index/)
Definition: txdb.h:109
bool ReadReindexing(bool &fReindex)
Definition: txdb.cpp:84
bool WriteTxIndex(const std::vector< std::pair< uint256, CDiskTxPos > > &list)
Definition: txdb.cpp:157
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:70
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo)
Definition: txdb.cpp:73
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos)
Definition: txdb.cpp:153
CBlockTreeDB(const CBlockTreeDB &)
bool LoadBlockIndexGuts(boost::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:176
bool WriteReindexing(bool fReindex)
Definition: txdb.cpp:77
void operator=(const CBlockTreeDB &)
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:141
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:168
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:89
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:164
Pruned version of CTransaction: only retains metadata and unspent transaction outputs.
Definition: coins.h:75
Cursor for iterating over CoinsView state.
Definition: coins.h:286
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:87
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:101
bool Valid() const
Definition: txdb.cpp:129
bool GetValue(CCoins &coins) const
Definition: txdb.cpp:119
bool GetKey(uint256 &key) const
Definition: txdb.cpp:109
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:99
~CCoinsViewDBCursor()
Definition: txdb.h:89
unsigned int GetValueSize() const
Definition: txdb.cpp:124
std::pair< char, uint256 > keyTmp
Definition: txdb.h:102
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:72
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:28
CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:93
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock)
Do a bulk modification (multiple CCoins changes + BestBlock change).
Definition: txdb.cpp:47
CDBWrapper db
Definition: txdb.h:74
bool GetCoins(const uint256 &txid, CCoins &coins) const
Retrieve the CCoins (unspent transaction outputs) for a given txid.
Definition: txdb.cpp:32
uint256 GetBestBlock() const
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:40
bool HaveCoins(const uint256 &txid) const
Just check whether we have data for a given txid.
Definition: txdb.cpp:36
Abstract view on the open txout dataset.
Definition: coins.h:307
256-bit opaque blob.
Definition: uint256.h:123
boost::unordered_map< uint256, CCoinsCacheEntry, SaltedTxidHasher > CCoinsMap
Definition: coins.h:282
#define VARINT(obj)
Definition: serialize.h:348
#define READWRITE(obj)
Definition: serialize.h:151
int nFile
Definition: chain.h:74
void SetNull()
Definition: chain.h:102
unsigned int nPos
Definition: chain.h:75
CDiskTxPos()
Definition: txdb.h:60
void SerializationOp(Stream &s, Operation ser_action)
Definition: txdb.h:52
void SetNull()
Definition: txdb.h:64
ADD_SERIALIZE_METHODS
Definition: txdb.h:49
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
Definition: txdb.h:57
unsigned int nTxOffset
Definition: txdb.h:47
bool fReindex
Definition: validation.cpp:70