text: // Copyright (c) 2009-2010 Satoshi Nakamoto
  // Copyright (c) 2009-2018 The Bitcoin Core developers
  // Distributed under the MIT software license, see the accompanying
  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  
  #ifndef BITCOIN_TXDB_H
  #define BITCOIN_TXDB_H
  
  #include <coins.h>
  #include <dbwrapper.h>
  #include <chain.h>
  #include <primitives/block.h>
  #include <libdevcore/Common.h>
  #include <libdevcore/FixedHash.h>
  
  #include <map>
  #include <memory>
  #include <string>
  #include <utility>
  #include <vector>
  
  class CBlockIndex;
  class CCoinsViewDBCursor;
  class uint256;
  struct CHeightTxIndexKey;
  struct CHeightTxIndexIteratorKey;
  #ifdef ENABLE_BITCORE_RPC
  //////////////////////////////////// //qtumcash
  struct CAddressIndexKey;
  struct CAddressUnspentKey;
  struct CAddressUnspentValue;
  struct CMempoolAddressDeltaKey;
  struct CTimestampIndexKey;
  struct CTimestampBlockIndexKey;
  struct CTimestampBlockIndexValue;
  ////////////////////////////////////
  #endif
  
  using valtype = std::vector<unsigned char>;
  
  //! Compensate for extra memory peak (x1.5-x1.9) at flush time.
  static constexpr int DB_PEAK_USAGE_FACTOR = 2;
  //! No need to periodic flush if at least this much space still available.
  static constexpr int MAX_BLOCK_COINSDB_USAGE = 10 * DB_PEAK_USAGE_FACTOR;
  //! -dbcache default (MiB)
  static const int64_t nDefaultDbCache = 450;
  //! -dbbatchsize default (bytes)
  static const int64_t nDefaultDbBatchSize = 16 << 20;
  //! max. -dbcache (MiB)
  static const int64_t nMaxDbCache = sizeof(void*) > 4 ? 16384 : 1024;
  //! min. -dbcache (MiB)
  static const int64_t nMinDbCache = 4;
  //! Max memory allocated to block tree DB specific cache, if no -txindex (MiB)
  static const int64_t nMaxBlockDBCache = 2;
  //! Max memory allocated to block tree DB specific cache, if -txindex (MiB)
  // Unlike for the UTXO database, for the txindex scenario the leveldb cache make
  // a meaningful difference: https://github.com/bitcoin/bitcoin/pull/8273#issuecomment-229601991
  static const int64_t nMaxTxIndexCache = 1024;
  //! Max memory allocated to all block filter index caches combined in MiB.
  static const int64_t max_filter_index_cache = 1024;
  //! Max memory allocated to coin DB specific cache (MiB)
  static const int64_t nMaxCoinsDBCache = 8;
  
  struct CDiskTxPos : public FlatFilePos
  {
      unsigned int nTxOffset; // after header
  
      ADD_SERIALIZE_METHODS;
  
      template <typename Stream, typename Operation>
      inline void SerializationOp(Stream& s, Operation ser_action) {
          READWRITEAS(FlatFilePos, *this);
          READWRITE(VARINT(nTxOffset));
      }
  
      CDiskTxPos(const FlatFilePos &blockIn, unsigned int nTxOffsetIn) : FlatFilePos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
      }
  
      CDiskTxPos() {
          SetNull();
      }
  
      void SetNull() {
          FlatFilePos::SetNull();
          nTxOffset = 0;
      }
  };
  
  /** CCoinsView backed by the coin database (chainstate/) */
  class CCoinsViewDB final : public CCoinsView
  {
  protected:
      CDBWrapper db;
  public:
      /**
       * @param[in] ldb_path    Location in the filesystem where leveldb data will be stored.
       */
      explicit CCoinsViewDB(fs::path ldb_path, size_t nCacheSize, bool fMemory, bool fWipe);
  
      bool GetCoin(const COutPoint &outpoint, Coin &coin) const override;
      bool HaveCoin(const COutPoint &outpoint) const override;
      uint256 GetBestBlock() const override;
      std::vector<uint256> GetHeadBlocks() const override;
      bool BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlock) override;
      CCoinsViewCursor *Cursor() const override;
  
      //! Attempt to update from an older database format. Returns whether an error occurred.
      bool Upgrade();
      size_t EstimateSize() const override;
  };
  
  /** Specialization of CCoinsViewCursor to iterate over a CCoinsViewDB */
  class CCoinsViewDBCursor: public CCoinsViewCursor
  {
  public:
      ~CCoinsViewDBCursor() {}
  
      bool GetKey(COutPoint &key) const override;
      bool GetValue(Coin &coin) const override;
      unsigned int GetValueSize() const override;
  
      bool Valid() const override;
      void Next() override;
  
  private:
      CCoinsViewDBCursor(CDBIterator* pcursorIn, const uint256 &hashBlockIn):
          CCoinsViewCursor(hashBlockIn), pcursor(pcursorIn) {}
      std::unique_ptr<CDBIterator> pcursor;
      std::pair<char, COutPoint> keyTmp;
  
      friend class CCoinsViewDB;
  };
  
  /** Access to the block database (blocks/index/) */
  class CBlockTreeDB : public CDBWrapper
  {
  public:
      explicit CBlockTreeDB(size_t nCacheSize, bool fMemory = false, bool fWipe = false);
  
      bool WriteBatchSync(const std::vector<std::pair<int, const CBlockFileInfo*> >& fileInfo, int nLastFile, const std::vector<const CBlockIndex*>& blockinfo);
      bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info);
      bool ReadLastBlockFile(int &nFile);
      bool WriteReindexing(bool fReindexing);
      void ReadReindexing(bool &fReindexing);
      bool WriteFlag(const std::string &name, bool fValue);
      bool ReadFlag(const std::string &name, bool &fValue);
      bool LoadBlockIndexGuts(const Consensus::Params& consensusParams, std::function<CBlockIndex*(const uint256&)> insertBlockIndex);
  
      ////////////////////////////////////////////////////////////////////////////// // qtumcash
      bool WriteHeightIndex(const CHeightTxIndexKey &heightIndex, const std::vector<uint256>& hash);
  
      /**
       * Iterates through blocks by height, starting from low.
       *
       * @param low start iterating from this block height
       * @param high end iterating at this block height (ignored if <= 0)
       * @param minconf stop iterating of the block height does not have enough confirmations (ignored if <= 0)
       * @param blocksOfHashes transaction hashes in blocks iterated are collected into this vector.
       * @param addresses filter out a block unless it matches one of the addresses in this set.
       *
       * @return the height of the latest block iterated. 0 if no block is iterated.
       */
      int ReadHeightIndex(int low, int high, int minconf,
              std::vector<std::vector<uint256>> &blocksOfHashes,
              std::set<dev::h160> const &addresses);
      bool EraseHeightIndex(const unsigned int &height);
      bool WipeHeightIndex();
  
  
      bool WriteStakeIndex(unsigned int height, uint160 address);
      bool ReadStakeIndex(unsigned int height, uint160& address);
      bool ReadStakeIndex(unsigned int high, unsigned int low, std::vector<uint160> addresses);
      bool EraseStakeIndex(unsigned int height);
  
  #ifdef ENABLE_BITCORE_RPC
      // Block explorer database functions
      bool WriteAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect);
      bool EraseAddressIndex(const std::vector<std::pair<CAddressIndexKey, CAmount> > &vect);
      bool ReadAddressIndex(uint256 addressHash, int type,
                          std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex,
                          int start = 0, int end = 0);
      bool UpdateAddressUnspentIndex(const std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue > >&vect);
      bool ReadAddressUnspentIndex(uint256 addressHash, int type,
                                  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &vect);
      bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex);
      bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &vect);
      bool WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts);
      bool ReadTimestampBlockIndex(const uint256 &hash, unsigned int &logicalTS);
      bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value);
      bool UpdateSpentIndex(const std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> >&vect);
      bool blockOnchainActive(const uint256 &hash);
  #endif
  
      //////////////////////////////////////////////////////////////////////////////
  };
  
  //////////////////////////////////////////////////////////// // qtumcash
  struct CHeightTxIndexIteratorKey {
      unsigned int height;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 4;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata32be(s, height);
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          height = ser_readdata32be(s);
      }
  
      CHeightTxIndexIteratorKey(unsigned int _height) {
          height = _height;
      }
  
      CHeightTxIndexIteratorKey() {
          SetNull();
      }
  
      void SetNull() {
          height = 0;
      }
  };
  
  struct CHeightTxIndexKey {
      unsigned int height;
      dev::h160 address;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 24;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata32be(s, height);
          s << address.asBytes();
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          height = ser_readdata32be(s);
          valtype tmp;
          s >> tmp;
          address = dev::h160(tmp);
      }
  
      CHeightTxIndexKey(unsigned int _height, dev::h160 _address) {
          height = _height;
          address = _address;
      }
  
      CHeightTxIndexKey() {
          SetNull();
      }
  
      void SetNull() {
          height = 0;
          address.clear();
      }
  };
  
  #ifdef ENABLE_BITCORE_RPC
  struct CTimestampIndexIteratorKey {
      unsigned int timestamp;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 4;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata32be(s, timestamp);
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          timestamp = ser_readdata32be(s);
      }
  
      CTimestampIndexIteratorKey(unsigned int time) {
          timestamp = time;
      }
  
      CTimestampIndexIteratorKey() {
          SetNull();
      }
  
      void SetNull() {
          timestamp = 0;
      }
  };
  
  struct CTimestampIndexKey {
      unsigned int timestamp;
      uint256 blockHash;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 36;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata32be(s, timestamp);
          blockHash.Serialize(s);
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          timestamp = ser_readdata32be(s);
          blockHash.Unserialize(s);
      }
  
      CTimestampIndexKey(unsigned int time, uint256 hash) {
          timestamp = time;
          blockHash = hash;
      }
  
      CTimestampIndexKey() {
          SetNull();
      }
  
      void SetNull() {
          timestamp = 0;
          blockHash.SetNull();
      }
  };
  
  struct CTimestampBlockIndexKey {
      uint256 blockHash;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 32;
      }
  
      template<typename Stream>
      void Serialize(Stream& s) const {
          blockHash.Serialize(s);
      }
  
      template<typename Stream>
      void Unserialize(Stream& s) {
          blockHash.Unserialize(s);
      }
  
      CTimestampBlockIndexKey(uint256 hash) {
          blockHash = hash;
      }
  
      CTimestampBlockIndexKey() {
          SetNull();
      }
  
      void SetNull() {
          blockHash.SetNull();
      }
  };
  
  struct CTimestampBlockIndexValue {
      unsigned int ltimestamp;
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 4;
      }
  
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata32be(s, ltimestamp);
      }
  
      template<typename Stream>
      void Unserialize(Stream& s) {
          ltimestamp = ser_readdata32be(s);
      }
  
      CTimestampBlockIndexValue (unsigned int time) {
          ltimestamp = time;
      }
  
      CTimestampBlockIndexValue() {
          SetNull();
      }
  
      void SetNull() {
          ltimestamp = 0;
      }
  };
  
  struct CAddressUnspentKey {
      unsigned int type;
      uint256 hashBytes;
      uint256 txhash;
      size_t index;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 69;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata8(s, type);
          hashBytes.Serialize(s);
          txhash.Serialize(s);
          ser_writedata32(s, index);
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          type = ser_readdata8(s);
          hashBytes.Unserialize(s);
          txhash.Unserialize(s);
          index = ser_readdata32(s);
      }
  
      CAddressUnspentKey(unsigned int addressType, uint256 addressHash, uint256 txid, size_t indexValue) {
          type = addressType;
          hashBytes = addressHash;
          txhash = txid;
          index = indexValue;
      }
  
      CAddressUnspentKey() {
          SetNull();
      }
  
      void SetNull() {
          type = 0;
          hashBytes.SetNull();
          txhash.SetNull();
          index = 0;
      }
  };
  
  struct CAddressUnspentValue {
      CAmount satoshis;
      CScript script;
      int blockHeight;
      bool coinStake;
  
      ADD_SERIALIZE_METHODS;
  
      template <typename Stream, typename Operation>
      inline void SerializationOp(Stream& s, Operation ser_action) {
          READWRITE(satoshis);
          READWRITE(*(CScriptBase*)(&script));
          READWRITE(blockHeight);
          READWRITE(coinStake);
      }
  
      CAddressUnspentValue(CAmount sats, CScript scriptPubKey, int height, bool isStake) {
          satoshis = sats;
          script = scriptPubKey;
          blockHeight = height;
          coinStake = isStake;
      }
  
      CAddressUnspentValue() {
          SetNull();
      }
  
      void SetNull() {
          satoshis = -1;
          script.clear();
          blockHeight = 0;
          coinStake = false;
      }
  
      bool IsNull() const {
          return (satoshis == -1);
      }
  };
  
  struct CAddressIndexKey {
      unsigned int type;
      uint256 hashBytes;
      int blockHeight;
      unsigned int txindex;
      uint256 txhash;
      size_t index;
      bool spending;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 78;
      }
      template<typename Stream>
     void Serialize(Stream& s) const {
          ser_writedata8(s, type);
          hashBytes.Serialize(s);
          // Heights are stored big-endian for key sorting in LevelDB
          ser_writedata32be(s, blockHeight);
          ser_writedata32be(s, txindex);
          txhash.Serialize(s);
          ser_writedata32(s, index);
          char f = spending;
          ser_writedata8(s, f);
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          type = ser_readdata8(s);
          hashBytes.Unserialize(s);
          blockHeight = ser_readdata32be(s);
          txindex = ser_readdata32be(s);
          txhash.Unserialize(s);
          index = ser_readdata32(s);
          char f = ser_readdata8(s);
          spending = f;
      }
  
      CAddressIndexKey(unsigned int addressType, uint256 addressHash, int height, int blockindex,
                       uint256 txid, size_t indexValue, bool isSpending) {
          type = addressType;
          hashBytes = addressHash;
          blockHeight = height;
          txindex = blockindex;
          txhash = txid;
          index = indexValue;
          spending = isSpending;
      }
  
      CAddressIndexKey() {
          SetNull();
      }
  
      void SetNull() {
          type = 0;
          hashBytes.SetNull();
          blockHeight = 0;
          txindex = 0;
          txhash.SetNull();
          index = 0;
          spending = false;
      }
  
  };
  
  struct CAddressIndexIteratorHeightKey {
      unsigned int type;
      uint256 hashBytes;
      int blockHeight;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 37;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata8(s, type);
          hashBytes.Serialize(s);
          ser_writedata32be(s, blockHeight);
     }
      template<typename Stream>
      void Unserialize(Stream& s) {
          type = ser_readdata8(s);
          hashBytes.Unserialize(s);
          blockHeight = ser_readdata32be(s);
      }
  
      CAddressIndexIteratorHeightKey(unsigned int addressType, uint256 addressHash, int height) {
          type = addressType;
          hashBytes = addressHash;
          blockHeight = height;
      }
  
      CAddressIndexIteratorHeightKey() {
          SetNull();
      }
  
      void SetNull() {
          type = 0;
          hashBytes.SetNull();
          blockHeight = 0;
      }
  };
  
  struct CAddressIndexIteratorKey {
      unsigned int type;
      uint256 hashBytes;
  
      size_t GetSerializeSize(int nType, int nVersion) const {
          return 33;
      }
      template<typename Stream>
      void Serialize(Stream& s) const {
          ser_writedata8(s, type);
          hashBytes.Serialize(s);
      }
      template<typename Stream>
      void Unserialize(Stream& s) {
          type = ser_readdata8(s);
          hashBytes.Unserialize(s);
      }
  
      CAddressIndexIteratorKey(unsigned int addressType, uint256 addressHash) {
          type = addressType;
          hashBytes = addressHash;
      }
  
      CAddressIndexIteratorKey() {
          SetNull();
      }
  
      void SetNull() {
          type = 0;
          hashBytes.SetNull();
      }
  };
  #endif
  ////////////////////////////////////////////////////////////
  
  #endif // BITCOIN_TXDB_H
  