Skip to content

Commit e42b5aa

Browse files
committed
refactor(wallet): make filter_utxos return just optional utxos
Manually selected utxos are already available. We don't need a method to hand them out.
1 parent 557a058 commit e42b5aa

File tree

1 file changed

+42
-41
lines changed
  • crates/wallet/src/wallet

1 file changed

+42
-41
lines changed

crates/wallet/src/wallet/mod.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,8 +1417,44 @@ impl Wallet {
14171417

14181418
fee_amount += fee_rate * tx.weight();
14191419

1420-
let (required_utxos, optional_utxos) =
1421-
self.filter_utxos(&params, current_height.to_consensus_u32());
1420+
let optional_utxos = self.filter_utxos(&params, current_height.to_consensus_u32());
1421+
1422+
let local_weighted_utxos = optional_utxos
1423+
// combine optional UTxOs with manually selected UTxOs
1424+
// NOTE: if manually_selected_only is true, then the previous iterator in the chain is
1425+
// empty and only manually selected UTxOs will be considered
1426+
// NOTE: manual selection overrides unspendable
1427+
.chain(params.utxos.clone())
1428+
// transform every UTxO into a WeightedUtxo
1429+
.map(|utxo| {
1430+
let keychain = utxo.keychain;
1431+
WeightedUtxo {
1432+
satisfaction_weight: self
1433+
.public_descriptor(keychain)
1434+
.max_weight_to_satisfy()
1435+
.unwrap(),
1436+
utxo: Utxo::Local(utxo),
1437+
}
1438+
});
1439+
1440+
// include foreign UTxOs
1441+
let all_weighted_utxos = local_weighted_utxos.chain(params.foreign_utxos.clone());
1442+
1443+
let (required_utxos, optional_utxos) = all_weighted_utxos
1444+
// split UTxOs in required and optional
1445+
// if drain_wallet is true, all UTxOs are required, if false, only manually selected
1446+
// and foreign are required
1447+
// TODO: collection could be kept separate from selection
1448+
.partition::<Vec<WeightedUtxo>, _>(|x| {
1449+
params.drain_wallet
1450+
// keep UTxO in required UTxOs if
1451+
|| match x.utxo {
1452+
// it is in manually selected group
1453+
Utxo::Local(ref lo) => params.utxos.contains(lo),
1454+
// it is foreign
1455+
Utxo::Foreign {..} => true,
1456+
}
1457+
});
14221458

14231459
// get drain script
14241460
let mut drain_index = Option::<(KeychainKind, u32)>::None;
@@ -1970,11 +2006,11 @@ impl Wallet {
19702006

19712007
/// Given the options returns the list of utxos that must be used to form the
19722008
/// transaction and any further that may be used if needed.
1973-
fn filter_utxos(
1974-
&self,
1975-
params: &TxParams,
2009+
fn filter_utxos<'a>(
2010+
&'a self,
2011+
params: &'a TxParams,
19762012
current_height: u32,
1977-
) -> (Vec<WeightedUtxo>, Vec<WeightedUtxo>) {
2013+
) -> impl Iterator<Item = LocalOutput> + 'a {
19782014
self
19792015
// get all unspent UTxOs from wallet
19802016
.list_unspent()
@@ -2010,41 +2046,6 @@ impl Wallet {
20102046
}))
20112047
})
20122048
})
2013-
// combine optional UTxOs with manually selected UTxOs
2014-
// NOTE: if manually_selected_only is true, then the previous iterator in the chain is
2015-
// empty and only manually selected UTxOs will be considered
2016-
// NOTE: manual selection overrides unspendable
2017-
.chain(params.utxos.clone())
2018-
// transform every UTxO into a WeightedUtxo
2019-
.map(|utxo| {
2020-
let keychain = utxo.keychain;
2021-
WeightedUtxo {
2022-
satisfaction_weight: self
2023-
.public_descriptor(keychain)
2024-
.max_weight_to_satisfy()
2025-
.unwrap(),
2026-
utxo: Utxo::Local(utxo),
2027-
}
2028-
})
2029-
// include foreign UTxOs
2030-
.chain(params.foreign_utxos.clone())
2031-
// TODO: here filter_utxos could be splitted further into two functions to allow the
2032-
// addition of extra filters for inputs based on properties of the WeightedUtxos
2033-
//
2034-
// split UTxOs in required and optional
2035-
// if drain_wallet is true, all UTxOs are required, if false, only manually selected
2036-
// and foreign are required
2037-
// TODO: collection could be kept separate from selection
2038-
.partition::<Vec<WeightedUtxo>, _>(|x| {
2039-
params.drain_wallet
2040-
// keep UTxO in required UTxOs if
2041-
|| match x.utxo {
2042-
// it is in manually selected group
2043-
Utxo::Local(ref lo) => params.utxos.contains(lo),
2044-
// it is foreign
2045-
Utxo::Foreign {..} => true,
2046-
}
2047-
})
20482049
}
20492050

20502051
fn complete_transaction(

0 commit comments

Comments
 (0)