@@ -1511,7 +1511,7 @@ impl Wallet {
1511
1511
1512
1512
let ( required_utxos, optional_utxos) = {
1513
1513
// 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 ( ) ;
1515
1515
let optional = self . filter_utxos ( & params, current_height. to_consensus_u32 ( ) ) ;
1516
1516
1517
1517
// if drain_wallet is true, all UTxOs are required
@@ -1785,7 +1785,7 @@ impl Wallet {
1785
1785
}
1786
1786
} )
1787
1787
} )
1788
- . collect :: < Result < indexmap :: IndexMap < OutPoint , WeightedUtxo > , BuildFeeBumpError > > ( ) ?;
1788
+ . collect :: < Result < OrderUtxos , BuildFeeBumpError > > ( ) ?;
1789
1789
1790
1790
if tx. output . len ( ) > 1 {
1791
1791
let mut change_index = None ;
@@ -2574,6 +2574,68 @@ impl Wallet {
2574
2574
}
2575
2575
}
2576
2576
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
+
2577
2639
impl AsRef < bdk_chain:: tx_graph:: TxGraph < ConfirmationBlockTime > > for Wallet {
2578
2640
fn as_ref ( & self ) -> & bdk_chain:: tx_graph:: TxGraph < ConfirmationBlockTime > {
2579
2641
self . indexed_graph . graph ( )
0 commit comments