Bitcoin ABC  0.22.13
P2P Digital Currency
coinstats.cpp
Go to the documentation of this file.
1 // Copyright (c) 2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <node/coinstats.h>
7 
8 #include <coins.h>
9 #include <hash.h>
10 #include <serialize.h>
11 #include <util/system.h>
12 #include <validation.h>
13 
14 #include <map>
15 
16 static void ApplyStats(CCoinsStats &stats, CHashWriter &ss, const uint256 &hash,
17  const std::map<uint32_t, Coin> &outputs) {
18  assert(!outputs.empty());
19  ss << hash;
20  ss << VARINT(outputs.begin()->second.GetHeight() * 2 +
21  outputs.begin()->second.IsCoinBase());
22  stats.nTransactions++;
23  for (const auto &output : outputs) {
24  ss << VARINT(output.first + 1);
25  ss << output.second.GetTxOut().scriptPubKey;
26  ss << VARINT_MODE(output.second.GetTxOut().nValue / SATOSHI,
28  stats.nTransactionOutputs++;
29  stats.nTotalAmount += output.second.GetTxOut().nValue;
30  stats.nBogoSize +=
31  32 /* txid */ + 4 /* vout index */ + 4 /* height + coinbase */ +
32  8 /* amount */ + 2 /* scriptPubKey len */ +
33  output.second.GetTxOut().scriptPubKey.size() /* scriptPubKey */;
34  }
35  ss << VARINT(0u);
36 }
37 
39 bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats,
40  const std::function<void()> &interruption_point) {
41  stats = CCoinsStats();
42  std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
43  assert(pcursor);
44 
46  stats.hashBlock = pcursor->GetBestBlock();
47  {
48  LOCK(cs_main);
49  stats.nHeight = LookupBlockIndex(stats.hashBlock)->nHeight;
50  }
51  ss << stats.hashBlock;
52  uint256 prevkey;
53  std::map<uint32_t, Coin> outputs;
54  while (pcursor->Valid()) {
55  interruption_point();
56  COutPoint key;
57  Coin coin;
58  if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
59  if (!outputs.empty() && key.GetTxId() != prevkey) {
60  ApplyStats(stats, ss, prevkey, outputs);
61  outputs.clear();
62  }
63  prevkey = key.GetTxId();
64  outputs[key.GetN()] = std::move(coin);
65  stats.coins_count++;
66  } else {
67  return error("%s: unable to read value", __func__);
68  }
69  pcursor->Next();
70  }
71  if (!outputs.empty()) {
72  ApplyStats(stats, ss, prevkey, outputs);
73  }
74  stats.hashSerialized = ss.GetHash();
75  stats.nDiskSize = view->EstimateSize();
76  return true;
77 }
uint32_t GetN() const
Definition: transaction.h:44
bool GetUTXOStats(CCoinsView *view, CCoinsStats &stats, const std::function< void()> &interruption_point)
Calculate statistics about the unspent transaction output set.
Definition: coinstats.cpp:39
#define VARINT(obj)
Definition: serialize.h:608
uint64_t nTransactionOutputs
Definition: coinstats.h:22
A UTXO entry.
Definition: coins.h:27
int nHeight
Definition: coinstats.h:19
virtual CCoinsViewCursor * Cursor() const
Get a cursor to iterate over the whole state.
Definition: coins.cpp:24
const TxId & GetTxId() const
Definition: transaction.h:43
uint64_t nTransactions
Definition: coinstats.h:21
static constexpr Amount SATOSHI
Definition: amount.h:151
uint64_t nDiskSize
Definition: coinstats.h:25
uint64_t nBogoSize
Definition: coinstats.h:23
uint256 hashSerialized
Definition: coinstats.h:24
#define VARINT_MODE(obj, mode)
Definition: serialize.h:607
Abstract view on the open txout dataset.
Definition: coins.h:175
#define LOCK(cs)
Definition: sync.h:230
uint64_t coins_count
The number of coins contained.
Definition: coinstats.h:29
RecursiveMutex cs_main
Global state.
Definition: validation.cpp:95
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
uint256 GetHash()
Definition: hash.h:139
256-bit opaque blob.
Definition: uint256.h:120
static void ApplyStats(CCoinsStats &stats, CHashWriter &ss, const uint256 &hash, const std::map< uint32_t, Coin > &outputs)
Definition: coinstats.cpp:16
virtual size_t EstimateSize() const
Estimate database size (0 if not implemented)
Definition: coins.h:208
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
BlockHash hashBlock
Definition: coinstats.h:20
CBlockIndex * LookupBlockIndex(const BlockHash &hash)
Definition: validation.cpp:145
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:120
Amount nTotalAmount
Definition: coinstats.h:26
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: blockindex.h:36
bool error(const char *fmt, const Args &... args)
Definition: system.h:47