Fabcoin Core  0.16.2
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_TXDB_H
7 #define FABCOIN_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 #include <validation.h> // temp
20 class CBlockIndex;
21 class CCoinsViewDBCursor;
22 class uint256;
23 
25 static constexpr int DB_PEAK_USAGE_FACTOR = 1;
26 
27 
29 static constexpr int MAX_BLOCK_COINSDB_USAGE = 10 * DB_PEAK_USAGE_FACTOR;
30 //?????? static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
32 static const int64_t nDefaultDbCache = 450;
34 static const int64_t nDefaultDbBatchSize = 16 << 20;
36 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
38 static const int64_t nMinDbCache = 4;
40 static const int64_t nMaxBlockDBCache = 2;
42 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
43 // a meaningful difference: https://github.com/blockchaingate/fabcoin/pull/8273#issuecomment-229601991
44 static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
46 static const int64_t nMaxCoinsDBCache = 8;
47 
48 struct CDiskTxPos : public CDiskBlockPos
49 {
50  unsigned int nTxOffset; // after header
51 
53 
54  template <typename Stream, typename Operation>
55  inline void SerializationOp(Stream& s, Operation ser_action) {
56  READWRITE(*(CDiskBlockPos*)this);
57  READWRITE(VARINT(nTxOffset));
58  }
59 
60  CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
61  }
62 
64  SetNull();
65  }
66 
67  void SetNull() {
69  nTxOffset = 0;
70  }
71 };
72 
74 class CCoinsViewDB : public CCoinsView
75 {
76 protected:
78 public:
79  CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
80 
81  bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
82  bool HaveCoin(const COutPoint &outpoint) const override;
83  uint256 GetBestBlock() const override;
84  std::vector<uint256> GetHeadBlocks() const override;
85  bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
86  CCoinsViewCursor *Cursor() const override;
87 
89  bool Upgrade();
90  size_t EstimateSize() const override;
91 };
92 
95 {
96 public:
98 
99  bool GetKey(COutPoint &key) const override;
100  bool GetValue(Coin &coin) const override;
101  unsigned int GetValueSize() const override;
102 
103  bool Valid() const override;
104  void Next() override;
105 
106 private:
107  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
108  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
109  std::unique_ptr<CDBIterator> pcursor;
110  std::pair<char, COutPoint> keyTmp;
111 
112  friend class CCoinsViewDB;
113 };
114 
116 class CBlockTreeDB : public CDBWrapper
117 {
118 public:
119  CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
120 private:
121  CBlockTreeDB(const CBlockTreeDB&);
122  void operator=(const CBlockTreeDB&);
123 public:
124  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
125  bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
126  bool ReadLastBlockFile(int &nFile);
127  bool WriteReindexing(bool fReindex);
128  bool ReadReindexing(bool &fReindex);
129  bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
130  bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
131  bool WriteFlag(const std::string &name, bool fValue);
132  bool ReadFlag(const std::string &name, bool &fValue);
133  bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
134 
136  bool WriteHeightIndex(const CHeightTxIndexKey &heightIndex, const std::vector<uint256>& hash);
137 
149  int ReadHeightIndex(int low, int high, int minconf,
150  std::vector<std::vector<uint256>> &blocksOfHashes,
151  std::set<dev::h160> const &addresses);
152  bool EraseHeightIndex(const unsigned int &height);
153  bool WipeHeightIndex();
154 
155 };
156 
158 #endif // FABCOIN_TXDB_H
void SerializationOp(Stream &s, Operation ser_action)
Definition: txdb.h:55
#define VARINT(obj)
Definition: serialize.h:389
#define function(a, b, c, d, k, s)
unsigned int nTxOffset
Definition: txdb.h:50
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:94
#define READWRITE(obj)
Definition: serialize.h:179
A UTXO entry.
Definition: coins.h:29
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:109
bool fReindex
Definition: validation.cpp:86
Access to the block database (blocks/index/)
Definition: txdb.h:116
Abstract view on the open txout dataset.
Definition: coins.h:145
const char * name
Definition: rest.cpp:36
ADD_SERIALIZE_METHODS
Definition: txdb.h:52
CDBWrapper db
Definition: txdb.h:77
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:122
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:107
CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn)
Definition: txdb.h:60
Parameters that influence chain consensus.
Definition: params.h:39
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:110
unsigned int nPos
Definition: chain.h:95
256-bit opaque blob.
Definition: uint256.h:132
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:177
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:74
void SetNull()
Definition: chain.h:122
void SetNull()
Definition: txdb.h:67
~CCoinsViewDBCursor()
Definition: txdb.h:97
int nFile
Definition: chain.h:94
CDiskTxPos()
Definition: txdb.h:63
Cursor for iterating over CoinsView state.
Definition: coins.h:125