Skip to content

test: fuzz: check CoinGrinder completed flag - #36905

Open
Conradgabe wants to merge 1 commit into
bitcoin:masterfrom
Conradgabe:test-coingrinder-algo-completed
Open

Conradgabe wants to merge 1 commit into
bitcoin:masterfrom
Conradgabe:test-coingrinder-algo-completed

Conversation

@Conradgabe

Copy link
Copy Markdown
Contributor

CoinGrinder marks its result as completed when it exhausts the search space before reaching TOTAL_TRIES, and leaves it incomplete when it stops at the limit. No unit test reads this flag, and the coin_grinder and coin_grinder_is_optimal fuzz targets only use it to decide whether to assert optimality, so a wrong value would go unnoticed.

This PR checks GetAlgoCompleted() in coin_grinder_tests for cases that finish below the attempt limit (100,000), and asserts in both fuzz targets that the flag is true below the limit and false once it is reached.

Kills the following mutants:

https://mutanthub.space/mutants/4402

--- a/src/wallet/coinselection.cpp
+++ b/src/wallet/coinselection.cpp
@@ -561,7 +561,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c
             if (curr_selection.empty()) {
                 // Exhausted search space before running into attempt limit
                 is_done = true;
-                result.SetAlgoCompleted(true);
+                result.SetAlgoCompleted(false);
                 break;
             }
             next_utxo = curr_selection.back() + 1;

https://mutanthub.space/mutants/4401

--- a/src/wallet/coinselection.cpp
+++ b/src/wallet/coinselection.cpp
@@ -539,7 +539,7 @@ util::Result<SelectionResult> CoinGrinder(std::vector<OutputGroup>& utxo_pool, c

         if (curr_try >= TOTAL_TRIES) {
             // Solution is not guaranteed to be optimal if `curr_try` hit TOTAL_TRIES
-            result.SetAlgoCompleted(false);
+            result.SetAlgoCompleted(true);
             break;
         }

CoinGrinder marks its result as completed when it exhausts the
search space before reaching TOTAL_TRIES. No unit test reads
this flag, and the coin_grinder and coin_grinder_is_optimal fuzz
targets only use it to decide whether to check optimality, so a
wrong value here goes unnoticed.

Check GetAlgoCompleted() in coin_grinder_tests for cases that
finish below the attempt limit, and assert in both fuzz targets
that the flag is true exactly when fewer than TOTAL_TRIES
selections were evaluated.
@DrahtBot DrahtBot added the Tests label Nov 17, 2026
@DrahtBot

DrahtBot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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

External sites

Reviews

See the guideline and AI policy for information on the review process.

Type Reviewers
ACK yancyribbens, Zippo76

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36255 (Refactor CoinGrinder tests by murchandamus)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@yancyribbens

Copy link
Copy Markdown
Contributor

ACK e5ee8a9

@Zippo76 Zippo76 left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

tACK e5ee8a9

I applied each mutant separately on macOS arm64. For each build, I ran coin_grinder_tests and replayed both seed corpora from qa-assets at 9c61fa724 using the standalone fuzz binary.

Build coin_grinder_tests coin_grinder (681 inputs) coin_grinder_is_optimal (231 inputs)
PR Pass Pass Pass
4402 (false on search exhaustion) All six new checks fail Assertion fails at line 125 Assertion fails at line 202
4401 (true at the attempt limit) Pass Same assertion fails at line 125 Pass, 231/231

Both assertion line numbers refer to src/wallet/test/fuzz/coinselection.cpp. I applied the mutations in src/wallet/coinselection.cpp.

cmake -B build_fuzz -DBUILD_FOR_FUZZING=ON
cmake --build build_fuzz
cmake --build build

build/bin/test_bitcoin --run_test=coinselector_tests/coin_grinder_tests
FUZZ=coin_grinder build_fuzz/bin/fuzz ../qa-assets/fuzz_corpora/coin_grinder
FUZZ=coin_grinder_is_optimal build_fuzz/bin/fuzz ../qa-assets/fuzz_corpora/coin_grinder_is_optimal

For 4401, f94e50c75af1ee1fbe07abb16850bcf5dd28775d was the first input to trigger the assertion in my run. The runner shuffles the corpus. [Reproducing input](https://github.com/bitcoin-core/qa-assets/blob/9c61fa724af7566eaa9f97ad23c637374ef469ea/fuzz_corpora/coin_grinder/f94e50c75af1ee1fbe07abb16850bcf5dd28775d)

One optional addition would be a unit test alongside bnb_exhaustion_with_solution_test in coinselection_tests.cpp. I tried 20 groups built with MakeCoin(COIN + i) for i from 0 to 19, a target of 10 * COIN, zero change target and MAX_STANDARD_TX_WEIGHT. With the PR, CoinGrinder returned enough to cover the target after exactly 100,000 evaluations, with GetAlgoCompleted() false. The same test passed with 4402. With 4401, only BOOST_CHECK(!result->GetAlgoCompleted()) failed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants