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.
10 #include "dbwrapper.h"
18 #include <boost/function.hpp>
21 class CCoinsViewDBCursor
;
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
) {
51 CDiskBlockPos::SetNull();
56 /** CCoinsView backed by the coin database (chainstate/) */
57 class CCoinsViewDB
: public CCoinsView
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
75 ~CCoinsViewDBCursor() {}
77 bool GetKey(uint256
&key
) const;
78 bool GetValue(CCoins
&coins
) const;
79 unsigned int GetValueSize() const;
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
97 CBlockTreeDB(size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false);
99 CBlockTreeDB(const CBlockTreeDB
&);
100 void operator=(const CBlockTreeDB
&);
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