Repository: bitcoin/bitcoin
File: src/kernel/coinstats.h (C/C++)

Code before:

    //! Signals if the coinstatsindex was used to retrieve the statistics.
    bool index_used{false};

    // Following values are only available from coinstats index

    //! Total cumulative amount of block subsidies up to and including this block
    CAmount total_subsidy{0};
    //! Total cumulative amount of unspendable coins up to and including this block
    CAmount total_unspendable_amount{0};
    //! Total cumulative amount of prevouts spent up to and including this block
    CAmount total_prevout_spent_amount{0};
    //! Total cumulative amount of outputs created up to and including this block
    CAmount total_new_outputs_ex_coinbase_amount{0};
    //! Total cumulative amount of coinbase outputs up to and including this block
    CAmount total_coinbase_amount{0};
    //! The unspendable coinbase amount from the genesis block
    CAmount total_unspendables_genesis_block{0};
    //! The two unspendable coinbase outputs total amount caused by BIP30
    CAmount total_unspendables_bip30{0};
    //! Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and including this block
    CAmount total_unspendables_scripts{0};
    //! Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block
    CAmount total_unspendables_unclaimed_rewards{0};

    CCoinsStats() = default;
    CCoinsStats(int block_height, const uint256& block_hash);
};

uint64_t GetBogoSize(const CScript& script_pub_key);

void ApplyCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin);
void RemoveCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin);

std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, node::BlockManager& blockman, const std::function<void()>& interruption_point = {});
} // namespace kernel

#endif // BITCOIN_KERNEL_COINSTATS_H


Review comment (suggestion):
Keeping the `block_unspendables_*` as cumulative totals would be more flexible, and would be nice to be able to directly query. (dropping `total_unspendable_amount` would be possible in that case, considering it's just the sum of these totals)

Likewise keeping `total_subsidy`.

With those values, `total_subsidy = total_amount + total_unspendable_amount` should be an accounting identity, with `total_amount` also matching a manual sum of values in the utxo set, and `total_subsidy` being precisely satoshi's issuance schedule. Having that be conveniently indexed would be good for quick auditing IMO.

Code after:
    uint64_t coins_count{0};

    //! Signals if the coinstatsindex was used to retrieve the statistics.
    bool index_used{false};

    // Following values are only available from coinstats index

    //! Total cumulative amount of block subsidies up to and including this block
    CAmount total_subsidy{0};
    //! The unspendable coinbase amount from the genesis block
    CAmount total_unspendables_genesis_block{0};
    //! The two unspendable coinbase outputs total amount caused by BIP30
    CAmount total_unspendables_bip30{0};
    //! Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and including this block
    CAmount total_unspendables_scripts{0};
    //! Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block
    CAmount total_unspendables_unclaimed_rewards{0};

    // Despite containing amounts the following values use a uint256 type to prevent overflowing

    //! Total cumulative amount of prevouts spent up to and including this block
    arith_uint256 total_prevout_spent_amount{0};
    //! Total cumulative amount of outputs created up to and including this block
    arith_uint256 total_new_outputs_ex_coinbase_amount{0};
    //! Total cumulative amount of coinbase outputs up to and including this block
    arith_uint256 total_coinbase_amount{0};

    CCoinsStats() = default;
    CCoinsStats(int block_height, const uint256& block_hash);
};

uint64_t GetBogoSize(const CScript& script_pub_key);

void ApplyCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin);
void RemoveCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin);

std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, node::BlockManager& blockman, const std::function<void()>& interruption_point = {});
} // namespace kernel

#endif // BITCOIN_KERNEL_COINSTATS_H