coinstatsindex.h
1 // Copyright (c) 2020-present The Bitcoin Core 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 BITCOIN_INDEX_COINSTATSINDEX_H 6 #define BITCOIN_INDEX_COINSTATSINDEX_H 7 8 #include <arith_uint256.h> 9 #include <consensus/amount.h> 10 #include <crypto/muhash.h> 11 #include <index/base.h> 12 #include <interfaces/chain.h> 13 #include <uint256.h> 14 15 #include <cstddef> 16 #include <cstdint> 17 #include <memory> 18 #include <optional> 19 20 class CBlockIndex; 21 namespace kernel { 22 struct CCoinsStats; 23 } 24 25 static constexpr bool DEFAULT_COINSTATSINDEX{false}; 26 27 /** 28 * CoinStatsIndex maintains statistics on the UTXO set. 29 */ 30 class CoinStatsIndex final : public BaseIndex 31 { 32 private: 33 std::unique_ptr<BaseIndex::DB> m_db; 34 35 MuHash3072 m_muhash; 36 uint64_t m_transaction_output_count{0}; 37 uint64_t m_bogo_size{0}; 38 CAmount m_total_amount{0}; 39 CAmount m_total_subsidy{0}; 40 arith_uint256 m_total_prevout_spent_amount{0}; 41 arith_uint256 m_total_new_outputs_ex_coinbase_amount{0}; 42 arith_uint256 m_total_coinbase_amount{0}; 43 CAmount m_total_unspendables_genesis_block{0}; 44 CAmount m_total_unspendables_bip30{0}; 45 CAmount m_total_unspendables_scripts{0}; 46 CAmount m_total_unspendables_unclaimed_rewards{0}; 47 48 uint256 m_current_block_hash{}; 49 50 [[nodiscard]] bool RevertBlock(const interfaces::BlockInfo& block); 51 52 bool AllowPrune() const override { return true; } 53 54 protected: 55 bool CustomInit(const std::optional<interfaces::BlockRef>& block) override; 56 57 bool CustomCommit(CDBBatch& batch) override; 58 59 bool CustomAppend(const interfaces::BlockInfo& block) override; 60 61 bool CustomRemove(const interfaces::BlockInfo& block) override; 62 63 BaseIndex::DB& GetDB() const override { return *m_db; } 64 65 public: 66 // Constructs the index, which becomes available to be queried. 67 explicit CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 68 69 interfaces::Chain::NotifyOptions CustomOptions() override; 70 71 // Look up stats for a specific block using CBlockIndex 72 std::optional<kernel::CCoinsStats> LookUpStats(const CBlockIndex& block_index) const; 73 }; 74 75 /// The global UTXO set hash object. 76 extern std::unique_ptr<CoinStatsIndex> g_coin_stats_index; 77 78 #endif // BITCOIN_INDEX_COINSTATSINDEX_H