Introduce enum ServiceFlags for service flags
[bitcoinplatinum.git] / src / txdb.h
blobce3c39d7fec11349353845d1601b0908fec51bd8
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 //! -dbcache default (MiB)
25 static const int64_t nDefaultDbCache = 100;
26 //! max. -dbcache in (MiB)
27 static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
28 //! min. -dbcache in (MiB)
29 static const int64_t nMinDbCache = 4;
31 struct CDiskTxPos : public CDiskBlockPos
33 unsigned int nTxOffset; // after header
35 ADD_SERIALIZE_METHODS;
37 template <typename Stream, typename Operation>
38 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
39 READWRITE(*(CDiskBlockPos*)this);
40 READWRITE(VARINT(nTxOffset));
43 CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
46 CDiskTxPos() {
47 SetNull();
50 void SetNull() {
51 CDiskBlockPos::SetNull();
52 nTxOffset = 0;
56 /** CCoinsView backed by the coin database (chainstate/) */
57 class CCoinsViewDB : public CCoinsView
59 protected:
60 CDBWrapper db;
61 public:
62 CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
64 bool GetCoins(const uint256 &txid, CCoins &coins) const;
65 bool HaveCoins(const uint256 &txid) const;
66 uint256 GetBestBlock() const;
67 bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
68 CCoinsViewCursor *Cursor() const;
71 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
72 class CCoinsViewDBCursor: public CCoinsViewCursor
74 public:
75 ~CCoinsViewDBCursor() {}
77 bool GetKey(uint256 &key) const;
78 bool GetValue(CCoins &coins) const;
79 unsigned int GetValueSize() const;
81 bool Valid() const;
82 void Next();
84 private:
85 CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
86 CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
87 boost::scoped_ptr<CDBIterator> pcursor;
88 std::pair<char, uint256> keyTmp;
90 friend class CCoinsViewDB;
93 /** Access to the block database (blocks/index/) */
94 class CBlockTreeDB : public CDBWrapper
96 public:
97 CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
98 private:
99 CBlockTreeDB(const CBlockTreeDB&);
100 void operator=(const CBlockTreeDB&);
101 public:
102 bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
103 bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
104 bool ReadLastBlockFile(int &nFile);
105 bool WriteReindexing(bool fReindex);
106 bool ReadReindexing(bool &fReindex);
107 bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
108 bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
109 bool WriteFlag(const std::string &name, bool fValue);
110 bool ReadFlag(const std::string &name, bool &fValue);
111 bool LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256&)> insertBlockIndex);
114 #endif // BITCOIN_TXDB_H