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.
10 #include "dbwrapper.h"
18 #include <boost/function.hpp>
21 class CCoinsViewDBCursor
;
24 //! -dbcache default (MiB)
25 static const int64_t nDefaultDbCache
= 300;
26 //! max. -dbcache (MiB)
27 static const int64_t nMaxDbCache
= sizeof(void*) > 4 ? 16384 : 1024;
28 //! min. -dbcache (MiB)
29 static const int64_t nMinDbCache
= 4;
30 //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
31 static const int64_t nMaxBlockDBCache
= 2;
32 //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
33 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
34 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
35 static const int64_t nMaxBlockDBAndTxIndexCache
= 1024;
36 //! Max memory allocated to coin DB specific cache (MiB)
37 static const int64_t nMaxCoinsDBCache
= 8;
39 struct CDiskTxPos
: public CDiskBlockPos
41 unsigned int nTxOffset
; // after header
43 ADD_SERIALIZE_METHODS
;
45 template <typename Stream
, typename Operation
>
46 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
47 READWRITE(*(CDiskBlockPos
*)this);
48 READWRITE(VARINT(nTxOffset
));
51 CDiskTxPos(const CDiskBlockPos
&blockIn
, unsigned int nTxOffsetIn
) : CDiskBlockPos(blockIn
.nFile
, blockIn
.nPos
), nTxOffset(nTxOffsetIn
) {
59 CDiskBlockPos::SetNull();
64 /** CCoinsView backed by the coin database (chainstate/) */
65 class CCoinsViewDB
: public CCoinsView
70 CCoinsViewDB(size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false);
72 bool GetCoins(const uint256
&txid
, CCoins
&coins
) const;
73 bool HaveCoins(const uint256
&txid
) const;
74 uint256
GetBestBlock() const;
75 bool BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
);
76 CCoinsViewCursor
*Cursor() const;
79 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
80 class CCoinsViewDBCursor
: public CCoinsViewCursor
83 ~CCoinsViewDBCursor() {}
85 bool GetKey(uint256
&key
) const;
86 bool GetValue(CCoins
&coins
) const;
87 unsigned int GetValueSize() const;
93 CCoinsViewDBCursor(CDBIterator
* pcursorIn
, const uint256
&hashBlockIn
):
94 CCoinsViewCursor(hashBlockIn
), pcursor(pcursorIn
) {}
95 std::unique_ptr
<CDBIterator
> pcursor
;
96 std::pair
<char, uint256
> keyTmp
;
98 friend class CCoinsViewDB
;
101 /** Access to the block database (blocks/index/) */
102 class CBlockTreeDB
: public CDBWrapper
105 CBlockTreeDB(size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false);
107 CBlockTreeDB(const CBlockTreeDB
&);
108 void operator=(const CBlockTreeDB
&);
110 bool WriteBatchSync(const std::vector
<std::pair
<int, const CBlockFileInfo
*> >& fileInfo
, int nLastFile
, const std::vector
<const CBlockIndex
*>& blockinfo
);
111 bool ReadBlockFileInfo(int nFile
, CBlockFileInfo
&fileinfo
);
112 bool ReadLastBlockFile(int &nFile
);
113 bool WriteReindexing(bool fReindex
);
114 bool ReadReindexing(bool &fReindex
);
115 bool ReadTxIndex(const uint256
&txid
, CDiskTxPos
&pos
);
116 bool WriteTxIndex(const std::vector
<std::pair
<uint256
, CDiskTxPos
> > &list
);
117 bool WriteFlag(const std::string
&name
, bool fValue
);
118 bool ReadFlag(const std::string
&name
, bool &fValue
);
119 bool LoadBlockIndexGuts(boost::function
<CBlockIndex
*(const uint256
&)> insertBlockIndex
);
122 #endif // BITCOIN_TXDB_H