Use the override specifier (C++11) where we expect to be overriding the virtual funct...
[bitcoinplatinum.git] / src / txdb.h
blobb14a0af147e490c00c150a58c55ad0be6406fc67
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 = 10 * DB_PEAK_USAGE_FACTOR;
26 //! -dbcache default (MiB)
27 static const int64_t nDefaultDbCache = 450;
28 //! max. -dbcache (MiB)
29 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
30 //! min. -dbcache (MiB)
31 static const int64_t nMinDbCache = 4;
32 //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
33 static const int64_t nMaxBlockDBCache = 2;
34 //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
35 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
36 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
37 static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
38 //! Max memory allocated to coin DB specific cache (MiB)
39 static const int64_t nMaxCoinsDBCache = 8;
41 struct CDiskTxPos : public CDiskBlockPos
43 unsigned int nTxOffset; // after header
45 ADD_SERIALIZE_METHODS;
47 template <typename Stream, typename Operation>
48 inline void SerializationOp(Stream& s, Operation ser_action) {
49 READWRITE(*(CDiskBlockPos*)this);
50 READWRITE(VARINT(nTxOffset));
53 CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
56 CDiskTxPos() {
57 SetNull();
60 void SetNull() {
61 CDiskBlockPos::SetNull();
62 nTxOffset = 0;
66 /** CCoinsView backed by the coin database (chainstate/) */
67 class CCoinsViewDB : public CCoinsView
69 protected:
70 CDBWrapper db;
71 public:
72 CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
74 bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
75 bool HaveCoin(const COutPoint &outpoint) const override;
76 uint256 GetBestBlock() const override;
77 bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
78 CCoinsViewCursor *Cursor() const override;
80 //! Attempt to update from an older database format. Returns whether an error occurred.
81 bool Upgrade();
82 size_t EstimateSize() const override;
85 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
86 class CCoinsViewDBCursor: public CCoinsViewCursor
88 public:
89 ~CCoinsViewDBCursor() {}
91 bool GetKey(COutPoint &key) const override;
92 bool GetValue(Coin &coin) const override;
93 unsigned int GetValueSize() const override;
95 bool Valid() const override;
96 void Next() override;
98 private:
99 CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
100 CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
101 std::unique_ptr<CDBIterator> pcursor;
102 std::pair<char, COutPoint> 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(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
128 #endif // BITCOIN_TXDB_H