1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
10 #include "dbwrapper.h"
19 #include <boost/function.hpp>
23 struct CAddressUnspentKey;
24 struct CAddressUnspentValue;
25 struct CAddressIndexKey;
26 struct CAddressIndexIteratorKey;
27 struct CAddressIndexIteratorHeightKey;
28 struct CSpentIndexKey;
29 struct CSpentIndexValue;
30 struct CTimestampIndexKey;
31 struct CTimestampIndexIteratorKey;
32 struct CTimestampBlockIndexKey;
33 struct CTimestampBlockIndexValue;
35 typedef std::pair<CAddressUnspentKey, CAddressUnspentValue> CAddressUnspentDbEntry;
36 typedef std::pair<CAddressIndexKey, CAmount> CAddressIndexDbEntry;
37 typedef std::pair<CSpentIndexKey, CSpentIndexValue> CSpentIndexDbEntry;
41 //! -dbcache default (MiB)
42 static const int64_t nDefaultDbCache = 450;
43 //! max. -dbcache (MiB)
44 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
45 //! min. -dbcache in (MiB)
46 static const int64_t nMinDbCache = 4;
48 struct CDiskTxPos : public CDiskBlockPos
50 unsigned int nTxOffset; // after header
52 ADD_SERIALIZE_METHODS;
54 template <typename Stream, typename Operation>
55 inline void SerializationOp(Stream& s, Operation ser_action) {
56 READWRITE(*(CDiskBlockPos*)this);
57 READWRITE(VARINT(nTxOffset));
60 CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
68 CDiskBlockPos::SetNull();
73 /** CCoinsView backed by the coin database (chainstate/) */
74 class CCoinsViewDB : public CCoinsView
78 CCoinsViewDB(std::string dbName, size_t nCacheSize, bool fMemory = false, bool fWipe = false);
80 CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
82 bool GetSproutAnchorAt(const uint256 &rt, SproutMerkleTree &tree) const;
83 bool GetSaplingAnchorAt(const uint256 &rt, SaplingMerkleTree &tree) const;
84 bool GetNullifier(const uint256 &nf, ShieldedType type) const;
85 bool GetCoins(const uint256 &txid, CCoins &coins) const;
86 bool HaveCoins(const uint256 &txid) const;
87 uint256 GetBestBlock() const;
88 uint256 GetBestAnchor(ShieldedType type) const;
89 bool BatchWrite(CCoinsMap &mapCoins,
90 const uint256 &hashBlock,
91 const uint256 &hashSproutAnchor,
92 const uint256 &hashSaplingAnchor,
93 CAnchorsSproutMap &mapSproutAnchors,
94 CAnchorsSaplingMap &mapSaplingAnchors,
95 CNullifiersMap &mapSproutNullifiers,
96 CNullifiersMap &mapSaplingNullifiers);
97 bool GetStats(CCoinsStats &stats) const;
100 /** Access to the block database (blocks/index/) */
101 class CBlockTreeDB : public CDBWrapper
104 CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false, bool compression = true, int maxOpenFiles = 1000);
106 CBlockTreeDB(const CBlockTreeDB&);
107 void operator=(const CBlockTreeDB&);
109 bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
110 bool EraseBatchSync(const std::vector<const CBlockIndex*>& blockinfo);
111 bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
112 bool ReadLastBlockFile(int &nFile);
113 bool WriteReindexing(bool fReindex);
114 bool ReadReindexing(bool &fReindex);
115 bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
116 bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
117 bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
118 bool UpdateSpentIndex(const std::vector<CSpentIndexDbEntry> &vect);
119 bool UpdateAddressUnspentIndex(const std::vector<CAddressUnspentDbEntry> &vect);
120 bool ReadAddressUnspentIndex(uint160 addressHash, int type, std::vector<CAddressUnspentDbEntry> &vect);
121 bool WriteAddressIndex(const std::vector<CAddressIndexDbEntry> &vect);
122 bool EraseAddressIndex(const std::vector<CAddressIndexDbEntry> &vect);
123 bool ReadAddressIndex(uint160 addressHash, int type, std::vector<CAddressIndexDbEntry> &addressIndex, int start = 0, int end = 0);
124 bool WriteTimestampIndex(const CTimestampIndexKey ×tampIndex);
125 bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &vect);
126 bool WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts);
127 bool ReadTimestampBlockIndex(const uint256 &hash, unsigned int &logicalTS);
128 bool WriteFlag(const std::string &name, bool fValue);
129 bool ReadFlag(const std::string &name, bool &fValue);
130 bool LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex);
131 bool blockOnchainActive(const uint256 &hash);
132 UniValue Snapshot(int top);
135 #endif // BITCOIN_TXDB_H