[wallet] Replace ChangeSpendPolicy::filter_utxos with a predicate

To make composing it with other filtering conditions easier.
This commit is contained in:
LLFourn 2020-10-21 15:53:00 +11:00
parent 365a91f805
commit c549281ace
No known key found for this signature in database
GPG Key ID: A27093B54DA11F65
2 changed files with 20 additions and 16 deletions

View File

@ -1015,16 +1015,11 @@ where
} }
// otherwise limit ourselves to the spendable utxos for the selected policy, and the `send_all` setting // otherwise limit ourselves to the spendable utxos for the selected policy, and the `send_all` setting
None => { None => {
let utxos = self.list_unspent()?.into_iter(); let utxos = self.list_unspent()?.into_iter().filter(|u| {
let utxos = change_policy.filter_utxos(utxos).into_iter(); change_policy.is_satisfied_by(u) && !unspendable_set.contains(&u.outpoint)
});
Ok(( Ok((utxos.map(add_weight).collect(), send_all))
utxos
.filter(|u| !unspendable_set.contains(&u.outpoint))
.map(add_weight)
.collect(),
send_all,
))
} }
} }
} }

View File

@ -397,11 +397,11 @@ impl Default for ChangeSpendPolicy {
} }
impl ChangeSpendPolicy { impl ChangeSpendPolicy {
pub(crate) fn filter_utxos<I: Iterator<Item = UTXO>>(&self, iter: I) -> Vec<UTXO> { pub(crate) fn is_satisfied_by(&self, utxo: &UTXO) -> bool {
match self { match self {
ChangeSpendPolicy::ChangeAllowed => iter.collect(), ChangeSpendPolicy::ChangeAllowed => true,
ChangeSpendPolicy::OnlyChange => iter.filter(|utxo| utxo.is_internal).collect(), ChangeSpendPolicy::OnlyChange => utxo.is_internal,
ChangeSpendPolicy::ChangeForbidden => iter.filter(|utxo| !utxo.is_internal).collect(), ChangeSpendPolicy::ChangeForbidden => !utxo.is_internal,
} }
} }
} }
@ -512,7 +512,10 @@ mod test {
#[test] #[test]
fn test_change_spend_policy_default() { fn test_change_spend_policy_default() {
let change_spend_policy = ChangeSpendPolicy::default(); let change_spend_policy = ChangeSpendPolicy::default();
let filtered = change_spend_policy.filter_utxos(get_test_utxos().into_iter()); let filtered = get_test_utxos()
.into_iter()
.filter(|u| change_spend_policy.is_satisfied_by(u))
.collect::<Vec<_>>();
assert_eq!(filtered.len(), 2); assert_eq!(filtered.len(), 2);
} }
@ -520,7 +523,10 @@ mod test {
#[test] #[test]
fn test_change_spend_policy_no_internal() { fn test_change_spend_policy_no_internal() {
let change_spend_policy = ChangeSpendPolicy::ChangeForbidden; let change_spend_policy = ChangeSpendPolicy::ChangeForbidden;
let filtered = change_spend_policy.filter_utxos(get_test_utxos().into_iter()); let filtered = get_test_utxos()
.into_iter()
.filter(|u| change_spend_policy.is_satisfied_by(u))
.collect::<Vec<_>>();
assert_eq!(filtered.len(), 1); assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].is_internal, false); assert_eq!(filtered[0].is_internal, false);
@ -529,7 +535,10 @@ mod test {
#[test] #[test]
fn test_change_spend_policy_only_internal() { fn test_change_spend_policy_only_internal() {
let change_spend_policy = ChangeSpendPolicy::OnlyChange; let change_spend_policy = ChangeSpendPolicy::OnlyChange;
let filtered = change_spend_policy.filter_utxos(get_test_utxos().into_iter()); let filtered = get_test_utxos()
.into_iter()
.filter(|u| change_spend_policy.is_satisfied_by(u))
.collect::<Vec<_>>();
assert_eq!(filtered.len(), 1); assert_eq!(filtered.len(), 1);
assert_eq!(filtered[0].is_internal, true); assert_eq!(filtered[0].is_internal, true);