test: fuzz: check CoinGrinder completed flag #36905

Open
Conradgabe wants to merge 1 commit from test-coingrinder-algo-completed into master AGit
Conradgabe commented 2026-11-17 21:36:22 +00:00 (Migrated from github.com)

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`, 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 ```diff --- 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 ```diff --- 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; } ```
DrahtBot commented 2026-11-17 21:36:32 +00:00 (Migrated from github.com)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36905 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36905.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36905#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. | Type | Reviewers | | ---- | --------- | | ACK | [yancyribbens](https://github.com/bitcoin/bitcoin/pull/36905#issuecomment-6975887313), [Zippo76](https://github.com/bitcoin/bitcoin/pull/36905#pullrequestreview-6047850084) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36255](https://github.com/bitcoin/bitcoin/pull/36255) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36255.svg"></sub> (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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
First-time contributor

No verified findings are available from this review.

Review coverage was incomplete.


Reviewed: e5ee8a965888 · Review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:2b20819f882b885f79f242c01722f54f2a55e27dcfa7d5e13701b4a0b5214248:c39fdacfa3b84799844a5e841c04997e3cd1142653dfad8e36dbb07c4b1e684b --> <!-- ralph-base:e211dd0186e9646167d063a16f0da88e5f72e05f --> <!-- ralph-head:e5ee8a9658888907306feb0af4bfa141ce7eed42 --> <!-- ralph-case:b0b1ad6e20d216b08a925ab452b7215f8aeb46367a46fa3e262764370b7ef739 --> No verified findings are available from this review. Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`e5ee8a965888`](<https://git.fish.foo/bitcoin/bitcoin/commit/e5ee8a9658888907306feb0af4bfa141ce7eed42>) · [Review report](<https://review.fish.foo/report/36905-e5ee8a9658888907306feb0af4bfa141ce7eed42-2771-1.html>) · [Previous reports](<https://review.fish.foo/traces/36905/>) [permalink](<https://review.fish.foo/report/36905-e5ee8a9658888907306feb0af4bfa141ce7eed42-2771-1.html>)
yancyribbens commented 2026-11-18 10:30:31 +00:00 (Migrated from github.com)
ACK e5ee8a9658888907306feb0af4bfa141ce7eed42
Zippo76 (Migrated from github.com) reviewed 2026-11-24 21:40:14 +00:00
Zippo76 (Migrated from github.com) left a comment

tACK e5ee8a9658

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](github.com/bitcoin/bitcoin@e5ee8a9658/src/wallet/test/fuzz/coinselection.cpp (L125)) Assertion fails at [line 202](github.com/bitcoin/bitcoin@e5ee8a9658/src/wallet/test/fuzz/coinselection.cpp (L202))
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](github.com/bitcoin-core/qa-assets@9c61fa724a/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.

tACK e5ee8a9658888907306feb0af4bfa141ce7eed42 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](https://github.com/bitcoin/bitcoin/blob/e5ee8a9658888907306feb0af4bfa141ce7eed42/src/wallet/test/fuzz/coinselection.cpp#L125)](https://github.com/bitcoin/bitcoin/blob/e5ee8a9658888907306feb0af4bfa141ce7eed42/src/wallet/test/fuzz/coinselection.cpp#L125) | Assertion fails at [[line 202](https://github.com/bitcoin/bitcoin/blob/e5ee8a9658888907306feb0af4bfa141ce7eed42/src/wallet/test/fuzz/coinselection.cpp#L202)](https://github.com/bitcoin/bitcoin/blob/e5ee8a9658888907306feb0af4bfa141ce7eed42/src/wallet/test/fuzz/coinselection.cpp#L202) | | 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`. ```sh 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)](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 in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36905
No description provided.