Repository navigation
Refactor CoinGrinder tests - #36255
murchandamus wants to merge 11 commits into
Conversation
The `is_eff_value` parameter is never used with a non-default value, so moving it to the back of the parameters allows dropping the mention of its default value in many instances.
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. LLM Linter (✨ experimental)Possible typos and grammar issues:
2026-09-14 22:56:14 |
|
Made available as a PR, because I was discussing it with a colleague who wanted to provide feedback, but there are still a couple minor things that I want to tweak anyway. |
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
l0rinc
left a comment
There was a problem hiding this comment.
Concept ACK
I left a few suggestions to make the test categories independently runnable (each one exercising multiple scenarios, grouping by category), untangling the separate test cases and simplified the helpers, assertions, and amount expressions.
I've implemented them in https://github.com/l0rinc/bitcoin/pull/297/commits (see last commit) - feel free to pick and choose and split and fold them back into your commits if you agree with the direction.
Note that the CI failure seems unrelated.
| @@ -229,7 +229,7 @@ BOOST_AUTO_TEST_CASE(bnb_exhaustion_with_solution_test) | |||
| // A hard case with no exact-match solution: BnB must still report that the algorithm did not complete once the | |||
| // search is pushed into the attempt limit, even though it finds a solution within cost_of_change of the target. | |||
| for (size_t i = 0; i < 19; ++i) { | |||
| utxo_pool.push_back(MakeCoin(100'000 + i, /*is_eff_value=*/true, default_cs_params)); | |||
There was a problem hiding this comment.
9a09643 refactor: Move
is_eff_valuein MakeCoin to back:
Here we can remove the default_cs_params as well now, right?
| * Two results are equivalent if they are composed of the same input values, even if they have different inputs (i.e., same value, different prevout) */ | ||
| static bool HaveEquivalentValues(const SelectionResult& a, const SelectionResult& b) | ||
| * Two results are equivalent if they are composed of inputs with the same amounts and sizes, even if they have different inputs (i.e., different prevouts). */ | ||
| static bool HaveEquivalentInputs(const SelectionResult& a, const SelectionResult& b) |
There was a problem hiding this comment.
29b47fb tests: Check amount and size of equivalent inputs:
The previous implementation was indeed hard to understand.
This is basically a permutation check of a subset of fields, and since we have very few elements here (given it's test code), we could either simplify further to:
static bool HaveEquivalentInputs(const SelectionResult& a, const SelectionResult& b)
{
return std::ranges::is_permutation(a.GetInputSet(), b.GetInputSet(), [](auto& x, auto& y) {
return x->txout.nValue == y->txout.nValue && x->input_bytes == y->input_bytes;
});
}or if you don't find this readable we could extract the common parts that prepare the final comparable objects and compare those directly at the call site:
/** The (amount, input size) pairs of a result's inputs in sorted order, which identify a selection regardless of the prevouts of its inputs. */
static std::vector<std::pair<CAmount, int>> SortedInputs(const SelectionResult& result)
{
std::vector<std::pair<CAmount, int>> inputs;
for (auto& coin : result.GetInputSet()) {
inputs.emplace_back(coin->txout.nValue, coin->input_bytes);
}
std::ranges::sort(inputs);
return inputs;
}and in the assertion we can have
BOOST_TEST(SortedInputs(*result) == SortedInputs(expected_result));directly.
| @@ -60,7 +60,7 @@ static CoinSelectionParams init_cs_params(int eff_feerate = 5000) | |||
| static const CoinSelectionParams default_cs_params = init_cs_params(); | |||
|
|
|||
| /** Make one OutputGroup with a single UTXO that either has a given effective value (default) or a given amount (`is_eff_value = false`). */ | |||
| static OutputGroup MakeCoin(const CAmount& amount, bool is_eff_value = true, CoinSelectionParams cs_params = default_cs_params, int custom_spending_vsize = P2WPKH_INPUT_VSIZE) | |||
| static OutputGroup MakeCoin(const CAmount& amount, CoinSelectionParams cs_params = default_cs_params, int custom_spending_vsize = P2WPKH_INPUT_VSIZE, bool is_eff_value = true) | |||
There was a problem hiding this comment.
9a09643 refactor: Move
is_eff_valuein MakeCoin to back:
Since every caller uses effective values, could we remove is_eff_value altogether?
| TestBnBSuccess("Prefer two light inputs over two heavy inputs at high feerates", high_feerate_pool, /*selection_target=*/13 * CENT, /*expected_input_amounts=*/{3 * CENT, 10 * CENT}, /*expected_attempts=*/9, high_feerate_params); | ||
| } | ||
|
|
||
| static void TestCGFail(std::string test_title, std::vector<OutputGroup>& utxo_pool, const CAmount& selection_target) |
There was a problem hiding this comment.
c147d3e test: migrate CoinGrinder insufficient funds coverage:
Assertions inside these helpers report the helper's source line, so test_title has to identify the individual scenario. Could we organize the scenarios into named Boost test cases and share the repeated assertions through small macros, as in the headers-sync tests? That would keep failure locations at each call site and make the test categories independently runnable. Cases with the same setup and checks can share a small table of inputs and expectations, while cases with different assertion flows can remain separate.
|
|
||
| BOOST_CHECK_MESSAGE(result, "Falsy result in CoinGrinder-Success: " + test_title); | ||
| BOOST_CHECK_MESSAGE(HaveEquivalentInputs(expected_result, *result), strprintf("Result mismatch in CoinGrinder-Success: %s. Expected %s, but got %s", test_title, InputAmountsToString(expected_result), InputAmountsToString(*result))); | ||
| BOOST_CHECK_MESSAGE(result->GetSelectedValue() == expected_amount, strprintf( "Selected amount mismatch in CoinGrinder-Success: %s. Expected %d, but got %d", test_title, expected_amount, result->GetSelectedValue())); |
There was a problem hiding this comment.
a3c07a5 test: migrate CoinGrinder bounded weight selection coverage:
HaveEquivalentInputs already compares the input values, and GetSelectedValue() just sums them. Could we drop expected_amount, its accumulation, and this assertion? For the remaining scalar checks, Boost already prints the operands:
BOOST_CHECK_LE(result->GetWeight(), max_selection_weight);
BOOST_CHECK_EQUAL(result->GetSelectionsEvaluated(), expected_attempts);I would keep the weight check because GetWeight() reads separately maintained state. The same simplifications apply to the BnB helper.
| { | ||
| { | ||
| std::vector<OutputGroup> utxo_pool; | ||
| AddDuplicateCoins(utxo_pool, /*count=*/60, /*amount=*/0.33 * COIN); |
There was a problem hiding this comment.
a3c07a5 test: migrate CoinGrinder bounded weight selection coverage:
Could we use integer amounts throughout the migrated tests, e.g. 33 * CENT here and 2'533 * CENT for the target below? This expresses the intended satoshi amounts directly and avoids floating-point multiplication followed by conversion to CAmount.
| utxo_pool.push_back(MakeCoin(amount, /*custom_spending_vsize=*/100)); | ||
| } | ||
| for (int j = 0; j < 100; ++j) { | ||
| utxo_pool.push_back(MakeCoin(8 * COIN, /*custom_spending_vsize=*/1000)); |
There was a problem hiding this comment.
1d626e5 test: migrate CoinGrinder cloned input coverage:
Could AddDuplicateCoins take an optional input size so these four loops become four calls to the existing helper? We could also build expected_inputs once and initialize utxo_pool from it before adding the heavier clones, which avoids constructing the four expected coins twice.
| } | ||
| } | ||
|
|
||
| /** Check if SelectionResult a is equivalent to SelectionResult b. | ||
| * Two results are equivalent if they are composed of the same input values, even if they have different inputs (i.e., same value, different prevout) */ | ||
| static bool HaveEquivalentValues(const SelectionResult& a, const SelectionResult& b) | ||
| * Two results are equivalent if they are composed of inputs with the same amounts and sizes, even if they have different inputs (i.e., different prevouts). */ |
There was a problem hiding this comment.
| * Two results are equivalent if they are composed of inputs with the same amounts and sizes, even if they have different inputs (i.e., different prevouts). */ | |
| * Two results are equivalent if they are composed of inputs with the same amounts and sizes, even if they are composed from different outputs (i.e., different prevouts). */ |
| std::vector<OutputGroup> utxo_pool; | ||
| AddDuplicateCoins(utxo_pool, /*count=*/10, /*amount=*/1 * COIN); | ||
| AddDuplicateCoins(utxo_pool, /*count=*/10, /*amount=*/2 * COIN); | ||
| TestCGFail("Exceed max weight", utxo_pool, /*selection_target=*/29.5L * COIN, /*max_selection_weight=*/1000, /*expect_max_weight_exceeded=*/true); |
There was a problem hiding this comment.
This is a strange max_selection_weight. It would be better to use real values, like max transaction weight of of 400_000 I think, although that's unrelated to this change.
| // 7) Test that lots of tiny UTXOs can be skipped if they are too heavy while there are enough funds in lookahead | ||
| // ################################################################################################################# | ||
| CAmount target = 1.9L * COIN; | ||
| int max_selection_weight = 40000; // WU |
There was a problem hiding this comment.
Unrelated to this change, but why choose a set and target of 40000? I had thought at first this was 400000 which would make more sense, so I wonder if this was a typo.
|
Concept ACK. Thanks for following up on this. It would be nice to see the coinselector test file removed entirely in the future since having multiple test files has caused confusion. Also minor nit, but your older commit messages have different capitalization than your newer messages. |
| @@ -312,7 +485,7 @@ BOOST_AUTO_TEST_CASE(srd_test) | |||
| // Create UTXO pool with UTXOs of same effective value but different weights | |||
| std::vector<OutputGroup> mixed_weight_pool; | |||
| AddDuplicateCoins(mixed_weight_pool, /*count=*/100, /*amount=*/5 * CENT, cs_params); | |||
| mixed_weight_pool.push_back(MakeCoin(5 * CENT, true, cs_params, /*custom_spending_vsize=*/P2WPKH_INPUT_VSIZE - 1)); | |||
| mixed_weight_pool.push_back(MakeCoin(5 * CENT, cs_params, /*custom_spending_vsize=*/P2WPKH_INPUT_VSIZE - 1)); | |||
| TestSRDSuccess("Tie-break same effective value with lower weight", utxo_pool, /*selection_target=*/9 * CENT, cs_params, | |||
There was a problem hiding this comment.
TestSRDSuccess calls utxo_pool instead of the mixed_weight_pool it builds. As a result, the tie-break test passes even when #33223 is reverted. There's nothing checking the tie-break. Also coin eviction never happens because the current max weight (804 WU) fits any two coins (544 WU).
There was a problem hiding this comment.
Yes, that looks like an oversight in the test. Although, I think it would be better addressed with a separate PR since this PR is specific to coin-grinder, and the test in question is for SRD. Also, that line is not touched by this PR.
There was a problem hiding this comment.
Looking a little closer at the test, I think there is more wrong than simply it should be mixed_weight_pool instead of utxo_pool. We want to test that the larger UTXO is evicted from the selection since all effective_values are tied. However, since the target only takes two utxos to reach, it is unlikely to hold the smaller weight utxo in the selection to be evicted. I would also have expected a sort of test to assert the resulting utxos on success. I'll let @murchandamus reply though before going further since maybe i'm missing some simple typo that would make the test work as expected.
There was a problem hiding this comment.
Yes, that looks like an oversight in the test. Although, I think it would be better addressed with a separate PR since this PR is specific to coin-grinder, and the test in question is for SRD. Also, that line is not touched by this PR.
sure, I have a draft; I wanted to check before opening a PR
There was a problem hiding this comment.
I will take a look if you want to open a PR to fix the test. It might take murch a while to respond.

This PR migrates the CoinGrinder tests from
coinselector_tests.cppto the new coin selection test suitecoinselection_tests.cppwhere we use effective values, actual feerates, and avoid setting coin selection parameters that never appear in production.Most of the tests are exact replications of the originals in the new framework, just the CoinGrinder exhaustion test was slightly refactored to simplify: it was using different weights for every single UTXO, but just two different weights are sufficient to replicate the relevant behavior for the test.
Each commit migrates one test to simplify review.