5#include <chainparams.h>
16#include <validation.h>
31 uint64_t transaction_output_count;
35 CAmount total_unspendable_amount;
36 CAmount total_prevout_spent_amount;
37 CAmount total_new_outputs_ex_coinbase_amount;
39 CAmount total_unspendables_genesis_block;
40 CAmount total_unspendables_bip30;
41 CAmount total_unspendables_scripts;
42 CAmount total_unspendables_unclaimed_rewards;
52 READWRITE(obj.total_prevout_spent_amount);
53 READWRITE(obj.total_new_outputs_ex_coinbase_amount);
55 READWRITE(obj.total_unspendables_genesis_block);
57 READWRITE(obj.total_unspendables_scripts);
58 READWRITE(obj.total_unspendables_unclaimed_rewards);
65 explicit DBHeightKey(
int height_in) : height(height_in) {}
67 template <
typename Stream>
74 template <
typename Stream>
79 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB height key");
88 explicit DBHashKey(
const uint256& hash_in) : block_hash(hash_in) {}
95 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB hash key");
112 m_db = std::make_unique<CoinStatsIndex::DB>(path /
"db", n_cache_size, f_memory, f_wipe);
130 std::pair<uint256, DBVal> read_out;
131 if (!
m_db->Read(DBHeightKey(block.
height - 1), read_out)) {
136 if (read_out.first != expected_block_hash) {
137 LogPrintf(
"WARNING: previous block header belongs to unexpected block %s; expected %s\n",
138 read_out.first.ToString(), expected_block_hash.ToString());
140 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
141 LogError(
"%s: previous block header not found; expected %s\n",
142 __func__, expected_block_hash.ToString());
149 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
150 const auto& tx{block.
data->
vtx.at(i)};
159 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
165 if (coin.out.scriptPubKey.IsUnspendable()) {
173 if (tx->IsCoinBase()) {
185 if (!tx->IsCoinBase()) {
186 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
188 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
189 Coin coin{tx_undo.vprevout[j]};
190 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
216 std::pair<uint256, DBVal> value;
217 value.first = block.
hash;
233 value.second.muhash =
out;
237 return m_db->Write(DBHeightKey(block.
height), value);
241 const std::string& index_name,
242 int start_height,
int stop_height)
244 DBHeightKey key{start_height};
247 for (
int height = start_height; height <= stop_height; ++height) {
248 if (!db_it.
GetKey(key) || key.height != height) {
249 LogError(
"%s: unexpected key in %s: expected (%c, %d)\n",
254 std::pair<uint256, DBVal> value;
256 LogError(
"%s: unable to read value in %s at key (%c, %d)\n",
261 batch.
Write(DBHashKey(value.first), std::move(value.second));
271 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
280 if (!
m_db->WriteBatch(batch))
return false;
291 LogError(
"%s: Failed to read block %s from disk\n",
292 __func__, iter_tip->GetBlockHash().ToString());
300 iter_tip = iter_tip->GetAncestor(iter_tip->nHeight - 1);
301 }
while (new_tip_index != iter_tip);
312 std::pair<uint256, DBVal> read_out;
313 if (!db.
Read(DBHeightKey(block.
height), read_out)) {
316 if (read_out.first == block.
hash) {
317 result = std::move(read_out.second);
323 return db.
Read(DBHashKey(block.
hash), result);
329 stats.index_used =
true;
336 stats.hashSerialized = entry.muhash;
337 stats.nTransactionOutputs = entry.transaction_output_count;
338 stats.nBogoSize = entry.bogo_size;
339 stats.total_amount = entry.total_amount;
340 stats.total_subsidy = entry.total_subsidy;
341 stats.total_unspendable_amount = entry.total_unspendable_amount;
342 stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
343 stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
344 stats.total_coinbase_amount = entry.total_coinbase_amount;
345 stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
346 stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
347 stats.total_unspendables_scripts = entry.total_unspendables_scripts;
348 stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
360 LogError(
"%s: Cannot read current %s state; index may be corrupted\n",
369 LogError(
"%s: Cannot read current %s state; index may be corrupted\n",
376 if (entry.muhash !=
out) {
377 LogError(
"%s: Cannot read current %s state; index may be corrupted\n",
411 std::pair<uint256, DBVal> read_out;
422 if (!
m_db->Read(DBHeightKey(pindex->
nHeight - 1), read_out)) {
427 if (read_out.first != expected_block_hash) {
428 LogPrintf(
"WARNING: previous block header belongs to unexpected block %s; expected %s\n",
429 read_out.first.ToString(), expected_block_hash.ToString());
431 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
432 LogError(
"%s: previous block header not found; expected %s\n",
433 __func__, expected_block_hash.ToString());
440 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
441 const auto& tx{block.
vtx.at(i)};
443 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
449 if (coin.out.scriptPubKey.IsUnspendable()) {
457 if (tx->IsCoinBase()) {
469 if (!tx->IsCoinBase()) {
470 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
472 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
473 Coin coin{tx_undo.vprevout[j]};
474 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
int64_t CAmount
Amount in satoshis (Can be negative)
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.
Chainstate * m_chainstate
std::vector< CTransactionRef > vtx
The block chain is a tree shaped structure starting with the genesis block at the root,...
CBlockIndex * pprev
pointer to the index of the predecessor of this block
uint256 GetBlockHash() const
int nHeight
height of the entry in the chain. The genesis block has height 0
Undo information for a CBlock.
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.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances.
bool CustomInit(const std::optional< interfaces::BlockRef > &block) override
Initialize internal state from the database and block index.
CAmount m_total_unspendables_genesis_block
CAmount m_total_unspendable_amount
CAmount m_total_new_outputs_ex_coinbase_amount
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.
CAmount m_total_prevout_spent_amount
bool ReverseBlock(const CBlock &block, const CBlockIndex *pindex)
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...
CAmount m_total_coinbase_amount
bool CustomRewind(const interfaces::BlockRef ¤t_tip, const interfaces::BlockRef &new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
CAmount m_total_unspendables_bip30
CAmount m_total_unspendables_unclaimed_rewards
void Finalize(uint256 &out) noexcept
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos) const
Functions for disk access for blocks.
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex &index) const
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 bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int start_height, int stop_height)
static constexpr uint8_t DB_BLOCK_HASH
static constexpr uint8_t DB_BLOCK_HEIGHT
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
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...
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)
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
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
Hash/height pair to help track and identify blocks.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool IsBIP30Unspendable(const CBlockIndex &block_index)
Identifies blocks which coinbase output was subsequently overwritten in the UTXO set (see BIP30)