13#include <validation.h>
23 uint64_t transaction_output_count;
27 CAmount total_unspendable_amount;
28 CAmount total_prevout_spent_amount;
29 CAmount total_new_outputs_ex_coinbase_amount;
31 CAmount total_unspendables_genesis_block;
32 CAmount total_unspendables_bip30;
33 CAmount total_unspendables_scripts;
34 CAmount total_unspendables_unclaimed_rewards;
44 READWRITE(obj.total_prevout_spent_amount);
45 READWRITE(obj.total_new_outputs_ex_coinbase_amount);
47 READWRITE(obj.total_unspendables_genesis_block);
49 READWRITE(obj.total_unspendables_scripts);
50 READWRITE(obj.total_unspendables_unclaimed_rewards);
57 explicit DBHeightKey(
int height_in) : height(height_in) {}
59 template <
typename Stream>
66 template <
typename Stream>
71 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB height key");
80 explicit DBHashKey(
const uint256& hash_in) : block_hash(hash_in) {}
87 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB hash key");
101 fs::create_directories(path);
103 m_db = std::make_unique<CoinStatsIndex::DB>(path /
"db", n_cache_size, f_memory, f_wipe);
118 std::pair<uint256, DBVal> read_out;
119 if (!
m_db->Read(DBHeightKey(pindex->
nHeight - 1), read_out)) {
124 if (read_out.first != expected_block_hash) {
125 LogPrintf(
"WARNING: previous block header belongs to unexpected block %s; expected %s\n",
126 read_out.first.ToString(), expected_block_hash.ToString());
128 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
129 return error(
"%s: previous block header not found; expected %s",
130 __func__, expected_block_hash.ToString());
135 bool is_bip30_block{(pindex->
nHeight == 91722 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
136 (pindex->
nHeight == 91812 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
139 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
140 const auto& tx{block.
vtx.at(i)};
143 if (is_bip30_block && tx->IsCoinBase()) {
149 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
150 const CTxOut& out{tx->vout[j]};
155 if (coin.out.scriptPubKey.IsUnspendable()) {
163 if (tx->IsCoinBase()) {
175 if (!tx->IsCoinBase()) {
176 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
178 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
179 Coin coin{tx_undo.vprevout[j]};
180 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
206 std::pair<uint256, DBVal> value;
223 value.second.muhash = out;
228 return m_db->WriteBatch(batch);
232 const std::string& index_name,
233 int start_height,
int stop_height)
235 DBHeightKey key{start_height};
238 for (
int height = start_height; height <= stop_height; ++height) {
239 if (!db_it.
GetKey(key) || key.height != height) {
240 return error(
"%s: unexpected key in %s: expected (%c, %d)",
244 std::pair<uint256, DBVal> value;
246 return error(
"%s: unable to read value in %s at key (%c, %d)",
250 batch.
Write(DBHashKey(value.first), std::move(value.second));
262 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
271 if (!
m_db->WriteBatch(batch))
return false;
282 return error(
"%s: Failed to read block %s from disk",
283 __func__, iter_tip->GetBlockHash().ToString());
288 iter_tip = iter_tip->GetAncestor(iter_tip->nHeight - 1);
289 }
while (new_tip != iter_tip);
300 std::pair<uint256, DBVal> read_out;
301 if (!db.
Read(DBHeightKey(block_index->
nHeight), read_out)) {
305 result = std::move(read_out.second);
345 return error(
"%s: Cannot read current %s state; index may be corrupted",
381 std::pair<uint256, DBVal> read_out;
392 if (!
m_db->Read(DBHeightKey(pindex->
nHeight - 1), read_out)) {
397 if (read_out.first != expected_block_hash) {
398 LogPrintf(
"WARNING: previous block header belongs to unexpected block %s; expected %s\n",
399 read_out.first.ToString(), expected_block_hash.ToString());
401 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
402 return error(
"%s: previous block header not found; expected %s",
403 __func__, expected_block_hash.ToString());
409 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
410 const auto& tx{block.
vtx.at(i)};
412 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
413 const CTxOut& out{tx->vout[j]};
418 if (coin.out.scriptPubKey.IsUnspendable()) {
426 if (tx->IsCoinBase()) {
438 if (!tx->IsCoinBase()) {
439 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
441 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
442 Coin coin{tx_undo.vprevout[j]};
443 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
463 Assert(read_out.second.muhash == out);
int64_t CAmount
Amount in satoshis (Can be negative)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
const CChainParams & Params()
Return the currently selected parameters.
#define Assert(val)
Identity function.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.
virtual bool Init()
Initialize internal state from the database and block index.
CChainState * m_chainstate
const CBlockIndex * CurrentIndex()
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
CBlockIndex * LookupBlockIndex(const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
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
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
int nHeight
height of the entry in the chain. The genesis block has height 0
Undo information for a CBlock.
std::vector< CTxUndo > vtxundo
const Consensus::Params & GetConsensus() const
BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all CChainState instances.
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.
const char * GetName() const override
Get the name of the index for display in logs.
CAmount m_total_unspendables_genesis_block
bool LookUpStats(const CBlockIndex *block_index, CCoinsStats &coins_stats) const
CAmount m_total_unspendable_amount
CAmount m_total_new_outputs_ex_coinbase_amount
CAmount m_total_unspendables_scripts
uint64_t m_transaction_output_count
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
CAmount m_total_prevout_spent_amount
bool ReverseBlock(const CBlock &block, const CBlockIndex *pindex)
std::unique_ptr< BaseIndex::DB > m_db
CoinStatsIndex(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
CAmount m_total_coinbase_amount
CAmount m_total_unspendables_bip30
CAmount m_total_unspendables_unclaimed_rewards
bool Init() override
Initialize internal state from the database and block index.
void Finalize(uint256 &out) noexcept
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
Path class wrapper to prepare application code for transition from boost::filesystem library to std::...
CDataStream TxOutSer(const COutPoint &outpoint, const Coin &coin)
uint64_t GetBogoSize(const CScript &script_pub_key)
static bool LookUpOne(const CDBWrapper &db, const CBlockIndex *block_index, DBVal &result)
static constexpr uint8_t DB_MUHASH
std::unique_ptr< CoinStatsIndex > g_coin_stats_index
The global UTXO set hash object.
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
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 Serialize(Stream &s, char a)
void Unserialize(Stream &s, char &a)
void ser_writedata8(Stream &s, uint8_t obj)
uint32_t ser_readdata32be(Stream &s)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
uint64_t nTransactionOutputs
CAmount total_prevout_spent_amount
Total cumulative amount of prevouts spent up to and including this block.
CAmount total_unspendable_amount
Total cumulative amount of unspendable coins 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_new_outputs_ex_coinbase_amount
Total cumulative amount of outputs created up to and including this block.
CAmount total_unspendables_genesis_block
The unspendable coinbase amount from the genesis block.
CAmount total_unspendables_bip30
The two unspendable coinbase outputs total amount caused by BIP30.
CAmount total_unspendables_scripts
Total cumulative amount of outputs sent to unspendable scripts (OP_RETURN for example) up to and incl...
CAmount total_coinbase_amount
Total cumulative amount of coinbase outputs up to and including this block.
CAmount total_subsidy
Total cumulative amount of block subsidies up to and including this block.
bool error(const char *fmt, const Args &... args)
uint256 uint256S(const char *str)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)