Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- **Breaking:** `BnbMetric` metrics now decide the change output themselves. The trait gains a `drain(&mut self, cs) -> Drain` method; call it on a branch-and-bound solution (or the `LowestFee` metric directly) to get the change output the metric optimized against, instead of computing a separate `ChangePolicy`.
- **Breaking:** `CoinSelector::run_bnb` now returns `(Ordf64, Drain)` instead of just `Ordf32`, handing back the change output the metric decided on for the winning selection.
- **Breaking:** `LowestFee` no longer takes a `change_policy`. It now takes `dust_relay_feerate: FeeRate` and `drain_weights: DrainWeights`, and adds change only when doing so lowers the long-term fee and the change would not be dust.
- Add `CoinSelector::clear_selection`, which deselects every candidate while keeping the bans and the candidate order.
- Add `DrainWeights::dust_threshold(dust_relay_feerate)`, the minimum value a change output with these weights must have to not be dust.
- Add `CoinSelector::select_srd`, a Single Random Draw selector (port of Bitcoin Core's `SelectCoinsSRD`) that adds candidates in random order until the change reaches `change_lower`, producing a healthy-sized (privacy-friendly) change output instead of minimizing fees. Adds the `CHANGE_LOWER` constant for Core's value.
- **Breaking:** `Changeless` is now `Changeless<M>`, wrapping an inner metric it constrains to changeless solutions (e.g. `Changeless<LowestFee>`), replacing the previous tuple-composition approach.
Expand Down
4 changes: 4 additions & 0 deletions src/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ impl Bitset {
was_set
}

pub(crate) fn clear(&mut self) {
self.words.fill(0);
}

/// Iterate over set bits in ascending index order.
pub fn iter(&self) -> BitsetIter<'_> {
BitsetIter {
Expand Down
5 changes: 5 additions & 0 deletions src/coin_selector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ impl<'a> CoinSelector<'a> {
self.selected.remove(index)
}

/// Deselect every candidate. Bans and the candidate order are kept.
pub fn clear_selection(&mut self) {
self.selected.clear();
}

/// Convienince method to pick elements of a slice by the indexes that are currently selected.
/// Obviously the slice must represent the inputs ordered in the same way as when they were
/// passed to `Candidates::new`.
Expand Down
26 changes: 26 additions & 0 deletions tests/coin_selector.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use bdk_coin_select::{Candidate, CoinSelector, Target, TargetFee, TargetOutputs};

#[test]
fn clear_selection_keeps_bans_and_order() {
let candidates = [100_000, 300_000, 200_000, 400_000, 50_000]
.iter()
.map(|&value| Candidate::new_tr_keyspend(value))
.collect::<Vec<_>>();
let target = Target {
fee: TargetFee::default(),
outputs: TargetOutputs::fund_outputs([(43 * 4, 150_000)]),
max_weight: None,
};
let mut cs = CoinSelector::new(&candidates, target);
cs.sort_candidates_by_key(|(_, c)| core::cmp::Reverse(c.value));
cs.ban(3);
cs.select(1);
cs.select(4);

cs.clear_selection();

assert!(cs.is_empty());
assert!(cs.selected_indices().is_empty());
assert_eq!(cs.banned().iter().collect::<Vec<_>>(), [3]);
assert_eq!(cs.unselected_indices().collect::<Vec<_>>(), [1, 2, 0, 4]);
}
Loading