PIVX Core  5.6.99
P2P Digital Currency
txdb.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2016-2021 The PIVX 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 PIVX_TXDB_H
8 #define PIVX_TXDB_H
9 
10 #include "coins.h"
11 #include "chain.h"
12 #include "dbwrapper.h"
13 #include "libzerocoin/Coin.h"
14 #include "libzerocoin/CoinSpend.h"
15 
16 #include <map>
17 #include <string>
18 #include <utility>
19 #include <vector>
20 
21 class CCoinsViewDBCursor;
22 class uint256;
23 
25 static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
27 static const int64_t nDefaultDbCache = 300;
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 nMaxBlockDBAndTxIndexCache = 1024;
41 static const int64_t nMaxCoinsDBCache = 8;
42 
43 struct CDiskTxPos : public FlatFilePos
44 {
45  unsigned int nTxOffset; // after header
46 
48  {
50  READWRITE(VARINT(obj.nTxOffset));
51  }
52 
53  CDiskTxPos(const FlatFilePos& blockIn, unsigned int nTxOffsetIn) : FlatFilePos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn)
54  {
55  }
56 
58  {
59  SetNull();
60  }
61 
62  void SetNull()
63  {
65  nTxOffset = 0;
66  }
67 };
68 
70 class CCoinsViewDB : public CCoinsView
71 {
72 protected:
74 
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  CCoinsViewCursor* Cursor() const override;
83 
85  bool Upgrade();
86  size_t EstimateSize() const override;
87 
88  bool BatchWrite(CCoinsMap& mapCoins,
89  const uint256& hashBlock,
90  const uint256& hashSaplingAnchor,
91  CAnchorsSaplingMap& mapSaplingAnchors,
92  CNullifiersMap& mapSaplingNullifiers) override;
93 
94  // Sapling, the implementation of the following functions can be found in sapling_txdb.cpp.
95  bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const override;
96  bool GetNullifier(const uint256 &nf) const override;
97  uint256 GetBestAnchor() const override;
98  bool BatchWriteSapling(const uint256& hashSaplingAnchor,
99  CAnchorsSaplingMap& mapSaplingAnchors,
100  CNullifiersMap& mapSaplingNullifiers,
101  CDBBatch& batch);
102 };
103 
106 {
107 public:
109 
110  bool GetKey(COutPoint& key) const;
111  bool GetValue(Coin& coin) const;
112  unsigned int GetValueSize() const;
113 
114  bool Valid() const;
115  void Next();
116 
117 private:
118  CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256& hashBlockIn):
119  CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
120  std::unique_ptr<CDBIterator> pcursor;
121  std::pair<char, COutPoint> keyTmp;
122 
123  friend class CCoinsViewDB;
124 };
125 
127 class CBlockTreeDB : public CDBWrapper
128 {
129 public:
130  explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
131 
132  CBlockTreeDB(const CBlockTreeDB&) = delete;
134 
135  bool WriteBlockIndex(const CDiskBlockIndex& blockindex);
136  bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
137  bool ReadBlockFileInfo(int nFile, CBlockFileInfo& info);
138  bool ReadLastBlockFile(int& nFile);
139  bool WriteReindexing(bool fReindexing);
140  bool ReadReindexing(bool& fReindexing);
141  bool ReadTxIndex(const uint256& txid, CDiskTxPos& pos);
142  bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> >& vect);
143  bool WriteFlag(const std::string& name, bool fValue);
144  bool ReadFlag(const std::string& name, bool& fValue);
145  bool WriteInt(const std::string& name, int nValue);
146  bool ReadInt(const std::string& name, int& nValue);
147  bool LoadBlockIndexGuts(std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
148 };
149 
151 class CZerocoinDB : public CDBWrapper
152 {
153 public:
154  explicit CZerocoinDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
155 
156 private:
158  void operator=(const CZerocoinDB&);
159 
160 public:
164  bool WriteCoinSpendBatch(const std::vector<std::pair<CBigNum, uint256> >& spendInfo);
165  bool ReadCoinSpend(const CBigNum& bnSerial, uint256& txHash);
166  bool EraseCoinSpend(const CBigNum& bnSerial);
167 
169  bool WriteAccChecksum(const uint32_t nChecksum, const libzerocoin::CoinDenomination denom, const int nHeight);
170  bool ReadAccChecksum(const uint32_t nChecksum, const libzerocoin::CoinDenomination denom, int& nHeightRet);
171  bool ReadAll(std::map<std::pair<uint32_t, libzerocoin::CoinDenomination>, int>& mapCheckpoints);
172  bool EraseAccChecksum(const uint32_t nChecksum, const libzerocoin::CoinDenomination denom);
173  void WipeAccChecksums();
174 };
175 
177 {
178 private:
179  // underlying database
180  CZerocoinDB* db{nullptr};
181  // in-memory map [checksum, denom] --> block height
182  std::map<std::pair<uint32_t, libzerocoin::CoinDenomination>, int> mapCheckpoints;
183 
184 public:
185  explicit AccumulatorCache(CZerocoinDB* _db) : db(_db)
186  {
187  assert(db != nullptr);
188  bool res = db->ReadAll(mapCheckpoints);
189  assert(res);
190  }
191 
192  Optional<int> Get(uint32_t checksum, libzerocoin::CoinDenomination denom);
193  void Set(uint32_t checksum, libzerocoin::CoinDenomination denom, int height);
194  void Erase(uint32_t checksum, libzerocoin::CoinDenomination denom);
195  void Flush();
196  void Wipe();
197 };
198 
199 #endif // PIVX_TXDB_H
PublicCoin class for the Zerocoin library.
CoinSpend class for the Zerocoin library.
void Erase(uint32_t checksum, libzerocoin::CoinDenomination denom)
Definition: txdb.cpp:583
CZerocoinDB * db
Definition: txdb.h:180
void Wipe()
Definition: txdb.cpp:598
Optional< int > Get(uint32_t checksum, libzerocoin::CoinDenomination denom)
Definition: txdb.cpp:555
std::map< std::pair< uint32_t, libzerocoin::CoinDenomination >, int > mapCheckpoints
Definition: txdb.h:182
AccumulatorCache(CZerocoinDB *_db)
Definition: txdb.h:185
void Set(uint32_t checksum, libzerocoin::CoinDenomination denom, int height)
Definition: txdb.cpp:577
void Flush()
Definition: txdb.cpp:590
C++ wrapper for BIGNUM.
Definition: bignum.h:35
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:139
Access to the block database (blocks/index/)
Definition: txdb.h:128
CBlockTreeDB(const CBlockTreeDB &)=delete
bool WriteTxIndex(const std::vector< std::pair< uint256, CDiskTxPos > > &vect)
Definition: txdb.cpp:256
bool ReadInt(const std::string &name, int &nValue)
Definition: txdb.cpp:283
bool LoadBlockIndexGuts(std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:288
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:160
CBlockTreeDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:151
bool WriteReindexing(bool fReindexing)
Definition: txdb.cpp:165
bool WriteBlockIndex(const CDiskBlockIndex &blockindex)
Definition: txdb.cpp:155
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos)
Definition: txdb.cpp:251
bool WriteInt(const std::string &name, int nValue)
Definition: txdb.cpp:278
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo * > > &fileInfo, int nLastFile, const std::vector< const CBlockIndex * > &blockinfo)
Definition: txdb.cpp:239
CBlockTreeDB & operator=(const CBlockTreeDB &)=delete
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:269
bool ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:173
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:179
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:264
Cursor for iterating over CoinsView state.
Definition: coins.h:181
Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB.
Definition: txdb.h:106
std::unique_ptr< CDBIterator > pcursor
Definition: txdb.h:120
bool Valid() const
Definition: txdb.cpp:223
bool GetKey(COutPoint &key) const
Definition: txdb.cpp:203
CCoinsViewDBCursor(CDBIterator *pcursorIn, const uint256 &hashBlockIn)
Definition: txdb.h:118
~CCoinsViewDBCursor()
Definition: txdb.h:108
unsigned int GetValueSize() const
Definition: txdb.cpp:218
std::pair< char, COutPoint > keyTmp
Definition: txdb.h:121
bool GetValue(Coin &coin) const
Definition: txdb.cpp:213
CCoinsView backed by the LevelDB coin database (chainstate/)
Definition: txdb.h:71
uint256 GetBestAnchor() const override
Get the current "tip" or the latest anchored tree root in the chain.
bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const override
Retrieve the tree (Sapling) at a particular anchored root in the chain.
CCoinsViewDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:47
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txdb.cpp:51
CDBWrapper db
Definition: txdb.h:73
bool GetNullifier(const uint256 &nf) const override
Determine whether a nullifier is spent or not.
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether we have data for a given outpoint.
Definition: txdb.cpp:56
bool Upgrade()
Attempt to update from an older database format. Returns whether an error occurred.
Definition: txdb.cpp:514
CCoinsViewCursor * Cursor() const override
Get a cursor to iterate over the whole state.
Definition: txdb.cpp:184
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: txdb.cpp:61
bool BatchWriteSapling(const uint256 &hashSaplingAnchor, CAnchorsSaplingMap &mapSaplingAnchors, CNullifiersMap &mapSaplingNullifiers, CDBBatch &batch)
bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock, const uint256 &hashSaplingAnchor, CAnchorsSaplingMap &mapSaplingAnchors, CNullifiersMap &mapSaplingNullifiers) override
Do a bulk modification (multiple Coin changes + BestBlock change).
Definition: txdb.cpp:77
std::vector< uint256 > GetHeadBlocks() const override
Retrieve the range of blocks that may have been only partially written.
Definition: txdb.cpp:69
size_t EstimateSize() const override
Estimate database size (0 if not implemented)
Definition: txdb.cpp:146
Abstract view on the open txout dataset.
Definition: coins.h:201
Batch of changes queued to be written to a CDBWrapper.
Definition: dbwrapper.h:46
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
Zerocoin database (zerocoin/)
Definition: txdb.h:152
void operator=(const CZerocoinDB &)
bool WriteAccChecksum(const uint32_t nChecksum, const libzerocoin::CoinDenomination denom, const int nHeight)
Accumulators (only for zPoS IBD): [checksum, denom] --> block height.
Definition: txdb.cpp:387
CZerocoinDB(const CZerocoinDB &)
bool ReadCoinSpend(const CBigNum &bnSerial, uint256 &txHash)
Definition: txdb.cpp:365
bool ReadAccChecksum(const uint32_t nChecksum, const libzerocoin::CoinDenomination denom, int &nHeightRet)
Definition: txdb.cpp:392
bool EraseAccChecksum(const uint32_t nChecksum, const libzerocoin::CoinDenomination denom)
Definition: txdb.cpp:397
CZerocoinDB(size_t nCacheSize, bool fMemory=false, bool fWipe=false)
Definition: txdb.cpp:344
void WipeAccChecksums()
Definition: txdb.cpp:426
bool EraseCoinSpend(const CBigNum &bnSerial)
Definition: txdb.cpp:374
bool WriteCoinSpendBatch(const std::vector< std::pair< CBigNum, uint256 > > &spendInfo)
Write zPIV spends to the zerocoinDB in a batch Pair of: CBigNum -> coinSerialNumber and uint256 -> tx...
Definition: txdb.cpp:348
bool ReadAll(std::map< std::pair< uint32_t, libzerocoin::CoinDenomination >, int > &mapCheckpoints)
Definition: txdb.cpp:402
A UTXO entry.
Definition: coins.h:32
256-bit opaque blob.
Definition: uint256.h:138
std::unordered_map< uint256, CAnchorsSaplingCacheEntry, SaltedIdHasher > CAnchorsSaplingMap
Definition: coins.h:174
std::unordered_map< COutPoint, CCoinsCacheEntry, SaltedOutpointHasher > CCoinsMap
Definition: coins.h:177
std::unordered_map< uint256, CNullifiersCacheEntry, SaltedIdHasher > CNullifiersMap
Definition: coins.h:175
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
const char * name
Definition: rest.cpp:37
#define VARINT(obj)
Definition: serialize.h:513
#define READWRITEAS(type, obj)
Definition: serialize.h:184
#define READWRITE(...)
Definition: serialize.h:183
CDiskTxPos()
Definition: txdb.h:57
void SetNull()
Definition: txdb.h:62
SERIALIZE_METHODS(CDiskTxPos, obj)
Definition: txdb.h:47
CDiskTxPos(const FlatFilePos &blockIn, unsigned int nTxOffsetIn)
Definition: txdb.h:53
unsigned int nTxOffset
Definition: txdb.h:45
int nFile
Definition: flatfile.h:16
void SetNull()
Definition: flatfile.h:36
unsigned int nPos
Definition: flatfile.h:17