2019-03-28 12:41:01 -03:00
|
|
|
// Copyright (c) 2010 Satoshi Nakamoto
|
2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2019-03-28 12:41:01 -03: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>
|
|
|
|
#include <hash.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
#include <util/system.h>
|
2020-06-02 17:41:04 -04:00
|
|
|
#include <validation.h>
|
2019-03-28 12:41:01 -03:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
2020-06-02 17:41:04 -04:00
|
|
|
static uint64_t GetBogoSize(const CScript& scriptPubKey)
|
|
|
|
{
|
|
|
|
return 32 /* txid */ +
|
|
|
|
4 /* vout index */ +
|
|
|
|
4 /* height + coinbase */ +
|
|
|
|
8 /* amount */ +
|
|
|
|
2 /* scriptPubKey len */ +
|
|
|
|
scriptPubKey.size() /* scriptPubKey */;
|
|
|
|
}
|
|
|
|
|
2019-03-28 12:41:01 -03:00
|
|
|
static void ApplyStats(CCoinsStats &stats, CHashWriter& ss, const uint256& hash, const std::map<uint32_t, Coin>& outputs)
|
|
|
|
{
|
|
|
|
assert(!outputs.empty());
|
|
|
|
ss << hash;
|
|
|
|
ss << VARINT(outputs.begin()->second.nHeight * 2 + outputs.begin()->second.fCoinBase ? 1u : 0u);
|
|
|
|
stats.nTransactions++;
|
|
|
|
for (const auto& output : outputs) {
|
|
|
|
ss << VARINT(output.first + 1);
|
|
|
|
ss << output.second.out.scriptPubKey;
|
2020-02-07 00:57:32 -03:00
|
|
|
ss << VARINT_MODE(output.second.out.nValue, VarIntMode::NONNEGATIVE_SIGNED);
|
2019-03-28 12:41:01 -03:00
|
|
|
stats.nTransactionOutputs++;
|
|
|
|
stats.nTotalAmount += output.second.out.nValue;
|
2020-06-02 17:41:04 -04:00
|
|
|
stats.nBogoSize += GetBogoSize(output.second.out.scriptPubKey);
|
2019-03-28 12:41:01 -03:00
|
|
|
}
|
|
|
|
ss << VARINT(0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Calculate statistics about the unspent transaction output set
|
2020-05-22 16:09:34 -04:00
|
|
|
bool GetUTXOStats(CCoinsView* view, CCoinsStats& stats, const std::function<void()>& interruption_point)
|
2019-03-28 12:41:01 -03:00
|
|
|
{
|
2019-03-28 13:09:06 -03:00
|
|
|
stats = CCoinsStats();
|
2019-03-28 12:41:01 -03:00
|
|
|
std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
|
|
|
|
assert(pcursor);
|
|
|
|
|
|
|
|
CHashWriter ss(SER_GETHASH, PROTOCOL_VERSION);
|
|
|
|
stats.hashBlock = pcursor->GetBestBlock();
|
|
|
|
{
|
|
|
|
LOCK(cs_main);
|
|
|
|
stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
|
|
|
|
}
|
|
|
|
ss << stats.hashBlock;
|
|
|
|
uint256 prevkey;
|
|
|
|
std::map<uint32_t, Coin> outputs;
|
|
|
|
while (pcursor->Valid()) {
|
2020-05-22 16:09:34 -04:00
|
|
|
interruption_point();
|
2019-03-28 12:41:01 -03:00
|
|
|
COutPoint key;
|
|
|
|
Coin coin;
|
|
|
|
if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
|
|
|
|
if (!outputs.empty() && key.hash != prevkey) {
|
|
|
|
ApplyStats(stats, ss, prevkey, outputs);
|
|
|
|
outputs.clear();
|
|
|
|
}
|
|
|
|
prevkey = key.hash;
|
|
|
|
outputs[key.n] = std::move(coin);
|
2019-03-28 13:09:06 -03:00
|
|
|
stats.coins_count++;
|
2019-03-28 12:41:01 -03:00
|
|
|
} else {
|
|
|
|
return error("%s: unable to read value", __func__);
|
|
|
|
}
|
|
|
|
pcursor->Next();
|
|
|
|
}
|
|
|
|
if (!outputs.empty()) {
|
|
|
|
ApplyStats(stats, ss, prevkey, outputs);
|
|
|
|
}
|
|
|
|
stats.hashSerialized = ss.GetHash();
|
|
|
|
stats.nDiskSize = view->EstimateSize();
|
|
|
|
return true;
|
|
|
|
}
|