refactor: add function to filter coins for selection
This commit is contained in:
parent
934d0c76f4
commit
0514f743da
@ -68,6 +68,29 @@ pub trait Step {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Filter the coins that should be available for selection.
|
||||||
|
///
|
||||||
|
/// `selected` will be `None` if the coins are being filtered for the first time.
|
||||||
|
/// In that case, no coins will be selected.
|
||||||
|
fn filter_coins(coins: &[Coin], selected: Option<HashSet<OutPoint>>) -> Vec<(Coin, bool)> {
|
||||||
|
coins
|
||||||
|
.iter()
|
||||||
|
.filter_map(|c| {
|
||||||
|
if c.spend_info.is_none() && !c.is_immature {
|
||||||
|
Some((
|
||||||
|
c.clone(),
|
||||||
|
selected
|
||||||
|
.as_ref()
|
||||||
|
.map(|sel| sel.contains(&c.outpoint))
|
||||||
|
.unwrap_or(false),
|
||||||
|
))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
pub struct DefineSpend {
|
pub struct DefineSpend {
|
||||||
recipients: Vec<Recipient>,
|
recipients: Vec<Recipient>,
|
||||||
/// If set, this is the index of a recipient that should
|
/// If set, this is the index of a recipient that should
|
||||||
@ -100,16 +123,7 @@ impl DefineSpend {
|
|||||||
coins: &[Coin],
|
coins: &[Coin],
|
||||||
timelock: u16,
|
timelock: u16,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let coins: Vec<(Coin, bool)> = coins
|
let coins = filter_coins(coins, None);
|
||||||
.iter()
|
|
||||||
.filter_map(|c| {
|
|
||||||
if c.spend_info.is_none() && !c.is_immature {
|
|
||||||
Some((c.clone(), false))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
network,
|
network,
|
||||||
@ -563,17 +577,7 @@ impl Step for DefineSpend {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
self.coins = coins
|
self.coins = filter_coins(&coins, Some(selected));
|
||||||
.into_iter()
|
|
||||||
.filter_map(|coin| {
|
|
||||||
if coin.spend_info.is_none() && !coin.is_immature {
|
|
||||||
let selected = selected.contains(&coin.outpoint);
|
|
||||||
Some((coin, selected))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
self.sort_coins(cache.blockheight as u32);
|
self.sort_coins(cache.blockheight as u32);
|
||||||
// In case some selected coins are not spendable anymore and
|
// In case some selected coins are not spendable anymore and
|
||||||
// new coins make more sense to be selected. A redraft is triggered
|
// new coins make more sense to be selected. A redraft is triggered
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user