From 3ba1a4f232261fcff65a0e62527dcb396c952e38 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Fri, 30 Oct 2026 11:11:16 +1100 Subject: [PATCH] feat: add CoinSelector::clear_selection --- CHANGELOG.md | 1 + src/bitset.rs | 4 ++++ src/coin_selector.rs | 5 +++++ tests/coin_selector.rs | 26 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 tests/coin_selector.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index dc5408d..1a7739d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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`, wrapping an inner metric it constrains to changeless solutions (e.g. `Changeless`), replacing the previous tuple-composition approach. diff --git a/src/bitset.rs b/src/bitset.rs index b357db2..5041b37 100644 --- a/src/bitset.rs +++ b/src/bitset.rs @@ -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 { diff --git a/src/coin_selector.rs b/src/coin_selector.rs index 049825f..0e58b42 100644 --- a/src/coin_selector.rs +++ b/src/coin_selector.rs @@ -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`. diff --git a/tests/coin_selector.rs b/tests/coin_selector.rs new file mode 100644 index 0000000..e9877e6 --- /dev/null +++ b/tests/coin_selector.rs @@ -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::>(); + 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::>(), [3]); + assert_eq!(cs.unselected_indices().collect::>(), [1, 2, 0, 4]); +}