6#include <chainparams.h>
17#include <validation.h>
32 uint64_t transaction_output_count{0};
33 uint64_t bogo_size{0};
39 CAmount total_unspendables_genesis_block{0};
40 CAmount total_unspendables_bip30{0};
41 CAmount total_unspendables_scripts{0};
42 CAmount total_unspendables_unclaimed_rewards{0};
46 uint256 prevout_spent, new_outputs, coinbase;
59 READWRITE(obj.total_unspendables_genesis_block);
61 READWRITE(obj.total_unspendables_scripts);
62 READWRITE(obj.total_unspendables_unclaimed_rewards);
73 explicit DBHeightKey(
int height_in) : height(height_in) {}
75 template <
typename Stream>
82 template <
typename Stream>
87 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB height key");
96 explicit DBHashKey(
const uint256& hash_in) : block_hash(hash_in) {}
103 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB hash key");
124 LogWarning(
"Old version of coinstatsindex found at %s. This folder can be safely deleted unless you " \
125 "plan to downgrade your node to version 29 or lower.",
fs::PathToString(old_path));
130 m_db = std::make_unique<CoinStatsIndex::DB>(path /
"db", n_cache_size, f_memory, f_wipe);
142 LogError(
"previous block header belongs to unexpected block %s; expected %s",
149 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
150 const auto& tx{block.
data->
vtx.at(i)};
151 const bool is_coinbase{tx->IsCoinBase()};
159 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
162 const COutPoint outpoint{tx->GetHash(), j};
165 if (coin.out.scriptPubKey.IsUnspendable()) {
187 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
188 const Coin& coin{tx_undo.vprevout[j]};
189 const COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
215 std::pair<uint256, DBVal> value;
216 value.first = block.
hash;
231 value.second.muhash =
out;
237 return m_db->Write(DBHeightKey(block.
height), value);
241 const std::string& index_name,
int height)
243 DBHeightKey key{height};
246 if (!db_it.
GetKey(key) || key.height != height) {
247 LogError(
"unexpected key in %s: expected (%c, %d)",
252 std::pair<uint256, DBVal> value;
254 LogError(
"unable to read value in %s at key (%c, %d)",
259 batch.
Write(DBHashKey(value.first), value.second);
266 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
274 if (!
m_db->WriteBatch(batch))
return false;
288 std::pair<uint256, DBVal> read_out;
289 if (!db.
Read(DBHeightKey(block.
height), read_out)) {
292 if (read_out.first == block.
hash) {
293 result = std::move(read_out.second);
299 return db.
Read(DBHashKey(block.
hash), result);
305 stats.index_used =
true;
312 stats.hashSerialized = entry.muhash;
313 stats.nTransactionOutputs = entry.transaction_output_count;
314 stats.nBogoSize = entry.bogo_size;
315 stats.total_amount = entry.total_amount;
316 stats.total_subsidy = entry.total_subsidy;
317 stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
318 stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
319 stats.total_coinbase_amount = entry.total_coinbase_amount;
320 stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
321 stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
322 stats.total_unspendables_scripts = entry.total_unspendables_scripts;
323 stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
335 LogError(
"Cannot read current %s state; index may be corrupted",
344 LogError(
"Cannot read current %s state; index may be corrupted",
351 if (entry.muhash !=
out) {
352 LogError(
"Cannot read current %s state; index may be corrupted",
394 std::pair<uint256, DBVal> read_out;
398 if (!
m_db->Read(DBHeightKey(block.
height - 1), read_out)) {
403 if (read_out.first != expected_block_hash) {
404 LogWarning(
"previous block header belongs to unexpected block %s; expected %s",
405 read_out.first.ToString(), expected_block_hash.ToString());
407 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
408 LogError(
"previous block header not found; expected %s",
409 expected_block_hash.ToString());
419 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
420 const auto& tx{block.
data->
vtx.at(i)};
421 const bool is_coinbase{tx->IsCoinBase()};
427 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
429 const COutPoint outpoint{tx->GetHash(), j};
432 if (!coin.out.scriptPubKey.IsUnspendable()) {
441 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
442 const Coin& coin{tx_undo.vprevout[j]};
443 const COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
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
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,...
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)
bool Read(const K &key, V &value) const
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 LookUpOne(const CDBWrapper &db, const interfaces::BlockRef &block, DBVal &result)
static constexpr uint8_t DB_BLOCK_HASH
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int height)
static constexpr uint8_t DB_BLOCK_HEIGHT
static bool create_directories(const std::filesystem::path &p)
Create directory (and if necessary its parents), unless the leaf directory already exists or is a sym...
static bool exists(const path &p)
static std::string PathToString(const path &path)
Convert path object to a byte string.
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)
void Serialize(Stream &, V)=delete
uint8_t ser_readdata8(Stream &s)
void ser_writedata32be(Stream &s, uint32_t obj)
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
void Unserialize(Stream &, V)=delete
#define SER_READ(obj, code)
void ser_writedata8(Stream &s, uint8_t obj)
uint32_t ser_readdata32be(Stream &s)
Block data sent with blockConnected, blockDisconnected notifications.
const uint256 * prev_hash
const CBlockUndo * undo_data
Hash/height pair to help track and identify blocks.
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.
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)