You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

txdb.h 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.
  5. #ifndef BITCOIN_TXDB_H
  6. #define BITCOIN_TXDB_H
  7. #include "coins.h"
  8. #include "dbwrapper.h"
  9. #include "chain.h"
  10. #include <map>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. class CBlockIndex;
  15. class CCoinsViewDBCursor;
  16. class uint256;
  17. //! Compensate for extra memory peak (x1.5-x1.9) at flush time.
  18. static constexpr int DB_PEAK_USAGE_FACTOR = 2;
  19. //! No need to periodic flush if at least this much space still available.
  20. static constexpr int MAX_BLOCK_COINSDB_USAGE = 200 * DB_PEAK_USAGE_FACTOR;
  21. //! Always periodic flush if less than this much space still available.
  22. static constexpr int MIN_BLOCK_COINSDB_USAGE = 50 * DB_PEAK_USAGE_FACTOR;
  23. //! -dbcache default (MiB)
  24. static const int64_t nDefaultDbCache = 450;
  25. //! max. -dbcache (MiB)
  26. static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
  27. //! min. -dbcache (MiB)
  28. static const int64_t nMinDbCache = 4;
  29. //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
  30. static const int64_t nMaxBlockDBCache = 2;
  31. //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
  32. // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
  33. // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
  34. static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
  35. //! Max memory allocated to coin DB specific cache (MiB)
  36. static const int64_t nMaxCoinsDBCache = 8;
  37. struct CDiskTxPos : public CDiskBlockPos
  38. {
  39. unsigned int nTxOffset; // after header
  40. ADD_SERIALIZE_METHODS;
  41. template <typename Stream, typename Operation>
  42. inline void SerializationOp(Stream& s, Operation ser_action) {
  43. READWRITE(*(CDiskBlockPos*)this);
  44. READWRITE(VARINT(nTxOffset));
  45. }
  46. CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
  47. }
  48. CDiskTxPos() {
  49. SetNull();
  50. }
  51. void SetNull() {
  52. CDiskBlockPos::SetNull();
  53. nTxOffset = 0;
  54. }
  55. };
  56. /** CCoinsView backed by the coin database (chainstate/) */
  57. class CCoinsViewDB : public CCoinsView
  58. {
  59. protected:
  60. CDBWrapper db;
  61. public:
  62. CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
  63. bool GetCoins(const uint256 &txid, CCoins &coins) const;
  64. bool HaveCoins(const uint256 &txid) const;
  65. uint256 GetBestBlock() const;
  66. bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock);
  67. CCoinsViewCursor *Cursor() const;
  68. };
  69. /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
  70. class CCoinsViewDBCursor: public CCoinsViewCursor
  71. {
  72. public:
  73. ~CCoinsViewDBCursor() {}
  74. bool GetKey(uint256 &key) const;
  75. bool GetValue(CCoins &coins) const;
  76. unsigned int GetValueSize() const;
  77. bool Valid() const;
  78. void Next();
  79. private:
  80. CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
  81. CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
  82. std::unique_ptr<CDBIterator> pcursor;
  83. std::pair<char, uint256> keyTmp;
  84. friend class CCoinsViewDB;
  85. };
  86. /** Access to the block database (blocks/index/) */
  87. class CBlockTreeDB : public CDBWrapper
  88. {
  89. public:
  90. CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
  91. private:
  92. CBlockTreeDB(const CBlockTreeDB&);
  93. void operator=(const CBlockTreeDB&);
  94. public:
  95. bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
  96. bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
  97. bool ReadLastBlockFile(int &nFile);
  98. bool WriteReindexing(bool fReindex);
  99. bool ReadReindexing(bool &fReindex);
  100. bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
  101. bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
  102. bool WriteFlag(const std::string &name, bool fValue);
  103. bool ReadFlag(const std::string &name, bool &fValue);
  104. bool LoadBlockIndexGuts(std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
  105. };
  106. #endif // BITCOIN_TXDB_H