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 //! Compensate for extra memory peak (x1.5-x1.9) at flush time.
25 static constexpr int DB_PEAK_USAGE_FACTOR
= 2;
26 //! No need to periodic flush if at least this much space still available.
27 static constexpr int MAX_BLOCK_COINSDB_USAGE
= 200 * DB_PEAK_USAGE_FACTOR
;
28 //! Always periodic flush if less than this much space still available.
29 static constexpr int MIN_BLOCK_COINSDB_USAGE
= 50 * DB_PEAK_USAGE_FACTOR
;
30 //! -dbcache default (MiB)
31 static const int64_t nDefaultDbCache
= 450;
32 //! max. -dbcache (MiB)
33 static const int64_t nMaxDbCache
= sizeof(void*) > 4 ? 16384 : 1024;
34 //! min. -dbcache (MiB)
35 static const int64_t nMinDbCache
= 4;
36 //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
37 static const int64_t nMaxBlockDBCache
= 2;
38 //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
39 // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
40 // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
41 static const int64_t nMaxBlockDBAndTxIndexCache
= 1024;
42 //! Max memory allocated to coin DB specific cache (MiB)
43 static const int64_t nMaxCoinsDBCache
= 8;
45 struct CDiskTxPos
: public CDiskBlockPos
47 unsigned int nTxOffset
; // after header
49 ADD_SERIALIZE_METHODS
;
51 template <typename Stream
, typename Operation
>
52 inline void SerializationOp(Stream
& s
, Operation ser_action
) {
53 READWRITE(*(CDiskBlockPos
*)this);
54 READWRITE(VARINT(nTxOffset
));
57 CDiskTxPos(const CDiskBlockPos
&blockIn
, unsigned int nTxOffsetIn
) : CDiskBlockPos(blockIn
.nFile
, blockIn
.nPos
), nTxOffset(nTxOffsetIn
) {
65 CDiskBlockPos::SetNull();
70 /** CCoinsView backed by the coin database (chainstate/) */
71 class CCoinsViewDB
: public CCoinsView
76 CCoinsViewDB(size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false);
78 bool GetCoins(const uint256
&txid
, CCoins
&coins
) const;
79 bool HaveCoins(const uint256
&txid
) const;
80 uint256
GetBestBlock() const;
81 bool BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
);
82 CCoinsViewCursor
*Cursor() const;
85 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
86 class CCoinsViewDBCursor
: public CCoinsViewCursor
89 ~CCoinsViewDBCursor() {}
91 bool GetKey(uint256
&key
) const;
92 bool GetValue(CCoins
&coins
) const;
93 unsigned int GetValueSize() const;
99 CCoinsViewDBCursor(CDBIterator
* pcursorIn
, const uint256
&hashBlockIn
):
100 CCoinsViewCursor(hashBlockIn
), pcursor(pcursorIn
) {}
101 std::unique_ptr
<CDBIterator
> pcursor
;
102 std::pair
<char, uint256
> keyTmp
;
104 friend class CCoinsViewDB
;
107 /** Access to the block database (blocks/index/) */
108 class CBlockTreeDB
: public CDBWrapper
111 CBlockTreeDB(size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false);
113 CBlockTreeDB(const CBlockTreeDB
&);
114 void operator=(const CBlockTreeDB
&);
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(boost::function
<CBlockIndex
*(const uint256
&)> insertBlockIndex
);
128 #endif // BITCOIN_TXDB_H