Skip to content

Commit e297133

Browse files
committed
drop dependencies indexmap
1 parent 10fe35a commit e297133

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

wallet/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ bdk_chain = { version = "0.23.0", features = [ "miniscript", "serde" ], default-
2626
# Optional dependencies
2727
bip39 = { version = "2.0", optional = true }
2828
bdk_file_store = { version = "0.21.0", optional = true }
29-
indexmap = "2.9.0"
3029

3130
[features]
3231
default = ["std"]

wallet/src/wallet/mod.rs

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1511,7 +1511,7 @@ impl Wallet {
15111511

15121512
let (required_utxos, optional_utxos) = {
15131513
// NOTE: manual selection overrides unspendable
1514-
let mut required: Vec<WeightedUtxo> = params.utxos.values().cloned().collect();
1514+
let mut required: Vec<WeightedUtxo> = params.utxos.order_weighted_utxos();
15151515
let optional = self.filter_utxos(&params, current_height.to_consensus_u32());
15161516

15171517
// if drain_wallet is true, all UTxOs are required
@@ -1785,7 +1785,7 @@ impl Wallet {
17851785
}
17861786
})
17871787
})
1788-
.collect::<Result<indexmap::IndexMap<OutPoint, WeightedUtxo>, BuildFeeBumpError>>()?;
1788+
.collect::<Result<OrderUtxos, BuildFeeBumpError>>()?;
17891789

17901790
if tx.output.len() > 1 {
17911791
let mut change_index = None;
@@ -2574,6 +2574,68 @@ impl Wallet {
25742574
}
25752575
}
25762576

2577+
#[derive(Debug, Default, Clone)]
2578+
pub(crate) struct OrderUtxos {
2579+
utxos: Vec<OutPoint>,
2580+
utxos_map: HashMap<OutPoint, WeightedUtxo>,
2581+
}
2582+
2583+
impl Deref for OrderUtxos {
2584+
type Target = HashMap<OutPoint, WeightedUtxo>;
2585+
2586+
fn deref(&self) -> &Self::Target {
2587+
&self.utxos_map
2588+
}
2589+
}
2590+
2591+
impl OrderUtxos {
2592+
fn insert(&mut self, outpoint: OutPoint, weighted_utxo: WeightedUtxo) -> Option<WeightedUtxo> {
2593+
let v = self.utxos_map.insert(outpoint, weighted_utxo);
2594+
if v.is_none() {
2595+
self.utxos.push(outpoint);
2596+
}
2597+
debug_assert!(self.utxos.len() == self.utxos_map.len());
2598+
v
2599+
}
2600+
2601+
fn order_weighted_utxos(&self) -> Vec<WeightedUtxo> {
2602+
self.utxos
2603+
.iter()
2604+
.map(|outpoint| self.utxos_map.get(outpoint).cloned().unwrap())
2605+
.collect::<Vec<_>>()
2606+
}
2607+
}
2608+
2609+
impl FromIterator<(OutPoint, WeightedUtxo)> for OrderUtxos {
2610+
fn from_iter<T: IntoIterator<Item = (OutPoint, WeightedUtxo)>>(iter: T) -> Self {
2611+
let mut r = OrderUtxos::default();
2612+
for (outpoint, weighted_utxo) in iter {
2613+
r.insert(outpoint, weighted_utxo);
2614+
}
2615+
r
2616+
}
2617+
}
2618+
2619+
impl IntoIterator for OrderUtxos {
2620+
type Item = (OutPoint, WeightedUtxo);
2621+
type IntoIter = std::vec::IntoIter<Self::Item>;
2622+
fn into_iter(mut self) -> Self::IntoIter {
2623+
self.utxos
2624+
.into_iter()
2625+
.map(|outpoint| (outpoint, self.utxos_map.remove(&outpoint).unwrap()))
2626+
.collect::<Vec<_>>()
2627+
.into_iter()
2628+
}
2629+
}
2630+
2631+
impl Extend<(OutPoint, WeightedUtxo)> for OrderUtxos {
2632+
fn extend<T: IntoIterator<Item = (OutPoint, WeightedUtxo)>>(&mut self, iter: T) {
2633+
for (outpoint, weighted_utxo) in iter {
2634+
self.insert(outpoint, weighted_utxo);
2635+
}
2636+
}
2637+
}
2638+
25772639
impl AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationBlockTime>> for Wallet {
25782640
fn as_ref(&self) -> &bdk_chain::tx_graph::TxGraph<ConfirmationBlockTime> {
25792641
self.indexed_graph.graph()

wallet/src/wallet/tx_builder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use super::coin_selection::CoinSelectionAlgorithm;
5353
use super::utils::shuffle_slice;
5454
use super::{CreateTxError, Wallet};
5555
use crate::collections::{BTreeMap, HashSet};
56-
use crate::{KeychainKind, LocalOutput, Utxo, WeightedUtxo};
56+
use crate::{KeychainKind, LocalOutput, OrderUtxos, Utxo, WeightedUtxo};
5757

5858
/// A transaction builder
5959
///
@@ -126,7 +126,7 @@ pub(crate) struct TxParams {
126126
pub(crate) fee_policy: Option<FeePolicy>,
127127
pub(crate) internal_policy_path: Option<BTreeMap<String, Vec<usize>>>,
128128
pub(crate) external_policy_path: Option<BTreeMap<String, Vec<usize>>>,
129-
pub(crate) utxos: indexmap::IndexMap<OutPoint, WeightedUtxo>,
129+
pub(crate) utxos: OrderUtxos,
130130
pub(crate) unspendable: HashSet<OutPoint>,
131131
pub(crate) manually_selected_only: bool,
132132
pub(crate) sighash: Option<psbt::PsbtSighashType>,
@@ -297,7 +297,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
297297
)
298298
})
299299
})
300-
.collect::<Result<indexmap::IndexMap<OutPoint, WeightedUtxo>, AddUtxoError>>()?;
300+
.collect::<Result<OrderUtxos, AddUtxoError>>()?;
301301
self.params.utxos.extend(utxo_batch);
302302

303303
Ok(self)

0 commit comments

Comments
 (0)