]> Untitled Git - bdk/commit
refactor(wallet): use iterators and adaptors in preselect_utxos
authornymius <155548262+nymius@users.noreply.github.com>
Mon, 13 Jan 2025 15:40:13 +0000 (12:40 -0300)
committernymius <155548262+nymius@users.noreply.github.com>
Wed, 5 Mar 2025 23:47:35 +0000 (10:47 +1100)
commit79bd7da87603c8dfe65bee4bd7061c56e39d0953
tree3244b083582d09cdd37294a83e806d1185225777
parent362c3dc85d469efbfe2046e7b02f9ca8ff64cb03
refactor(wallet): use iterators and adaptors in preselect_utxos

There were multiple calls for de-duplication of selected UTxOs.

As the test `test_filter_duplicates` shows, there are four possible
cases for duplication of UTxOs while feeding the coin selection
algorithms.

1. no duplication: out of concern
2. duplication in the required utxos only: covered by the source of
   `required_utxos`, `Wallet::list_unspent`, which roots back the
   provided `UTxOs` to a `HashMap` which should avoid any duplication by
   definition
3. duplication in the optional utxos only: is the only one possible as
   optional `UTxOs` are stored in a `Vec` and no checks are performed
   about the duplicity of their members.
4. duplication across the required and optional utxos: is already
   covered by `Wallet::preselect_utxos`, which avoid the processing of
   required UTxOs while listing the unspent available UTxOs in the
   wallet.

This refactor changes:
- `TxParams::utxos` type to be `HashSet<LocalOutput>` avoiding the
  duplication case 3, and allowing a query closer to O(1) on avg. to
  cover duplication case 4 (before was O(n) where n is the size of
  required utxos).
- Moves the computation of the `WeightedUtxos` to the last part of UTxO
  filtering, allowing the unification of the computation for local
  outputs.
- Removes some extra allocations done for helpers structures or
  intermediate results while filtering UTxOs.
- Allows for future integration of UTxO filtering methods for other
  utilities.
- Adds more comments for each filtering step.

With these changes all four cases would be covered, and
`coin_selection::filter_duplicates` would be no longer needed.
crates/wallet/src/wallet/mod.rs
crates/wallet/src/wallet/tx_builder.rs