I haven’t written a test for this, so all the following is not verified and just my guess based on the code understanding.
This is not a regression in this particular PR, but this could lead to misleading errors in a situation when a proper solution exists, but our coin selection failed to find it and only found solution that exceeded weight. Let’s imagine following scenario:
- UTXO pool: two big coins 50BTC, a ton of 0.001BTC.
- Target amount: 90BTC
- Proper solution: just take two big coins and create a change output.
I think none of the coin selection algorithms will be able to reliably find the proper solution.
- BnB won’t be able to find it, because proper solution includes change.
- Knapsack again optimises for the match, therefore it discards the proper solution in favour of a closer match that exceeds the weight limit.
- SRD misses proper solution with 75% chance, because it checks each coin exactly once. If it skipped at least one of two big coins it will never find the proper solution.
No wait, have you checked the introduced SRD changes? https://github.com/bitcoin/bitcoin/pull/26720/commits/fd8cf58d736ed9614f46b4f5b3c92f71ff9c46d4.
The algorithm now uses a min-effective-value heap; it discards the least valuable input/s
while the selected weight exceeds the maximum allowed weight.
So in your scenario, SRD will always find a solution.
Basically, it adds coins to a heap while the target is not met. So, if the max weight is exceeded, it pops the least valuable coin and adds the new one. Then keeps walking-through the coins vector until (1) target is covered, or (2) fail due an insufficient funds or (3) fail due having enough funds but the max allowed weight was exceeded.
I think we should a) make the message more explicit about the fact that solution might exist, but we just failed to find one b) propose users to select coins manually as another remedy c) better document the shortcomings of our coin selection.
I think that you are only having in mind part of the process and maybe not contemplating the expanded coin eligibility process.
If no solution was found in the first selection attempt round, we are running the selection algorithms multiple times. Not only once.
An edge case would be that the coins that are needed to create a valid selection are in the last coin eligibility filter, but even in that situation, SRD will find a solution.
Still, let’s say that also SRD doesn’t return a result for some reason. And we don’t find a solution even when the user has enough funds. The solution shouldn’t be placed here.
It can be placed at the end of AutomaticCoinSelection
, because there we know the total available balance (and also the discarded coins, which since acf0119d24c9f8fae825093249a46cd38e4a3a91 we are also contemplating) so we can easily return a “you have enough funds but the wallet coin selection process failed, please select coins manually” instead of the general “Insufficient Funds”.
Side note:
As this PR isn’t conflicting with master, I haven’t rebase it. So, there are improvements like the discarded coins early return (due the coin eligibility filtering process) that are not here.