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>
19 class CCoinsViewDBCursor
;
22 //! No need to periodic flush if at least this much space still available.
23 static constexpr int MAX_BLOCK_COINSDB_USAGE
= 10;
24 //! -dbcache default (MiB)
25 static const int64_t nDefaultDbCache
= 450;
26 //! -dbbatchsize default (bytes)
27 static const int64_t nDefaultDbBatchSize
= 16 << 20;
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
) {
61 CDiskBlockPos::SetNull();
66 /** CCoinsView backed by the coin database (chainstate/) */
67 class CCoinsViewDB final
: public CCoinsView
72 explicit 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 std::vector
<uint256
> GetHeadBlocks() const override
;
78 bool BatchWrite(CCoinsMap
&mapCoins
, const uint256
&hashBlock
) override
;
79 CCoinsViewCursor
*Cursor() const override
;
81 //! Attempt to update from an older database format. Returns whether an error occurred.
83 size_t EstimateSize() const override
;
86 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
87 class CCoinsViewDBCursor
: public CCoinsViewCursor
90 ~CCoinsViewDBCursor() {}
92 bool GetKey(COutPoint
&key
) const override
;
93 bool GetValue(Coin
&coin
) const override
;
94 unsigned int GetValueSize() const override
;
96 bool Valid() const override
;
100 CCoinsViewDBCursor(CDBIterator
* pcursorIn
, const uint256
&hashBlockIn
):
101 CCoinsViewCursor(hashBlockIn
), pcursor(pcursorIn
) {}
102 std::unique_ptr
<CDBIterator
> pcursor
;
103 std::pair
<char, COutPoint
> keyTmp
;
105 friend class CCoinsViewDB
;
108 /** Access to the block database (blocks/index/) */
109 class CBlockTreeDB
: public CDBWrapper
112 explicit CBlockTreeDB(size_t nCacheSize
, bool fMemory
= false, bool fWipe
= false);
114 CBlockTreeDB(const CBlockTreeDB
&) = delete;
115 CBlockTreeDB
& operator=(const CBlockTreeDB
&) = delete;
117 bool WriteBatchSync(const std::vector
<std::pair
<int, const CBlockFileInfo
*> >& fileInfo
, int nLastFile
, const std::vector
<const CBlockIndex
*>& blockinfo
);
118 bool ReadBlockFileInfo(int nFile
, CBlockFileInfo
&info
);
119 bool ReadLastBlockFile(int &nFile
);
120 bool WriteReindexing(bool fReindexing
);
121 bool ReadReindexing(bool &fReindexing
);
122 bool ReadTxIndex(const uint256
&txid
, CDiskTxPos
&pos
);
123 bool WriteTxIndex(const std::vector
<std::pair
<uint256
, CDiskTxPos
> > &vect
);
124 bool WriteFlag(const std::string
&name
, bool fValue
);
125 bool ReadFlag(const std::string
&name
, bool &fValue
);
126 bool LoadBlockIndexGuts(const Consensus::Params
& consensusParams
, std::function
<CBlockIndex
*(const uint256
&)> insertBlockIndex
);
129 #endif // BITCOIN_TXDB_H