Skip to content

Avoid selecting redundant UTXOs during coin selection#2094

Open
oliv3rdrt wants to merge 1 commit into
mintlayer:masterfrom
oliv3rdrt:fix_redundant_utxo_selection
Open

Avoid selecting redundant UTXOs during coin selection#2094
oliv3rdrt wants to merge 1 commit into
mintlayer:masterfrom
oliv3rdrt:fix_redundant_utxo_selection

Conversation

@oliv3rdrt

Copy link
Copy Markdown

Problem

The wallet's coin selection could pick UTXOs that were not needed. It is easy to see with config-broadcast no, where a transaction ends up spending the largest UTXO together with several smaller ones that are not required.

There were two causes:

  1. In select_coins_srd, once the randomly drawn effective value reached the target, the whole heap was added to the result. If a small UTXO was drawn before a larger one that already covered the target, the small one was included even though it was redundant.

  2. In select_random_coins, results from the different algorithms were compared by waste only. When the waste was equal (which is common), the first result (srd) always won, even if another algorithm selected fewer inputs.

Fix

  1. select_coins_srd now adds only the most valuable groups needed to reach the target (the heap is already ordered by effective value), leaving out the redundant smaller ones.

  2. select_random_coins now breaks waste ties by the number of selected inputs, preferring the result with fewer inputs.

Tests

Added test_srd_solver_omits_redundant_utxos: it gives SRD one large UTXO that already covers the target plus several small ones, and asserts only the large UTXO is selected. The existing selection tests still pass.

Closes #1923

Two things caused the coin selectors to include UTXOs that were not needed:

- In select_coins_srd, once the drawn effective value reached the target the
  whole heap was added to the result, including smaller UTXOs that happened to
  be drawn before a larger one that already covered the target. Only the most
  valuable groups needed to reach the target are added now.

- In select_random_coins the results of the different algorithms were compared
  by waste only, so on a tie the first one (srd) always won even when another
  result selected fewer inputs. Ties are now broken by the number of selected
  inputs.

Add a test that checks srd no longer keeps redundant UTXOs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UTXO selection algorithms select redundant UTXOs

1 participant