Branch data Line data Source code
1 : : // Copyright (c) 2023-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 <coins.h>
6 : : #include <crypto/sha256.h>
7 : : #include <primitives/transaction.h>
8 : : #include <test/fuzz/FuzzedDataProvider.h>
9 : : #include <test/fuzz/fuzz.h>
10 : : #include <test/fuzz/util.h>
11 : :
12 : : #include <cassert>
13 : : #include <cstdint>
14 : : #include <memory>
15 : : #include <optional>
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 : : 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 : 1 : PrecomputedData()
38 [ + + + + : 513 : {
- - ]
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 : : }
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 : 52 : 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 [ + - ]: 104 : 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 : 51 : 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 [ + - ]: 102 : 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 : 51 : 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 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
80 : : break;
81 : 51 : 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 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
86 : : break;
87 : 51 : 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 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
92 : : 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 : : }
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 : 95707 : void Wipe() {
132 [ + + + + : 24596699 : for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
+ + + + ]
133 : 24500992 : entry[i].entrytype = EntryType::NONE;
134 : : }
135 : : }
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.
141 : : */
142 : 610 : class CoinsViewBottom final : public CCoinsView
143 : : {
144 : : std::map<COutPoint, Coin> m_data;
145 : :
146 : : public:
147 : 621448 : std::optional<Coin> GetCoin(const COutPoint& outpoint) const final
148 : : {
149 [ + + ]: 621448 : if (auto it{m_data.find(outpoint)}; it != m_data.end()) {
150 [ - + ]: 73402 : assert(!it->second.IsSpent());
151 : 73402 : return it->second;
152 : : }
153 : 548046 : return std::nullopt;
154 : : }
155 : :
156 : 0 : uint256 GetBestBlock() const final { return {}; }
157 : 0 : std::vector<uint256> GetHeadBlocks() const final { return {}; }
158 : 0 : std::unique_ptr<CCoinsViewCursor> Cursor() const final { return {}; }
159 : 0 : size_t EstimateSize() const final { return m_data.size(); }
160 : :
161 : 51006 : void BatchWrite(CoinsViewCacheCursor& cursor, const uint256&) final
162 : : {
163 [ + + ]: 131668 : for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
164 [ + - ]: 80662 : if (it->second.IsDirty()) {
165 [ + + ]: 80662 : if (it->second.coin.IsSpent()) {
166 : 14740 : m_data.erase(it->first);
167 : : } else {
168 [ + + ]: 65922 : if (cursor.WillErase(*it)) {
169 : 18004 : m_data[it->first] = std::move(it->second.coin);
170 : : } else {
171 : 47918 : m_data[it->first] = it->second.coin;
172 : : }
173 : : }
174 : : } else {
175 : : /* For non-dirty entries being written, compare them with what we have. */
176 : 0 : auto it2 = m_data.find(it->first);
177 [ # # ]: 0 : if (it->second.coin.IsSpent()) {
178 [ # # ]: 0 : assert(it2 == m_data.end());
179 : : } else {
180 [ # # ]: 0 : assert(it2 != m_data.end());
181 [ # # ]: 0 : assert(it->second.coin.out == it2->second.out);
182 [ # # ]: 0 : assert(it->second.coin.fCoinBase == it2->second.fCoinBase);
183 [ # # ]: 0 : assert(it->second.coin.nHeight == it2->second.nHeight);
184 : : }
185 : : }
186 : : }
187 : 51006 : }
188 : : };
189 : :
190 : : } // namespace
191 : :
192 [ + - ]: 1066 : FUZZ_TARGET(coinscache_sim)
193 : : {
194 : : /** Precomputed COutPoint and CCoins values. */
195 [ + + + - : 610 : static const PrecomputedData data;
+ - ]
196 : :
197 : : /** Dummy coinsview instance (base of the hierarchy). */
198 : 610 : CoinsViewBottom bottom;
199 : : /** Real CCoinsViewCache objects. */
200 : 610 : std::vector<std::unique_ptr<CCoinsViewCache>> caches;
201 : : /** Simulated cache data (sim_caches[0] matches bottom, sim_caches[i+1] matches caches[i]). */
202 : 610 : CacheLevel sim_caches[MAX_CACHES + 1];
203 : : /** Current height in the simulation. */
204 : 610 : uint32_t current_height = 1U;
205 : :
206 : : // Initialize bottom simulated cache.
207 : 610 : sim_caches[0].Wipe();
208 : :
209 : : /** Helper lookup function in the simulated cache stack. */
210 : 773192 : auto lookup = [&](uint32_t outpointidx, int sim_idx = -1) -> std::optional<std::pair<coinidx_type, uint32_t>> {
211 [ + + - + ]: 772582 : uint32_t cache_idx = sim_idx == -1 ? caches.size() : sim_idx;
212 : 2888698 : while (true) {
213 : 1830640 : const auto& entry = sim_caches[cache_idx].entry[outpointidx];
214 [ + + ]: 1830640 : if (entry.entrytype == EntryType::UNSPENT) {
215 : 211246 : return {{entry.coinidx, entry.height}};
216 [ + + ]: 1619394 : } else if (entry.entrytype == EntryType::SPENT) {
217 : 105353 : return std::nullopt;
218 : 1514041 : };
219 [ + + ]: 1514041 : if (cache_idx == 0) break;
220 : 1058058 : --cache_idx;
221 : 1058058 : }
222 : 455983 : return std::nullopt;
223 : 610 : };
224 : :
225 : : /** Flush changes in top cache to the one below. */
226 : 152297 : auto flush = [&]() {
227 [ - + - + ]: 151687 : assert(caches.size() >= 1);
228 : 151687 : auto& cache = sim_caches[caches.size()];
229 : 151687 : auto& prev_cache = sim_caches[caches.size() - 1];
230 [ + + ]: 38983559 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
231 [ + + ]: 38831872 : if (cache.entry[outpointidx].entrytype != EntryType::NONE) {
232 : 336876 : prev_cache.entry[outpointidx] = cache.entry[outpointidx];
233 : 336876 : cache.entry[outpointidx].entrytype = EntryType::NONE;
234 : : }
235 : : }
236 : 152297 : };
237 : :
238 : : // Main simulation loop: read commands from the fuzzer input, and apply them
239 : : // to both the real cache stack and the simulation.
240 : 610 : FuzzedDataProvider provider(buffer.data(), buffer.size());
241 [ + + + + ]: 1053877 : LIMITED_WHILE(provider.remaining_bytes(), 10000) {
242 : : // Every operation (except "Change height") moves current height forward,
243 : : // so it functions as a kind of epoch, making ~all UTXOs unique.
244 : 1053267 : ++current_height;
245 : : // Make sure there is always at least one CCoinsViewCache.
246 [ + + ]: 1053267 : if (caches.empty()) {
247 [ + - + - : 22125 : caches.emplace_back(new CCoinsViewCache(&bottom, /*deterministic=*/true));
+ - - - ]
248 [ - + ]: 22125 : sim_caches[caches.size()].Wipe();
249 : : }
250 : :
251 : : // Execute command.
252 [ + - ]: 1053267 : CallOneOf(
253 : : provider,
254 : :
255 : 91604 : [&]() { // GetCoin
256 : 91604 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
257 : : // Look up in simulation data.
258 : 91604 : auto sim = lookup(outpointidx);
259 : : // Look up in real caches.
260 [ + + ]: 91604 : auto realcoin = provider.ConsumeBool() ?
261 : 55432 : caches.back()->PeekCoin(data.outpoints[outpointidx]) :
262 : 91604 : caches.back()->GetCoin(data.outpoints[outpointidx]);
263 : : // Compare results.
264 [ + + ]: 91604 : if (!sim.has_value()) {
265 [ - + ]: 61838 : assert(!realcoin);
266 : : } else {
267 [ + - - + ]: 29766 : assert(realcoin && !realcoin->IsSpent());
268 : 29766 : const auto& simcoin = data.coins[sim->first];
269 [ - + ]: 29766 : assert(realcoin->out == simcoin.out);
270 [ - + ]: 29766 : assert(realcoin->fCoinBase == simcoin.fCoinBase);
271 [ - + ]: 29766 : assert(realcoin->nHeight == sim->second);
272 : : }
273 : 91604 : },
274 : :
275 : 36016 : [&]() { // HaveCoin
276 : 36016 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
277 : : // Look up in simulation data.
278 : 36016 : auto sim = lookup(outpointidx);
279 : : // Look up in real caches.
280 : 36016 : auto real = caches.back()->HaveCoin(data.outpoints[outpointidx]);
281 : : // Compare results.
282 [ - + ]: 36016 : assert(sim.has_value() == real);
283 : 36016 : },
284 : :
285 : 43419 : [&]() { // HaveCoinInCache
286 : 43419 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
287 : : // Invoke on real cache (there is no equivalent in simulation, so nothing to compare result with).
288 : 43419 : (void)caches.back()->HaveCoinInCache(data.outpoints[outpointidx]);
289 : 43419 : },
290 : :
291 : 40689 : [&]() { // AccessCoin
292 : 40689 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
293 : : // Look up in simulation data.
294 : 40689 : auto sim = lookup(outpointidx);
295 : : // Look up in real caches.
296 : 40689 : const auto& realcoin = caches.back()->AccessCoin(data.outpoints[outpointidx]);
297 : : // Compare results.
298 [ + + ]: 40689 : if (!sim.has_value()) {
299 [ - + ]: 26191 : assert(realcoin.IsSpent());
300 : : } else {
301 [ - + ]: 14498 : assert(!realcoin.IsSpent());
302 : 14498 : const auto& simcoin = data.coins[sim->first];
303 [ - + ]: 14498 : assert(simcoin.out == realcoin.out);
304 [ - + ]: 14498 : assert(simcoin.fCoinBase == realcoin.fCoinBase);
305 [ - + ]: 14498 : assert(realcoin.nHeight == sim->second);
306 : : }
307 : 40689 : },
308 : :
309 : 101379 : [&]() { // AddCoin (only possible_overwrite if necessary)
310 : 101379 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
311 : 101379 : uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
312 : : // Look up in simulation data (to know whether we must set possible_overwrite or not).
313 : 101379 : auto sim = lookup(outpointidx);
314 : : // Invoke on real caches.
315 : 101379 : Coin coin = data.coins[coinidx];
316 : 101379 : coin.nHeight = current_height;
317 [ + - ]: 101379 : caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), sim.has_value());
318 : : // Apply to simulation data.
319 [ - + ]: 101379 : auto& entry = sim_caches[caches.size()].entry[outpointidx];
320 : 101379 : entry.entrytype = EntryType::UNSPENT;
321 : 101379 : entry.coinidx = coinidx;
322 : 101379 : entry.height = current_height;
323 : 101379 : },
324 : :
325 : 151269 : [&]() { // AddCoin (always possible_overwrite)
326 : 151269 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
327 : 151269 : uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
328 : : // Invoke on real caches.
329 : 151269 : Coin coin = data.coins[coinidx];
330 : 151269 : coin.nHeight = current_height;
331 [ + - ]: 151269 : caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), true);
332 : : // Apply to simulation data.
333 [ - + ]: 151269 : auto& entry = sim_caches[caches.size()].entry[outpointidx];
334 : 151269 : entry.entrytype = EntryType::UNSPENT;
335 : 151269 : entry.coinidx = coinidx;
336 : 151269 : entry.height = current_height;
337 : 151269 : },
338 : :
339 : 77927 : [&]() { // SpendCoin (moveto = nullptr)
340 : 77927 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
341 : : // Invoke on real caches.
342 : 77927 : caches.back()->SpendCoin(data.outpoints[outpointidx], nullptr);
343 : : // Apply to simulation data.
344 [ - + ]: 77927 : sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
345 : 77927 : },
346 : :
347 : 67438 : [&]() { // SpendCoin (with moveto)
348 : 67438 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
349 : : // Look up in simulation data (to compare the returned *moveto with).
350 : 67438 : auto sim = lookup(outpointidx);
351 : : // Invoke on real caches.
352 : 67438 : Coin realcoin;
353 [ + - ]: 67438 : caches.back()->SpendCoin(data.outpoints[outpointidx], &realcoin);
354 : : // Apply to simulation data.
355 [ - + ]: 67438 : sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
356 : : // Compare *moveto with the value expected based on simulation data.
357 [ + + ]: 67438 : if (!sim.has_value()) {
358 [ - + ]: 35557 : assert(realcoin.IsSpent());
359 : : } else {
360 [ - + ]: 31881 : assert(!realcoin.IsSpent());
361 : 31881 : const auto& simcoin = data.coins[sim->first];
362 [ - + ]: 31881 : assert(simcoin.out == realcoin.out);
363 [ - + ]: 31881 : assert(simcoin.fCoinBase == realcoin.fCoinBase);
364 [ - + ]: 31881 : assert(realcoin.nHeight == sim->second);
365 : : }
366 : 67438 : },
367 : :
368 : 32081 : [&]() { // Uncache
369 : 32081 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
370 : : // Apply to real caches (there is no equivalent in our simulation).
371 : 32081 : caches.back()->Uncache(data.outpoints[outpointidx]);
372 : 32081 : },
373 : :
374 : 52796 : [&]() { // Add a cache level (if not already at the max).
375 [ - + + + ]: 52796 : if (caches.size() != MAX_CACHES) {
376 : : // Apply to real caches.
377 [ + + ]: 22553 : if (provider.ConsumeBool()) {
378 [ + - ]: 10607 : caches.emplace_back(new CCoinsViewCache(&*caches.back(), /*deterministic=*/true));
379 : : } else {
380 [ + - ]: 11946 : caches.emplace_back(new CoinsViewOverlay(&*caches.back(), /*deterministic=*/true));
381 : : }
382 : : // Apply to simulation data.
383 [ - + ]: 22553 : sim_caches[caches.size()].Wipe();
384 : : }
385 : 52796 : },
386 : :
387 : 43587 : [&]() { // Remove a cache level.
388 : : // Apply to real caches (this reduces caches.size(), implicitly doing the same on the simulation data).
389 : 43587 : caches.back()->SanityCheck();
390 : 43587 : caches.pop_back();
391 : 43587 : },
392 : :
393 : 53335 : [&]() { // Flush.
394 : : // Apply to simulation data.
395 : 53335 : flush();
396 : : // Apply to real caches.
397 : 53335 : caches.back()->Flush(/*reallocate_cache=*/provider.ConsumeBool());
398 : 53335 : },
399 : :
400 : 98352 : [&]() { // Sync.
401 : : // Apply to simulation data (note that in our simulation, syncing and flushing is the same thing).
402 : 98352 : flush();
403 : : // Apply to real caches.
404 : 98352 : caches.back()->Sync();
405 : 98352 : },
406 : :
407 : 50419 : [&]() { // Reset.
408 [ - + ]: 50419 : sim_caches[caches.size()].Wipe();
409 : : // Apply to real caches.
410 : 50419 : {
411 : 50419 : const auto reset_guard{caches.back()->CreateResetGuard()};
412 : 50419 : }
413 : 50419 : },
414 : :
415 : 38889 : [&]() { // GetCacheSize
416 : 38889 : (void)caches.back()->GetCacheSize();
417 : 38889 : },
418 : :
419 : 27829 : [&]() { // DynamicMemoryUsage
420 : 27829 : (void)caches.back()->DynamicMemoryUsage();
421 : 27829 : },
422 : :
423 : 46238 : [&]() { // Change height
424 : 46238 : current_height = provider.ConsumeIntegralInRange<uint32_t>(1, current_height - 1);
425 : 46238 : }
426 : : );
427 : : }
428 : :
429 : : // Sanity check all the remaining caches
430 [ + + ]: 1701 : for (const auto& cache : caches) {
431 [ + - ]: 1091 : cache->SanityCheck();
432 : : }
433 : :
434 : : // Full comparison between caches and simulation data, from bottom to top,
435 : : // as AccessCoin on a higher cache may affect caches below it.
436 [ - + + + ]: 1701 : for (unsigned sim_idx = 1; sim_idx <= caches.size(); ++sim_idx) {
437 : 1091 : auto& cache = *caches[sim_idx - 1];
438 : 1091 : size_t cache_size = 0;
439 : :
440 [ + + ]: 280387 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
441 [ + - ]: 279296 : cache_size += cache.HaveCoinInCache(data.outpoints[outpointidx]);
442 [ + - ]: 279296 : const auto& real = cache.AccessCoin(data.outpoints[outpointidx]);
443 : 279296 : auto sim = lookup(outpointidx, sim_idx);
444 [ + + ]: 279296 : if (!sim.has_value()) {
445 [ - + ]: 234734 : assert(real.IsSpent());
446 : : } else {
447 [ - + ]: 44562 : assert(!real.IsSpent());
448 [ - + ]: 44562 : assert(real.out == data.coins[sim->first].out);
449 [ - + ]: 44562 : assert(real.fCoinBase == data.coins[sim->first].fCoinBase);
450 [ - + ]: 44562 : assert(real.nHeight == sim->second);
451 : : }
452 : : }
453 : :
454 : : // HaveCoinInCache ignores spent coins, so GetCacheSize() may exceed it. */
455 [ + - - + ]: 1091 : assert(cache.GetCacheSize() >= cache_size);
456 : : }
457 : :
458 : : // Compare the bottom coinsview (not a CCoinsViewCache) with sim_cache[0].
459 [ + + ]: 156770 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
460 : 156160 : auto realcoin = bottom.GetCoin(data.outpoints[outpointidx]);
461 : 156160 : auto sim = lookup(outpointidx, 0);
462 [ + + ]: 156160 : if (!sim.has_value()) {
463 [ - + ]: 138910 : assert(!realcoin);
464 : : } else {
465 [ + - - + ]: 17250 : assert(realcoin && !realcoin->IsSpent());
466 [ - + ]: 17250 : assert(realcoin->out == data.coins[sim->first].out);
467 [ - + ]: 17250 : assert(realcoin->fCoinBase == data.coins[sim->first].fCoinBase);
468 [ - + ]: 17250 : assert(realcoin->nHeight == sim->second);
469 : : }
470 : 156160 : }
471 : 610 : }
|