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 <coins.h>
6 : : #include <consensus/amount.h>
7 : : #include <consensus/tx_check.h>
8 : : #include <consensus/tx_verify.h>
9 : : #include <consensus/validation.h>
10 : : #include <kernel/chainstatemanager_opts.h>
11 : : #include <kernel/cs_main.h>
12 : : #include <policy/policy.h>
13 : : #include <primitives/block.h>
14 : : #include <primitives/transaction.h>
15 : : #include <script/interpreter.h>
16 : : #include <test/fuzz/FuzzedDataProvider.h>
17 : : #include <test/fuzz/fuzz.h>
18 : : #include <test/fuzz/util.h>
19 : : #include <test/util/coins.h>
20 : : #include <test/util/setup_common.h>
21 : : #include <txdb.h>
22 : : #include <util/hasher.h>
23 : : #include <util/threadpool.h>
24 : :
25 : : #include <cassert>
26 : : #include <algorithm>
27 : : #include <cstdint>
28 : : #include <functional>
29 : : #include <limits>
30 : : #include <memory>
31 : : #include <optional>
32 : : #include <ranges>
33 : : #include <stdexcept>
34 : : #include <string>
35 : : #include <utility>
36 : : #include <vector>
37 : :
38 : : namespace {
39 : : /**
40 : : * MutationGuardCoinsViewCache asserts that nothing mutates cacheCoins until
41 : : * BatchWrite is called. It keeps a snapshot of the cacheCoins state, which it
42 : : * uses for the assertion in BatchWrite. After the call to the superclass
43 : : * CCoinsViewCache::BatchWrite returns, it recomputes the snapshot at that
44 : : * moment.
45 : : */
46 : : class MutationGuardCoinsViewCache final : public CCoinsViewCache
47 : : {
48 : : private:
49 [ + + ]: 423679 : struct CacheCoinSnapshot {
50 : 81898 : COutPoint outpoint;
51 : 81898 : bool dirty{false};
52 : 81898 : bool fresh{false};
53 : 81898 : Coin coin;
54 [ + - + - : 81898 : bool operator==(const CacheCoinSnapshot&) const = default;
+ - - + ]
55 : : };
56 : :
57 : 42125 : std::vector<CacheCoinSnapshot> ComputeCacheCoinsSnapshot() const
58 : : {
59 : 42125 : std::vector<CacheCoinSnapshot> snapshot;
60 [ + - ]: 42125 : snapshot.reserve(cacheCoins.size());
61 : :
62 [ + + + - ]: 220554 : for (const auto& [outpoint, entry] : cacheCoins) {
63 [ + - ]: 178429 : snapshot.emplace_back(outpoint, entry.IsDirty(), entry.IsFresh(), entry.coin);
64 : : }
65 : :
66 : 42125 : std::ranges::sort(snapshot, std::less<>{}, &CacheCoinSnapshot::outpoint);
67 : 42125 : return snapshot;
68 : 0 : }
69 : :
70 : : mutable std::vector<CacheCoinSnapshot> m_expected_snapshot{ComputeCacheCoinsSnapshot()};
71 : :
72 : : public:
73 : 19383 : void BatchWrite(CoinsViewCacheCursor& cursor, const uint256& block_hash) override
74 : : {
75 : : // Nothing must modify cacheCoins other than BatchWrite.
76 [ - + ]: 19383 : assert(ComputeCacheCoinsSnapshot() == m_expected_snapshot);
77 : 19383 : CCoinsViewCache::BatchWrite(cursor, block_hash);
78 : 19383 : m_expected_snapshot = ComputeCacheCoinsSnapshot();
79 : 19383 : }
80 : :
81 : : using CCoinsViewCache::CCoinsViewCache;
82 : : };
83 : :
84 : : // Reuse a single global thread pool across fuzz iterations. Creating and destroying a pool every
85 : : // iteration leaks memory, since iterations can run faster than the OS can tear down the threads.
86 : : std::shared_ptr<ThreadPool> g_thread_pool{std::make_shared<ThreadPool>("view_fuzz")};
87 : :
88 : 5901 : void StartPoolIfNeeded()
89 : : {
90 [ + + ]: 5901 : if (!g_thread_pool->WorkersCount()) g_thread_pool->Start(DEFAULT_PREVOUTFETCH_THREADS);
91 : 5901 : }
92 : :
93 : : //! Build a random block and seed a view with utxos for its inputs.
94 : 5901 : CBlock BuildRandomBlock(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& view)
95 : : {
96 : 5901 : CBlock block;
97 [ + - ]: 5901 : CMutableTransaction coinbase;
98 [ + - ]: 5901 : coinbase.vin.emplace_back();
99 [ + - + - : 11802 : block.vtx.push_back(MakeTransactionRef(coinbase));
- + ]
100 : :
101 [ + - ]: 5901 : CCoinsViewCache seed_cache{&view, /*deterministic=*/true};
102 [ + - ]: 5901 : seed_cache.SetBestBlock(uint256::ONE);
103 : :
104 : 5901 : Txid prevhash{Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))};
105 [ + + + + ]: 23054 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
106 : : {
107 [ + - ]: 17153 : CMutableTransaction tx;
108 [ + + + + ]: 83258 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 100)
109 : : {
110 : 66105 : const Txid txid{fuzzed_data_provider.ConsumeBool()
111 [ + + ]: 66105 : ? Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider))
112 : 8713 : : prevhash};
113 : 66105 : const COutPoint outpoint{txid, fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
114 [ + + + - ]: 66105 : if (auto coin{ConsumeDeserializable<Coin>(fuzzed_data_provider)}; coin && !coin->IsSpent()) {
115 [ + - ]: 24066 : seed_cache.AddCoin(outpoint, std::move(*coin), /*possible_overwrite=*/true);
116 : 0 : }
117 [ + - ]: 66105 : tx.vin.emplace_back(outpoint);
118 : : }
119 [ + - ]: 17153 : prevhash = tx.GetHash();
120 [ + - + - : 34306 : block.vtx.push_back(MakeTransactionRef(tx));
- + ]
121 : 17153 : }
122 : :
123 [ + - ]: 5901 : seed_cache.Flush();
124 : 11802 : return block;
125 : 11802 : }
126 : :
127 : : } // namespace
128 : :
129 : 4 : void initialize_coins_view()
130 : : {
131 [ + - + - : 4 : static const auto testing_setup = MakeNoLogFileContext<>();
+ - ]
132 : 4 : }
133 : :
134 : 18799 : void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsViewCache& coins_view_cache, CCoinsView* backend_coins_view)
135 : : {
136 [ + - ]: 18799 : auto* const db{dynamic_cast<CCoinsViewDB*>(backend_coins_view)};
137 : 18799 : auto* const overlay{dynamic_cast<CoinsViewOverlay*>(&coins_view_cache)};
138 : 18799 : const bool is_db{db != nullptr};
139 : 18799 : bool good_data{true};
140 : 18799 : auto* original_backend{backend_coins_view};
141 : :
142 [ + + ]: 18799 : if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
143 : 18799 : COutPoint random_out_point;
144 : 18799 : Coin random_coin;
145 [ + - ]: 18799 : CMutableTransaction random_mutable_transaction;
146 [ + + + + : 9385577 : LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
+ + ]
147 [ + - ]: 4675206 : CallOneOf(
148 : : fuzzed_data_provider,
149 : 351033 : [&] {
150 [ + + ]: 351033 : if (random_coin.IsSpent()) {
151 : : return;
152 : : }
153 : 313220 : COutPoint outpoint{random_out_point};
154 : 313220 : Coin coin{random_coin};
155 [ + + ]: 313220 : if (fuzzed_data_provider.ConsumeBool()) {
156 : : // We can only skip the check if no unspent coin exists for this outpoint.
157 [ + - + + : 345367 : const bool possible_overwrite{coins_view_cache.PeekCoin(outpoint) || fuzzed_data_provider.ConsumeBool()};
+ + ]
158 [ + - ]: 293397 : coins_view_cache.AddCoin(outpoint, std::move(coin), possible_overwrite);
159 : : } else {
160 [ + - ]: 19823 : coins_view_cache.EmplaceCoinInternalDANGER(outpoint, std::move(coin));
161 : : }
162 : 313220 : },
163 : 1288798 : [&] {
164 [ + + - + : 1288798 : if (overlay && !overlay->AllInputsConsumed()) return; // CoinsViewOverlay::Flush() must have all inputs consumed before being called
+ + ]
165 : 1284321 : coins_view_cache.Flush(/*reallocate_cache=*/fuzzed_data_provider.ConsumeBool());
166 : : },
167 : 2186182 : [&] {
168 [ + + ]: 2186182 : if (overlay) return; // CoinsViewOverlay::Sync() is never called in production code
169 : 2123930 : coins_view_cache.Sync();
170 : : },
171 : 430174 : [&] {
172 [ + + + - : 1695421 : if (db) WITH_LOCK(::cs_main, (void)db->CompactFullAsync());
+ - ]
173 : 430174 : },
174 : 32578 : [&] {
175 : 32578 : uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
176 : : // `CCoinsViewDB::BatchWrite()` requires a non-null best block.
177 [ + + + + ]: 60635 : if (is_db && best_block.IsNull()) best_block = uint256::ONE;
178 : 32578 : coins_view_cache.SetBestBlock(best_block);
179 : 32578 : },
180 : 27248 : [&] {
181 : 27248 : (void)coins_view_cache.CreateResetGuard();
182 : : // Reset() clears the best block, so reseed db-backed caches.
183 [ + + ]: 27248 : if (is_db) {
184 : 16693 : const uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
185 [ + + ]: 33386 : if (best_block.IsNull()) {
186 : 62 : good_data = false;
187 : 62 : return;
188 : : }
189 : 16631 : coins_view_cache.SetBestBlock(best_block);
190 : : }
191 : : },
192 : 123767 : [&] {
193 : 123767 : Coin move_to;
194 [ + + + - ]: 140112 : (void)coins_view_cache.SpendCoin(random_out_point, fuzzed_data_provider.ConsumeBool() ? &move_to : nullptr);
195 : 123767 : },
196 : 35563 : [&] {
197 : 35563 : coins_view_cache.Uncache(random_out_point);
198 : 35563 : },
199 : 114646 : [&] {
200 [ + + ]: 114646 : if (overlay) return; // // CoinsViewOverlay::SetBackend() is never called in production code
201 : 95172 : const bool use_original_backend{fuzzed_data_provider.ConsumeBool()};
202 [ + + + + ]: 95172 : if (use_original_backend && backend_coins_view != original_backend) {
203 : : // FRESH flags valid against the empty backend may be invalid
204 : : // against the original backend, so reset before restoring it.
205 : 15123 : (void)coins_view_cache.CreateResetGuard();
206 : : // Reset() clears the best block; db backends require a non-null hash.
207 [ + - ]: 15123 : if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
208 : : }
209 : 95172 : backend_coins_view = use_original_backend ? original_backend : &CoinsViewEmpty::Get();
210 : 95172 : coins_view_cache.SetBackend(*backend_coins_view);
211 : : },
212 : 13145 : [&] {
213 : 13145 : const std::optional<COutPoint> opt_out_point = ConsumeDeserializable<COutPoint>(fuzzed_data_provider);
214 [ + + ]: 13145 : if (!opt_out_point) {
215 : 346 : good_data = false;
216 : 346 : return;
217 : : }
218 : 12799 : random_out_point = *opt_out_point;
219 : : },
220 : 21943 : [&] {
221 : 21943 : const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
222 [ + + ]: 21943 : if (!opt_coin) {
223 : 489 : good_data = false;
224 : 489 : return;
225 : : }
226 : 21454 : random_coin = *opt_coin;
227 : 21943 : },
228 : 18336 : [&] {
229 : 18336 : const std::optional<CMutableTransaction> opt_mutable_transaction = ConsumeDeserializable<CMutableTransaction>(fuzzed_data_provider, TX_WITH_WITNESS);
230 [ + + ]: 18336 : if (!opt_mutable_transaction) {
231 : 1345 : good_data = false;
232 [ - + ]: 1345 : return;
233 : : }
234 [ + - ]: 16991 : random_mutable_transaction = *opt_mutable_transaction;
235 : 18336 : },
236 : 31793 : [&] {
237 : 31793 : CoinsCachePair sentinel{};
238 : 31793 : sentinel.second.SelfRef(sentinel);
239 : 31793 : size_t dirty_count{0};
240 [ + - ]: 31793 : CCoinsMapMemoryResource resource;
241 [ + - + - ]: 31793 : CCoinsMap coins_map{0, SaltedCoinsCacheHasher{/*deterministic=*/true}, CCoinsMap::key_equal{}, &resource};
242 [ + - + + : 1458848 : LIMITED_WHILE (good_data && fuzzed_data_provider.ConsumeBool(), 10'000) {
+ + ]
243 : 1427246 : CCoinsCacheEntry coins_cache_entry;
244 [ + + ]: 1427246 : if (fuzzed_data_provider.ConsumeBool()) {
245 : 1423851 : coins_cache_entry.coin = random_coin;
246 : : } else {
247 : 3395 : const std::optional<Coin> opt_coin = ConsumeDeserializable<Coin>(fuzzed_data_provider);
248 [ + + ]: 3395 : if (!opt_coin) {
249 : 191 : good_data = false;
250 : 191 : return;
251 : : }
252 : 3204 : coins_cache_entry.coin = *opt_coin;
253 : 3204 : }
254 : : // Avoid setting FRESH for an outpoint that already exists unspent in the parent view.
255 [ + - + + : 1436141 : bool fresh{!coins_view_cache.PeekCoin(random_out_point) && fuzzed_data_provider.ConsumeBool()};
+ + ]
256 [ + + + + ]: 1427055 : bool dirty{fresh || fuzzed_data_provider.ConsumeBool()};
257 [ + - ]: 1427055 : auto it{coins_map.emplace(random_out_point, std::move(coins_cache_entry)).first};
258 [ + + ]: 1427055 : if (dirty) CCoinsCacheEntry::SetDirty(*it, sentinel);
259 [ + + ]: 1427055 : if (fresh) CCoinsCacheEntry::SetFresh(*it, sentinel);
260 : 1427055 : dirty_count += dirty;
261 : 1427246 : }
262 [ + - ]: 31602 : auto cursor{CoinsViewCacheCursor(dirty_count, sentinel, coins_map, /*will_erase=*/true)};
263 [ + - ]: 31602 : uint256 best_block{coins_view_cache.GetBestBlock()};
264 [ + + ]: 31602 : if (fuzzed_data_provider.ConsumeBool()) best_block = ConsumeUInt256(fuzzed_data_provider);
265 : : // Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
266 [ + + + + ]: 43047 : if (is_db && best_block.IsNull()) best_block = uint256::ONE;
267 [ + - ]: 31602 : coins_view_cache.BatchWrite(cursor, best_block);
268 : 31793 : });
269 : : }
270 : :
271 : 18799 : {
272 [ + - ]: 18799 : (void)coins_view_cache.DynamicMemoryUsage();
273 [ + - ]: 18799 : (void)coins_view_cache.EstimateSize();
274 [ + - ]: 18799 : (void)coins_view_cache.GetBestBlock();
275 [ + - ]: 18799 : (void)coins_view_cache.GetCacheSize();
276 [ + - ]: 18799 : (void)coins_view_cache.GetHeadBlocks();
277 [ + - + - ]: 18799 : (void)coins_view_cache.HaveInputs(CTransaction{random_mutable_transaction});
278 : : }
279 : :
280 : 18799 : {
281 [ + + + + ]: 18799 : if (is_db && backend_coins_view == original_backend) {
282 [ + - - + ]: 8700 : assert(db->Cursor());
283 : : }
284 [ + - ]: 18799 : (void)backend_coins_view->EstimateSize();
285 [ + - ]: 18799 : (void)backend_coins_view->GetBestBlock();
286 [ + - ]: 18799 : (void)backend_coins_view->GetHeadBlocks();
287 : : }
288 : :
289 [ + + ]: 18799 : if (fuzzed_data_provider.ConsumeBool()) {
290 [ + - ]: 9420 : CallOneOf(
291 : : fuzzed_data_provider,
292 : 861 : [&] {
293 : 861 : const CTransaction transaction{random_mutable_transaction};
294 : 861 : bool is_spent = false;
295 [ + + ]: 124992 : for (const CTxOut& tx_out : transaction.vout) {
296 [ + + ]: 124131 : if (Coin{tx_out, 0, transaction.IsCoinBase()}.IsSpent()) {
297 : 189 : is_spent = true;
298 : : }
299 : : }
300 [ + + ]: 861 : if (is_spent) {
301 : : // Avoid:
302 : : // coins.cpp:69: void CCoinsViewCache::AddCoin(const COutPoint &, Coin &&, bool): Assertion `!coin.IsSpent()' failed.
303 : 22 : return;
304 : : }
305 : 839 : const int height{int(fuzzed_data_provider.ConsumeIntegral<uint32_t>() >> 1)};
306 [ + + + + ]: 1670 : const bool check_for_overwrite{transaction.IsCoinBase() || [&] {
307 [ - + + + ]: 123836 : for (uint32_t i{0}; i < transaction.vout.size(); ++i) {
308 [ + + ]: 123009 : if (coins_view_cache.PeekCoin(COutPoint{transaction.GetHash(), i})) return true;
309 : : }
310 : 827 : return fuzzed_data_provider.ConsumeBool();
311 [ + - ]: 831 : }()}; // We can only skip the check if the current txid has no unspent outputs
312 [ + - ]: 839 : AddCoins(coins_view_cache, transaction, height, check_for_overwrite);
313 : 861 : },
314 : 4713 : [&] {
315 [ + - ]: 9426 : (void)ValidateInputsStandardness(CTransaction{random_mutable_transaction}, coins_view_cache);
316 : 4713 : },
317 : 600 : [&] {
318 [ + - ]: 600 : TxValidationState state;
319 : 600 : CAmount tx_fee_out;
320 [ + - ]: 600 : const CTransaction transaction{random_mutable_transaction};
321 [ + + ]: 600 : if (ContainsSpentInput(transaction, coins_view_cache)) {
322 : : // Avoid:
323 : : // consensus/tx_verify.cpp:171: bool Consensus::CheckTxInputs(const CTransaction &, TxValidationState &, const CCoinsViewCache &, int, CAmount &): Assertion `!coin.IsSpent()' failed.
324 : : return;
325 : : }
326 [ + - ]: 582 : TxValidationState dummy;
327 [ + - + + ]: 582 : if (!CheckTransaction(transaction, dummy)) {
328 : : // It is not allowed to call CheckTxInputs if CheckTransaction failed
329 : 468 : return;
330 : : }
331 [ + - + + ]: 114 : if (Consensus::CheckTxInputs(transaction, state, coins_view_cache, fuzzed_data_provider.ConsumeIntegralInRange<int>(0, std::numeric_limits<int>::max()), tx_fee_out)) {
332 [ - + ]: 9 : assert(MoneyRange(tx_fee_out));
333 : : }
334 : 1782 : },
335 : 987 : [&] {
336 : 987 : const CTransaction transaction{random_mutable_transaction};
337 [ + + ]: 987 : if (ContainsSpentInput(transaction, coins_view_cache)) {
338 : : // Avoid:
339 : : // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
340 : 103 : return;
341 : : }
342 [ + - ]: 884 : (void)GetP2SHSigOpCount(transaction, coins_view_cache);
343 : 987 : },
344 : 790 : [&] {
345 : 790 : const CTransaction transaction{random_mutable_transaction};
346 [ + + ]: 790 : if (ContainsSpentInput(transaction, coins_view_cache)) {
347 : : // Avoid:
348 : : // consensus/tx_verify.cpp:130: unsigned int GetP2SHSigOpCount(const CTransaction &, const CCoinsViewCache &): Assertion `!coin.IsSpent()' failed.
349 : : return;
350 : : }
351 [ + + ]: 740 : const auto flags = script_verify_flags::from_int(fuzzed_data_provider.ConsumeIntegral<script_verify_flags::value_type>());
352 [ + + + + : 740 : if (!transaction.vin.empty() && (flags & SCRIPT_VERIFY_WITNESS) != 0 && (flags & SCRIPT_VERIFY_P2SH) == 0) {
+ + ]
353 : : // Avoid:
354 : : // script/interpreter.cpp:1705: size_t CountWitnessSigOps(const CScript &, const CScript &, const CScriptWitness &, unsigned int): Assertion `(flags & SCRIPT_VERIFY_P2SH) != 0' failed.
355 : : return;
356 : : }
357 [ + - ]: 734 : (void)GetTransactionSigOpCost(transaction, coins_view_cache, flags);
358 : 790 : },
359 : 1469 : [&] {
360 [ + - ]: 1469 : (void)IsWitnessStandard(CTransaction{random_mutable_transaction}, coins_view_cache);
361 : 1469 : });
362 : : }
363 : :
364 : 18799 : {
365 [ + - ]: 18799 : const Coin& coin_using_access_coin = coins_view_cache.AccessCoin(random_out_point);
366 [ + - ]: 18799 : const bool exists_using_access_coin = !coin_using_access_coin.IsSpent();
367 [ + - ]: 18799 : const bool exists_using_have_coin = coins_view_cache.HaveCoin(random_out_point);
368 [ + - ]: 18799 : const bool exists_using_have_coin_in_cache = coins_view_cache.HaveCoinInCache(random_out_point);
369 [ + - + + ]: 18799 : if (auto coin{coins_view_cache.GetCoin(random_out_point)}) {
370 [ - + ]: 6448 : assert(*coin == coin_using_access_coin);
371 [ + - - + ]: 6448 : assert(exists_using_access_coin && exists_using_have_coin_in_cache && exists_using_have_coin);
372 : : } else {
373 [ + - - + ]: 12351 : assert(!exists_using_access_coin && !exists_using_have_coin_in_cache && !exists_using_have_coin);
374 : 18799 : }
375 : : // If HaveCoin on the backend is true, it must also be on the cache if the coin wasn't spent.
376 : 18799 : std::optional<Coin> coin_in_backend;
377 : 18799 : bool exists_using_have_coin_in_backend;
378 [ + + ]: 18799 : if (dynamic_cast<CoinsViewOverlay*>(&coins_view_cache)) {
379 : : // PeekCoin does not mutate cacheCoins, so async workers can keep running.
380 [ + - ]: 11802 : coin_in_backend = backend_coins_view->PeekCoin(random_out_point);
381 : 5901 : exists_using_have_coin_in_backend = coin_in_backend.has_value();
382 : : } else {
383 [ + - ]: 12898 : exists_using_have_coin_in_backend = backend_coins_view->HaveCoin(random_out_point);
384 [ + - ]: 25796 : coin_in_backend = backend_coins_view->GetCoin(random_out_point);
385 : : }
386 [ + + + + ]: 18799 : if (!coin_using_access_coin.IsSpent() && exists_using_have_coin_in_backend) {
387 [ - + ]: 2568 : assert(exists_using_have_coin);
388 : : }
389 [ + + ]: 18799 : if (coin_in_backend) {
390 [ - + ]: 2676 : assert(exists_using_have_coin_in_backend);
391 : : // Note we can't assert that `coin_using_get_coin == *coin` because the coin in
392 : : // the cache may have been modified but not yet flushed.
393 : : } else {
394 [ - + ]: 16123 : assert(!exists_using_have_coin_in_backend);
395 : : }
396 : 18799 : }
397 : 18799 : }
398 : :
399 [ + - ]: 4018 : FUZZ_TARGET(coins_view, .init = initialize_coins_view)
400 : : {
401 : 3542 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
402 : 3542 : CCoinsViewCache coins_view_cache{&CoinsViewEmpty::Get(), /*deterministic=*/true};
403 [ + - + - ]: 3542 : TestCoinsView(fuzzed_data_provider, coins_view_cache, &CoinsViewEmpty::Get());
404 : 3542 : }
405 : :
406 [ + - ]: 4748 : FUZZ_TARGET(coins_view_db, .init = initialize_coins_view)
407 : : {
408 : 4272 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
409 : 4272 : auto db_params = DBParams{
410 : : .path = "",
411 : : .cache_bytes = 1_MiB,
412 : : .memory_only = true,
413 : 4272 : };
414 [ + - ]: 4272 : CCoinsViewDB backend_coins_view{std::move(db_params), CoinsViewOptions{}};
415 [ + - ]: 4272 : CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
416 [ + - ]: 4272 : TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_coins_view);
417 : 8544 : }
418 : :
419 : : // Creates a CoinsViewOverlay and a MutationGuardCoinsViewCache as the base.
420 : : // This allows us to exercise all methods on a CoinsViewOverlay, while also
421 : : // ensuring that nothing can mutate the underlying cache until Flush or Sync is
422 : : // called.
423 [ + - ]: 3835 : FUZZ_TARGET(coins_view_overlay, .init = initialize_coins_view)
424 : : {
425 : 3359 : SeedRandomStateForTest(SeedRand::ZEROS); // for SaltedCoinsCacheHasher
426 : 3359 : StartPoolIfNeeded();
427 : 3359 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
428 : 3359 : MutationGuardCoinsViewCache backend_cache{&CoinsViewEmpty::Get(), /*deterministic=*/true};
429 [ + - - + ]: 6718 : CoinsViewOverlay coins_view_cache{&backend_cache, g_thread_pool, /*deterministic=*/true};
430 [ + - ]: 3359 : CBlock block{BuildRandomBlock(fuzzed_data_provider, backend_cache)};
431 : 3359 : const auto reset_guard{coins_view_cache.StartFetching(block)};
432 [ + - ]: 3359 : TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_cache);
433 : 3359 : }
434 : :
435 [ + - ]: 3018 : FUZZ_TARGET(coins_view_stacked, .init = initialize_coins_view)
436 : : {
437 : 2542 : SeedRandomStateForTest(SeedRand::ZEROS); // for SaltedCoinsCacheHasher
438 : 2542 : StartPoolIfNeeded();
439 : 2542 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
440 : 2542 : auto db_params = DBParams{
441 : : .path = "",
442 : : .cache_bytes = 1_MiB,
443 : : .memory_only = true,
444 : 2542 : };
445 [ + - ]: 2542 : CCoinsViewDB backend_base_coins_view{std::move(db_params), CoinsViewOptions{}};
446 [ + - ]: 2542 : CCoinsViewCache backend_cache{&backend_base_coins_view, /*deterministic=*/true};
447 [ + - ]: 2542 : TestCoinsView(fuzzed_data_provider, backend_cache, &backend_base_coins_view);
448 [ + - - + ]: 5084 : CoinsViewOverlay coins_view_cache{&backend_cache, g_thread_pool, /*deterministic=*/true};
449 [ + - ]: 2542 : CBlock block{BuildRandomBlock(fuzzed_data_provider, backend_base_coins_view)};
450 : 2542 : {
451 : 2542 : const auto reset_guard{coins_view_cache.StartFetching(block)};
452 [ + - ]: 2542 : TestCoinsView(fuzzed_data_provider, coins_view_cache, &backend_cache);
453 : 0 : }
454 [ + - ]: 2542 : TestCoinsView(fuzzed_data_provider, backend_cache, &backend_base_coins_view);
455 : 5084 : }
|