13 #include <validation.h> 23 uint64_t transaction_output_count;
27 CAmount block_unspendable_amount;
28 CAmount block_prevout_spent_amount;
29 CAmount block_new_outputs_ex_coinbase_amount;
31 CAmount unspendables_genesis_block;
34 CAmount unspendables_unclaimed_rewards;
44 READWRITE(obj.block_prevout_spent_amount);
45 READWRITE(obj.block_new_outputs_ex_coinbase_amount);
47 READWRITE(obj.unspendables_genesis_block);
50 READWRITE(obj.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 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
126 return error(
"%s: previous block header belongs to unexpected block %s; expected %s",
127 __func__, read_out.first.ToString(), expected_block_hash.ToString());
132 bool is_bip30_block{(pindex->
nHeight == 91722 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
133 (pindex->
nHeight == 91812 && pindex->
GetBlockHash() ==
uint256S(
"0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
136 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
137 const auto& tx{block.
vtx.at(i)};
140 if (is_bip30_block && tx->IsCoinBase()) {
146 for (
size_t j = 0; j < tx->vout.size(); ++j) {
147 const CTxOut& out{tx->vout[j]};
149 COutPoint outpoint{tx->GetHash(),
static_cast<uint32_t
>(j)};
152 if (coin.out.scriptPubKey.IsUnspendable()) {
160 if (tx->IsCoinBase()) {
172 if (!tx->IsCoinBase()) {
173 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
175 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
176 Coin coin{tx_undo.vprevout[j]};
177 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
203 std::pair<uint256, DBVal> value;
220 value.second.muhash = out;
226 const std::string& index_name,
227 int start_height,
int stop_height)
229 DBHeightKey key{start_height};
232 for (
int height = start_height; height <= stop_height; ++height) {
233 if (!db_it.
GetKey(key) || key.height != height) {
234 return error(
"%s: unexpected key in %s: expected (%c, %d)",
238 std::pair<uint256, DBVal> value;
240 return error(
"%s: unable to read value in %s at key (%c, %d)",
244 batch.
Write(DBHashKey(value.first), std::move(value.second));
256 std::unique_ptr<CDBIterator> db_it(
m_db->NewIterator());
265 if (!
m_db->WriteBatch(batch))
return false;
276 return error(
"%s: Failed to read block %s from disk",
277 __func__, iter_tip->GetBlockHash().ToString());
282 iter_tip = iter_tip->GetAncestor(iter_tip->nHeight - 1);
283 }
while (new_tip != iter_tip);
294 std::pair<uint256, DBVal> read_out;
295 if (!db.
Read(DBHeightKey(block_index->
nHeight), read_out)) {
299 result = std::move(read_out.second);
339 return error(
"%s: Cannot read current %s state; index may be corrupted",
377 std::pair<uint256, DBVal> read_out;
388 if (!
m_db->Read(DBHeightKey(pindex->
nHeight - 1), read_out)) {
393 if (read_out.first != expected_block_hash) {
394 if (!
m_db->Read(DBHashKey(expected_block_hash), read_out)) {
395 return error(
"%s: previous block header belongs to unexpected block %s; expected %s",
396 __func__, read_out.first.ToString(), expected_block_hash.ToString());
402 for (
size_t i = 0; i < block.
vtx.size(); ++i) {
403 const auto& tx{block.
vtx.at(i)};
405 for (
size_t j = 0; j < tx->vout.size(); ++j) {
406 const CTxOut& out{tx->vout[j]};
407 COutPoint outpoint{tx->GetHash(),
static_cast<uint32_t
>(j)};
411 if (coin.out.scriptPubKey.IsUnspendable()) {
419 if (tx->IsCoinBase()) {
431 if (!tx->IsCoinBase()) {
432 const auto& tx_undo{block_undo.
vtxundo.at(i - 1)};
434 for (
size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
435 Coin coin{tx_undo.vprevout[j]};
436 COutPoint outpoint{tx->vin[j].prevout.
hash, tx->vin[j].prevout.n};
456 Assert(read_out.second.muhash == out);
CAmount m_unspendables_scripts
CChainState * m_chainstate
const char * GetName() const override
Get the name of the index for display in logs.
uint64_t nTransactionOutputs
bool WriteBlock(const CBlock &block, const CBlockIndex *pindex) override
Write update index entries for a newly connected block.
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
virtual bool Init()
Initialize internal state from the database and block index.
CAmount m_block_prevout_spent_amount
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.
CAmount unspendables_genesis_block
CDataStream TxOutSer(const COutPoint &outpoint, const Coin &coin)
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
CBlockIndex * LookupBlockIndex(const uint256 &hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_main)
std::unique_ptr< BaseIndex::DB > m_db
virtual bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip)
Rewind index to an earlier chain tip during a chain reorg.
CAmount m_block_unspendable_amount
CAmount block_coinbase_amount
CAmount block_unspendable_amount
void Serialize(Stream &s, char a)
uint64_t GetBogoSize(const CScript &script_pub_key)
int64_t CAmount
Amount in satoshis (Can be negative)
uint256 GetBlockHash() const
static constexpr uint8_t DB_BLOCK_HASH
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
bool Rewind(const CBlockIndex *current_tip, const CBlockIndex *new_tip) override
Rewind index to an earlier chain tip during a chain reorg.
const CBlockIndex * CurrentIndex()
CAmount block_new_outputs_ex_coinbase_amount
static constexpr uint8_t DB_BLOCK_HEIGHT
uint256 uint256S(const char *str)
CAmount m_block_new_outputs_ex_coinbase_amount
bool UndoReadFromDisk(CBlockUndo &blockundo, const CBlockIndex *pindex)
void Write(const K &key, const V &value)
CAmount m_unspendables_bip30
An output of a transaction.
bool Init() override
Initialize internal state from the database and block index.
CAmount m_unspendables_unclaimed_rewards
CoinStatsIndex(size_t n_cache_size, bool f_memory=false, bool f_wipe=false)
void ser_writedata32be(Stream &s, uint32_t obj)
An outpoint - a combination of a transaction hash and an index n into its vout.
bool Read(const K &key, V &value) const
MuHash3072 & Remove(Span< const unsigned char > in) noexcept
void Finalize(uint256 &out) noexcept
CAmount m_block_coinbase_amount
#define SERIALIZE_METHODS(cls, obj)
Implement the Serialize and Unserialize methods by delegating to a single templated static method tha...
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
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.
static bool LookUpOne(const CDBWrapper &db, const CBlockIndex *block_index, DBVal &result)
uint32_t ser_readdata32be(Stream &s)
CAmount unspendables_unclaimed_rewards
BlockManager & m_blockman
Reference to a BlockManager instance which itself is shared across all CChainState instances...
void Unserialize(Stream &s, char &a)
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
CAmount block_prevout_spent_amount
CAmount m_unspendables_genesis_block
MuHash3072 & Insert(Span< const unsigned char > in) noexcept
uint64_t m_transaction_output_count
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
bool LookUpStats(const CBlockIndex *block_index, CCoinsStats &coins_stats) const
std::vector< CTxUndo > vtxundo
bool error(const char *fmt, const Args &... args)
bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
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.
CAmount unspendables_scripts
CAmount unspendables_bip30