1
- use std:: collections:: HashMap ;
1
+ use std:: collections:: { HashMap , HashSet } ;
2
+ use std:: iter:: FromIterator ;
2
3
use std:: str:: FromStr ;
3
4
use std:: sync:: Arc ;
4
5
@@ -140,6 +141,11 @@ impl DefineSpend {
140
141
}
141
142
142
143
pub fn with_coins_sorted ( mut self , blockheight : u32 ) -> Self {
144
+ self . sort_coins ( blockheight) ;
145
+ self
146
+ }
147
+
148
+ fn sort_coins ( & mut self , blockheight : u32 ) {
143
149
let timelock = self . timelock ;
144
150
self . coins . sort_by ( |( a, a_selected) , ( b, b_selected) | {
145
151
if * a_selected && !b_selected || !a_selected && * b_selected {
@@ -154,7 +160,6 @@ impl DefineSpend {
154
160
a. block_height . cmp ( & b. block_height )
155
161
}
156
162
} ) ;
157
- self
158
163
}
159
164
160
165
pub fn self_send ( mut self ) -> Self {
@@ -406,6 +411,35 @@ impl Step for DefineSpend {
406
411
}
407
412
Err ( e) => self . warning = Some ( e) ,
408
413
} ,
414
+ Message :: Coins ( res) => match res {
415
+ Ok ( coins) => {
416
+ let selected: HashSet < OutPoint > =
417
+ HashSet :: from_iter ( self . coins . iter ( ) . filter_map ( |( c, selected) | {
418
+ if * selected {
419
+ Some ( c. outpoint )
420
+ } else {
421
+ None
422
+ }
423
+ } ) ) ;
424
+ self . coins = coins
425
+ . into_iter ( )
426
+ . filter_map ( |coin| {
427
+ if coin. spend_info . is_none ( ) && !coin. is_immature {
428
+ let selected = selected. contains ( & coin. outpoint ) ;
429
+ Some ( ( coin, selected) )
430
+ } else {
431
+ None
432
+ }
433
+ } )
434
+ . collect ( ) ;
435
+ self . sort_coins ( cache. blockheight as u32 ) ;
436
+ // In case some selected coins are not spendable anymore and
437
+ // new coins make more sense to be selected. A redraft is triggered
438
+ // if all forms are valid (checked in the redraft method)
439
+ self . redraft ( daemon) ;
440
+ }
441
+ Err ( e) => self . warning = Some ( e) ,
442
+ } ,
409
443
_ => { }
410
444
} ;
411
445
Command :: none ( )
0 commit comments