Branch data Line data Source code
1 : : // Copyright (c) 2022-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <kernel/coinstats.h>
6 : :
7 : : #include <chain.h>
8 : : #include <coins.h>
9 : : #include <crypto/muhash.h>
10 : : #include <hash.h>
11 : : #include <logging.h>
12 : : #include <node/blockstorage.h>
13 : : #include <primitives/transaction.h>
14 : : #include <script/script.h>
15 : : #include <span.h>
16 : : #include <streams.h>
17 : : #include <sync.h>
18 : : #include <uint256.h>
19 : : #include <util/check.h>
20 : : #include <util/overflow.h>
21 : : #include <validation.h>
22 : :
23 : : #include <map>
24 : : #include <memory>
25 : : #include <span>
26 : : #include <utility>
27 : : #include <version>
28 : :
29 : : namespace kernel {
30 : :
31 : 50 : CCoinsStats::CCoinsStats(int block_height, const uint256& block_hash)
32 : 50 : : nHeight(block_height),
33 : 50 : hashBlock(block_hash) {}
34 : :
35 : : // Database-independent metric indicating the UTXO set size
36 : 5619 : uint64_t GetBogoSize(const CScript& script_pub_key)
37 : : {
38 : 5619 : return 32 /* txid */ +
39 : : 4 /* vout index */ +
40 : : 4 /* height + coinbase */ +
41 : : 8 /* amount */ +
42 : 5619 : 2 /* scriptPubKey len */ +
43 [ - + ]: 5619 : script_pub_key.size() /* scriptPubKey */;
44 : : }
45 : :
46 : : template <typename T>
47 : 5619 : static void TxOutSer(T& ss, const COutPoint& outpoint, const Coin& coin)
48 : : {
49 : 5619 : ss << outpoint;
50 : 5619 : ss << static_cast<uint32_t>((coin.nHeight << 1) + coin.fCoinBase);
51 : 5619 : ss << coin.out;
52 : 5619 : }
53 : :
54 : 5417 : static void ApplyCoinHash(HashWriter& ss, const COutPoint& outpoint, const Coin& coin)
55 : : {
56 : 5417 : TxOutSer(ss, outpoint, coin);
57 : 5417 : }
58 : :
59 : 202 : void ApplyCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin)
60 : : {
61 : 202 : DataStream ss{};
62 [ + - ]: 202 : TxOutSer(ss, outpoint, coin);
63 : 202 : muhash.Insert(MakeUCharSpan(ss));
64 : 202 : }
65 : :
66 : 0 : void RemoveCoinHash(MuHash3072& muhash, const COutPoint& outpoint, const Coin& coin)
67 : : {
68 : 0 : DataStream ss{};
69 [ # # ]: 0 : TxOutSer(ss, outpoint, coin);
70 : 0 : muhash.Remove(MakeUCharSpan(ss));
71 : 0 : }
72 : :
73 : : static void ApplyCoinHash(std::nullptr_t, const COutPoint& outpoint, const Coin& coin) {}
74 : :
75 : : //! Warning: be very careful when changing this! assumeutxo and UTXO snapshot
76 : : //! validation commitments are reliant on the hash constructed by this
77 : : //! function.
78 : : //!
79 : : //! If the construction of this hash is changed, it will invalidate
80 : : //! existing UTXO snapshots. This will not result in any kind of consensus
81 : : //! failure, but it will force clients that were expecting to make use of
82 : : //! assumeutxo to do traditional IBD instead.
83 : : //!
84 : : //! It is also possible, though very unlikely, that a change in this
85 : : //! construction could cause a previously invalid (and potentially malicious)
86 : : //! UTXO snapshot to be considered valid.
87 : : template <typename T>
88 : 5417 : static void ApplyHash(T& hash_obj, const Txid& hash, const std::map<uint32_t, Coin>& outputs)
89 : : {
90 [ + + ]: 10834 : for (auto it = outputs.begin(); it != outputs.end(); ++it) {
91 : 5417 : COutPoint outpoint = COutPoint(hash, it->first);
92 : 5417 : Coin coin = it->second;
93 [ + - ]: 5417 : ApplyCoinHash(hash_obj, outpoint, coin);
94 : : }
95 : 5417 : }
96 : :
97 : 5417 : static void ApplyStats(CCoinsStats& stats, const std::map<uint32_t, Coin>& outputs)
98 : : {
99 [ - + ]: 5417 : assert(!outputs.empty());
100 : 5417 : stats.nTransactions++;
101 [ + + ]: 10834 : for (auto it = outputs.begin(); it != outputs.end(); ++it) {
102 : 5417 : stats.nTransactionOutputs++;
103 [ + - ]: 5417 : if (stats.total_amount.has_value()) {
104 : 5417 : stats.total_amount = CheckedAdd(*stats.total_amount, it->second.out.nValue);
105 : : }
106 : 5417 : stats.nBogoSize += GetBogoSize(it->second.out.scriptPubKey);
107 : : }
108 : 5417 : }
109 : :
110 : : //! Calculate statistics about the unspent transaction output set
111 : : template <typename T>
112 : 46 : static bool ComputeUTXOStats(CCoinsView* view, CCoinsStats& stats, T hash_obj, const std::function<void()>& interruption_point)
113 : : {
114 [ - + ]: 46 : std::unique_ptr<CCoinsViewCursor> pcursor(view->Cursor());
115 [ - + ]: 46 : assert(pcursor);
116 : :
117 : 46 : Txid prevkey;
118 : 46 : std::map<uint32_t, Coin> outputs;
119 [ + - + + ]: 5463 : while (pcursor->Valid()) {
120 [ + - + - ]: 5417 : if (interruption_point) interruption_point();
121 : 5417 : COutPoint key;
122 [ + - ]: 5417 : Coin coin;
123 [ + - + - : 5417 : if (pcursor->GetKey(key) && pcursor->GetValue(coin)) {
+ - + - ]
124 [ + + + - ]: 5417 : if (!outputs.empty() && key.hash != prevkey) {
125 [ + - ]: 5371 : ApplyStats(stats, outputs);
126 [ + - ]: 5371 : ApplyHash(hash_obj, prevkey, outputs);
127 : 5371 : outputs.clear();
128 : : }
129 : 5417 : prevkey = key.hash;
130 [ + - ]: 5417 : outputs[key.n] = std::move(coin);
131 [ + - ]: 5417 : stats.coins_count++;
132 : : } else {
133 [ # # ]: 0 : LogError("%s: unable to read value\n", __func__);
134 : 0 : return false;
135 : : }
136 [ + - ]: 5417 : pcursor->Next();
137 : : }
138 [ + - ]: 46 : if (!outputs.empty()) {
139 [ + - ]: 46 : ApplyStats(stats, outputs);
140 [ + - ]: 46 : ApplyHash(hash_obj, prevkey, outputs);
141 : : }
142 : :
143 [ + - ]: 46 : FinalizeHash(hash_obj, stats);
144 : :
145 [ + - ]: 46 : stats.nDiskSize = view->EstimateSize();
146 : :
147 : 46 : return true;
148 : 46 : }
149 : :
150 : 46 : std::optional<CCoinsStats> ComputeUTXOStats(CoinStatsHashType hash_type, CCoinsView* view, node::BlockManager& blockman, const std::function<void()>& interruption_point)
151 : : {
152 [ + - + - : 138 : CBlockIndex* pindex = WITH_LOCK(::cs_main, return blockman.LookupBlockIndex(view->GetBestBlock()));
+ - ]
153 [ - + ]: 46 : CCoinsStats stats{Assert(pindex)->nHeight, pindex->GetBlockHash()};
154 : :
155 : 92 : bool success = [&]() -> bool {
156 [ + - - - ]: 46 : switch (hash_type) {
157 : 46 : case(CoinStatsHashType::HASH_SERIALIZED): {
158 : 46 : HashWriter ss{};
159 : 46 : return ComputeUTXOStats(view, stats, ss, interruption_point);
160 : : }
161 : 0 : case(CoinStatsHashType::MUHASH): {
162 : 0 : MuHash3072 muhash;
163 : 0 : return ComputeUTXOStats(view, stats, muhash, interruption_point);
164 : : }
165 : 0 : case(CoinStatsHashType::NONE): {
166 : 0 : return ComputeUTXOStats(view, stats, nullptr, interruption_point);
167 : : }
168 : : } // no default case, so the compiler can warn about missing cases
169 : 0 : assert(false);
170 : 46 : }();
171 : :
172 [ - + ]: 46 : if (!success) {
173 : 0 : return std::nullopt;
174 : : }
175 : 46 : return stats;
176 : : }
177 : :
178 : 46 : static void FinalizeHash(HashWriter& ss, CCoinsStats& stats)
179 : : {
180 : 46 : stats.hashSerialized = ss.GetHash();
181 : 46 : }
182 : 0 : static void FinalizeHash(MuHash3072& muhash, CCoinsStats& stats)
183 : : {
184 : 0 : uint256 out;
185 : 0 : muhash.Finalize(out);
186 : 0 : stats.hashSerialized = out;
187 : 0 : }
188 : : static void FinalizeHash(std::nullptr_t, CCoinsStats& stats) {}
189 : :
190 : : } // namespace kernel
|