15 #include <validation.h> 32 uint64_t transaction_output_count;
36 CAmount total_unspendable_amount;
37 CAmount total_prevout_spent_amount;
38 CAmount total_new_outputs_ex_coinbase_amount;
40 CAmount total_unspendables_genesis_block;
41 CAmount total_unspendables_bip30;
42 CAmount total_unspendables_scripts;
43 CAmount total_unspendables_unclaimed_rewards;
53 READWRITE(obj.total_prevout_spent_amount);
54 READWRITE(obj.total_new_outputs_ex_coinbase_amount);
56 READWRITE(obj.total_unspendables_genesis_block);
58 READWRITE(obj.total_unspendables_scripts);
59 READWRITE(obj.total_unspendables_unclaimed_rewards);
66 explicit DBHeightKey(
int height_in) : height(height_in) {}
68 template <
typename Stream>
75 template <
typename Stream>
80 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB height key");
89 explicit DBHashKey(
const uint256& hash_in) : block_hash(hash_in) {}
96 throw std::ios_base::failure(
"Invalid format for coinstatsindex DB hash key");
113 m_db = std::make_unique<CoinStatsIndex::DB>(path /
"db", n_cache_size, f_memory, f_wipe);
131 std::pair<uint256, DBVal> read_out;
132 if (!
m_db->Read(DBHeightKey(block.
height - 1), read_out)) {
137 if (read_out.first != expected_block_hash) {
138 LogPrintf(
"WARNING: previous block header belongs to unexpected block %s; expected %s\n",
139 read_out.first.ToString(), expected_block_hash.ToString());
141 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
142 return error(
"%s: previous block header not found; expected %s",
143 __func__, expected_block_hash.ToString());
148 bool is_bip30_block{(block.
height == 91722 && block.
hash ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
149 (block.
height == 91812 && block.
hash ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
153 for (
size_t i = 0; i < block.
data->
vtx.size(); ++i) {
154 const auto& tx{block.
data->
vtx.at(i)};
157 if (is_bip30_block && tx->IsCoinBase()) {
163 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
164 const CTxOut& out{tx->vout[j]};
165 Coin coin{out, block.
height, tx->IsCoinBase()};
169 if (coin.out.scriptPubKey.IsUnspendable()) {
177 if (tx->IsCoinBase()) {
189 if (!tx->IsCoinBase()) {
190 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
192 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
193 Coin coin{tx_undo.vprevout[j]};
194 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
220 std::pair<uint256, DBVal> value;
221 value.first = block.
hash;
237 value.second.muhash = out;
241 return m_db->Write(DBHeightKey(block.
height), value);
245 const std::string& index_name,
246 int start_height,
int stop_height)
248 DBHeightKey key{start_height};
251 for (
int height = start_height; height <= stop_height; ++height) {
252 if (!db_it.
GetKey(key) || key.height != height) {
253 return error(
"%s: unexpected key in %s: expected (%c, %d)",
257 std::pair<uint256, DBVal> value;
259 return error(
"%s: unable to read value in %s at key (%c, %d)",
263 batch.
Write(DBHashKey(value.first), std::move(value.second));
273 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
282 if (!
m_db->WriteBatch(batch))
return false;
294 return error(
"%s: Failed to read block %s from disk",
295 __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 return error(
"%s: Cannot read current %s state; index may be corrupted",
368 return error(
"%s: Cannot read current %s state; index may be corrupted",
374 if (entry.muhash != out) {
375 return error(
"%s: Cannot read current %s state; index may be corrupted",
408 std::pair<uint256, DBVal> read_out;
419 if (!
m_db->Read(DBHeightKey(pindex->
nHeight - 1), read_out)) {
424 if (read_out.first != expected_block_hash) {
425 LogPrintf(
"WARNING: previous block header belongs to unexpected block %s; expected %s\n",
426 read_out.first.ToString(), expected_block_hash.ToString());
428 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
429 return error(
"%s: previous block header not found; expected %s",
430 __func__, expected_block_hash.ToString());
436 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
437 const auto& tx{block.
vtx.at(i)};
439 for (uint32_t j = 0; j < tx->vout.size(); ++j) {
440 const CTxOut& out{tx->vout[j]};
445 if (coin.out.scriptPubKey.IsUnspendable()) {
453 if (tx->IsCoinBase()) {
465 if (!tx->IsCoinBase()) {
466 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
468 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
469 Coin coin{tx_undo.vprevout[j]};
470 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
490 Assert(read_out.second.muhash == out);
uint64_t GetBogoSize(const CScript &script_pub_key)
CDataStream TxOutSer(const COutPoint &outpoint, const Coin &coin)
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
uint8_t ser_readdata8(Stream &s)
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Batch of changes queued to be written to a CDBWrapper.
node::BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all Chainstate instances...
bool CustomCommit(CDBBatch &batch) override
Virtual method called internally by Commit that can be overridden to atomically commit more index sta...
void Unserialize(Stream &, char)=delete
bool CustomRewind(const interfaces::BlockKey ¤t_tip, const interfaces::BlockKey &new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
static bool CopyHeightIndexToHashIndex(CDBIterator &db_it, CDBBatch &batch, const std::string &index_name, int start_height, int stop_height)
static constexpr uint8_t DB_MUHASH
static bool LookUpOne(const CDBWrapper &db, const interfaces::BlockKey &block, DBVal &result)
std::unique_ptr< BaseIndex::DB > m_db
CAmount m_total_unspendables_genesis_block
bool CustomAppend(const interfaces::BlockInfo &block) override
Write update index entries for a newly connected block.
int64_t CAmount
Amount in satoshis (Can be negative)
uint256 GetBlockHash() const
bool CustomInit(const std::optional< interfaces::BlockKey > &block) override
Initialize internal state from the database and block index.
Block data sent with blockConnected, blockDisconnected notifications.
static constexpr uint8_t DB_BLOCK_HASH
Base class for indices of blockchain data.
CAmount m_total_new_outputs_ex_coinbase_amount
const uint256 * prev_hash
const std::string & GetName() const LIFETIMEBOUND
Get the name of the index for display in logs.
static constexpr uint8_t DB_BLOCK_HEIGHT
uint256 uint256S(const char *str)
std::optional< kernel::CCoinsStats > LookUpStats(const CBlockIndex &block_index) const
void Serialize(Stream &, char)=delete
void Write(const K &key, const V &value)
An output of a transaction.
void ser_writedata32be(Stream &s, uint32_t obj)
An outpoint - a combination of a transaction hash and an index n into its vout.
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...
CBlockIndex * LookupBlockIndex(const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
bool Read(const K &key, V &value) const
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
void Finalize(uint256 &out) noexcept
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
std::vector< CTransactionRef > vtx
bool ReverseBlock(const CBlock &block, const CBlockIndex *pindex)
The block chain is a tree shaped structure starting with the genesis block at the root...
const CChainParams & Params()
Return the currently selected parameters.
Undo information for a CBlock.
Hash/height pair to help track and identify blocks.
Chainstate * m_chainstate
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
uint32_t ser_readdata32be(Stream &s)
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span
Like the Span constructor, but for (const) unsigned char member types only.
std::unique_ptr< CoinStatsIndex > g_coin_stats_index
The global UTXO set hash object.
int nHeight
height of the entry in the chain. The genesis block has height 0
const Consensus::Params & GetConsensus() const
CoinStatsIndex(std::unique_ptr< interfaces::Chain > chain, size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
uint64_t m_transaction_output_count
CAmount m_total_unspendable_amount
std::vector< CTxUndo > vtxundo
CAmount m_total_unspendables_unclaimed_rewards
CAmount m_total_unspendables_bip30
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
CAmount m_total_unspendables_scripts
bool error(const char *fmt, const Args &... args)
CAmount m_total_prevout_spent_amount
CAmount m_total_coinbase_amount
void ser_writedata8(Stream &s, uint8_t obj)
#define Assert(val)
Identity function.
const fs::path & GetDataDirNet() const
Get data directory path with appended network identifier.