coinstatsindex.h raw

   1  // Copyright (c) 2020-2022 The Limenka developers
   2  // Distributed under the MIT software license, see the accompanying
   3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
   4  
   5  #ifndef LIMENKA_INDEX_COINSTATSINDEX_H
   6  #define LIMENKA_INDEX_COINSTATSINDEX_H
   7  
   8  #include <crypto/muhash.h>
   9  #include <index/base.h>
  10  
  11  class CBlockIndex;
  12  class CDBBatch;
  13  namespace kernel {
  14  struct CCoinsStats;
  15  }
  16  
  17  static constexpr bool DEFAULT_COINSTATSINDEX{false};
  18  
  19  /**
  20   * CoinStatsIndex maintains statistics on the UTXO set.
  21   */
  22  class CoinStatsIndex final : public BaseIndex
  23  {
  24  private:
  25      std::unique_ptr<BaseIndex::DB> m_db;
  26  
  27      MuHash3072 m_muhash;
  28      uint64_t m_transaction_output_count{0};
  29      uint64_t m_bogo_size{0};
  30      CAmount m_total_amount{0};
  31      CAmount m_total_subsidy{0};
  32      CAmount m_total_unspendable_amount{0};
  33      CAmount m_total_prevout_spent_amount{0};
  34      CAmount m_total_new_outputs_ex_coinbase_amount{0};
  35      CAmount m_total_coinbase_amount{0};
  36      CAmount m_total_unspendables_genesis_block{0};
  37      CAmount m_total_unspendables_bip30{0};
  38      CAmount m_total_unspendables_scripts{0};
  39      CAmount m_total_unspendables_unclaimed_rewards{0};
  40  
  41      [[nodiscard]] bool ReverseBlock(const CBlock& block, const CBlockIndex* pindex);
  42  
  43      bool AllowPrune() const override { return true; }
  44      bilingual_str GetDisableAction() const override { return _("set -coinstatsindex=0"); }
  45  
  46  protected:
  47      bool CustomInit(const std::optional<interfaces::BlockRef>& block) override;
  48  
  49      bool CustomCommit(CDBBatch& batch) override;
  50  
  51      bool CustomAppend(const interfaces::BlockInfo& block) override;
  52  
  53      bool CustomRewind(const interfaces::BlockRef& current_tip, const interfaces::BlockRef& new_tip) override;
  54  
  55      BaseIndex::DB& GetDB() const override { return *m_db; }
  56  
  57  public:
  58      // Constructs the index, which becomes available to be queried.
  59      explicit CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false);
  60  
  61      // Look up stats for a specific block using CBlockIndex
  62      std::optional<kernel::CCoinsStats> LookUpStats(const CBlockIndex& block_index) const;
  63  };
  64  
  65  /// The global UTXO set hash object.
  66  extern std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
  67  
  68  #endif // LIMENKA_INDEX_COINSTATSINDEX_H
  69