9#include <chainparams.h>
29#include <validation.h>
48 uint64_t transaction_output_count{0};
55 CAmount total_unspendables_genesis_block{0};
56 CAmount total_unspendables_bip30{0};
57 CAmount total_unspendables_scripts{0};
58 CAmount total_unspendables_unclaimed_rewards{0};
90 :
BaseIndex(
std::move(chain),
"coinstatsindex",
"coinstatsidx")
99 LogWarning(
"Old version of coinstatsindex found at %s. This folder can be safely deleted unless you " \
103 fs::create_directories(path);
117 LogError(
"previous block header belongs to unexpected block %s; expected %s",
124 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
125 const auto& tx{block.
data->
vtx.at(i)};
126 const bool is_coinbase{tx->IsCoinBase()};
135 const CTxOut& out{tx->vout[
j]};
136 const Coin coin{out, block.
height, is_coinbase};
140 if (coin.out.scriptPubKey.IsUnspendable()) {
145 ApplyCoinHash(
m_muhash, outpoint, coin);
162 for (
size_t j = 0;
j <
tx_undo.vprevout.size(); ++
j) {
164 const COutPoint outpoint{tx->vin[
j].prevout.
hash, tx->vin[
j].prevout.n};
166 RemoveCoinHash(
m_muhash, outpoint, coin);
190 std::pair<uint256, DBVal> value;
191 value.first = block.
hash;
206 value.second.muhash = out;
219 std::unique_ptr<CDBIterator>
db_it(
m_db->NewIterator());
223 if (!index_util::CopyHeightIndexToHashIndex<DBVal>(*
db_it, batch,
m_name, block.
height)) {
227 m_db->WriteBatch(batch);
239 stats.index_used =
true;
246 stats.hashSerialized = entry.muhash;
247 stats.nTransactionOutputs = entry.transaction_output_count;
248 stats.nBogoSize = entry.bogo_size;
249 stats.total_amount = entry.total_amount;
250 stats.total_subsidy = entry.total_subsidy;
251 stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
252 stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
253 stats.total_coinbase_amount = entry.total_coinbase_amount;
254 stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
255 stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
256 stats.total_unspendables_scripts = entry.total_unspendables_scripts;
257 stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
269 LogError(
"Cannot read current %s state; index may be corrupted",
278 LogError(
"Cannot read current %s state; index may be corrupted",
285 if (entry.muhash != out) {
286 LogError(
"Cannot read current %s state; index may be corrupted",
338 LogWarning(
"previous block header belongs to unexpected block %s; expected %s",
342 LogError(
"previous block header not found; expected %s",
353 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
354 const auto& tx{block.
data->
vtx.at(i)};
355 const bool is_coinbase{tx->IsCoinBase()};
362 const CTxOut& out{tx->vout[
j]};
364 const Coin coin{out, block.
height, is_coinbase};
366 if (!coin.out.scriptPubKey.IsUnspendable()) {
367 RemoveCoinHash(
m_muhash, outpoint, coin);
375 for (
size_t j = 0;
j <
tx_undo.vprevout.size(); ++
j) {
377 const COutPoint outpoint{tx->vin[
j].prevout.
hash, tx->vin[
j].prevout.n};
378 ApplyCoinHash(
m_muhash, outpoint, coin);
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.
fs::path GetDataDirNet() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Get data directory path with appended network identifier.
Base class for indices of blockchain data.
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
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.
An output of a transaction.
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)
void Finalize(uint256 &out) noexcept
256-bit unsigned big integer.
std::string ToString() 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.
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
static bool LookUpOne(const CDBWrapper &db, const interfaces::BlockRef &block, DBVal &result)
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.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
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)