Skip to content

UTXO selection algorithms select redundant UTXOs #1923

@ImplOfAnImpl

Description

@ImplOfAnImpl

This is especially noticeable/reproducible in the config-broadcast no mode (introduced in #1904), in which the new transaction's summary is printed to the console without it being broadcast to the network.

E.g. when trying to create a new transaction I can see that my biggest UTXO (100k TML) is selected, but it's also accompanied by smaller UTXOs, which should not be needed.

There seem to be multiple reasons for this:

  1. The selection algirithm to blame seems to be srd; in select_coins_srd we randomly select UTXOs, populating a heap, and in the end do:

    if selected_eff_value >= target_value {
        // Result found, add it.
        while let Some(group) = heap.pop() {
            result.add_input(&group.0, pay_fees)?;
        }
        return Ok(result);
    }
    

    So the reason for the smaller UTXO being initially selected is that the large UTXO was selected later. But then the above-mentioned code shouldn't put the entire heap into the result; only the top items necessary to meet the target should be put there.

  2. In select_random_coins, several algoriths (like select_coins_srd) are executed and the best result is taken. And the best result selection is currently implemented as

    results.into_iter().min_by_key(|res| res.waste)
    

    In my case, the waste value was the same for all algorithms; and since select_coins_srd is executed first, it was always winning.
    So:

    • probably the best result should be selected first by waste, but if it's equal then the UTXO count should be taken into account as well.
    • perhaps waste calculation can be improved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    walletEverything related to the node wallets (whether GUI or CLI)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions