minor cleanup: remove unnecessary variable
[bitcoinplatinum.git] / src / txdb.h
blobd9214ba618546c100ed767b1d7e4ee6211f07045
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_TXDB_H
7 #define BITCOIN_TXDB_H
9 #include "coins.h"
10 #include "dbwrapper.h"
11 #include "chain.h"
13 #include <map>
14 #include <string>
15 #include <utility>
16 #include <vector>
18 #include <boost/function.hpp>
20 class CBlockIndex;
21 class CCoinsViewDBCursor;
22 class uint256;
24 //! Compensate for extra memory peak (x1.5-x1.9) at flush time.
25 static constexpr int DB_PEAK_USAGE_FACTOR = 2;
26 //! No need to periodic flush if at least this much space still available.
27 static constexpr int MAX_BLOCK_COINSDB_USAGE = 200 * DB_PEAK_USAGE_FACTOR;
28 //! Always periodic flush if less than this much space still available.
29 static constexpr int MIN_BLOCK_COINSDB_USAGE = 50 * DB_PEAK_USAGE_FACTOR;
30 //! -dbcache default (MiB)
31 static const int64_t nDefaultDbCache = 450;
32 //! max. -dbcache (MiB)
33 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
34 //! min. -dbcache (MiB)
35 static const int64_t nMinDbCache = 4;
36 //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
37 static const int64_t nMaxBlockDBCache = 2;
38 //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
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;
42 //! Max memory allocated to coin DB specific cache (MiB)
43 static const int64_t nMaxCoinsDBCache = 8;
45 struct CDiskTxPos : public CDiskBlockPos
47 unsigned int nTxOffset; // after header
49 ADD_SERIALIZE_METHODS;
51 template <typename Stream, typename Operation>
52 inline void SerializationOp(Stream& s, Operation ser_action) {
53 READWRITE(*(CDiskBlockPos*)this);
54 READWRITE(VARINT(nTxOffset));
57 CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
60 CDiskTxPos() {
61 SetNull();
64 void SetNull() {
65 CDiskBlockPos::SetNull();
66 nTxOffset = 0;
70 /** CCoinsView backed by the coin database (chainstate/) */
71 class CCoinsViewDB : public CCoinsView
73 protected:
74 CDBWrapper db;
75 public:
76 CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
78 bool GetCoins(const uint256 &txid, CCoins &coins) const;
79 bool HaveCoins(const uint256 &txid) const;
80 uint256 GetBestBlock() const;
81 bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
82 CCoinsViewCursor *Cursor() const;
85 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
86 class CCoinsViewDBCursor: public CCoinsViewCursor
88 public:
89 ~CCoinsViewDBCursor() {}
91 bool GetKey(uint256 &key) const;
92 bool GetValue(CCoins &coins) const;
93 unsigned int GetValueSize() const;
95 bool Valid() const;
96 void Next();
98 private:
99 CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
100 CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
101 std::unique_ptr<CDBIterator> pcursor;
102 std::pair<char, uint256> keyTmp;
104 friend class CCoinsViewDB;
107 /** Access to the block database (blocks/index/) */
108 class CBlockTreeDB : public CDBWrapper
110 public:
111 CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
112 private:
113 CBlockTreeDB(const CBlockTreeDB&);
114 void operator=(const CBlockTreeDB&);
115 public:
116 bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
117 bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
118 bool ReadLastBlockFile(int &nFile);
119 bool WriteReindexing(bool fReindex);
120 bool ReadReindexing(bool &fReindex);
121 bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
122 bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
123 bool WriteFlag(const std::string &name, bool fValue);
124 bool ReadFlag(const std::string &name, bool &fValue);
125 bool LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex);
128 #endif // BITCOIN_TXDB_H