2020-01-24 18:56:47 +01:00
|
|
|
// Copyright (c) 2020-2021 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <chainparams.h>
|
|
|
|
#include <coins.h>
|
|
|
|
#include <crypto/muhash.h>
|
|
|
|
#include <index/coinstatsindex.h>
|
|
|
|
#include <node/blockstorage.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <txdb.h>
|
|
|
|
#include <undo.h>
|
2022-07-19 14:15:31 +02:00
|
|
|
#include <util/system.h>
|
2020-01-24 18:56:47 +01:00
|
|
|
#include <validation.h>
|
|
|
|
|
2022-02-16 17:58:41 -05:00
|
|
|
using kernel::CCoinsStats;
|
|
|
|
using kernel::GetBogoSize;
|
|
|
|
using kernel::TxOutSer;
|
2022-02-17 22:02:48 -05:00
|
|
|
|
|
|
|
using node::ReadBlockFromDisk;
|
2021-11-12 10:06:00 -05:00
|
|
|
using node::UndoReadFromDisk;
|
|
|
|
|
2021-05-01 13:49:37 +02:00
|
|
|
static constexpr uint8_t DB_BLOCK_HASH{'s'};
|
|
|
|
static constexpr uint8_t DB_BLOCK_HEIGHT{'t'};
|
|
|
|
static constexpr uint8_t DB_MUHASH{'M'};
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct DBVal {
|
|
|
|
uint256 muhash;
|
|
|
|
uint64_t transaction_output_count;
|
|
|
|
uint64_t bogo_size;
|
|
|
|
CAmount total_amount;
|
2020-08-22 20:21:20 +02:00
|
|
|
CAmount total_subsidy;
|
2021-06-03 01:31:47 +02:00
|
|
|
CAmount total_unspendable_amount;
|
|
|
|
CAmount total_prevout_spent_amount;
|
|
|
|
CAmount total_new_outputs_ex_coinbase_amount;
|
|
|
|
CAmount total_coinbase_amount;
|
|
|
|
CAmount total_unspendables_genesis_block;
|
|
|
|
CAmount total_unspendables_bip30;
|
|
|
|
CAmount total_unspendables_scripts;
|
|
|
|
CAmount total_unspendables_unclaimed_rewards;
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
SERIALIZE_METHODS(DBVal, obj)
|
|
|
|
{
|
|
|
|
READWRITE(obj.muhash);
|
|
|
|
READWRITE(obj.transaction_output_count);
|
|
|
|
READWRITE(obj.bogo_size);
|
|
|
|
READWRITE(obj.total_amount);
|
2020-08-22 20:21:20 +02:00
|
|
|
READWRITE(obj.total_subsidy);
|
2021-06-03 01:31:47 +02:00
|
|
|
READWRITE(obj.total_unspendable_amount);
|
|
|
|
READWRITE(obj.total_prevout_spent_amount);
|
|
|
|
READWRITE(obj.total_new_outputs_ex_coinbase_amount);
|
|
|
|
READWRITE(obj.total_coinbase_amount);
|
|
|
|
READWRITE(obj.total_unspendables_genesis_block);
|
|
|
|
READWRITE(obj.total_unspendables_bip30);
|
|
|
|
READWRITE(obj.total_unspendables_scripts);
|
|
|
|
READWRITE(obj.total_unspendables_unclaimed_rewards);
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DBHeightKey {
|
|
|
|
int height;
|
|
|
|
|
|
|
|
explicit DBHeightKey(int height_in) : height(height_in) {}
|
|
|
|
|
|
|
|
template <typename Stream>
|
|
|
|
void Serialize(Stream& s) const
|
|
|
|
{
|
|
|
|
ser_writedata8(s, DB_BLOCK_HEIGHT);
|
|
|
|
ser_writedata32be(s, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Stream>
|
|
|
|
void Unserialize(Stream& s)
|
|
|
|
{
|
2021-05-01 13:49:37 +02:00
|
|
|
const uint8_t prefix{ser_readdata8(s)};
|
2020-01-24 18:56:47 +01:00
|
|
|
if (prefix != DB_BLOCK_HEIGHT) {
|
|
|
|
throw std::ios_base::failure("Invalid format for coinstatsindex DB height key");
|
|
|
|
}
|
|
|
|
height = ser_readdata32be(s);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DBHashKey {
|
|
|
|
uint256 block_hash;
|
|
|
|
|
|
|
|
explicit DBHashKey(const uint256& hash_in) : block_hash(hash_in) {}
|
|
|
|
|
|
|
|
SERIALIZE_METHODS(DBHashKey, obj)
|
|
|
|
{
|
2021-05-01 13:49:37 +02:00
|
|
|
uint8_t prefix{DB_BLOCK_HASH};
|
2020-01-24 18:56:47 +01:00
|
|
|
READWRITE(prefix);
|
|
|
|
if (prefix != DB_BLOCK_HASH) {
|
|
|
|
throw std::ios_base::failure("Invalid format for coinstatsindex DB hash key");
|
|
|
|
}
|
|
|
|
|
|
|
|
READWRITE(obj.block_hash);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}; // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
|
|
|
|
|
2022-01-13 07:57:54 -05:00
|
|
|
CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
|
|
|
|
: BaseIndex(std::move(chain))
|
2020-01-24 18:56:47 +01:00
|
|
|
{
|
2021-05-04 13:00:25 +02:00
|
|
|
fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstats"};
|
2020-01-24 18:56:47 +01:00
|
|
|
fs::create_directories(path);
|
|
|
|
|
|
|
|
m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:33:16 -05:00
|
|
|
bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
|
2020-01-24 18:56:47 +01:00
|
|
|
{
|
|
|
|
CBlockUndo block_undo;
|
2022-01-17 20:33:16 -05:00
|
|
|
const CAmount block_subsidy{GetBlockSubsidy(block.height, Params().GetConsensus())};
|
2020-08-22 20:21:20 +02:00
|
|
|
m_total_subsidy += block_subsidy;
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
// Ignore genesis block
|
2022-01-17 20:33:16 -05:00
|
|
|
if (block.height > 0) {
|
|
|
|
// pindex variable gives indexing code access to node internals. It
|
|
|
|
// will be removed in upcoming commit
|
|
|
|
const CBlockIndex* pindex = WITH_LOCK(cs_main, return m_chainstate->m_blockman.LookupBlockIndex(block.hash));
|
2020-01-24 18:56:47 +01:00
|
|
|
if (!UndoReadFromDisk(block_undo, pindex)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<uint256, DBVal> read_out;
|
2022-01-17 20:33:16 -05:00
|
|
|
if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) {
|
2020-01-24 18:56:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:33:16 -05:00
|
|
|
uint256 expected_block_hash{*Assert(block.prev_hash)};
|
2020-01-24 18:56:47 +01:00
|
|
|
if (read_out.first != expected_block_hash) {
|
2021-05-24 18:37:40 +02:00
|
|
|
LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
|
|
|
|
read_out.first.ToString(), expected_block_hash.ToString());
|
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
|
2021-05-24 18:37:40 +02:00
|
|
|
return error("%s: previous block header not found; expected %s",
|
|
|
|
__func__, expected_block_hash.ToString());
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Deduplicate BIP30 related code
|
2022-01-17 20:33:16 -05:00
|
|
|
bool is_bip30_block{(block.height == 91722 && block.hash == uint256S("0x00000000000271a2dc26e7667f8419f2e15416dc6955e5a6c6cdf3f2574dd08e")) ||
|
|
|
|
(block.height == 91812 && block.hash == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"))};
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
// Add the new utxos created from the block
|
2022-01-17 20:33:16 -05:00
|
|
|
assert(block.data);
|
|
|
|
for (size_t i = 0; i < block.data->vtx.size(); ++i) {
|
|
|
|
const auto& tx{block.data->vtx.at(i)};
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
// Skip duplicate txid coinbase transactions (BIP30).
|
|
|
|
if (is_bip30_block && tx->IsCoinBase()) {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_unspendable_amount += block_subsidy;
|
|
|
|
m_total_unspendables_bip30 += block_subsidy;
|
2020-01-24 18:56:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:46:08 +02:00
|
|
|
for (uint32_t j = 0; j < tx->vout.size(); ++j) {
|
2020-01-24 18:56:47 +01:00
|
|
|
const CTxOut& out{tx->vout[j]};
|
2022-01-17 20:33:16 -05:00
|
|
|
Coin coin{out, block.height, tx->IsCoinBase()};
|
2021-05-24 00:46:08 +02:00
|
|
|
COutPoint outpoint{tx->GetHash(), j};
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
// Skip unspendable coins
|
2020-08-22 20:21:20 +02:00
|
|
|
if (coin.out.scriptPubKey.IsUnspendable()) {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_unspendable_amount += coin.out.nValue;
|
|
|
|
m_total_unspendables_scripts += coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
m_muhash.Insert(MakeUCharSpan(TxOutSer(outpoint, coin)));
|
|
|
|
|
2020-08-22 20:21:20 +02:00
|
|
|
if (tx->IsCoinBase()) {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_coinbase_amount += coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
} else {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_new_outputs_ex_coinbase_amount += coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
}
|
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
++m_transaction_output_count;
|
|
|
|
m_total_amount += coin.out.nValue;
|
|
|
|
m_bogo_size += GetBogoSize(coin.out.scriptPubKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The coinbase tx has no undo data since no former output is spent
|
|
|
|
if (!tx->IsCoinBase()) {
|
|
|
|
const auto& tx_undo{block_undo.vtxundo.at(i - 1)};
|
|
|
|
|
|
|
|
for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
|
|
|
|
Coin coin{tx_undo.vprevout[j]};
|
|
|
|
COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
|
|
|
|
|
|
|
|
m_muhash.Remove(MakeUCharSpan(TxOutSer(outpoint, coin)));
|
|
|
|
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_prevout_spent_amount += coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
--m_transaction_output_count;
|
|
|
|
m_total_amount -= coin.out.nValue;
|
|
|
|
m_bogo_size -= GetBogoSize(coin.out.scriptPubKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 20:21:20 +02:00
|
|
|
} else {
|
|
|
|
// genesis block
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_unspendable_amount += block_subsidy;
|
|
|
|
m_total_unspendables_genesis_block += block_subsidy;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2020-08-22 20:21:20 +02:00
|
|
|
// If spent prevouts + block subsidy are still a higher amount than
|
|
|
|
// new outputs + coinbase + current unspendable amount this means
|
|
|
|
// the miner did not claim the full block reward. Unclaimed block
|
|
|
|
// rewards are also unspendable.
|
2021-06-03 01:31:47 +02:00
|
|
|
const CAmount unclaimed_rewards{(m_total_prevout_spent_amount + m_total_subsidy) - (m_total_new_outputs_ex_coinbase_amount + m_total_coinbase_amount + m_total_unspendable_amount)};
|
|
|
|
m_total_unspendable_amount += unclaimed_rewards;
|
|
|
|
m_total_unspendables_unclaimed_rewards += unclaimed_rewards;
|
2020-08-22 20:21:20 +02:00
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
std::pair<uint256, DBVal> value;
|
2022-01-17 20:33:16 -05:00
|
|
|
value.first = block.hash;
|
2020-01-24 18:56:47 +01:00
|
|
|
value.second.transaction_output_count = m_transaction_output_count;
|
|
|
|
value.second.bogo_size = m_bogo_size;
|
|
|
|
value.second.total_amount = m_total_amount;
|
2020-08-22 20:21:20 +02:00
|
|
|
value.second.total_subsidy = m_total_subsidy;
|
2021-06-03 01:31:47 +02:00
|
|
|
value.second.total_unspendable_amount = m_total_unspendable_amount;
|
|
|
|
value.second.total_prevout_spent_amount = m_total_prevout_spent_amount;
|
|
|
|
value.second.total_new_outputs_ex_coinbase_amount = m_total_new_outputs_ex_coinbase_amount;
|
|
|
|
value.second.total_coinbase_amount = m_total_coinbase_amount;
|
|
|
|
value.second.total_unspendables_genesis_block = m_total_unspendables_genesis_block;
|
|
|
|
value.second.total_unspendables_bip30 = m_total_unspendables_bip30;
|
|
|
|
value.second.total_unspendables_scripts = m_total_unspendables_scripts;
|
|
|
|
value.second.total_unspendables_unclaimed_rewards = m_total_unspendables_unclaimed_rewards;
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
uint256 out;
|
|
|
|
m_muhash.Finalize(out);
|
|
|
|
value.second.muhash = out;
|
|
|
|
|
2022-01-24 14:34:28 +01:00
|
|
|
// Intentionally do not update DB_MUHASH here so it stays in sync with
|
|
|
|
// DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
|
2022-01-17 20:33:16 -05:00
|
|
|
return m_db->Write(DBHeightKey(block.height), value);
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
|
|
|
|
const std::string& index_name,
|
|
|
|
int start_height, int stop_height)
|
|
|
|
{
|
|
|
|
DBHeightKey key{start_height};
|
|
|
|
db_it.Seek(key);
|
|
|
|
|
|
|
|
for (int height = start_height; height <= stop_height; ++height) {
|
|
|
|
if (!db_it.GetKey(key) || key.height != height) {
|
|
|
|
return error("%s: unexpected key in %s: expected (%c, %d)",
|
|
|
|
__func__, index_name, DB_BLOCK_HEIGHT, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<uint256, DBVal> value;
|
|
|
|
if (!db_it.GetValue(value)) {
|
|
|
|
return error("%s: unable to read value in %s at key (%c, %d)",
|
|
|
|
__func__, index_name, DB_BLOCK_HEIGHT, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Write(DBHashKey(value.first), std::move(value.second));
|
|
|
|
|
|
|
|
db_it.Next();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-01-17 20:33:52 -05:00
|
|
|
bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
|
2020-01-24 18:56:47 +01:00
|
|
|
{
|
|
|
|
CDBBatch batch(*m_db);
|
|
|
|
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
|
|
|
|
|
|
|
|
// During a reorg, we need to copy all hash digests for blocks that are
|
|
|
|
// getting disconnected from the height index to the hash index so we can
|
|
|
|
// still find them when the height index entries are overwritten.
|
2022-01-17 20:33:52 -05:00
|
|
|
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip.height, current_tip.height)) {
|
2020-01-24 18:56:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_db->WriteBatch(batch)) return false;
|
|
|
|
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
2022-01-17 20:33:52 -05:00
|
|
|
const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip.hash)};
|
|
|
|
const CBlockIndex* new_tip_index{m_chainstate->m_blockman.LookupBlockIndex(new_tip.hash)};
|
2020-01-24 18:56:47 +01:00
|
|
|
const auto& consensus_params{Params().GetConsensus()};
|
|
|
|
|
|
|
|
do {
|
|
|
|
CBlock block;
|
|
|
|
|
|
|
|
if (!ReadBlockFromDisk(block, iter_tip, consensus_params)) {
|
|
|
|
return error("%s: Failed to read block %s from disk",
|
|
|
|
__func__, iter_tip->GetBlockHash().ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
ReverseBlock(block, iter_tip);
|
|
|
|
|
|
|
|
iter_tip = iter_tip->GetAncestor(iter_tip->nHeight - 1);
|
2022-01-17 20:33:52 -05:00
|
|
|
} while (new_tip_index != iter_tip);
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:33:52 -05:00
|
|
|
return true;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 17:32:19 -05:00
|
|
|
static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockKey& block, DBVal& result)
|
2020-01-24 18:56:47 +01:00
|
|
|
{
|
|
|
|
// First check if the result is stored under the height index and the value
|
|
|
|
// there matches the block hash. This should be the case if the block is on
|
|
|
|
// the active chain.
|
|
|
|
std::pair<uint256, DBVal> read_out;
|
2022-01-17 17:32:19 -05:00
|
|
|
if (!db.Read(DBHeightKey(block.height), read_out)) {
|
2020-01-24 18:56:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
2022-01-17 17:32:19 -05:00
|
|
|
if (read_out.first == block.hash) {
|
2020-01-24 18:56:47 +01:00
|
|
|
result = std::move(read_out.second);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If value at the height index corresponds to an different block, the
|
|
|
|
// result will be stored in the hash index.
|
2022-01-17 17:32:19 -05:00
|
|
|
return db.Read(DBHashKey(block.hash), result);
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-05-02 13:44:19 -04:00
|
|
|
std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex* block_index) const
|
2020-01-24 18:56:47 +01:00
|
|
|
{
|
2022-05-02 13:44:19 -04:00
|
|
|
CCoinsStats stats{Assert(block_index)->nHeight, block_index->GetBlockHash()};
|
|
|
|
stats.index_used = true;
|
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
DBVal entry;
|
2022-01-17 17:32:19 -05:00
|
|
|
if (!LookUpOne(*m_db, {block_index->GetBlockHash(), block_index->nHeight}, entry)) {
|
2022-05-02 13:44:19 -04:00
|
|
|
return std::nullopt;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-05-02 13:44:19 -04:00
|
|
|
stats.hashSerialized = entry.muhash;
|
|
|
|
stats.nTransactionOutputs = entry.transaction_output_count;
|
|
|
|
stats.nBogoSize = entry.bogo_size;
|
|
|
|
stats.total_amount = entry.total_amount;
|
|
|
|
stats.total_subsidy = entry.total_subsidy;
|
|
|
|
stats.total_unspendable_amount = entry.total_unspendable_amount;
|
|
|
|
stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
|
|
|
|
stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
|
|
|
|
stats.total_coinbase_amount = entry.total_coinbase_amount;
|
|
|
|
stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
|
|
|
|
stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
|
|
|
|
stats.total_unspendables_scripts = entry.total_unspendables_scripts;
|
|
|
|
stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
|
|
|
|
|
|
|
|
return stats;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 18:36:40 -05:00
|
|
|
bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
|
2020-01-24 18:56:47 +01:00
|
|
|
{
|
|
|
|
if (!m_db->Read(DB_MUHASH, m_muhash)) {
|
|
|
|
// Check that the cause of the read failure is that the key does not
|
|
|
|
// exist. Any other errors indicate database corruption or a disk
|
|
|
|
// failure, and starting the index would cause further corruption.
|
|
|
|
if (m_db->Exists(DB_MUHASH)) {
|
|
|
|
return error("%s: Cannot read current %s state; index may be corrupted",
|
|
|
|
__func__, GetName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 18:36:40 -05:00
|
|
|
if (block) {
|
2021-05-24 01:24:05 +02:00
|
|
|
DBVal entry;
|
2022-01-17 18:36:40 -05:00
|
|
|
if (!LookUpOne(*m_db, *block, entry)) {
|
2022-01-20 22:21:56 +01:00
|
|
|
return error("%s: Cannot read current %s state; index may be corrupted",
|
|
|
|
__func__, GetName());
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
2022-01-23 15:53:07 +01:00
|
|
|
|
|
|
|
uint256 out;
|
|
|
|
m_muhash.Finalize(out);
|
|
|
|
if (entry.muhash != out) {
|
|
|
|
return error("%s: Cannot read current %s state; index may be corrupted",
|
|
|
|
__func__, GetName());
|
|
|
|
}
|
|
|
|
|
2021-05-24 01:24:05 +02:00
|
|
|
m_transaction_output_count = entry.transaction_output_count;
|
|
|
|
m_bogo_size = entry.bogo_size;
|
|
|
|
m_total_amount = entry.total_amount;
|
|
|
|
m_total_subsidy = entry.total_subsidy;
|
|
|
|
m_total_unspendable_amount = entry.total_unspendable_amount;
|
|
|
|
m_total_prevout_spent_amount = entry.total_prevout_spent_amount;
|
|
|
|
m_total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
|
|
|
|
m_total_coinbase_amount = entry.total_coinbase_amount;
|
|
|
|
m_total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
|
|
|
|
m_total_unspendables_bip30 = entry.total_unspendables_bip30;
|
|
|
|
m_total_unspendables_scripts = entry.total_unspendables_scripts;
|
|
|
|
m_total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2021-05-24 01:24:05 +02:00
|
|
|
return true;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
2022-01-17 20:35:02 -05:00
|
|
|
bool CoinStatsIndex::CustomCommit(CDBBatch& batch)
|
2022-01-24 14:34:28 +01:00
|
|
|
{
|
|
|
|
// DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
|
|
|
|
// to prevent an inconsistent state of the DB.
|
|
|
|
batch.Write(DB_MUHASH, m_muhash);
|
2022-01-17 20:35:02 -05:00
|
|
|
return true;
|
2022-01-24 14:34:28 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
// Reverse a single block as part of a reorg
|
|
|
|
bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex)
|
|
|
|
{
|
|
|
|
CBlockUndo block_undo;
|
|
|
|
std::pair<uint256, DBVal> read_out;
|
|
|
|
|
2020-08-22 20:21:20 +02:00
|
|
|
const CAmount block_subsidy{GetBlockSubsidy(pindex->nHeight, Params().GetConsensus())};
|
|
|
|
m_total_subsidy -= block_subsidy;
|
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
// Ignore genesis block
|
|
|
|
if (pindex->nHeight > 0) {
|
|
|
|
if (!UndoReadFromDisk(block_undo, pindex)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_db->Read(DBHeightKey(pindex->nHeight - 1), read_out)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint256 expected_block_hash{pindex->pprev->GetBlockHash()};
|
|
|
|
if (read_out.first != expected_block_hash) {
|
2021-05-24 18:37:40 +02:00
|
|
|
LogPrintf("WARNING: previous block header belongs to unexpected block %s; expected %s\n",
|
|
|
|
read_out.first.ToString(), expected_block_hash.ToString());
|
|
|
|
|
2020-01-24 18:56:47 +01:00
|
|
|
if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) {
|
2021-05-24 18:37:40 +02:00
|
|
|
return error("%s: previous block header not found; expected %s",
|
|
|
|
__func__, expected_block_hash.ToString());
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the new UTXOs that were created from the block
|
|
|
|
for (size_t i = 0; i < block.vtx.size(); ++i) {
|
|
|
|
const auto& tx{block.vtx.at(i)};
|
|
|
|
|
2021-05-24 00:46:08 +02:00
|
|
|
for (uint32_t j = 0; j < tx->vout.size(); ++j) {
|
2020-01-24 18:56:47 +01:00
|
|
|
const CTxOut& out{tx->vout[j]};
|
2021-05-24 00:46:08 +02:00
|
|
|
COutPoint outpoint{tx->GetHash(), j};
|
2020-01-24 18:56:47 +01:00
|
|
|
Coin coin{out, pindex->nHeight, tx->IsCoinBase()};
|
|
|
|
|
|
|
|
// Skip unspendable coins
|
2020-08-22 20:21:20 +02:00
|
|
|
if (coin.out.scriptPubKey.IsUnspendable()) {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_unspendable_amount -= coin.out.nValue;
|
|
|
|
m_total_unspendables_scripts -= coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-24 18:56:47 +01:00
|
|
|
|
|
|
|
m_muhash.Remove(MakeUCharSpan(TxOutSer(outpoint, coin)));
|
2020-08-22 20:21:20 +02:00
|
|
|
|
|
|
|
if (tx->IsCoinBase()) {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_coinbase_amount -= coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
} else {
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_new_outputs_ex_coinbase_amount -= coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
--m_transaction_output_count;
|
|
|
|
m_total_amount -= coin.out.nValue;
|
|
|
|
m_bogo_size -= GetBogoSize(coin.out.scriptPubKey);
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The coinbase tx has no undo data since no former output is spent
|
|
|
|
if (!tx->IsCoinBase()) {
|
|
|
|
const auto& tx_undo{block_undo.vtxundo.at(i - 1)};
|
|
|
|
|
|
|
|
for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
|
|
|
|
Coin coin{tx_undo.vprevout[j]};
|
|
|
|
COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
|
|
|
|
|
|
|
|
m_muhash.Insert(MakeUCharSpan(TxOutSer(outpoint, coin)));
|
2020-08-22 20:21:20 +02:00
|
|
|
|
2021-06-03 01:31:47 +02:00
|
|
|
m_total_prevout_spent_amount -= coin.out.nValue;
|
2020-08-22 20:21:20 +02:00
|
|
|
|
|
|
|
m_transaction_output_count++;
|
|
|
|
m_total_amount += coin.out.nValue;
|
|
|
|
m_bogo_size += GetBogoSize(coin.out.scriptPubKey);
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 01:31:47 +02:00
|
|
|
const CAmount unclaimed_rewards{(m_total_new_outputs_ex_coinbase_amount + m_total_coinbase_amount + m_total_unspendable_amount) - (m_total_prevout_spent_amount + m_total_subsidy)};
|
|
|
|
m_total_unspendable_amount -= unclaimed_rewards;
|
|
|
|
m_total_unspendables_unclaimed_rewards -= unclaimed_rewards;
|
2020-08-22 20:21:20 +02:00
|
|
|
|
|
|
|
// Check that the rolled back internal values are consistent with the DB read out
|
2020-01-24 18:56:47 +01:00
|
|
|
uint256 out;
|
|
|
|
m_muhash.Finalize(out);
|
|
|
|
Assert(read_out.second.muhash == out);
|
|
|
|
|
2020-08-22 20:21:20 +02:00
|
|
|
Assert(m_transaction_output_count == read_out.second.transaction_output_count);
|
|
|
|
Assert(m_total_amount == read_out.second.total_amount);
|
|
|
|
Assert(m_bogo_size == read_out.second.bogo_size);
|
|
|
|
Assert(m_total_subsidy == read_out.second.total_subsidy);
|
2021-06-03 01:31:47 +02:00
|
|
|
Assert(m_total_unspendable_amount == read_out.second.total_unspendable_amount);
|
|
|
|
Assert(m_total_prevout_spent_amount == read_out.second.total_prevout_spent_amount);
|
|
|
|
Assert(m_total_new_outputs_ex_coinbase_amount == read_out.second.total_new_outputs_ex_coinbase_amount);
|
|
|
|
Assert(m_total_coinbase_amount == read_out.second.total_coinbase_amount);
|
|
|
|
Assert(m_total_unspendables_genesis_block == read_out.second.total_unspendables_genesis_block);
|
|
|
|
Assert(m_total_unspendables_bip30 == read_out.second.total_unspendables_bip30);
|
|
|
|
Assert(m_total_unspendables_scripts == read_out.second.total_unspendables_scripts);
|
|
|
|
Assert(m_total_unspendables_unclaimed_rewards == read_out.second.total_unspendables_unclaimed_rewards);
|
2020-01-24 18:56:47 +01:00
|
|
|
|
2022-01-24 14:34:28 +01:00
|
|
|
return true;
|
2020-01-24 18:56:47 +01:00
|
|
|
}
|