9#include <chainparams.h>
29#include <validation.h>
49 uint64_t transaction_output_count{0};
50 uint64_t bogo_size{0};
53 arith_uint256 total_prevout_spent_amount{0};
54 arith_uint256 total_new_outputs_ex_coinbase_amount{0};
55 arith_uint256 total_coinbase_amount{0};
56 CAmount total_unspendables_genesis_block{0};
57 CAmount total_unspendables_bip30{0};
58 CAmount total_unspendables_scripts{0};
59 CAmount total_unspendables_unclaimed_rewards{0};
63 uint256 prevout_spent, new_outputs, coinbase;
76 READWRITE(obj.total_unspendables_genesis_block);
78 READWRITE(obj.total_unspendables_scripts);
79 READWRITE(obj.total_unspendables_unclaimed_rewards);
97 auto old_path =
gArgs.GetDataDirNet() /
"indexes" /
"coinstats";
98 if (fs::exists(old_path)) {
100 LogWarning(
"Old version of coinstatsindex found at %s. This folder can be safely deleted unless you " \
101 "plan to downgrade your node to version 29 or lower.", fs::PathToString(old_path));
103 fs::path path{
gArgs.GetDataDirNet() /
"indexes" /
"coinstatsindex"};
104 fs::create_directories(path);
106 m_db = std::make_unique<CoinStatsIndex::DB>(path /
"db", n_cache_size, f_memory, f_wipe);
118 LogError(
"previous block header belongs to unexpected block %s; expected %s",
125 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
126 const auto& tx{block.
data->
vtx.at(i)};
127 const bool is_coinbase{tx->IsCoinBase()};
135 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
136 const CTxOut& out{tx->vout[j]};
137 const Coin coin{out, block.
height, is_coinbase};
138 const COutPoint outpoint{tx->GetHash(), j};
163 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
164 const Coin& coin{tx_undo.vprevout[j]};
165 const COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
191 std::pair<uint256, DBVal> value;
192 value.first = block.
hash;
207 value.second.muhash = out;
220 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
228 m_db->WriteBatch(batch);
270 LogError(
"Cannot read current %s state; index may be corrupted",
279 LogError(
"Cannot read current %s state; index may be corrupted",
286 if (entry.muhash != out) {
287 LogError(
"Cannot read current %s state; index may be corrupted",
329 std::pair<uint256, DBVal> read_out;
338 if (read_out.first != expected_block_hash) {
339 LogWarning(
"previous block header belongs to unexpected block %s; expected %s",
340 read_out.first.ToString(), expected_block_hash.
ToString());
343 LogError(
"previous block header not found; expected %s",
354 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
355 const auto& tx{block.
data->
vtx.at(i)};
356 const bool is_coinbase{tx->IsCoinBase()};
362 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
363 const CTxOut& out{tx->vout[j]};
364 const COutPoint outpoint{tx->GetHash(), j};
365 const Coin coin{out, block.
height, is_coinbase};
376 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
377 const Coin& coin{tx_undo.vprevout[j]};
378 const COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
387 Assert(read_out.second.muhash == out);
int64_t CAmount
Amount in satoshis (Can be negative).
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
BaseIndex(std::unique_ptr< interfaces::Chain > chain, std::string name)
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
std::vector< CTxUndo > vtxundo
Batch of changes queued to be written to a CDBWrapper.
void Write(const K &key, const V &value)
An outpoint - a combination of a transaction hash and an index n into its vout.
bool IsUnspendable() const
Returns whether the script is guaranteed to fail at execution, regardless of the initial stack.
An output of a transaction.
CTxOut out
unspent transaction output
arith_uint256 m_total_new_outputs_ex_coinbase_amount
interfaces::Chain::NotifyOptions CustomOptions() override
Return custom notification options for index.
bool CustomInit(const std::optional< interfaces::BlockRef > &block) override
Initialize internal state from the database and block index.
CAmount m_total_unspendables_genesis_block
bool CustomRemove(const interfaces::BlockInfo &block) override
Rewind index by one block during a chain reorg.
CoinStatsIndex(std::unique_ptr< interfaces::Chain > chain, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
CAmount m_total_unspendables_scripts
uint64_t m_transaction_output_count
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
arith_uint256 m_total_prevout_spent_amount
uint256 m_current_block_hash
std::optional< kernel::CCoinsStats > LookUpStats(const CBlockIndex &block_index) const
std::unique_ptr< BaseIndex::DB > m_db
bool CustomCommit(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
arith_uint256 m_total_coinbase_amount
CAmount m_total_unspendables_bip30
CAmount m_total_unspendables_unclaimed_rewards
bool RevertBlock(const interfaces::BlockInfo &block)
256-bit unsigned big integer.
std::string ToString() const
uint64_t GetLow64() const
static const uint256 ZERO
static constexpr uint8_t DB_MUHASH
std::unique_ptr< CoinStatsIndex > g_coin_stats_index
The global UTXO set hash object.
void RemoveCoinHash(MuHash3072 &muhash, const COutPoint &outpoint, const Coin &coin)
static void ApplyCoinHash(HashWriter &ss, const COutPoint &outpoint, const Coin &coin)
uint64_t GetBogoSize(const CScript &script_pub_key)
static bool LookUpOne(const CDBWrapper &db, const interfaces::BlockRef &block, DBVal &result)
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int height)
void RemoveCoinHash(MuHash3072 &muhash, const COutPoint &outpoint, const Coin &coin)
static void ApplyCoinHash(HashWriter &ss, const COutPoint &outpoint, const Coin &coin)
uint64_t GetBogoSize(const CScript &script_pub_key)
#define SER_WRITE(obj, code)
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
#define SER_READ(obj, code)
Block data sent with blockConnected, blockDisconnected notifications.
const uint256 * prev_hash
const CBlockUndo * undo_data
Options specifying which chain notifications are required.
bool connect_undo_data
Include undo data with block connected notifications.
bool disconnect_undo_data
Include undo data with block disconnected notifications.
bool disconnect_data
Include block data with block disconnected notifications.
arith_uint256 total_prevout_spent_amount
Total cumulative amount of prevouts spent up to and including this block.
std::optional< CAmount > total_amount
The total amount, or nullopt if an overflow occurred calculating it.
CAmount total_unspendables_scripts
Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and incl...
arith_uint256 total_coinbase_amount
Total cumulative amount of coinbase outputs up to and including this block.
uint64_t nTransactionOutputs
bool index_used
Signals if the coinstatsindex was used to retrieve the statistics.
CAmount total_unspendables_bip30
The two unspendable coinbase outputs total amount caused by BIP30.
CAmount total_unspendables_genesis_block
The unspendable coinbase amount from the genesis block.
arith_uint256 total_new_outputs_ex_coinbase_amount
Total cumulative amount of outputs created up to and including this block.
CAmount total_unspendables_unclaimed_rewards
Total cumulative amount of coins lost due to unclaimed miner rewards up to and including this block.
CAmount total_subsidy
Total cumulative amount of block subsidies up to and including this block.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool IsBIP30Unspendable(const uint256 &block_hash, int block_height)
Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30).