Skip to content

Conversation

@darosior
Copy link
Member

@darosior darosior commented Aug 3, 2023

This introduces a small fuzz target for CBlockTreeDB which asserts a few invariants by using an in-memory LevelDb.

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 3, 2023

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage

For detailed information about the code coverage, see the test coverage report.

Reviews

See the guideline for information on the review process.

Type Reviewers
ACK TheCharlatan, maflcko, brunoerg, achow101
Concept ACK dergoegge
Stale ACK jamesob

If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

@DrahtBot DrahtBot added the Tests label Aug 3, 2023
@fanquake fanquake requested a review from dergoegge August 3, 2023 10:51
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK, left two nits

@darosior
Copy link
Member Author

darosior commented Aug 3, 2023

Thanks, addressed your comments.

@dergoegge
Copy link
Member

Concept ACK

1 similar comment
@brunoerg
Copy link
Contributor

brunoerg commented Aug 3, 2023

Concept ACK

@darosior
Copy link
Member Author

Rebased on master to fix the macOS CI.

@DrahtBot
Copy link
Contributor

DrahtBot commented Sep 6, 2023

Needs rebase if still relevant

@darosior
Copy link
Member Author

darosior commented Sep 7, 2023

Ok @DrahtBot. Done.

@darosior
Copy link
Member Author

Added a MakeNoLogFileContext at init to avoid the ever-increasing memory usage due to log messages.

Copy link
Contributor

@sedited sedited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK ffee43efe845cbbfbf16d5e61a1d541cb316ef56

const std::string flag_name = fuzzed_data_provider.ConsumeBytesAsString(flag_size);
bool flag_value;
block_index.WriteFlag(flag_name, true);
block_index.ReadFlag(flag_name, flag_value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding something to exercise Erase would also be nice.

@DrahtBot DrahtBot requested review from brunoerg and jamesob May 13, 2024 08:19
@darosior darosior force-pushed the fuzz_block_index branch from ffee43e to 86b3852 Compare May 29, 2024 16:55
@darosior
Copy link
Member Author

Thanks for the review, i've also rebased on top of master for fresh CI.

Copy link
Contributor

@sedited sedited left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-ACK 86b3852

@glozow glozow requested review from brunoerg, dergoegge and maflcko and removed request for brunoerg, dergoegge and maflcko August 7, 2024 11:57

const BasicTestingSetup* g_setup;

// Hardcoded block hash and nBits to make sure the blocks we store pass the pow check.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 86b3852: nBits is not hardcoded anymore.

Suggested change
// Hardcoded block hash and nBits to make sure the blocks we store pass the pow check.
// Hardcoded block hash to make sure the blocks we store pass the pow check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it is. (At least the code I reviewed locally)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but not here as done before.

before:

// Hardcoded block hash and nBits to make sure the blocks we store pass the pow check.
const uint256 g_block_hash{uint256S("000000002c05cc2e78923c34df87fd108b22221ac6076c18f3ade378a4d915e9")};
uint32_t g_bits{0x1d00ffff};

anyway, feel free to ignore it.

@DrahtBot DrahtBot requested a review from brunoerg August 7, 2024 12:13
Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left some style-nits, feel free to ignore. lgtm

review ACK 86b3852 🥒

Show signature

Signature:

untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
trusted comment: review ACK 86b38529d5014612c3e7bb59fdc4dad3bff2aa64 🥒
cjVCcUx/BhmdYxcriNMuwwyayOaGH23QYrgOlQYTjyrvNxUXvUDplvgUguxjMPrdahNSoffgh3ITZYRvtcHNCg==

#include <test/fuzz/fuzz.h>
#include <test/fuzz/util.h>
#include <test/util/setup_common.h>
#include <txdb.h>
Copy link
Member

@maflcko maflcko Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style-nit: txdb.h is CCoinsViewDB (chainstate/), but you are fuzzing BlockTreeDB (blocks/index/).

Maybe just:

test/fuzz/block_index.cpp should add these lines:
#include <cassert>                        // for assert
#include <functional>                      // for function
#include <memory>                          // for unique_ptr, make_unique
#include <optional>                        // for optional
#include <string>                          // for string
#include <utility>                         // for pair, move
#include <vector>                          // for vector

#include "consensus/params.h"              // for Params
#include "dbwrapper.h"                     // for DBParams
#include "primitives/block.h"              // for CBlockHeader, CBlock
#include "sync.h"                          // for MaybeCheckNotHeld, UniqueLock, WITH_LOCK
#include "uint256.h"                       // for uint256
#include "util/fs.h"                       // for path

test/fuzz/block_index.cpp should remove these lines:
- #include <txdb.h>  // lines 12-12

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why i have this include here, i'll just copy what you suggest.

@@ -0,0 +1,133 @@
// Copyright (c) 2023 The Bitcoin Core developers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style-nit, to pre-empt touching this file on (or after) future changes.

Suggested change
// Copyright (c) 2023 The Bitcoin Core developers
// Copyright (c) 2023-present The Bitcoin Core developers


void init_block_index()
{
static const auto testing_setup = MakeNoLogFileContext<>(ChainType::MAIN);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

q/nit: Any reason to use MAIN here, instead of the default, or a test-net?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is any difference, i just default to test against mainnet when possible. On the other hand it sometimes makes sense to default to regtest instead (for instance in the context of a fuzz target touching validation it would enable the codepaths for all soft forks). So, yeah, 🤷.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually i think i can get rid of this now, and just disable logging like i do like in #29158.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just default to test against mainnet when possible.

I do the opposite 😅 , so that a corrupt test is less likely to corrupt a developers (default) main datadir for their testing by accident, but no strong opinion. Just a style-nit in any case.

(Even if you disable the logging manually, I presume you'll have to select a network) But if it works without, then it is fine as well.

@darosior
Copy link
Member Author

darosior commented Aug 7, 2024 via email

@maflcko
Copy link
Member

maflcko commented Aug 7, 2024

Thanks for the review everyone. Maintainers: there is still a determinism issue here as per Niklas, which i have yet to re-reproduce.

Are you sure? I think may be confused with #28216 (comment) ?

Even if not, I wonder if stability is a hard blocker for a fuzz target. Obviously, it should be fixed, but this can be done in a follow-up, if the fuzz target otherwise makes sense to merge.

@dergoegge
Copy link
Member

Are you sure? I think may be confused with #28216 (comment)?

Iirc, I pinged Antoine for both PRs on irc.

I also don't think the stability needs to be a blocker.

Copy link
Contributor

@brunoerg brunoerg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 86b3852

@achow101
Copy link
Member

ACK 86b3852

@hebasto
Copy link
Member

hebasto commented Aug 16, 2024

Ported to the CMake-based build system in hebasto#334.

@darosior darosior deleted the fuzz_block_index branch November 8, 2024 21:16
knst pushed a commit to knst/dash that referenced this pull request Oct 22, 2025
86b3852 qa: a fuzz target for the block index database (Antoine Poinsot)

Pull request description:

  This introduces a small fuzz target for `CBlockTreeDB` which asserts a few invariants by using an in-memory LevelDb.

ACKs for top commit:
  achow101:
    ACK 86b3852
  TheCharlatan:
    Re-ACK 86b3852
  maflcko:
    review ACK 86b3852 🥒
  brunoerg:
    utACK 86b3852

Tree-SHA512: ab75b4ae1c7e0a4b15f8a6ceffdf509fbc79833e6ea073ecef68558d53b83663d1b30362aaa2d77c22b8890a572f5b1d4b1c5abbca483c8c8f9b1fb5b276a59a
@bitcoin bitcoin locked and limited conversation to collaborators Nov 8, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants