Line data Source code
1 : // Copyright (c) 2020-2021 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 <chain.h> 9 : #include <crypto/muhash.h> 10 : #include <flatfile.h> 11 : #include <index/base.h> 12 : #include <kernel/coinstats.h> 13 : 14 : static constexpr bool DEFAULT_COINSTATSINDEX{false}; 15 : 16 : /** 17 : * CoinStatsIndex maintains statistics on the UTXO set. 18 : */ 19 : class CoinStatsIndex final : public BaseIndex 20 : { 21 : private: 22 : std::string m_name; 23 : std::unique_ptr<BaseIndex::DB> m_db; 24 : 25 : MuHash3072 m_muhash; 26 : uint64_t m_transaction_output_count{0}; 27 : uint64_t m_bogo_size{0}; 28 : CAmount m_total_amount{0}; 29 : CAmount m_total_subsidy{0}; 30 : CAmount m_total_unspendable_amount{0}; 31 : CAmount m_total_prevout_spent_amount{0}; 32 : CAmount m_total_new_outputs_ex_coinbase_amount{0}; 33 : CAmount m_total_coinbase_amount{0}; 34 : CAmount m_total_unspendables_genesis_block{0}; 35 : CAmount m_total_unspendables_bip30{0}; 36 : CAmount m_total_unspendables_scripts{0}; 37 : CAmount m_total_unspendables_unclaimed_rewards{0}; 38 : 39 : [[nodiscard]] bool ReverseBlock(const CBlock& block, const CBlockIndex* pindex); 40 : 41 601 : bool AllowPrune() const override { return true; } 42 : 43 : protected: 44 : bool Init() override; 45 : 46 : bool CommitInternal(CDBBatch& batch) override; 47 : 48 : bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override; 49 : 50 : bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override; 51 : 52 489 : BaseIndex::DB& GetDB() const override { return *m_db; } 53 : 54 782 : const char* GetName() const override { return "coinstatsindex"; } 55 : 56 : public: 57 : // Constructs the index, which becomes available to be queried. 58 : explicit CoinStatsIndex(size_t n_cache_size, bool f_memory = false, bool f_wipe = false); 59 : 60 : // Look up stats for a specific block using CBlockIndex 61 : std::optional<kernel::CCoinsStats> LookUpStats(const CBlockIndex* block_index) const; 62 : }; 63 : 64 : /// The global UTXO set hash object. 65 : extern std::unique_ptr<CoinStatsIndex> g_coin_stats_index; 66 : 67 : #endif // BITCOIN_INDEX_COINSTATSINDEX_H