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