Branch data Line data Source code
1 : : // Copyright (c) 2020-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 <index/coinstatsindex.h>
6 : :
7 : : #include <arith_uint256.h>
8 : : #include <chain.h>
9 : : #include <chainparams.h>
10 : : #include <coins.h>
11 : : #include <common/args.h>
12 : : #include <consensus/amount.h>
13 : : #include <crypto/muhash.h>
14 : : #include <dbwrapper.h>
15 : : #include <index/base.h>
16 : : #include <index/db_key.h>
17 : : #include <interfaces/chain.h>
18 : : #include <interfaces/types.h>
19 : : #include <kernel/coinstats.h>
20 : : #include <logging.h>
21 : : #include <primitives/block.h>
22 : : #include <primitives/transaction.h>
23 : : #include <script/script.h>
24 : : #include <serialize.h>
25 : : #include <uint256.h>
26 : : #include <undo.h>
27 : : #include <util/check.h>
28 : : #include <util/fs.h>
29 : : #include <validation.h>
30 : :
31 : : #include <compare>
32 : : #include <limits>
33 : : #include <span>
34 : : #include <string>
35 : : #include <utility>
36 : : #include <vector>
37 : :
38 : : using kernel::ApplyCoinHash;
39 : : using kernel::CCoinsStats;
40 : : using kernel::GetBogoSize;
41 : : using kernel::RemoveCoinHash;
42 : :
43 : : static constexpr uint8_t DB_MUHASH{'M'};
44 : :
45 : : namespace {
46 : :
47 : : struct DBVal {
48 : : uint256 muhash{uint256::ZERO};
49 : : uint64_t transaction_output_count{0};
50 : : uint64_t bogo_size{0};
51 : : CAmount total_amount{0};
52 : : CAmount total_subsidy{0};
53 : : arith_uint256 total_prevout_spent_amount{0};
54 : : arith_uint256 total_new_outputs_ex_coinbase_amount{0};
55 : : arith_uint256 total_coinbase_amount{0};
56 : : CAmount total_unspendables_genesis_block{0};
57 : : CAmount total_unspendables_bip30{0};
58 : : CAmount total_unspendables_scripts{0};
59 : : CAmount total_unspendables_unclaimed_rewards{0};
60 : :
61 : 416 : SERIALIZE_METHODS(DBVal, obj)
62 : : {
63 : 208 : uint256 prevout_spent, new_outputs, coinbase;
64 : 208 : SER_WRITE(obj, prevout_spent = ArithToUint256(obj.total_prevout_spent_amount));
65 : 208 : SER_WRITE(obj, new_outputs = ArithToUint256(obj.total_new_outputs_ex_coinbase_amount));
66 : 208 : SER_WRITE(obj, coinbase = ArithToUint256(obj.total_coinbase_amount));
67 : :
68 : 208 : READWRITE(obj.muhash);
69 : 208 : READWRITE(obj.transaction_output_count);
70 : 208 : READWRITE(obj.bogo_size);
71 : 208 : READWRITE(obj.total_amount);
72 : 208 : READWRITE(obj.total_subsidy);
73 : 208 : READWRITE(prevout_spent);
74 : 208 : READWRITE(new_outputs);
75 : 208 : READWRITE(coinbase);
76 : 208 : READWRITE(obj.total_unspendables_genesis_block);
77 : 208 : READWRITE(obj.total_unspendables_bip30);
78 : 208 : READWRITE(obj.total_unspendables_scripts);
79 : 208 : READWRITE(obj.total_unspendables_unclaimed_rewards);
80 : :
81 : 208 : SER_READ(obj, obj.total_prevout_spent_amount = UintToArith256(prevout_spent));
82 : 208 : SER_READ(obj, obj.total_new_outputs_ex_coinbase_amount = UintToArith256(new_outputs));
83 : 208 : SER_READ(obj, obj.total_coinbase_amount = UintToArith256(coinbase));
84 : 208 : }
85 : : };
86 : : }; // namespace
87 : :
88 : : std::unique_ptr<CoinStatsIndex> g_coin_stats_index;
89 : :
90 : 3 : CoinStatsIndex::CoinStatsIndex(std::unique_ptr<interfaces::Chain> chain, size_t n_cache_size, bool f_memory, bool f_wipe)
91 [ + - ]: 3 : : BaseIndex(std::move(chain), "coinstatsindex")
92 : : {
93 : : // An earlier version of the index used "indexes/coinstats" but it contained
94 : : // a bug and is superseded by a fixed version at "indexes/coinstatsindex".
95 : : // The original index is kept around until the next release in case users
96 : : // decide to downgrade their node.
97 [ + - + - : 12 : auto old_path = gArgs.GetDataDirNet() / "indexes" / "coinstats";
+ - ]
98 [ + - - + ]: 3 : if (fs::exists(old_path)) {
99 : : // TODO: Change this to deleting the old index with v31.
100 [ # # # # ]: 0 : LogWarning("Old version of coinstatsindex found at %s. This folder can be safely deleted unless you " \
101 : : "plan to downgrade your node to version 29 or lower.", fs::PathToString(old_path));
102 : : }
103 [ + - + - : 12 : fs::path path{gArgs.GetDataDirNet() / "indexes" / "coinstatsindex"};
+ - ]
104 [ + - ]: 3 : fs::create_directories(path);
105 : :
106 [ + - + - : 12 : m_db = std::make_unique<CoinStatsIndex::DB>(path / "db", n_cache_size, f_memory, f_wipe);
+ - ]
107 : 6 : }
108 : :
109 : 204 : bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
110 : : {
111 : 204 : const CAmount block_subsidy{GetBlockSubsidy(block.height, Params().GetConsensus())};
112 : 204 : m_total_subsidy += block_subsidy;
113 : :
114 : : // Ignore genesis block
115 [ + + ]: 204 : if (block.height > 0) {
116 [ - + ]: 202 : uint256 expected_block_hash{*Assert(block.prev_hash)};
117 [ - + ]: 202 : if (m_current_block_hash != expected_block_hash) {
118 [ # # # # ]: 0 : LogError("previous block header belongs to unexpected block %s; expected %s",
119 : : m_current_block_hash.ToString(), expected_block_hash.ToString());
120 : 0 : return false;
121 : : }
122 : :
123 : : // Add the new utxos created from the block
124 [ - + ]: 202 : assert(block.data);
125 [ - + + + ]: 404 : for (size_t i = 0; i < block.data->vtx.size(); ++i) {
126 : 202 : const auto& tx{block.data->vtx.at(i)};
127 [ + - ]: 202 : const bool is_coinbase{tx->IsCoinBase()};
128 : :
129 : : // Skip duplicate txid coinbase transactions (BIP30).
130 [ + - - + ]: 202 : if (is_coinbase && IsBIP30Unspendable(block.hash, block.height)) {
131 : 0 : m_total_unspendables_bip30 += block_subsidy;
132 : 0 : continue;
133 : : }
134 : :
135 [ - + + + ]: 606 : for (uint32_t j = 0; j < tx->vout.size(); ++j) {
136 : 404 : const CTxOut& out{tx->vout[j]};
137 : 404 : const Coin coin{out, block.height, is_coinbase};
138 [ + + ]: 404 : const COutPoint outpoint{tx->GetHash(), j};
139 : :
140 : : // Skip unspendable coins
141 [ + + ]: 404 : if (coin.out.scriptPubKey.IsUnspendable()) {
142 : 202 : m_total_unspendables_scripts += coin.out.nValue;
143 : 202 : continue;
144 : : }
145 : :
146 [ + - ]: 202 : ApplyCoinHash(m_muhash, outpoint, coin);
147 : :
148 [ + - ]: 202 : if (is_coinbase) {
149 : 202 : m_total_coinbase_amount += coin.out.nValue;
150 : : } else {
151 : 0 : m_total_new_outputs_ex_coinbase_amount += coin.out.nValue;
152 : : }
153 : :
154 : 202 : ++m_transaction_output_count;
155 : 202 : m_total_amount += coin.out.nValue;
156 [ + - ]: 202 : m_bogo_size += GetBogoSize(coin.out.scriptPubKey);
157 : 404 : }
158 : :
159 : : // The coinbase tx has no undo data since no former output is spent
160 [ - + ]: 202 : if (!is_coinbase) {
161 [ # # ]: 0 : const auto& tx_undo{Assert(block.undo_data)->vtxundo.at(i - 1)};
162 : :
163 [ # # # # ]: 0 : for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
164 : 0 : const Coin& coin{tx_undo.vprevout[j]};
165 : 0 : const COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
166 : :
167 : 0 : RemoveCoinHash(m_muhash, outpoint, coin);
168 : :
169 : 0 : m_total_prevout_spent_amount += coin.out.nValue;
170 : :
171 : 0 : --m_transaction_output_count;
172 : 0 : m_total_amount -= coin.out.nValue;
173 : 0 : m_bogo_size -= GetBogoSize(coin.out.scriptPubKey);
174 : : }
175 : : }
176 : : }
177 : : } else {
178 : : // genesis block
179 : 2 : m_total_unspendables_genesis_block += block_subsidy;
180 : : }
181 : :
182 : : // If spent prevouts + block subsidy are still a higher amount than
183 : : // new outputs + coinbase + current unspendable amount this means
184 : : // the miner did not claim the full block reward. Unclaimed block
185 : : // rewards are also unspendable.
186 : 204 : const CAmount temp_total_unspendable_amount{m_total_unspendables_genesis_block + m_total_unspendables_bip30 + m_total_unspendables_scripts + m_total_unspendables_unclaimed_rewards};
187 : 612 : const arith_uint256 unclaimed_rewards{(m_total_prevout_spent_amount + m_total_subsidy) - (m_total_new_outputs_ex_coinbase_amount + m_total_coinbase_amount + temp_total_unspendable_amount)};
188 [ - + ]: 204 : assert(unclaimed_rewards <= arith_uint256(std::numeric_limits<CAmount>::max()));
189 : 204 : m_total_unspendables_unclaimed_rewards += static_cast<CAmount>(unclaimed_rewards.GetLow64());
190 : :
191 : 204 : std::pair<uint256, DBVal> value;
192 : 204 : value.first = block.hash;
193 : 204 : value.second.transaction_output_count = m_transaction_output_count;
194 : 204 : value.second.bogo_size = m_bogo_size;
195 : 204 : value.second.total_amount = m_total_amount;
196 : 204 : value.second.total_subsidy = m_total_subsidy;
197 : 204 : value.second.total_prevout_spent_amount = m_total_prevout_spent_amount;
198 : 204 : value.second.total_new_outputs_ex_coinbase_amount = m_total_new_outputs_ex_coinbase_amount;
199 : 204 : value.second.total_coinbase_amount = m_total_coinbase_amount;
200 : 204 : value.second.total_unspendables_genesis_block = m_total_unspendables_genesis_block;
201 : 204 : value.second.total_unspendables_bip30 = m_total_unspendables_bip30;
202 : 204 : value.second.total_unspendables_scripts = m_total_unspendables_scripts;
203 : 204 : value.second.total_unspendables_unclaimed_rewards = m_total_unspendables_unclaimed_rewards;
204 : :
205 : 204 : uint256 out;
206 : 204 : m_muhash.Finalize(out);
207 : 204 : value.second.muhash = out;
208 : :
209 : 204 : m_current_block_hash = block.hash;
210 : :
211 : : // Intentionally do not update DB_MUHASH here so it stays in sync with
212 : : // DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
213 : 204 : m_db->Write(index_util::DBHeightKey(block.height), value);
214 : 204 : return true;
215 : : }
216 : :
217 : 0 : bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
218 : : {
219 : 0 : CDBBatch batch(*m_db);
220 [ # # # # ]: 0 : std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
221 : :
222 : : // During a reorg, copy the block's hash digest from the height index to the hash index,
223 : : // ensuring it's still accessible after the height index entry is overwritten.
224 [ # # # # ]: 0 : if (!index_util::CopyHeightIndexToHashIndex<DBVal>(*db_it, batch, m_name, block.height)) {
225 : : return false;
226 : : }
227 : :
228 [ # # ]: 0 : m_db->WriteBatch(batch);
229 : :
230 [ # # # # ]: 0 : if (!RevertBlock(block)) {
231 : 0 : return false; // failure cause logged internally
232 : : }
233 : :
234 : : return true;
235 : 0 : }
236 : :
237 : 4 : std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex& block_index) const
238 : : {
239 : 4 : CCoinsStats stats{block_index.nHeight, block_index.GetBlockHash()};
240 : 4 : stats.index_used = true;
241 : :
242 : 4 : DBVal entry;
243 [ + + ]: 4 : if (!index_util::LookUpOne(*m_db, {block_index.GetBlockHash(), block_index.nHeight}, entry)) {
244 : 1 : return std::nullopt;
245 : : }
246 : :
247 : 3 : stats.hashSerialized = entry.muhash;
248 : 3 : stats.nTransactionOutputs = entry.transaction_output_count;
249 : 3 : stats.nBogoSize = entry.bogo_size;
250 : 3 : stats.total_amount = entry.total_amount;
251 : 3 : stats.total_subsidy = entry.total_subsidy;
252 : 3 : stats.total_prevout_spent_amount = entry.total_prevout_spent_amount;
253 : 3 : stats.total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
254 : 3 : stats.total_coinbase_amount = entry.total_coinbase_amount;
255 : 3 : stats.total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
256 : 3 : stats.total_unspendables_bip30 = entry.total_unspendables_bip30;
257 : 3 : stats.total_unspendables_scripts = entry.total_unspendables_scripts;
258 : 3 : stats.total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
259 : :
260 : 3 : return stats;
261 : : }
262 : :
263 : 3 : bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockRef>& block)
264 : : {
265 [ + + ]: 3 : if (!m_db->Read(DB_MUHASH, m_muhash)) {
266 : : // Check that the cause of the read failure is that the key does not
267 : : // exist. Any other errors indicate database corruption or a disk
268 : : // failure, and starting the index would cause further corruption.
269 [ - + ]: 2 : if (m_db->Exists(DB_MUHASH)) {
270 : 0 : LogError("Cannot read current %s state; index may be corrupted",
271 : : GetName());
272 : 0 : return false;
273 : : }
274 : : }
275 : :
276 [ + + ]: 3 : if (block) {
277 : 1 : DBVal entry;
278 [ - + ]: 1 : if (!index_util::LookUpOne(*m_db, *block, entry)) {
279 : 0 : LogError("Cannot read current %s state; index may be corrupted",
280 : : GetName());
281 : 0 : return false;
282 : : }
283 : :
284 : 1 : uint256 out;
285 : 1 : m_muhash.Finalize(out);
286 [ - + ]: 1 : if (entry.muhash != out) {
287 : 0 : LogError("Cannot read current %s state; index may be corrupted",
288 : : GetName());
289 : 0 : return false;
290 : : }
291 : :
292 : 1 : m_transaction_output_count = entry.transaction_output_count;
293 : 1 : m_bogo_size = entry.bogo_size;
294 : 1 : m_total_amount = entry.total_amount;
295 : 1 : m_total_subsidy = entry.total_subsidy;
296 : 1 : m_total_prevout_spent_amount = entry.total_prevout_spent_amount;
297 : 1 : m_total_new_outputs_ex_coinbase_amount = entry.total_new_outputs_ex_coinbase_amount;
298 : 1 : m_total_coinbase_amount = entry.total_coinbase_amount;
299 : 1 : m_total_unspendables_genesis_block = entry.total_unspendables_genesis_block;
300 : 1 : m_total_unspendables_bip30 = entry.total_unspendables_bip30;
301 : 1 : m_total_unspendables_scripts = entry.total_unspendables_scripts;
302 : 1 : m_total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards;
303 : 1 : m_current_block_hash = block->hash;
304 : : }
305 : :
306 : : return true;
307 : : }
308 : :
309 : 2 : bool CoinStatsIndex::CustomCommit(CDBBatch& batch)
310 : : {
311 : : // DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
312 : : // to prevent an inconsistent state of the DB.
313 : 2 : batch.Write(DB_MUHASH, m_muhash);
314 : 2 : return true;
315 : : }
316 : :
317 : 204 : interfaces::Chain::NotifyOptions CoinStatsIndex::CustomOptions()
318 : : {
319 : 204 : interfaces::Chain::NotifyOptions options;
320 : 204 : options.connect_undo_data = true;
321 : 204 : options.disconnect_data = true;
322 : 204 : options.disconnect_undo_data = true;
323 : 204 : return options;
324 : : }
325 : :
326 : : // Revert a single block as part of a reorg
327 : 0 : bool CoinStatsIndex::RevertBlock(const interfaces::BlockInfo& block)
328 : : {
329 : 0 : std::pair<uint256, DBVal> read_out;
330 : :
331 : : // Ignore genesis block
332 [ # # ]: 0 : if (block.height > 0) {
333 [ # # ]: 0 : if (!m_db->Read(index_util::DBHeightKey(block.height - 1), read_out)) {
334 : : return false;
335 : : }
336 : :
337 : 0 : uint256 expected_block_hash{*block.prev_hash};
338 [ # # ]: 0 : if (read_out.first != expected_block_hash) {
339 [ # # # # ]: 0 : LogWarning("previous block header belongs to unexpected block %s; expected %s",
340 : : read_out.first.ToString(), expected_block_hash.ToString());
341 : :
342 [ # # ]: 0 : if (!m_db->Read(index_util::DBHashKey(expected_block_hash), read_out)) {
343 [ # # ]: 0 : LogError("previous block header not found; expected %s",
344 : : expected_block_hash.ToString());
345 : 0 : return false;
346 : : }
347 : : }
348 : : }
349 : :
350 : : // Roll back muhash by removing the new UTXOs that were created by the
351 : : // block and reapplying the old UTXOs that were spent by the block
352 [ # # ]: 0 : assert(block.data);
353 [ # # ]: 0 : assert(block.undo_data);
354 [ # # # # ]: 0 : for (size_t i = 0; i < block.data->vtx.size(); ++i) {
355 : 0 : const auto& tx{block.data->vtx.at(i)};
356 [ # # ]: 0 : const bool is_coinbase{tx->IsCoinBase()};
357 : :
358 [ # # # # ]: 0 : if (is_coinbase && IsBIP30Unspendable(block.hash, block.height)) {
359 : 0 : continue;
360 : : }
361 : :
362 [ # # # # ]: 0 : for (uint32_t j = 0; j < tx->vout.size(); ++j) {
363 : 0 : const CTxOut& out{tx->vout[j]};
364 : 0 : const COutPoint outpoint{tx->GetHash(), j};
365 : 0 : const Coin coin{out, block.height, is_coinbase};
366 : :
367 [ # # ]: 0 : if (!coin.out.scriptPubKey.IsUnspendable()) {
368 [ # # ]: 0 : RemoveCoinHash(m_muhash, outpoint, coin);
369 : : }
370 : 0 : }
371 : :
372 : : // The coinbase tx has no undo data since no former output is spent
373 [ # # ]: 0 : if (!is_coinbase) {
374 : 0 : const auto& tx_undo{block.undo_data->vtxundo.at(i - 1)};
375 : :
376 [ # # # # ]: 0 : for (size_t j = 0; j < tx_undo.vprevout.size(); ++j) {
377 : 0 : const Coin& coin{tx_undo.vprevout[j]};
378 : 0 : const COutPoint outpoint{tx->vin[j].prevout.hash, tx->vin[j].prevout.n};
379 : 0 : ApplyCoinHash(m_muhash, outpoint, coin);
380 : : }
381 : : }
382 : : }
383 : :
384 : : // Check that the rolled back muhash is consistent with the DB read out
385 : 0 : uint256 out;
386 : 0 : m_muhash.Finalize(out);
387 [ # # ]: 0 : Assert(read_out.second.muhash == out);
388 : :
389 : : // Apply the other values from the DB to the member variables
390 : 0 : m_transaction_output_count = read_out.second.transaction_output_count;
391 : 0 : m_total_amount = read_out.second.total_amount;
392 : 0 : m_bogo_size = read_out.second.bogo_size;
393 : 0 : m_total_subsidy = read_out.second.total_subsidy;
394 : 0 : m_total_prevout_spent_amount = read_out.second.total_prevout_spent_amount;
395 : 0 : m_total_new_outputs_ex_coinbase_amount = read_out.second.total_new_outputs_ex_coinbase_amount;
396 : 0 : m_total_coinbase_amount = read_out.second.total_coinbase_amount;
397 : 0 : m_total_unspendables_genesis_block = read_out.second.total_unspendables_genesis_block;
398 : 0 : m_total_unspendables_bip30 = read_out.second.total_unspendables_bip30;
399 : 0 : m_total_unspendables_scripts = read_out.second.total_unspendables_scripts;
400 : 0 : m_total_unspendables_unclaimed_rewards = read_out.second.total_unspendables_unclaimed_rewards;
401 : 0 : m_current_block_hash = *block.prev_hash;
402 : :
403 : 0 : return true;
404 : : }
|