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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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 STARWELS_TXDB_H
  6. #define STARWELS_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. //! No need to periodic flush if at least this much space still available.
  18. static constexpr int MAX_BLOCK_COINSDB_USAGE = 10;
  19. //! -dbcache default (MiB)
  20. static const int64_t nDefaultDbCache = 450;
  21. //! -dbbatchsize default (bytes)
  22. static const int64_t nDefaultDbBatchSize = 16 << 20;
  23. //! max. -dbcache (MiB)
  24. static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
  25. //! min. -dbcache (MiB)
  26. static const int64_t nMinDbCache = 4;
  27. //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
  28. static const int64_t nMaxBlockDBCache = 2;
  29. //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
  30. // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
  31. // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
  32. static const int64_t nMaxBlockDBAndTxIndexCache = 1024;
  33. //! Max memory allocated to coin DB specific cache (MiB)
  34. static const int64_t nMaxCoinsDBCache = 8;
  35. struct CDiskTxPos : public CDiskBlockPos
  36. {
  37. unsigned int nTxOffset; // after header
  38. ADD_SERIALIZE_METHODS;
  39. template <typename Stream, typename Operation>
  40. inline void SerializationOp(Stream& s, Operation ser_action) {
  41. READWRITE(*(CDiskBlockPos*)this);
  42. READWRITE(VARINT(nTxOffset));
  43. }
  44. CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
  45. }
  46. CDiskTxPos() {
  47. SetNull();
  48. }
  49. void SetNull() {
  50. CDiskBlockPos::SetNull();
  51. nTxOffset = 0;
  52. }
  53. };
  54. /** CCoinsView backed by the coin database (chainstate/) */
  55. class CCoinsViewDB : public CCoinsView
  56. {
  57. protected:
  58. CDBWrapper db;
  59. public:
  60. CCoinsViewDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
  61. bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
  62. bool HaveCoin(const COutPoint &outpoint) const override;
  63. uint256 GetBestBlock() const override;
  64. std::vector<uint256> GetHeadBlocks() const override;
  65. bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
  66. CCoinsViewCursor *Cursor() const override;
  67. //! Attempt to update from an older database format. Returns whether an error occurred.
  68. bool Upgrade();
  69. size_t EstimateSize() const override;
  70. };
  71. /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
  72. class CCoinsViewDBCursor: public CCoinsViewCursor
  73. {
  74. public:
  75. ~CCoinsViewDBCursor() {}
  76. bool GetKey(COutPoint &key) const override;
  77. bool GetValue(Coin &coin) const override;
  78. unsigned int GetValueSize() const override;
  79. bool Valid() const override;
  80. void Next() override;
  81. private:
  82. CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
  83. CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
  84. std::unique_ptr<CDBIterator> pcursor;
  85. std::pair<char, COutPoint> keyTmp;
  86. friend class CCoinsViewDB;
  87. };
  88. /** Access to the block database (blocks/index/) */
  89. class CBlockTreeDB : public CDBWrapper
  90. {
  91. public:
  92. CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
  93. private:
  94. CBlockTreeDB(const CBlockTreeDB&);
  95. void operator=(const CBlockTreeDB&);
  96. public:
  97. bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
  98. bool ReadBlockFileInfo(int nFile, CBlockFileInfo &fileinfo);
  99. bool ReadLastBlockFile(int &nFile);
  100. bool WriteReindexing(bool fReindex);
  101. bool ReadReindexing(bool &fReindex);
  102. bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos);
  103. bool WriteTxIndex(const std::vector<std::pair<uint256, CDiskTxPos> > &list);
  104. bool WriteFlag(const std::string &name, bool fValue);
  105. bool ReadFlag(const std::string &name, bool &fValue);
  106. bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
  107. };
  108. #endif // STARWELS_TXDB_H