2019-03-28 11:41:01 -04:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2021-12-30 19:36:57 +02:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2019-03-28 11:41:01 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include <node/coinstats.h>
|
|
|
|
|
|
|
|
#include <coins.h>
|
2020-06-02 23:55:32 +02:00
|
|
|
#include <crypto/muhash.h>
|
2019-03-28 11:41:01 -04:00
|
|
|
#include <hash.h>
|
2020-01-24 18:58:47 +01:00
|
|
|
#include <index/coinstatsindex.h>
|
2022-02-15 18:37:32 -05:00
|
|
|
#include <optional>
|
2019-03-28 11:41:01 -04:00
|
|
|
#include <serialize.h>
|
|
|
|
#include <uint256.h>
|
2021-11-01 17:03:09 +01:00
|
|
|
#include <util/overflow.h>
|
2019-03-28 11:41:01 -04:00
|
|
|
#include <util/system.h>
|
2020-06-02 23:41:04 +02:00
|
|
|
#include <validation.h>
|
2019-03-28 11:41:01 -04:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2021-11-12 10:06:00 -05:00
|
|
|
namespace node {
|
2022-02-16 14:11:19 -05:00
|
|
|
|
|
|
|
CCoinsStats::CCoinsStats(int block_height, const uint256& block_hash)
|
|
|
|
: nHeight(block_height),
|
|
|
|
hashBlock(block_hash) {}
|
|
|
|
|
2020-08-22 20:25:23 +02:00
|
|
|
// Database-independent metric indicating the UTXO set size
|
2020-01-24 18:56:47 +01:00
|
|
|
uint64_t GetBogoSize(const CScript& script_pub_key)
|
2020-06-02 23:41:04 +02:00
|
|
|
{
|
|
|
|
return 32 /* txid */ +
|
|
|
|
4 /* vout index */ +
|
|
|
|
4 /* height + coinbase */ +
|
|
|
|
8 /* amount */ +
|
|
|
|
2 /* scriptPubKey len */ +
|
2020-01-24 18:56:47 +01:00
|
|
|
script_pub_key.size() /* scriptPubKey */;
|
|
|
|
}
|
|
|
|
|
|
|
|
CDataStream TxOutSer(const COutPoint& outpoint, const Coin& coin) {
|
|
|
|
CDataStream ss(SER_DISK, PROTOCOL_VERSION);
|
|
|
|
ss << outpoint;
|
|
|
|
ss << static_cast<uint32_t>(coin.nHeight * 2 + coin.fCoinBase);
|
|
|
|
ss << coin.out;
|
|
|
|
return ss;
|
2020-06-02 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
2019-04-25 11:09:29 -04:00
|
|
|
//! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
|
|
|
|
//! validation commitments are reliant on the hash constructed by this
|
|
|
|
//! function.
|
|
|
|
//!
|
|
|
|
//! If the construction of this hash is changed, it will invalidate
|
|
|
|
//! existing UTXO snapshots. This will not result in any kind of consensus
|
|
|
|
//! failure, but it will force clients that were expecting to make use of
|
|
|
|
//! assumeutxo to do traditional IBD instead.
|
|
|
|
//!
|
|
|
|
//! It is also possible, though very unlikely, that a change in this
|
|
|
|
//! construction could cause a previously invalid (and potentially malicious)
|
|
|
|
//! UTXO snapshot to be considered valid.
|
2021-02-21 23:09:50 +01:00
|
|
|
static void ApplyHash(CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
|
|
|
{
|
|
|
|
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
|
|
|
if (it == outputs.begin()) {
|
|
|
|
ss << hash;
|
|
|
|
ss << VARINT(it->second.nHeight * 2 + it->second.fCoinBase ? 1u : 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
ss << VARINT(it->first + 1);
|
|
|
|
ss << it->second.out.scriptPubKey;
|
|
|
|
ss << VARINT_MODE(it->second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
|
|
|
|
|
|
|
|
if (it == std::prev(outputs.end())) {
|
|
|
|
ss << VARINT(0u);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ApplyHash(std::nullptr_t, const uint256& hash, const std::map<uint32_t, Coin>& outputs) {}
|
|
|
|
|
|
|
|
static void ApplyHash(MuHash3072& muhash, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
|
|
|
{
|
|
|
|
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
|
|
|
COutPoint outpoint = COutPoint(hash, it->first);
|
|
|
|
Coin coin = it->second;
|
2020-01-24 18:56:47 +01:00
|
|
|
muhash.Insert(MakeUCharSpan(TxOutSer(outpoint, coin)));
|
2021-02-21 23:09:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ApplyStats(CCoinsStats& stats, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
2020-06-02 23:56:28 +02:00
|
|
|
{
|
|
|
|
assert(!outputs.empty());
|
|
|
|
stats.nTransactions++;
|
2020-07-05 12:15:32 +02:00
|
|
|
for (auto it = outputs.begin(); it != outputs.end(); ++it) {
|
2020-06-02 23:56:28 +02:00
|
|
|
stats.nTransactionOutputs++;
|
2021-11-01 17:03:09 +01:00
|
|
|
if (stats.total_amount.has_value()) {
|
|
|
|
stats.total_amount = CheckedAdd(*stats.total_amount, it->second.out.nValue);
|
|
|
|
}
|
2020-07-05 12:15:32 +02:00
|
|
|
stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
|
2020-06-02 23:56:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 11:41:01 -04:00
|
|
|
//! Calculate statistics about the unspent transaction output set
|
2020-06-02 23:52:34 +02:00
|
|
|
template <typename T>
|
2022-02-16 14:11:19 -05:00
|
|
|
static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
|
2019-03-28 11:41:01 -04:00
|
|
|
{
|
|
|
|
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
|
|
|
|
assert(pcursor);
|
2020-05-15 16:14:07 +02:00
|
|
|
|
2020-06-02 23:52:34 +02:00
|
|
|
PrepareHash(hash_obj, stats);
|
|
|
|
|
2019-03-28 11:41:01 -04:00
|
|
|
uint256 prevkey;
|
|
|
|
std::map<uint32_t, Coin> outputs;
|
|
|
|
while (pcursor->Valid()) {
|
2020-05-22 16:09:34 -04:00
|
|
|
interruption_point();
|
2019-03-28 11:41:01 -04:00
|
|
|
COutPoint key;
|
|
|
|
Coin coin;
|
|
|
|
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
|
|
|
|
if (!outputs.empty() && key.hash != prevkey) {
|
2021-02-21 23:09:50 +01:00
|
|
|
ApplyStats(stats, prevkey, outputs);
|
|
|
|
ApplyHash(hash_obj, prevkey, outputs);
|
2019-03-28 11:41:01 -04:00
|
|
|
outputs.clear();
|
|
|
|
}
|
|
|
|
prevkey = key.hash;
|
|
|
|
outputs[key.n] = std::move(coin);
|
2019-03-28 12:09:06 -04:00
|
|
|
stats.coins_count++;
|
2019-03-28 11:41:01 -04:00
|
|
|
} else {
|
|
|
|
return error("%s: unable to read value", __func__);
|
|
|
|
}
|
|
|
|
pcursor->Next();
|
|
|
|
}
|
|
|
|
if (!outputs.empty()) {
|
2021-02-21 23:09:50 +01:00
|
|
|
ApplyStats(stats, prevkey, outputs);
|
|
|
|
ApplyHash(hash_obj, prevkey, outputs);
|
2019-03-28 11:41:01 -04:00
|
|
|
}
|
2020-06-02 23:52:34 +02:00
|
|
|
|
|
|
|
FinalizeHash(hash_obj, stats);
|
|
|
|
|
2019-03-28 11:41:01 -04:00
|
|
|
stats.nDiskSize = view->EstimateSize();
|
2022-02-16 14:11:19 -05:00
|
|
|
|
2019-03-28 11:41:01 -04:00
|
|
|
return true;
|
|
|
|
}
|
2020-06-02 23:52:34 +02:00
|
|
|
|
2022-02-16 14:11:19 -05:00
|
|
|
std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, BlockManager& blockman, const std::function<void()>& interruption_point)
|
2020-06-02 23:52:34 +02:00
|
|
|
{
|
2022-02-16 14:11:19 -05:00
|
|
|
CBlockIndex* pindex = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
|
|
|
|
CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()};
|
2022-02-15 18:37:32 -05:00
|
|
|
|
|
|
|
bool success = [&]() -> bool {
|
|
|
|
switch (hash_type) {
|
|
|
|
case(CoinStatsHashType::HASH_SERIALIZED): {
|
|
|
|
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
|
2022-02-16 14:11:19 -05:00
|
|
|
return ComputeUTXOStats(view, stats, ss, interruption_point);
|
2022-02-15 18:37:32 -05:00
|
|
|
}
|
|
|
|
case(CoinStatsHashType::MUHASH): {
|
|
|
|
MuHash3072 muhash;
|
2022-02-16 14:11:19 -05:00
|
|
|
return ComputeUTXOStats(view, stats, muhash, interruption_point);
|
2022-02-15 18:37:32 -05:00
|
|
|
}
|
|
|
|
case(CoinStatsHashType::NONE): {
|
2022-02-16 14:11:19 -05:00
|
|
|
return ComputeUTXOStats(view, stats, nullptr, interruption_point);
|
2022-02-15 18:37:32 -05:00
|
|
|
}
|
|
|
|
} // no default case, so the compiler can warn about missing cases
|
|
|
|
assert(false);
|
|
|
|
}();
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
return std::nullopt;
|
2020-06-02 23:56:28 +02:00
|
|
|
}
|
2022-02-15 18:37:32 -05:00
|
|
|
return stats;
|
2020-06-02 23:52:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// The legacy hash serializes the hashBlock
|
2020-12-06 00:14:17 +00:00
|
|
|
static void PrepareHash(CHashWriter& ss, const CCoinsStats& stats)
|
2020-06-02 23:52:34 +02:00
|
|
|
{
|
|
|
|
ss << stats.hashBlock;
|
|
|
|
}
|
2020-06-02 23:55:32 +02:00
|
|
|
// MuHash does not need the prepare step
|
|
|
|
static void PrepareHash(MuHash3072& muhash, CCoinsStats& stats) {}
|
2020-06-02 23:56:28 +02:00
|
|
|
static void PrepareHash(std::nullptr_t, CCoinsStats& stats) {}
|
2020-06-02 23:52:34 +02:00
|
|
|
|
|
|
|
static void FinalizeHash(CHashWriter& ss, CCoinsStats& stats)
|
|
|
|
{
|
|
|
|
stats.hashSerialized = ss.GetHash();
|
|
|
|
}
|
2020-06-02 23:55:32 +02:00
|
|
|
static void FinalizeHash(MuHash3072& muhash, CCoinsStats& stats)
|
|
|
|
{
|
|
|
|
uint256 out;
|
|
|
|
muhash.Finalize(out);
|
|
|
|
stats.hashSerialized = out;
|
|
|
|
}
|
2020-06-02 23:56:28 +02:00
|
|
|
static void FinalizeHash(std::nullptr_t, CCoinsStats& stats) {}
|
2022-02-16 14:11:19 -05:00
|
|
|
|
|
|
|
std::optional<CCoinsStats> LookupUTXOStatsWithIndex(CoinStatsIndex& coin_stats_index, const CBlockIndex* pindex)
|
|
|
|
{
|
|
|
|
CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()};
|
|
|
|
|
|
|
|
stats.index_used = true;
|
|
|
|
if (!coin_stats_index.LookUpStats(pindex, stats)) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stats;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<CCoinsStats> LookupUTXOStatsWithIndex(CoinStatsIndex& coin_stats_index, CCoinsView* view, BlockManager& blockman)
|
|
|
|
{
|
|
|
|
CBlockIndex* pindex = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
|
|
|
|
|
|
|
|
return LookupUTXOStatsWithIndex(coin_stats_index, pindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::optional<CCoinsStats> GetUTXOStats(CCoinsView* view, BlockManager& blockman, CoinStatsHashType hash_type, const std::function<void()>& interruption_point, const CBlockIndex* pindex, bool index_requested)
|
|
|
|
{
|
|
|
|
// Use CoinStatsIndex if it is requested and available and a hash_type of Muhash or None was requested
|
|
|
|
if ((hash_type == CoinStatsHashType::MUHASH || hash_type == CoinStatsHashType::NONE) && g_coin_stats_index && index_requested) {
|
|
|
|
if (pindex) {
|
|
|
|
return LookupUTXOStatsWithIndex(*g_coin_stats_index, pindex);
|
|
|
|
} else {
|
|
|
|
return LookupUTXOStatsWithIndex(*g_coin_stats_index, view, blockman);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the coinstats index isn't requested or is otherwise not usable, the
|
|
|
|
// pindex should either be null or equal to the view's best block. This is
|
|
|
|
// because without the coinstats index we can only get coinstats about the
|
|
|
|
// best block.
|
|
|
|
assert(!pindex || pindex->GetBlockHash() == view->GetBestBlock());
|
|
|
|
|
|
|
|
return ComputeUTXOStats(hash_type, view, blockman, interruption_point);
|
|
|
|
}
|
2021-11-12 10:06:00 -05:00
|
|
|
} // namespace node
|