Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef RAVEN_TXDB_H
8 #define RAVEN_TXDB_H
9 
10 #include "coins.h"
11 #include "dbwrapper.h"
12 #include "chain.h"
13 #include "addressindex.h"
14 #include "spentindex.h"
15 #include "timestampindex.h"
16 
17 #include <map>
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 = 450;
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/RavenProject/Ravencoin/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);
54  READWRITE(VARINT(nTxOffset));
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  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
79  bool HaveCoin(const COutPoint &outpoint) const override;
80  uint256 GetBestBlock() const override;
81  std::vector<uint256> GetHeadBlocks() const override;
82  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
83  CCoinsViewCursor *Cursor() const override;
84 
86  bool Upgrade();
87  size_t EstimateSize() const override;
88 };
89 
92 {
93 public:
95 
96  bool GetKey(COutPoint &key) const override;
97  bool GetValue(Coin &coin) const override;
98  unsigned int GetValueSize() const override;
99 
100  bool Valid() const override;
101  void Next() override;
102 
103 private:
104  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
105  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
106  std::unique_ptr<CDBIterator> pcursor;
107  std::pair<char, COutPoint> keyTmp;
108 
109  friend class CCoinsViewDB;
110 };
111 
113 class CBlockTreeDB : public CDBWrapper
114 {
115 public:
116  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false, size_t maxFileSize = 2 << 20);
117 
118  CBlockTreeDB(const CBlockTreeDB&) = delete;
119  CBlockTreeDB& operator=(const CBlockTreeDB&) = delete;
120 
121  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
122  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
123  bool ReadLastBlockFile(int &nFile);
124  bool WriteReindexing(bool fReindexing);
125  bool ReadReindexing(bool &fReindexing);
126  bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
127  bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &vect);
128  bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
129  bool UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect);
130  bool UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect);
131  bool ReadAddressUnspentIndex(uint160 addressHash, int type, std::string assetName,
132  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &vect);
133  bool ReadAddressUnspentIndex(uint160 addressHash, int type,
134  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &vect);
135  bool WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect);
136  bool EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect);
137  bool ReadAddressIndex(uint160 addressHash, int type, std::string assetName,
138  std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
139  int start = 0, int end = 0);
140  bool ReadAddressIndex(uint160 addressHash, int type,
141  std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
142  int start = 0, int end = 0);
143  bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex);
144  bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &vect);
145  bool WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts);
146  bool ReadTimestampBlockIndex(const uint256 &hash, unsigned int &logicalTS);
147  bool WriteFlag(const std::string &name, bool fValue);
148  bool ReadFlag(const std::string &name, bool &fValue);
149  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
150 };
151 
152 #endif // RAVEN_TXDB_H
void SerializationOp(Stream &s, Operation ser_action)
Definition: txdb.h:52
#define VARINT(obj)
Definition: serialize.h:367
unsigned int nTxOffset
Definition: txdb.h:47
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:91
#define READWRITE(obj)
Definition: serialize.h:163
A UTXO entry.
Definition: coins.h:32
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:106
sph_u32 high
Definition: keccak.c:370
Access to the block database (blocks/index/)
Definition: txdb.h:113
Abstract view on the open txout dataset.
Definition: coins.h:152
ADD_SERIALIZE_METHODS
Definition: txdb.h:49
CDBWrapper db
Definition: txdb.h:74
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:129
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:104
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
Definition: txdb.h:57
Parameters that influence chain consensus.
Definition: params.h:47
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:107
unsigned int nPos
Definition: chain.h:90
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:172
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:71
void SetNull()
Definition: chain.h:117
sph_u32 low
Definition: keccak.c:370
160-bit opaque blob.
Definition: uint256.h:112
void SetNull()
Definition: txdb.h:64
~CCoinsViewDBCursor()
Definition: txdb.h:94
int nFile
Definition: chain.h:89
CDiskTxPos()
Definition: txdb.h:60
Cursor for iterating over CoinsView state.
Definition: coins.h:132