Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 <limitedmap.h>
13 #include <spentindex.h>
14 #include <sync.h>
15 
16 #include <map>
17 #include <memory>
18 #include <string>
19 #include <utility>
20 #include <vector>
21 
22 class CBlockIndex;
23 class CCoinsViewDBCursor;
24 class uint256;
25 
27 static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
29 static const int64_t nDefaultDbCache = 300;
31 static const int64_t nDefaultDbBatchSize = 16 << 20;
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 final : public CCoinsView
72 {
73 protected:
75 public:
76  explicit CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
77 
78 
79  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
80  bool HaveCoin(const COutPoint &outpoint) const override;
81  uint256 GetBestBlock() const override;
82  std::vector<uint256> GetHeadBlocks() const override;
83  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
84  CCoinsViewCursor *Cursor() const override;
85 
87  bool Upgrade();
88  size_t EstimateSize() const override;
89 };
90 
93 {
94 public:
96 
97  bool GetKey(COutPoint &key) const override;
98  bool GetValue(Coin &coin) const override;
99  unsigned int GetValueSize() const override;
100 
101  bool Valid() const override;
102  void Next() override;
103 
104 private:
105  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
106  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
107  std::unique_ptr<CDBIterator> pcursor;
108  std::pair<char, COutPoint> keyTmp;
109 
110  friend class CCoinsViewDB;
111 };
112 
114 class CBlockTreeDB : public CDBWrapper
115 {
116 private:
119 
120 public:
121  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
122 
123  CBlockTreeDB(const CBlockTreeDB&) = delete;
124  CBlockTreeDB& operator=(const CBlockTreeDB&) = delete;
125 
126  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
127  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
128  bool ReadLastBlockFile(int &nFile);
129  bool WriteReindexing(bool fReindexing);
130  bool ReadReindexing(bool &fReindexing);
131  bool HasTxIndex(const uint256 &txid);
132  bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
133  bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &vect);
135  bool UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect);
136  bool UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect);
137  bool ReadAddressUnspentIndex(uint160 addressHash, int type,
138  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &vect);
139  bool WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect);
140  bool EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect);
141  bool ReadAddressIndex(uint160 addressHash, int type,
142  std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
143  int start = 0, int end = 0);
144  bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex);
145  bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector<uint256> &vect);
146  bool WriteFlag(const std::string &name, bool fValue);
147  bool ReadFlag(const std::string &name, bool &fValue);
148  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
149 };
150 
151 #endif // BITCOIN_TXDB_H
void SerializationOp(Stream &s, Operation ser_action)
Definition: txdb.h:52
bool GetValue(Coin &coin) const override
Definition: txdb.cpp:205
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:65
#define VARINT(obj)
Definition: serialize.h:375
unsigned int nTxOffset
Definition: txdb.h:47
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:518
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:92
#define READWRITE(obj)
Definition: serialize.h:165
A UTXO entry.
Definition: coins.h:29
CBlockTreeDB & operator=(const CBlockTreeDB &)=delete
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:173
bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, std::vector< uint256 > &vect)
Definition: txdb.cpp:384
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:107
sph_u32 high
Definition: keccak.c:370
static const int64_t nMaxBlockDBAndTxIndexCache
Max memory allocated to block tree DB specific cache, if -txindex (MiB)
Definition: txdb.h:41
static const int64_t nMinDbCache
min. -dbcache (MiB)
Definition: txdb.h:35
bool ReadAddressIndex(uint160 addressHash, int type, std::vector< std::pair< CAddressIndexKey, CAmount > > &addressIndex, int start=0, int end=0)
Definition: txdb.cpp:344
bool GetKey(COutPoint &key) const override
Definition: txdb.cpp:195
bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
Definition: txdb.cpp:276
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:161
bool ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:168
static constexpr int MAX_BLOCK_COINSDB_USAGE
No need to periodic flush if at least this much space still available.
Definition: txdb.h:27
bool EraseAddressIndex(const std::vector< std::pair< CAddressIndexKey, CAmount > > &vect)
Definition: txdb.cpp:337
bool UpdateAddressUnspentIndex(const std::vector< std::pair< CAddressUnspentKey, CAddressUnspentValue > > &vect)
Definition: txdb.cpp:292
bool ReadAddressUnspentIndex(uint160 addressHash, int type, std::vector< std::pair< CAddressUnspentKey, CAddressUnspentValue > > &vect)
Definition: txdb.cpp:304
Access to the block database (blocks/index/)
Definition: txdb.h:114
Abstract view on the open txout dataset.
Definition: coins.h:145
const char * name
Definition: rest.cpp:36
bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex)
Definition: txdb.cpp:378
ADD_SERIALIZE_METHODS
Definition: txdb.h:49
CDBWrapper db
Definition: txdb.h:74
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:122
static const int64_t nDefaultDbCache
-dbcache default (MiB)
Definition: txdb.h:29
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:105
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
Definition: txdb.h:57
static const int64_t nDefaultDbBatchSize
-dbbatchsize default (bytes)
Definition: txdb.h:31
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:149
Parameters that influence chain consensus.
Definition: params.h:130
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:108
bool WriteAddressIndex(const std::vector< std::pair< CAddressIndexKey, CAmount > > &vect)
Definition: txdb.cpp:330
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: txdb.cpp:69
unsigned int nPos
Definition: chain.h:88
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:408
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:154
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo *> > &fileInfo, int nLastFile, const std::vector< const CBlockIndex *> &blockinfo)
Definition: txdb.cpp:231
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:157
static const int64_t nMaxBlockDBCache
Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
Definition: txdb.h:37
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos)
Definition: txdb.cpp:257
CCriticalSection cs
Definition: txdb.h:117
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:61
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:73
256-bit opaque blob.
Definition: uint256.h:123
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:71
static const int64_t nMaxCoinsDBCache
Max memory allocated to coin DB specific cache (MiB)
Definition: txdb.h:43
void SetNull()
Definition: chain.h:115
static const int64_t nMaxDbCache
max. -dbcache (MiB)
Definition: txdb.h:33
sph_u32 low
Definition: keccak.c:370
160-bit opaque blob.
Definition: uint256.h:112
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:416
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:404
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:88
void Next() override
Definition: txdb.cpp:220
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:177
bool Valid() const override
Definition: txdb.cpp:215
bool HasTxIndex(const uint256 &txid)
Definition: txdb.cpp:243
void SetNull()
Definition: txdb.h:64
~CCoinsViewDBCursor()
Definition: txdb.h:95
unsigned int GetValueSize() const override
Definition: txdb.cpp:210
int nFile
Definition: chain.h:87
CDiskTxPos()
Definition: txdb.h:60
unordered_limitedmap< uint256, bool > mapHasTxIndexCache
Definition: txdb.h:118
bool UpdateSpentIndex(const std::vector< std::pair< CSpentIndexKey, CSpentIndexValue > > &vect)
Definition: txdb.cpp:280
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:80
bool WriteTxIndex(const std::vector< std::pair< uint256, CDiskTxPos > > &vect)
Definition: txdb.cpp:264
Wrapped mutex: supports recursive locking, but no waiting TODO: We should move away from using the re...
Definition: sync.h:94
Cursor for iterating over CoinsView state.
Definition: coins.h:125
Released under the MIT license