Skip to content

fuzz: Fix difficulty target generation in `p2p_headers_presync`

In the p2p_headers_presync fuzz target, this assertion failed:

 assert(total_work < chainman.MinimumChainWork());

Input that triggered the failure: p2ppresync_crash.txt

The test previously used ConsumeIntegralInRange to generate header difficulty targets within a hardcoded range. The fuzzer found specific values in that range that correspond to very low thresholds due to how SetCompact works. The total work of a long enough test chain ended up exceeding MinimumChainWork.

Fix this by adding a new ConsumeArithUInt256InRange helper function and use it in the fuzz test to generate target values within the originally intended range. The target is then converted to an nBits value using GetCompact().

For some more context, see https://github.com/bitcoin/bitcoin/pull/30918.

Merge request reports

Loading