[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / src / txdb.h
blobec9f571b1355a058fa29480d33a7140b25e361c2
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.
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 class CBlockIndex;
19 class CCoinsViewDBCursor;
20 class uint256;
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) {
56 CDiskTxPos() {
57 SetNull();
60 void SetNull() {
61 CDiskBlockPos::SetNull();
62 nTxOffset = 0;
66 /** CCoinsView backed by the coin database (chainstate/) */
67 class CCoinsViewDB final : public CCoinsView
69 protected:
70 CDBWrapper db;
71 public:
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.
82 bool Upgrade();
83 size_t EstimateSize() const override;
86 /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
87 class CCoinsViewDBCursor: public CCoinsViewCursor
89 public:
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;
97 void Next() override;
99 private:
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
111 public:
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