Skip to content

Commit cfc42ae

Browse files
darosiormarcofleon
authored andcommitted
fuzz: add a target for the coins database
It reuses the logic from the `coins_view` target, except it uses an in-memory CCoinsViewDB as the backend. Note `CCoinsViewDB` will assert the best block hash is never null, so we slightly modify the coins_view fuzz logic to take care of this.
1 parent 46e1463 commit cfc42ae

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

src/test/fuzz/coins_view.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2020-2022 The Bitcoin Core developers
1+
// Copyright (c) 2020-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -42,11 +42,12 @@ void initialize_coins_view()
4242
static const auto testing_setup = MakeNoLogFileContext<>();
4343
}
4444

45-
void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend_coins_view)
45+
void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend_coins_view, bool is_db)
4646
{
4747
bool good_data{true};
4848

4949
CCoinsViewCache coins_view_cache{&backend_coins_view, /*deterministic=*/true};
50+
if (is_db) coins_view_cache.SetBestBlock(uint256::ONE);
5051
COutPoint random_out_point;
5152
Coin random_coin;
5253
CMutableTransaction random_mutable_transaction;
@@ -85,7 +86,10 @@ void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend
8586
(void)coins_view_cache.Sync();
8687
},
8788
[&] {
88-
coins_view_cache.SetBestBlock(ConsumeUInt256(fuzzed_data_provider));
89+
uint256 best_block{ConsumeUInt256(fuzzed_data_provider)};
90+
// Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
91+
if (is_db && best_block.IsNull()) best_block = uint256::ONE;
92+
coins_view_cache.SetBestBlock(best_block);
8993
},
9094
[&] {
9195
Coin move_to;
@@ -153,7 +157,11 @@ void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend
153157
bool expected_code_path = false;
154158
try {
155159
auto cursor{CoinsViewCacheCursor(usage, sentinel, coins_map, /*will_erase=*/true)};
156-
coins_view_cache.BatchWrite(cursor, fuzzed_data_provider.ConsumeBool() ? ConsumeUInt256(fuzzed_data_provider) : coins_view_cache.GetBestBlock());
160+
uint256 best_block{coins_view_cache.GetBestBlock()};
161+
if (fuzzed_data_provider.ConsumeBool()) best_block = ConsumeUInt256(fuzzed_data_provider);
162+
// Set best block hash to non-null to satisfy the assertion in CCoinsViewDB::BatchWrite().
163+
if (is_db && best_block.IsNull()) best_block = uint256::ONE;
164+
coins_view_cache.BatchWrite(cursor, best_block);
157165
expected_code_path = true;
158166
} catch (const std::logic_error& e) {
159167
if (e.what() == std::string{"FRESH flag misapplied to coin that exists in parent cache"}) {
@@ -207,7 +215,7 @@ void TestCoinsView(FuzzedDataProvider& fuzzed_data_provider, CCoinsView& backend
207215

208216
{
209217
std::unique_ptr<CCoinsViewCursor> coins_view_cursor = backend_coins_view.Cursor();
210-
assert(!coins_view_cursor);
218+
assert(is_db == !!coins_view_cursor);
211219
(void)backend_coins_view.EstimateSize();
212220
(void)backend_coins_view.GetBestBlock();
213221
(void)backend_coins_view.GetHeadBlocks();
@@ -298,5 +306,17 @@ FUZZ_TARGET(coins_view, .init = initialize_coins_view)
298306
{
299307
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
300308
CCoinsView backend_coins_view;
301-
TestCoinsView(fuzzed_data_provider, backend_coins_view);
309+
TestCoinsView(fuzzed_data_provider, backend_coins_view, /*is_db=*/false);
310+
}
311+
312+
FUZZ_TARGET(coins_view_db, .init = initialize_coins_view)
313+
{
314+
FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
315+
auto db_params = DBParams{
316+
.path = "",
317+
.cache_bytes = 1_MiB,
318+
.memory_only = true,
319+
};
320+
CCoinsViewDB coins_db{std::move(db_params), CoinsViewOptions{}};
321+
TestCoinsView(fuzzed_data_provider, coins_db, /*is_db=*/true);
302322
}

0 commit comments

Comments
 (0)