Branch data Line data Source code
1 : : // Copyright (c) 2023 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 <coins.h>
6 : : #include <crypto/sha256.h>
7 : : #include <primitives/transaction.h>
8 : : #include <test/fuzz/fuzz.h>
9 : : #include <test/fuzz/FuzzedDataProvider.h>
10 : : #include <test/fuzz/util.h>
11 : :
12 : : #include <assert.h>
13 : : #include <optional>
14 : : #include <memory>
15 : : #include <stdint.h>
16 : : #include <vector>
17 : :
18 : : namespace {
19 : :
20 : : /** Number of distinct COutPoint values used in this test. */
21 : : constexpr uint32_t NUM_OUTPOINTS = 256;
22 : : /** Number of distinct Coin values used in this test (ignoring nHeight). */
23 : : constexpr uint32_t NUM_COINS = 256;
24 : : /** Maximum number CCoinsViewCache objects used in this test. */
25 : : constexpr uint32_t MAX_CACHES = 4;
26 : : /** Data type large enough to hold NUM_COINS-1. */
27 : : using coinidx_type = uint8_t;
28 : :
29 : 1 : struct PrecomputedData
30 : : {
31 : : //! Randomly generated COutPoint values.
32 : : COutPoint outpoints[NUM_OUTPOINTS];
33 : :
34 : : //! Randomly generated Coin values.
35 : : Coin coins[NUM_COINS];
36 : :
37 [ + - + + ]: 256 : PrecomputedData()
38 : : {
39 : : static const uint8_t PREFIX_O[1] = {'o'}; /** Hash prefix for outpoint hashes. */
40 : : static const uint8_t PREFIX_S[1] = {'s'}; /** Hash prefix for coins scriptPubKeys. */
41 : : static const uint8_t PREFIX_M[1] = {'m'}; /** Hash prefix for coins nValue/fCoinBase. */
42 : :
43 [ + + ]: 257 : for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
44 : 256 : uint32_t idx = (i * 1200U) >> 12; /* Map 3 or 4 entries to same txid. */
45 : 256 : const uint8_t ser[4] = {uint8_t(idx), uint8_t(idx >> 8), uint8_t(idx >> 16), uint8_t(idx >> 24)};
46 [ + - ]: 256 : uint256 txid;
47 [ + - + - : 256 : CSHA256().Write(PREFIX_O, 1).Write(ser, sizeof(ser)).Finalize(txid.begin());
+ - + - +
- ]
48 [ - + ]: 256 : outpoints[i].hash = Txid::FromUint256(txid);
49 : 256 : outpoints[i].n = i;
50 : 256 : }
51 : :
52 [ + + ]: 257 : for (uint32_t i = 0; i < NUM_COINS; ++i) {
53 : 256 : const uint8_t ser[4] = {uint8_t(i), uint8_t(i >> 8), uint8_t(i >> 16), uint8_t(i >> 24)};
54 [ + - ]: 256 : uint256 hash;
55 [ + - + - : 256 : CSHA256().Write(PREFIX_S, 1).Write(ser, sizeof(ser)).Finalize(hash.begin());
+ - + - +
- ]
56 : : /* Convert hash to scriptPubkeys (of different lengths, so SanityCheck's cached memory
57 : : * usage check has a chance to detect mismatches). */
58 [ + + + + : 256 : switch (i % 5U) {
+ - ]
59 : : case 0: /* P2PKH */
60 [ + - ]: 52 : coins[i].out.scriptPubKey.resize(25);
61 [ + - ]: 52 : coins[i].out.scriptPubKey[0] = OP_DUP;
62 [ + - ]: 52 : coins[i].out.scriptPubKey[1] = OP_HASH160;
63 [ + - ]: 52 : coins[i].out.scriptPubKey[2] = 20;
64 [ + - + - : 52 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 3);
+ - + - +
- ]
65 [ + - ]: 52 : coins[i].out.scriptPubKey[23] = OP_EQUALVERIFY;
66 [ + - ]: 52 : coins[i].out.scriptPubKey[24] = OP_CHECKSIG;
67 : 52 : break;
68 : : case 1: /* P2SH */
69 [ + - ]: 51 : coins[i].out.scriptPubKey.resize(23);
70 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_HASH160;
71 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 20;
72 [ + - + - : 51 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
+ - + - +
- ]
73 [ + - ]: 51 : coins[i].out.scriptPubKey[12] = OP_EQUAL;
74 : 51 : break;
75 : : case 2: /* P2WPKH */
76 [ + - ]: 51 : coins[i].out.scriptPubKey.resize(22);
77 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_0;
78 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 20;
79 [ + - + - : 51 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
+ - + - +
- ]
80 : 51 : break;
81 : : case 3: /* P2WSH */
82 [ + - ]: 51 : coins[i].out.scriptPubKey.resize(34);
83 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_0;
84 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 32;
85 [ + - + - : 51 : std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
+ - + - +
- ]
86 : 51 : break;
87 : : case 4: /* P2TR */
88 [ + - ]: 51 : coins[i].out.scriptPubKey.resize(34);
89 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_1;
90 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 32;
91 [ + - + - : 51 : std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
+ - + - +
- ]
92 : 51 : break;
93 : : }
94 : : /* Hash again to construct nValue and fCoinBase. */
95 [ + - + - : 256 : CSHA256().Write(PREFIX_M, 1).Write(ser, sizeof(ser)).Finalize(hash.begin());
+ - + - +
- ]
96 [ + - ]: 256 : coins[i].out.nValue = CAmount(hash.GetUint64(0) % MAX_MONEY);
97 [ + - ]: 256 : coins[i].fCoinBase = (hash.GetUint64(1) & 7) == 0;
98 : 256 : coins[i].nHeight = 0; /* Real nHeight used in simulation is set dynamically. */
99 : 256 : }
100 : 1 : }
101 : : };
102 : :
103 : : enum class EntryType : uint8_t
104 : : {
105 : : /* This entry in the cache does not exist (so we'd have to look in the parent cache). */
106 : : NONE,
107 : :
108 : : /* This entry in the cache corresponds to an unspent coin. */
109 : : UNSPENT,
110 : :
111 : : /* This entry in the cache corresponds to a spent coin. */
112 : : SPENT,
113 : : };
114 : :
115 : : struct CacheEntry
116 : : {
117 : : /* Type of entry. */
118 : : EntryType entrytype;
119 : :
120 : : /* Index in the coins array this entry corresponds to (only if entrytype == UNSPENT). */
121 : : coinidx_type coinidx;
122 : :
123 : : /* nHeight value for this entry (so the coins[coinidx].nHeight value is ignored; only if entrytype == UNSPENT). */
124 : : uint32_t height;
125 : : };
126 : :
127 : : struct CacheLevel
128 : : {
129 : : CacheEntry entry[NUM_OUTPOINTS];
130 : :
131 : 55092 : void Wipe() {
132 [ + + ]: 14158644 : for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
133 : 14103552 : entry[i].entrytype = EntryType::NONE;
134 : 14103552 : }
135 : 55092 : }
136 : : };
137 : :
138 : : /** Class for the base of the hierarchy (roughly simulating a memory-backed CCoinsViewDB).
139 : : *
140 : : * The initial state consists of the empty UTXO set, though coins whose output index
141 : : * is 3 (mod 5) always have GetCoin() succeed (but returning an IsSpent() coin unless a UTXO
142 : : * exists). Coins whose output index is 4 (mod 5) have GetCoin() always succeed after being spent.
143 : : * This exercises code paths with spent, non-DIRTY cache entries.
144 : : */
145 : : class CoinsViewBottom final : public CCoinsView
146 : : {
147 : : std::map<COutPoint, Coin> m_data;
148 : :
149 : : public:
150 : 530144 : bool GetCoin(const COutPoint& outpoint, Coin& coin) const final
151 : : {
152 : 530144 : auto it = m_data.find(outpoint);
153 [ + + ]: 530144 : if (it == m_data.end()) {
154 [ + + ]: 430026 : if ((outpoint.n % 5) == 3) {
155 : 71427 : coin.Clear();
156 : 71427 : return true;
157 : : }
158 : 358599 : return false;
159 : : } else {
160 : 100118 : coin = it->second;
161 : 100118 : return true;
162 : : }
163 : 530144 : }
164 : :
165 : 0 : bool HaveCoin(const COutPoint& outpoint) const final
166 : : {
167 : 0 : return m_data.count(outpoint);
168 : : }
169 : :
170 : 0 : uint256 GetBestBlock() const final { return {}; }
171 : 0 : std::vector<uint256> GetHeadBlocks() const final { return {}; }
172 : 0 : std::unique_ptr<CCoinsViewCursor> Cursor() const final { return {}; }
173 : 0 : size_t EstimateSize() const final { return m_data.size(); }
174 : :
175 : 85981 : bool BatchWrite(CoinsViewCacheCursor& cursor, const uint256&) final
176 : : {
177 [ + + ]: 247128 : for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
178 [ + + ]: 161147 : if (it->second.IsDirty()) {
179 [ + + + + ]: 150993 : if (it->second.coin.IsSpent() && (it->first.n % 5) != 4) {
180 : 33289 : m_data.erase(it->first);
181 [ + + ]: 150993 : } else if (cursor.WillErase(*it)) {
182 : 51048 : m_data[it->first] = std::move(it->second.coin);
183 : 51048 : } else {
184 : 66656 : m_data[it->first] = it->second.coin;
185 : : }
186 : 150993 : } else {
187 : : /* For non-dirty entries being written, compare them with what we have. */
188 : 10154 : auto it2 = m_data.find(it->first);
189 [ + - ]: 10154 : if (it->second.coin.IsSpent()) {
190 [ + + + - ]: 10154 : assert(it2 == m_data.end() || it2->second.IsSpent());
191 : 10154 : } else {
192 [ # # ]: 0 : assert(it2 != m_data.end());
193 [ # # ]: 0 : assert(it->second.coin.out == it2->second.out);
194 [ # # ]: 0 : assert(it->second.coin.fCoinBase == it2->second.fCoinBase);
195 [ # # ]: 0 : assert(it->second.coin.nHeight == it2->second.nHeight);
196 : : }
197 : 10154 : }
198 : 161147 : }
199 : 85981 : return true;
200 : : }
201 : : };
202 : :
203 : : } // namespace
204 : :
205 [ + - + - ]: 592 : FUZZ_TARGET(coinscache_sim)
206 : : {
207 : : /** Precomputed COutPoint and CCoins values. */
208 [ + + - + : 589 : static const PrecomputedData data;
+ - ]
209 : :
210 : : /** Dummy coinsview instance (base of the hierarchy). */
211 : 589 : CoinsViewBottom bottom;
212 : : /** Real CCoinsViewCache objects. */
213 : 589 : std::vector<std::unique_ptr<CCoinsViewCache>> caches;
214 : : /** Simulated cache data (sim_caches[0] matches bottom, sim_caches[i+1] matches caches[i]). */
215 : 589 : CacheLevel sim_caches[MAX_CACHES + 1];
216 : : /** Current height in the simulation. */
217 : 589 : uint32_t current_height = 1U;
218 : :
219 : : // Initialize bottom simulated cache.
220 [ + - ]: 589 : sim_caches[0].Wipe();
221 : :
222 : : /** Helper lookup function in the simulated cache stack. */
223 : 721773 : auto lookup = [&](uint32_t outpointidx, int sim_idx = -1) -> std::optional<std::pair<coinidx_type, uint32_t>> {
224 [ + + ]: 721184 : uint32_t cache_idx = sim_idx == -1 ? caches.size() : sim_idx;
225 : 1683996 : while (true) {
226 : 1683996 : const auto& entry = sim_caches[cache_idx].entry[outpointidx];
227 [ + + ]: 1683996 : if (entry.entrytype == EntryType::UNSPENT) {
228 : 222793 : return {{entry.coinidx, entry.height}};
229 [ + + ]: 1461203 : } else if (entry.entrytype == EntryType::SPENT) {
230 : 127530 : return std::nullopt;
231 : : };
232 [ + + ]: 1333673 : if (cache_idx == 0) break;
233 : 962812 : --cache_idx;
234 [ + + + ]: 1683996 : }
235 : 370861 : return std::nullopt;
236 : 721184 : };
237 : :
238 : : /** Flush changes in top cache to the one below. */
239 : 171072 : auto flush = [&]() {
240 [ + - ]: 170483 : assert(caches.size() >= 1);
241 : 170483 : auto& cache = sim_caches[caches.size()];
242 : 170483 : auto& prev_cache = sim_caches[caches.size() - 1];
243 [ + + ]: 43814131 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
244 [ + + ]: 43643648 : if (cache.entry[outpointidx].entrytype != EntryType::NONE) {
245 : 446294 : prev_cache.entry[outpointidx] = cache.entry[outpointidx];
246 : 446294 : cache.entry[outpointidx].entrytype = EntryType::NONE;
247 : 446294 : }
248 : 43643648 : }
249 : 170483 : };
250 : :
251 : : // Main simulation loop: read commands from the fuzzer input, and apply them
252 : : // to both the real cache stack and the simulation.
253 [ + - ]: 589 : FuzzedDataProvider provider(buffer.data(), buffer.size());
254 [ + - + + : 868424 : LIMITED_WHILE(provider.remaining_bytes(), 10000) {
+ + ]
255 : : // Every operation (except "Change height") moves current height forward,
256 : : // so it functions as a kind of epoch, making ~all UTXOs unique.
257 : 867835 : ++current_height;
258 : : // Make sure there is always at least one CCoinsViewCache.
259 [ + + ]: 867835 : if (caches.empty()) {
260 [ + - + - : 31326 : caches.emplace_back(new CCoinsViewCache(&bottom, /*deterministic=*/true));
+ - ]
261 [ + - ]: 31326 : sim_caches[caches.size()].Wipe();
262 : 31326 : }
263 : :
264 : : // Execute command.
265 [ + - ]: 867835 : CallOneOf(
266 : : provider,
267 : :
268 : 950808 : [&]() { // GetCoin
269 : 82973 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
270 : : // Look up in simulation data.
271 : 82973 : auto sim = lookup(outpointidx);
272 : : // Look up in real caches.
273 : 82973 : Coin realcoin;
274 [ + - ]: 82973 : auto real = caches.back()->GetCoin(data.outpoints[outpointidx], realcoin);
275 : : // Compare results.
276 [ + + ]: 82973 : if (!sim.has_value()) {
277 [ - + # # : 49880 : assert(!real || realcoin.IsSpent());
+ - ]
278 : 49880 : } else {
279 [ + - + - : 33093 : assert(real && !realcoin.IsSpent());
+ - ]
280 : 33093 : const auto& simcoin = data.coins[sim->first];
281 [ + - + - ]: 33093 : assert(realcoin.out == simcoin.out);
282 [ + - ]: 33093 : assert(realcoin.fCoinBase == simcoin.fCoinBase);
283 [ + - ]: 33093 : assert(realcoin.nHeight == sim->second);
284 : 33093 : }
285 : 82973 : },
286 : :
287 : 899516 : [&]() { // HaveCoin
288 : 31681 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
289 : : // Look up in simulation data.
290 : 31681 : auto sim = lookup(outpointidx);
291 : : // Look up in real caches.
292 : 31681 : auto real = caches.back()->HaveCoin(data.outpoints[outpointidx]);
293 : : // Compare results.
294 [ + - ]: 31681 : assert(sim.has_value() == real);
295 : 31681 : },
296 : :
297 : 899174 : [&]() { // HaveCoinInCache
298 : 31339 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
299 : : // Invoke on real cache (there is no equivalent in simulation, so nothing to compare result with).
300 : 31339 : (void)caches.back()->HaveCoinInCache(data.outpoints[outpointidx]);
301 : 31339 : },
302 : :
303 : 895683 : [&]() { // AccessCoin
304 : 27848 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
305 : : // Look up in simulation data.
306 : 27848 : auto sim = lookup(outpointidx);
307 : : // Look up in real caches.
308 : 27848 : const auto& realcoin = caches.back()->AccessCoin(data.outpoints[outpointidx]);
309 : : // Compare results.
310 [ + + ]: 27848 : if (!sim.has_value()) {
311 [ + - ]: 13149 : assert(realcoin.IsSpent());
312 : 13149 : } else {
313 [ + - ]: 14699 : assert(!realcoin.IsSpent());
314 : 14699 : const auto& simcoin = data.coins[sim->first];
315 [ + - ]: 14699 : assert(simcoin.out == realcoin.out);
316 [ + - ]: 14699 : assert(simcoin.fCoinBase == realcoin.fCoinBase);
317 [ + - ]: 14699 : assert(realcoin.nHeight == sim->second);
318 : 14699 : }
319 : 27848 : },
320 : :
321 : 973324 : [&]() { // AddCoin (only possible_overwrite if necessary)
322 : 105489 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
323 : 105489 : uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
324 : : // Look up in simulation data (to know whether we must set possible_overwrite or not).
325 : 105489 : auto sim = lookup(outpointidx);
326 : : // Invoke on real caches.
327 : 105489 : Coin coin = data.coins[coinidx];
328 : 105489 : coin.nHeight = current_height;
329 [ + - ]: 105489 : caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), sim.has_value());
330 : : // Apply to simulation data.
331 : 105489 : auto& entry = sim_caches[caches.size()].entry[outpointidx];
332 : 105489 : entry.entrytype = EntryType::UNSPENT;
333 : 105489 : entry.coinidx = coinidx;
334 : 105489 : entry.height = current_height;
335 : 105489 : },
336 : :
337 : 947977 : [&]() { // AddCoin (always possible_overwrite)
338 : 80142 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
339 : 80142 : uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
340 : : // Invoke on real caches.
341 : 80142 : Coin coin = data.coins[coinidx];
342 : 80142 : coin.nHeight = current_height;
343 [ + - ]: 80142 : caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), true);
344 : : // Apply to simulation data.
345 : 80142 : auto& entry = sim_caches[caches.size()].entry[outpointidx];
346 : 80142 : entry.entrytype = EntryType::UNSPENT;
347 : 80142 : entry.coinidx = coinidx;
348 : 80142 : entry.height = current_height;
349 : 80142 : },
350 : :
351 : 917363 : [&]() { // SpendCoin (moveto = nullptr)
352 : 49528 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
353 : : // Invoke on real caches.
354 : 49528 : caches.back()->SpendCoin(data.outpoints[outpointidx], nullptr);
355 : : // Apply to simulation data.
356 : 49528 : sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
357 : 49528 : },
358 : :
359 : 938084 : [&]() { // SpendCoin (with moveto)
360 : 70249 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
361 : : // Look up in simulation data (to compare the returned *moveto with).
362 : 70249 : auto sim = lookup(outpointidx);
363 : : // Invoke on real caches.
364 : 70249 : Coin realcoin;
365 [ + - ]: 70249 : caches.back()->SpendCoin(data.outpoints[outpointidx], &realcoin);
366 : : // Apply to simulation data.
367 : 70249 : sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
368 : : // Compare *moveto with the value expected based on simulation data.
369 [ + + ]: 70249 : if (!sim.has_value()) {
370 [ + - + - ]: 35565 : assert(realcoin.IsSpent());
371 : 35565 : } else {
372 [ + - + - ]: 34684 : assert(!realcoin.IsSpent());
373 : 34684 : const auto& simcoin = data.coins[sim->first];
374 [ + - + - ]: 34684 : assert(simcoin.out == realcoin.out);
375 [ + - ]: 34684 : assert(simcoin.fCoinBase == realcoin.fCoinBase);
376 [ + - ]: 34684 : assert(realcoin.nHeight == sim->second);
377 : 34684 : }
378 : 70249 : },
379 : :
380 : 900182 : [&]() { // Uncache
381 : 32347 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
382 : : // Apply to real caches (there is no equivalent in our simulation).
383 : 32347 : caches.back()->Uncache(data.outpoints[outpointidx]);
384 : 32347 : },
385 : :
386 : 903580 : [&]() { // Add a cache level (if not already at the max).
387 [ + + ]: 35745 : if (caches.size() != MAX_CACHES) {
388 : : // Apply to real caches.
389 [ + - ]: 23177 : caches.emplace_back(new CCoinsViewCache(&*caches.back(), /*deterministic=*/true));
390 : : // Apply to simulation data.
391 : 23177 : sim_caches[caches.size()].Wipe();
392 : 23177 : }
393 : 35745 : },
394 : :
395 : 921353 : [&]() { // Remove a cache level.
396 : : // Apply to real caches (this reduces caches.size(), implicitly doing the same on the simulation data).
397 : 53518 : caches.back()->SanityCheck();
398 : 53518 : caches.pop_back();
399 : 53518 : },
400 : :
401 : 925288 : [&]() { // Flush.
402 : : // Apply to simulation data.
403 : 57453 : flush();
404 : : // Apply to real caches.
405 : 57453 : caches.back()->Flush();
406 : 57453 : },
407 : :
408 : 937683 : [&]() { // Sync.
409 : : // Apply to simulation data (note that in our simulation, syncing and flushing is the same thing).
410 : 69848 : flush();
411 : : // Apply to real caches.
412 : 69848 : caches.back()->Sync();
413 : 69848 : },
414 : :
415 : 911017 : [&]() { // Flush + ReallocateCache.
416 : : // Apply to simulation data.
417 : 43182 : flush();
418 : : // Apply to real caches.
419 : 43182 : caches.back()->Flush();
420 : 43182 : caches.back()->ReallocateCache();
421 : 43182 : },
422 : :
423 : 903406 : [&]() { // GetCacheSize
424 : 35571 : (void)caches.back()->GetCacheSize();
425 : 35571 : },
426 : :
427 : 896239 : [&]() { // DynamicMemoryUsage
428 : 28404 : (void)caches.back()->DynamicMemoryUsage();
429 : 28404 : },
430 : :
431 : 900353 : [&]() { // Change height
432 : 32518 : current_height = provider.ConsumeIntegralInRange<uint32_t>(1, current_height - 1);
433 : 32518 : }
434 : : );
435 : 867835 : }
436 : :
437 : : // Sanity check all the remaining caches
438 [ + + ]: 1574 : for (const auto& cache : caches) {
439 [ + - ]: 985 : cache->SanityCheck();
440 : 985 : }
441 : :
442 : : // Full comparison between caches and simulation data, from bottom to top,
443 : : // as AccessCoin on a higher cache may affect caches below it.
444 [ + + ]: 1574 : for (unsigned sim_idx = 1; sim_idx <= caches.size(); ++sim_idx) {
445 : 985 : auto& cache = *caches[sim_idx - 1];
446 : 985 : size_t cache_size = 0;
447 : :
448 [ + + ]: 253145 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
449 [ + - ]: 252160 : cache_size += cache.HaveCoinInCache(data.outpoints[outpointidx]);
450 [ - + ]: 252160 : const auto& real = cache.AccessCoin(data.outpoints[outpointidx]);
451 [ + - ]: 252160 : auto sim = lookup(outpointidx, sim_idx);
452 [ + + ]: 252160 : if (!sim.has_value()) {
453 [ + - + - ]: 200276 : assert(real.IsSpent());
454 : 200276 : } else {
455 [ + - + - ]: 51884 : assert(!real.IsSpent());
456 [ + - - + ]: 51884 : assert(real.out == data.coins[sim->first].out);
457 [ - + ]: 51884 : assert(real.fCoinBase == data.coins[sim->first].fCoinBase);
458 [ + - ]: 51884 : assert(real.nHeight == sim->second);
459 : : }
460 : 252160 : }
461 : :
462 : : // HaveCoinInCache ignores spent coins, so GetCacheSize() may exceed it. */
463 [ + - + - ]: 985 : assert(cache.GetCacheSize() >= cache_size);
464 : 985 : }
465 : :
466 : : // Compare the bottom coinsview (not a CCoinsViewCache) with sim_cache[0].
467 [ + + ]: 151373 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
468 [ - + ]: 150784 : Coin realcoin;
469 [ - + ]: 150784 : bool real = bottom.GetCoin(data.outpoints[outpointidx], realcoin);
470 [ + - ]: 150784 : auto sim = lookup(outpointidx, 0);
471 [ + + ]: 150784 : if (!sim.has_value()) {
472 [ + + + - : 128410 : assert(!real || realcoin.IsSpent());
- + ]
473 : 128410 : } else {
474 [ + - + - : 22374 : assert(real && !realcoin.IsSpent());
- + ]
475 [ + - - + ]: 22374 : assert(realcoin.out == data.coins[sim->first].out);
476 [ - + ]: 22374 : assert(realcoin.fCoinBase == data.coins[sim->first].fCoinBase);
477 [ - + ]: 22374 : assert(realcoin.nHeight == sim->second);
478 : : }
479 : 150784 : }
480 : 589 : }
|