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