]> Untitled Git - bdk/commitdiff
[wallet] Make 'unspendable' into a HashSet
authorLLFourn <lloyd.fourn@gmail.com>
Wed, 21 Oct 2020 04:53:55 +0000 (15:53 +1100)
committerLLFourn <lloyd.fourn@gmail.com>
Fri, 23 Oct 2020 02:54:59 +0000 (13:54 +1100)
to avoid awkwardly later on.

src/wallet/mod.rs
src/wallet/tx_builder.rs

index ba4100e3d15b978c64ba464403fb241220caadfb..ac350d45d1fd532bd938ee199d26b87afe57de1c 100644 (file)
@@ -969,14 +969,9 @@ where
         &self,
         change_policy: tx_builder::ChangeSpendPolicy,
         utxo: &Option<Vec<OutPoint>>,
-        unspendable: &Option<Vec<OutPoint>>,
+        unspendable: &HashSet<OutPoint>,
         send_all: bool,
     ) -> Result<(Vec<(UTXO, usize)>, bool), Error> {
-        let unspendable_set = match unspendable {
-            None => HashSet::new(),
-            Some(vec) => vec.iter().collect(),
-        };
-
         let external_weight = self
             .get_descriptor_for_script_type(ScriptType::External)
             .0
@@ -1016,7 +1011,7 @@ where
             // otherwise limit ourselves to the spendable utxos for the selected policy, and the `send_all` setting
             None => {
                 let utxos = self.list_unspent()?.into_iter().filter(|u| {
-                    change_policy.is_satisfied_by(u) && !unspendable_set.contains(&u.outpoint)
+                    change_policy.is_satisfied_by(u) && !unspendable.contains(&u.outpoint)
                 });
 
                 Ok((utxos.map(add_weight).collect(), send_all))
index 65171c3e3031bfa012b0d5c6308c0431f40a78d3..f72bf48d8e09d3a9053cfd8d21713547c430ad43 100644 (file)
@@ -42,6 +42,7 @@
 //! ```
 
 use std::collections::BTreeMap;
+use std::collections::HashSet;
 use std::default::Default;
 use std::marker::PhantomData;
 
@@ -63,7 +64,7 @@ pub struct TxBuilder<D: Database, Cs: CoinSelectionAlgorithm<D>> {
     pub(crate) fee_policy: Option<FeePolicy>,
     pub(crate) policy_path: Option<BTreeMap<String, Vec<usize>>>,
     pub(crate) utxos: Option<Vec<OutPoint>>,
-    pub(crate) unspendable: Option<Vec<OutPoint>>,
+    pub(crate) unspendable: HashSet<OutPoint>,
     pub(crate) sighash: Option<SigHashType>,
     pub(crate) ordering: TxOrdering,
     pub(crate) locktime: Option<u32>,
@@ -197,7 +198,7 @@ impl<D: Database, Cs: CoinSelectionAlgorithm<D>> TxBuilder<D, Cs> {
     /// [`TxBuilder::add_utxo`] have priority over these. See the docs of the two linked methods
     /// for more details.
     pub fn unspendable(mut self, unspendable: Vec<OutPoint>) -> Self {
-        self.unspendable = Some(unspendable);
+        self.unspendable = unspendable.into_iter().collect();
         self
     }
 
@@ -207,7 +208,7 @@ impl<D: Database, Cs: CoinSelectionAlgorithm<D>> TxBuilder<D, Cs> {
     /// [`TxBuilder::add_utxo`] have priority over this. See the docs of the two linked methods
     /// for more details.
     pub fn add_unspendable(mut self, unspendable: OutPoint) -> Self {
-        self.unspendable.get_or_insert(vec![]).push(unspendable);
+        self.unspendable.insert(unspendable);
         self
     }