Replace boost::function with std::function (C++11)
[bitcoinplatinum.git] / src / txdb.h
blob117e7201fb69ed707d87964c991f257b6957b403
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 class CBlockIndex;
19 class CCoinsViewDBCursor;
20 class uint256;
22 //! Compensate for extra memory peak (x1.5-x1.9) at flush time.
23 static constexpr int DB_PEAK_USAGE_FACTOR = 2;
24 //! No need to periodic flush if at least this much space still available.
25 static constexpr int MAX_BLOCK_COINSDB_USAGE = 200 * DB_PEAK_USAGE_FACTOR;
26 //! Always periodic flush if less than this much space still available.
27 static constexpr int MIN_BLOCK_COINSDB_USAGE = 50 * DB_PEAK_USAGE_FACTOR;
28 //! -dbcache default (MiB)
29 static const int64_t nDefaultDbCache = 450;
30 //! max. -dbcache (MiB)
31 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
32 //! min. -dbcache (MiB)
33 static const int64_t nMinDbCache = 4;
34 //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
35 static const int64_t nMaxBlockDBCache = 2;
36 //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
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;
40 //! Max memory allocated to coin DB specific cache (MiB)
41 static const int64_t nMaxCoinsDBCache = 8;
43 struct CDiskTxPos : public CDiskBlockPos
45 unsigned int nTxOffset; // after header
47 ADD_SERIALIZE_METHODS;
49 template <typename Stream, typename Operation>
50 inline void SerializationOp(Stream& s, Operation ser_action) {
51 READWRITE(*(CDiskBlockPos*)this);
52 READWRITE(VARINT(nTxOffset));
55 CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
58 CDiskTxPos() {
59 SetNull();
62 void SetNull() {
63 CDiskBlockPos::SetNull();
64 nTxOffset = 0;
68 /** CCoinsView backed by the coin database (chainstate/) */
69 class CCoinsViewDB : public CCoinsView
71 protected:
72 CDBWrapper db;
73 public:
74 CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
76 bool GetCoins(const uint256 &txid, CCoins &coins) const;
77 bool HaveCoins(const uint256 &txid) const;
78 uint256 GetBestBlock() const;
79 bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
80 CCoinsViewCursor *Cursor() const;
83 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
84 class CCoinsViewDBCursor: public CCoinsViewCursor
86 public:
87 ~CCoinsViewDBCursor() {}
89 bool GetKey(uint256 &key) const;
90 bool GetValue(CCoins &coins) const;
91 unsigned int GetValueSize() const;
93 bool Valid() const;
94 void Next();
96 private:
97 CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
98 CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
99 std::unique_ptr<CDBIterator> pcursor;
100 std::pair<char, uint256> keyTmp;
102 friend class CCoinsViewDB;
105 /** Access to the block database (blocks/index/) */
106 class CBlockTreeDB : public CDBWrapper
108 public:
109 CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
110 private:
111 CBlockTreeDB(const CBlockTreeDB&);
112 void operator=(const CBlockTreeDB&);
113 public:
114 bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
115 bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
116 bool ReadLastBlockFile(int &nFile);
117 bool WriteReindexing(bool fReindex);
118 bool ReadReindexing(bool &fReindex);
119 bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
120 bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
121 bool WriteFlag(const std::string &name, bool fValue);
122 bool ReadFlag(const std::string &name, bool &fValue);
123 bool LoadBlockIndexGuts(std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
126 #endif // BITCOIN_TXDB_H