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 <kernel/chainstatemanager_opts.h>
8 : : #include <primitives/block.h>
9 : : #include <primitives/transaction.h>
10 : : #include <test/fuzz/FuzzedDataProvider.h>
11 : : #include <test/fuzz/fuzz.h>
12 : : #include <test/fuzz/util.h>
13 : : #include <test/util/setup_common.h>
14 : : #include <util/threadpool.h>
15 : :
16 : : #include <cassert>
17 : : #include <cstdint>
18 : : #include <memory>
19 : : #include <optional>
20 : : #include <vector>
21 : :
22 : : namespace {
23 : :
24 : : /** Number of distinct COutPoint values used in this test. */
25 : : constexpr uint32_t NUM_OUTPOINTS = 256;
26 : : /** Number of distinct Coin values used in this test (ignoring nHeight). */
27 : : constexpr uint32_t NUM_COINS = 256;
28 : : /** Maximum number CCoinsViewCache objects used in this test. */
29 : : constexpr uint32_t MAX_CACHES = 4;
30 : : /** Data type large enough to hold NUM_COINS-1. */
31 : : using coinidx_type = uint8_t;
32 : :
33 : : struct PrecomputedData
34 : : {
35 : : //! Randomly generated COutPoint values.
36 : : COutPoint outpoints[NUM_OUTPOINTS];
37 : :
38 : : //! Randomly generated Coin values.
39 : : Coin coins[NUM_COINS];
40 : :
41 : : //! Block with a tx containing as inputs the above outpoints.
42 : : CBlock block;
43 : :
44 : 1 : PrecomputedData()
45 [ + + + + ]: 513 : {
46 : 1 : static const uint8_t PREFIX_O[1] = {'o'}; /** Hash prefix for outpoint hashes. */
47 : 1 : static const uint8_t PREFIX_S[1] = {'s'}; /** Hash prefix for coins scriptPubKeys. */
48 : 1 : static const uint8_t PREFIX_M[1] = {'m'}; /** Hash prefix for coins nValue/fCoinBase. */
49 : :
50 [ + - ]: 1 : CMutableTransaction coinbase;
51 [ + - ]: 1 : coinbase.vin.emplace_back();
52 [ + - + - : 2 : block.vtx.push_back(MakeTransactionRef(coinbase));
- + ]
53 : :
54 [ + - ]: 1 : CMutableTransaction tx;
55 [ + + ]: 257 : for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
56 : 256 : uint32_t idx = (i * 1200U) >> 12; /* Map 3 or 4 entries to same txid. */
57 : 256 : const uint8_t ser[4] = {uint8_t(idx), uint8_t(idx >> 8), uint8_t(idx >> 16), uint8_t(idx >> 24)};
58 : 256 : uint256 txid;
59 [ + - + - : 256 : CSHA256().Write(PREFIX_O, 1).Write(ser, sizeof(ser)).Finalize(txid.begin());
+ - + - ]
60 [ + - ]: 256 : outpoints[i].hash = Txid::FromUint256(txid);
61 : 256 : outpoints[i].n = i;
62 [ + - ]: 256 : tx.vin.emplace_back(outpoints[i]);
63 : : }
64 [ + - + - : 2 : block.vtx.push_back(MakeTransactionRef(tx));
- + ]
65 : :
66 [ + + ]: 257 : for (uint32_t i = 0; i < NUM_COINS; ++i) {
67 : 256 : const uint8_t ser[4] = {uint8_t(i), uint8_t(i >> 8), uint8_t(i >> 16), uint8_t(i >> 24)};
68 : 256 : uint256 hash;
69 [ + - + - : 256 : CSHA256().Write(PREFIX_S, 1).Write(ser, sizeof(ser)).Finalize(hash.begin());
+ - + - ]
70 : : /* Convert hash to scriptPubkeys (of different lengths, so SanityCheck's cached memory
71 : : * usage check has a chance to detect mismatches). */
72 [ + + + + : 256 : switch (i % 5U) {
+ - ]
73 : 52 : case 0: /* P2PKH */
74 : 52 : coins[i].out.scriptPubKey.resize(25);
75 [ + - ]: 52 : coins[i].out.scriptPubKey[0] = OP_DUP;
76 [ + - ]: 52 : coins[i].out.scriptPubKey[1] = OP_HASH160;
77 [ + - ]: 52 : coins[i].out.scriptPubKey[2] = 20;
78 [ + - ]: 104 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 3);
79 [ + - ]: 52 : coins[i].out.scriptPubKey[23] = OP_EQUALVERIFY;
80 [ + - ]: 52 : coins[i].out.scriptPubKey[24] = OP_CHECKSIG;
81 : 52 : break;
82 : 51 : case 1: /* P2SH */
83 : 51 : coins[i].out.scriptPubKey.resize(23);
84 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_HASH160;
85 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 20;
86 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
87 [ + - ]: 51 : coins[i].out.scriptPubKey[22] = OP_EQUAL;
88 : 51 : break;
89 : 51 : case 2: /* P2WPKH */
90 : 51 : coins[i].out.scriptPubKey.resize(22);
91 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_0;
92 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 20;
93 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 20, coins[i].out.scriptPubKey.begin() + 2);
94 : : break;
95 : 51 : case 3: /* P2WSH */
96 : 51 : coins[i].out.scriptPubKey.resize(34);
97 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_0;
98 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 32;
99 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
100 : : break;
101 : 51 : case 4: /* P2TR */
102 : 51 : coins[i].out.scriptPubKey.resize(34);
103 [ + - ]: 51 : coins[i].out.scriptPubKey[0] = OP_1;
104 [ + - ]: 51 : coins[i].out.scriptPubKey[1] = 32;
105 [ + - ]: 102 : std::copy(hash.begin(), hash.begin() + 32, coins[i].out.scriptPubKey.begin() + 2);
106 : : break;
107 : : }
108 : : /* Hash again to construct nValue and fCoinBase. */
109 [ + - + - : 256 : CSHA256().Write(PREFIX_M, 1).Write(ser, sizeof(ser)).Finalize(hash.begin());
+ - + - ]
110 : 256 : coins[i].out.nValue = CAmount(hash.GetUint64(0) % MAX_MONEY);
111 : 256 : coins[i].fCoinBase = (hash.GetUint64(1) & 7) == 0;
112 : 256 : coins[i].nHeight = 0; /* Real nHeight used in simulation is set dynamically. */
113 : : }
114 [ - - ]: 2 : }
115 : : };
116 : :
117 : : enum class EntryType : uint8_t
118 : : {
119 : : /* This entry in the cache does not exist (so we'd have to look in the parent cache). */
120 : : NONE,
121 : :
122 : : /* This entry in the cache corresponds to an unspent coin. */
123 : : UNSPENT,
124 : :
125 : : /* This entry in the cache corresponds to a spent coin. */
126 : : SPENT,
127 : : };
128 : :
129 : : struct CacheEntry
130 : : {
131 : : /* Type of entry. */
132 : : EntryType entrytype;
133 : :
134 : : /* Index in the coins array this entry corresponds to (only if entrytype == UNSPENT). */
135 : : coinidx_type coinidx;
136 : :
137 : : /* nHeight value for this entry (so the coins[coinidx].nHeight value is ignored; only if entrytype == UNSPENT). */
138 : : uint32_t height;
139 : : };
140 : :
141 : : struct CacheLevel
142 : : {
143 : : CacheEntry entry[NUM_OUTPOINTS];
144 : :
145 : 183123 : void Wipe() {
146 [ + + + + : 47062611 : for (uint32_t i = 0; i < NUM_OUTPOINTS; ++i) {
+ + + + ]
147 : 46879488 : entry[i].entrytype = EntryType::NONE;
148 : : }
149 : : }
150 : : };
151 : :
152 : : /** Class for the base of the hierarchy (roughly simulating a memory-backed CCoinsViewDB).
153 : : *
154 : : * The initial state consists of the empty UTXO set.
155 : : */
156 : 432 : class CoinsViewBottom final : public CoinsViewEmpty
157 : : {
158 : : std::map<COutPoint, Coin> m_data;
159 : :
160 : : public:
161 : 20357353 : std::optional<Coin> GetCoin(const COutPoint& outpoint) const final
162 : : {
163 [ + + ]: 20357353 : if (auto it{m_data.find(outpoint)}; it != m_data.end()) {
164 [ - + ]: 732254 : assert(!it->second.IsSpent());
165 : 732254 : return it->second;
166 : : }
167 : 19625099 : return std::nullopt;
168 : : }
169 : :
170 : 42884 : void BatchWrite(CoinsViewCacheCursor& cursor, const uint256&) final
171 : : {
172 [ + + ]: 93613 : for (auto it{cursor.Begin()}; it != cursor.End(); it = cursor.NextAndMaybeErase(*it)) {
173 [ + - ]: 50729 : if (it->second.IsDirty()) {
174 [ + + ]: 50729 : if (it->second.coin.IsSpent()) {
175 : 11648 : m_data.erase(it->first);
176 : : } else {
177 [ + + ]: 39081 : if (cursor.WillErase(*it)) {
178 : 11768 : m_data[it->first] = std::move(it->second.coin);
179 : : } else {
180 : 27313 : m_data[it->first] = it->second.coin;
181 : : }
182 : : }
183 : : } else {
184 : : /* For non-dirty entries being written, compare them with what we have. */
185 : 0 : auto it2 = m_data.find(it->first);
186 [ # # ]: 0 : if (it->second.coin.IsSpent()) {
187 [ # # ]: 0 : assert(it2 == m_data.end());
188 : : } else {
189 [ # # ]: 0 : assert(it2 != m_data.end());
190 [ # # ]: 0 : assert(it->second.coin.out == it2->second.out);
191 [ # # ]: 0 : assert(it->second.coin.fCoinBase == it2->second.fCoinBase);
192 [ # # ]: 0 : assert(it->second.coin.nHeight == it2->second.nHeight);
193 : : }
194 : : }
195 : : }
196 : 42884 : }
197 : : };
198 : :
199 : : // Hold a non-movable ResetGuard on the heap so StartFetching can remain active
200 : : // for the lifetime of a CoinsViewOverlay cache level.
201 : 111202 : struct OverlayFetchScope
202 : : {
203 : : CCoinsViewCache::ResetGuard guard;
204 : 111202 : OverlayFetchScope(CoinsViewOverlay& view, const CBlock& block) : guard(view.StartFetching(block)) {}
205 : : };
206 : :
207 : : // Reuse a single global thread pool across fuzz iterations. Creating and destroying a pool every
208 : : // iteration leaks memory, since iterations can run faster than the OS can tear down the threads.
209 : : std::shared_ptr<ThreadPool> g_thread_pool{std::make_shared<ThreadPool>("cache_fuzz")};
210 : :
211 : 432 : void StartPoolIfNeeded()
212 : : {
213 [ + + ]: 432 : if (!g_thread_pool->WorkersCount()) g_thread_pool->Start(DEFAULT_PREVOUTFETCH_THREADS);
214 : 432 : }
215 : :
216 : : } // namespace
217 : :
218 [ + - ][ + - : 478 : FUZZ_TARGET(coinscache_sim, .init = [] { static auto setup{MakeNoLogFileContext<>()}; })
+ - + - ]
219 : : {
220 : 432 : SeedRandomStateForTest(SeedRand::ZEROS);
221 : 432 : StartPoolIfNeeded();
222 : : /** Precomputed COutPoint and CCoins values. */
223 [ + + + - : 433 : static const PrecomputedData data;
+ - ]
224 : :
225 : : /** Dummy coinsview instance (base of the hierarchy). */
226 : 432 : CoinsViewBottom bottom;
227 : : /** Real CCoinsViewCache objects. */
228 : 432 : std::vector<std::unique_ptr<CCoinsViewCache>> caches;
229 : : /** Long-lived StartFetching guards, parallel to `caches` (entries are nullptr unless corresponding level is a CoinsViewOverlay). */
230 : 432 : std::vector<std::unique_ptr<OverlayFetchScope>> fetch_scopes;
231 : : /** Simulated cache data (sim_caches[0] matches bottom, sim_caches[i+1] matches caches[i]). */
232 : 432 : CacheLevel sim_caches[MAX_CACHES + 1];
233 : : /** Current height in the simulation. */
234 : 432 : uint32_t current_height = 1U;
235 : :
236 : : // Initialize bottom simulated cache.
237 : 432 : sim_caches[0].Wipe();
238 : :
239 : : /** Helper lookup function in the simulated cache stack. */
240 : 625191 : auto lookup = [&](uint32_t outpointidx, int sim_idx = -1) -> std::optional<std::pair<coinidx_type, uint32_t>> {
241 [ + + - + ]: 624759 : uint32_t cache_idx = sim_idx == -1 ? caches.size() : sim_idx;
242 : 2561131 : while (true) {
243 : 1592945 : const auto& entry = sim_caches[cache_idx].entry[outpointidx];
244 [ + + ]: 1592945 : if (entry.entrytype == EntryType::UNSPENT) {
245 : 160748 : return {{entry.coinidx, entry.height}};
246 [ + + ]: 1432197 : } else if (entry.entrytype == EntryType::SPENT) {
247 : 85726 : return std::nullopt;
248 : 1346471 : };
249 [ + + ]: 1346471 : if (cache_idx == 0) break;
250 : 968186 : --cache_idx;
251 : 968186 : }
252 : 378285 : return std::nullopt;
253 : 432 : };
254 : :
255 : : /** Flush changes in top cache to the one below. */
256 : 92485 : auto flush = [&]() {
257 [ - + - + ]: 92053 : assert(caches.size() >= 1);
258 : 92053 : auto& cache = sim_caches[caches.size()];
259 : 92053 : auto& prev_cache = sim_caches[caches.size() - 1];
260 [ + + ]: 23657621 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
261 [ + + ]: 23565568 : if (cache.entry[outpointidx].entrytype != EntryType::NONE) {
262 : 183820 : prev_cache.entry[outpointidx] = cache.entry[outpointidx];
263 : 183820 : cache.entry[outpointidx].entrytype = EntryType::NONE;
264 : : }
265 : : }
266 : 92485 : };
267 : :
268 : : /** Helper creating a fetch scope for the top cache (which must be a CoinsViewOverlay). */
269 : 111634 : const auto make_fetch_scope{[&] {
270 : 111202 : auto& overlay{static_cast<CoinsViewOverlay&>(*caches.back())};
271 : 111202 : return std::make_unique<OverlayFetchScope>(overlay, data.block);
272 : 432 : }};
273 : :
274 : : // Main simulation loop: read commands from the fuzzer input, and apply them
275 : : // to both the real cache stack and the simulation.
276 : 432 : FuzzedDataProvider provider(buffer.data(), buffer.size());
277 [ + + + + ]: 1106174 : LIMITED_WHILE (provider.remaining_bytes(), 10000) {
278 : : // Every operation (except "Change height") moves current height forward,
279 : : // so it functions as a kind of epoch, making ~all UTXOs unique.
280 : 1105742 : ++current_height;
281 : : // Make sure there is always at least one CCoinsViewCache.
282 [ + + ]: 1105742 : if (caches.empty()) {
283 [ + - + - : 20416 : caches.emplace_back(new CCoinsViewCache(&bottom, /*deterministic=*/true));
+ - - - ]
284 [ + - ]: 20416 : fetch_scopes.emplace_back();
285 [ - + ]: 20416 : sim_caches[caches.size()].Wipe();
286 : : }
287 [ - + - + : 1105742 : assert(caches.size() == fetch_scopes.size());
- + ]
288 : :
289 : : // Execute command.
290 [ + - ]: 1105742 : CallOneOf(
291 : : provider,
292 : :
293 : 74861 : [&]() { // PeekCoin/GetCoin
294 : 74861 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
295 : : // Look up in simulation data.
296 : 74861 : auto sim = lookup(outpointidx);
297 : : // Look up in real caches.
298 [ + + ]: 74861 : auto realcoin = provider.ConsumeBool() ?
299 : 34172 : caches.back()->PeekCoin(data.outpoints[outpointidx]) :
300 : 74861 : caches.back()->GetCoin(data.outpoints[outpointidx]);
301 : : // Compare results.
302 [ + + ]: 74861 : if (!sim.has_value()) {
303 [ - + ]: 50832 : assert(!realcoin);
304 : : } else {
305 [ + - - + ]: 24029 : assert(realcoin && !realcoin->IsSpent());
306 : 24029 : const auto& simcoin = data.coins[sim->first];
307 [ - + ]: 24029 : assert(realcoin->out == simcoin.out);
308 [ - + ]: 24029 : assert(realcoin->fCoinBase == simcoin.fCoinBase);
309 [ - + ]: 24029 : assert(realcoin->nHeight == sim->second);
310 : : }
311 : 74861 : },
312 : :
313 : 33035 : [&]() { // HaveCoin
314 : 33035 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
315 : : // Look up in simulation data.
316 : 33035 : auto sim = lookup(outpointidx);
317 : : // Look up in real caches.
318 : 33035 : auto real = caches.back()->HaveCoin(data.outpoints[outpointidx]);
319 : : // Compare results.
320 [ - + ]: 33035 : assert(sim.has_value() == real);
321 : 33035 : },
322 : :
323 : 30538 : [&]() { // HaveCoinInCache
324 : 30538 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
325 : : // Invoke on real cache (there is no equivalent in simulation, so nothing to compare result with).
326 : 30538 : (void)caches.back()->HaveCoinInCache(data.outpoints[outpointidx]);
327 : 30538 : },
328 : :
329 : 31654 : [&]() { // AccessCoin
330 : 31654 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
331 : : // Look up in simulation data.
332 : 31654 : auto sim = lookup(outpointidx);
333 : : // Look up in real caches.
334 : 31654 : const auto& realcoin = caches.back()->AccessCoin(data.outpoints[outpointidx]);
335 : : // Compare results.
336 [ + + ]: 31654 : if (!sim.has_value()) {
337 [ - + ]: 22445 : assert(realcoin.IsSpent());
338 : : } else {
339 [ - + ]: 9209 : assert(!realcoin.IsSpent());
340 : 9209 : const auto& simcoin = data.coins[sim->first];
341 [ - + ]: 9209 : assert(simcoin.out == realcoin.out);
342 [ - + ]: 9209 : assert(simcoin.fCoinBase == realcoin.fCoinBase);
343 [ - + ]: 9209 : assert(realcoin.nHeight == sim->second);
344 : : }
345 : 31654 : },
346 : :
347 : 98398 : [&]() { // AddCoin (only possible_overwrite if necessary)
348 : 98398 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
349 : 98398 : uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
350 : : // Look up in simulation data (to know whether we must set possible_overwrite or not).
351 : 98398 : auto sim = lookup(outpointidx);
352 : : // Invoke on real caches.
353 : 98398 : Coin coin = data.coins[coinidx];
354 : 98398 : coin.nHeight = current_height;
355 [ + - ]: 98398 : caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), sim.has_value());
356 : : // Apply to simulation data.
357 [ - + ]: 98398 : auto& entry = sim_caches[caches.size()].entry[outpointidx];
358 : 98398 : entry.entrytype = EntryType::UNSPENT;
359 : 98398 : entry.coinidx = coinidx;
360 : 98398 : entry.height = current_height;
361 : 98398 : },
362 : :
363 : 116481 : [&]() { // AddCoin (always possible_overwrite)
364 : 116481 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
365 : 116481 : uint32_t coinidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_COINS - 1);
366 : : // Invoke on real caches.
367 : 116481 : Coin coin = data.coins[coinidx];
368 : 116481 : coin.nHeight = current_height;
369 [ + - ]: 116481 : caches.back()->AddCoin(data.outpoints[outpointidx], std::move(coin), true);
370 : : // Apply to simulation data.
371 [ - + ]: 116481 : auto& entry = sim_caches[caches.size()].entry[outpointidx];
372 : 116481 : entry.entrytype = EntryType::UNSPENT;
373 : 116481 : entry.coinidx = coinidx;
374 : 116481 : entry.height = current_height;
375 : 116481 : },
376 : :
377 : 54948 : [&]() { // SpendCoin (moveto = nullptr)
378 : 54948 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
379 : : // Invoke on real caches.
380 : 54948 : caches.back()->SpendCoin(data.outpoints[outpointidx], nullptr);
381 : : // Apply to simulation data.
382 [ - + ]: 54948 : sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
383 : 54948 : },
384 : :
385 : 60667 : [&]() { // SpendCoin (with moveto)
386 : 60667 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
387 : : // Look up in simulation data (to compare the returned *moveto with).
388 : 60667 : auto sim = lookup(outpointidx);
389 : : // Invoke on real caches.
390 : 60667 : Coin realcoin;
391 [ + - ]: 60667 : caches.back()->SpendCoin(data.outpoints[outpointidx], &realcoin);
392 : : // Apply to simulation data.
393 [ - + ]: 60667 : sim_caches[caches.size()].entry[outpointidx].entrytype = EntryType::SPENT;
394 : : // Compare *moveto with the value expected based on simulation data.
395 [ + + ]: 60667 : if (!sim.has_value()) {
396 [ - + ]: 32074 : assert(realcoin.IsSpent());
397 : : } else {
398 [ - + ]: 28593 : assert(!realcoin.IsSpent());
399 : 28593 : const auto& simcoin = data.coins[sim->first];
400 [ - + ]: 28593 : assert(simcoin.out == realcoin.out);
401 [ - + ]: 28593 : assert(simcoin.fCoinBase == realcoin.fCoinBase);
402 [ - + ]: 28593 : assert(realcoin.nHeight == sim->second);
403 : : }
404 : 60667 : },
405 : :
406 : 31054 : [&]() { // Uncache
407 : 31054 : uint32_t outpointidx = provider.ConsumeIntegralInRange<uint32_t>(0, NUM_OUTPOINTS - 1);
408 : : // Apply to real caches (there is no equivalent in our simulation).
409 : 31054 : caches.back()->Uncache(data.outpoints[outpointidx]);
410 : 31054 : },
411 : :
412 : 156309 : [&]() { // Add a cache level (if not already at the max).
413 [ - + + + ]: 156309 : if (caches.size() != MAX_CACHES) {
414 : : // Apply to real caches.
415 [ + + ]: 115941 : if (provider.ConsumeBool()) {
416 [ + - ]: 11798 : caches.emplace_back(new CCoinsViewCache(&*caches.back(), /*deterministic=*/true));
417 : 11798 : fetch_scopes.emplace_back();
418 : : } else {
419 [ + - + - ]: 208286 : caches.emplace_back(new CoinsViewOverlay(&*caches.back(), g_thread_pool, /*deterministic=*/true));
420 [ + - ]: 104143 : fetch_scopes.emplace_back(make_fetch_scope());
421 : : }
422 : : // Apply to simulation data.
423 [ - + ]: 115941 : sim_caches[caches.size()].Wipe();
424 : : }
425 : 156309 : },
426 : :
427 : 135515 : [&]() { // Remove a cache level.
428 : : // Apply to real caches (this reduces caches.size(), implicitly doing the same on the simulation data).
429 : 135515 : caches.back()->SanityCheck();
430 : 135515 : fetch_scopes.pop_back();
431 : 135515 : caches.pop_back();
432 : 135515 : },
433 : :
434 : 42759 : [&]() { // Flush.
435 : : // CoinsViewOverlay::Flush() must have all inputs consumed before being called
436 [ + - ]: 42759 : if (auto* overlay{dynamic_cast<CoinsViewOverlay*>(caches.back().get())};
437 [ + + + + ]: 55235 : overlay && !overlay->AllInputsConsumed()) {
438 : : return;
439 : : }
440 : : // Apply to simulation data.
441 : 33306 : flush();
442 : : // Apply to real caches.
443 : 33306 : caches.back()->Flush(/*reallocate_cache=*/provider.ConsumeBool());
444 : : },
445 : :
446 : 85370 : [&]() { // Sync.
447 [ + + ]: 85370 : if (fetch_scopes.back()) return; // CoinsViewOverlay::Sync() is never called in production
448 : : // Apply to simulation data (note that in our simulation, syncing and flushing is the same thing).
449 : 58747 : flush();
450 : : // Apply to real caches.
451 : 58747 : caches.back()->Sync();
452 : : },
453 : :
454 : 46334 : [&]() { // Reset.
455 [ - + ]: 46334 : sim_caches[caches.size()].Wipe();
456 : : // Apply to real caches. Optionally start fetching again.
457 [ + + + + ]: 46334 : if (fetch_scopes.back() && provider.ConsumeBool()) {
458 [ + - ]: 7059 : fetch_scopes.back().reset(); // Stop fetching before starting again.
459 : 7059 : fetch_scopes.back() = make_fetch_scope();
460 : : } else {
461 : 39275 : (void)caches.back()->CreateResetGuard();
462 : : }
463 : 46334 : },
464 : :
465 : 41929 : [&]() { // GetCacheSize
466 : 41929 : (void)caches.back()->GetCacheSize();
467 : 41929 : },
468 : :
469 : 30146 : [&]() { // DynamicMemoryUsage
470 : 30146 : (void)caches.back()->DynamicMemoryUsage();
471 : 30146 : },
472 : :
473 : 35744 : [&]() { // Change height
474 : 35744 : current_height = provider.ConsumeIntegralInRange<uint32_t>(1, current_height - 1);
475 : 35744 : }
476 : : );
477 : : }
478 : :
479 : : // Sanity check all the remaining caches
480 [ + + ]: 1274 : for (const auto& cache : caches) {
481 [ + - ]: 842 : cache->SanityCheck();
482 : : }
483 : :
484 : : // Full comparison between caches and simulation data, from bottom to top,
485 [ - + + + ]: 1274 : for (unsigned sim_idx = 1; sim_idx <= caches.size(); ++sim_idx) {
486 : 842 : auto& cache = *caches[sim_idx - 1];
487 : 842 : size_t cache_size = 0;
488 : :
489 [ + + ]: 216394 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
490 [ + - ]: 215552 : cache_size += cache.HaveCoinInCache(data.outpoints[outpointidx]);
491 [ + - ]: 215552 : const auto real{cache.PeekCoin(data.outpoints[outpointidx])};
492 : 215552 : auto sim = lookup(outpointidx, sim_idx);
493 [ + + ]: 215552 : if (!sim.has_value()) {
494 [ - + ]: 185701 : assert(!real);
495 : : } else {
496 [ - + ]: 29851 : assert(!real->IsSpent());
497 [ - + ]: 29851 : assert(real->out == data.coins[sim->first].out);
498 [ - + ]: 29851 : assert(real->fCoinBase == data.coins[sim->first].fCoinBase);
499 [ - + ]: 29851 : assert(real->nHeight == sim->second);
500 : : }
501 : 215552 : }
502 : :
503 : : // HaveCoinInCache ignores spent coins, so GetCacheSize() may exceed it.
504 [ + - - + ]: 842 : assert(cache.GetCacheSize() >= cache_size);
505 : : }
506 : :
507 : : // Compare the bottom coinsview (not a CCoinsViewCache) with sim_cache[0].
508 [ + + ]: 111024 : for (uint32_t outpointidx = 0; outpointidx < NUM_OUTPOINTS; ++outpointidx) {
509 : 110592 : auto realcoin = bottom.GetCoin(data.outpoints[outpointidx]);
510 : 110592 : auto sim = lookup(outpointidx, 0);
511 [ + + ]: 110592 : if (!sim.has_value()) {
512 [ - + ]: 99772 : assert(!realcoin);
513 : : } else {
514 [ + - - + ]: 10820 : assert(realcoin && !realcoin->IsSpent());
515 [ - + ]: 10820 : assert(realcoin->out == data.coins[sim->first].out);
516 [ - + ]: 10820 : assert(realcoin->fCoinBase == data.coins[sim->first].fCoinBase);
517 [ - + ]: 10820 : assert(realcoin->nHeight == sim->second);
518 : : }
519 : 110592 : }
520 : :
521 : : // Tear down the fetch scopes top down. Otherwise lower level could reset while upper level is reading from it.
522 [ + + ]: 1274 : while (!fetch_scopes.empty()) fetch_scopes.pop_back();
523 : 432 : }
|