@@ -415,69 +415,70 @@ where
415415 tolerate_high_network_feerates : bool , target_feerate_sat_per_1000_weight : u32 ,
416416 preexisting_tx_weight : u64 , input_amount_sat : Amount , target_amount_sat : Amount ,
417417 ) -> Result < CoinSelection , ( ) > {
418- let mut locked_utxos = self . locked_utxos . lock ( ) . unwrap ( ) ;
419- let mut eligible_utxos = utxos
420- . iter ( )
421- . filter_map ( |utxo| {
422- if let Some ( utxo_claim_id) = locked_utxos. get ( & utxo. outpoint ) {
423- if * utxo_claim_id != claim_id && !force_conflicting_utxo_spend {
424- log_trace ! (
425- self . logger,
426- "Skipping UTXO {} to prevent conflicting spend" ,
427- utxo. outpoint
428- ) ;
429- return None ;
430- }
431- }
432- let fee_to_spend_utxo = Amount :: from_sat ( fee_for_weight (
433- target_feerate_sat_per_1000_weight,
434- BASE_INPUT_WEIGHT + utxo. satisfaction_weight ,
435- ) ) ;
436- let should_spend = if tolerate_high_network_feerates {
437- utxo. output . value > fee_to_spend_utxo
438- } else {
439- utxo. output . value >= fee_to_spend_utxo * 2
440- } ;
441- if should_spend {
442- Some ( ( utxo, fee_to_spend_utxo) )
443- } else {
444- log_trace ! (
445- self . logger,
446- "Skipping UTXO {} due to dust proximity after spend" ,
447- utxo. outpoint
448- ) ;
449- None
450- }
451- } )
452- . collect :: < Vec < _ > > ( ) ;
453- eligible_utxos. sort_unstable_by_key ( |( utxo, _) | utxo. output . value ) ;
454-
455418 let mut selected_amount = input_amount_sat;
456419 let mut total_fees = Amount :: from_sat ( fee_for_weight (
457420 target_feerate_sat_per_1000_weight,
458421 preexisting_tx_weight,
459422 ) ) ;
460423 let mut selected_utxos = Vec :: new ( ) ;
461- for ( utxo, fee_to_spend_utxo) in eligible_utxos {
462- if selected_amount >= target_amount_sat + total_fees {
463- break ;
424+ {
425+ let mut locked_utxos = self . locked_utxos . lock ( ) . unwrap ( ) ;
426+ let mut eligible_utxos = utxos
427+ . iter ( )
428+ . filter_map ( |utxo| {
429+ if let Some ( utxo_claim_id) = locked_utxos. get ( & utxo. outpoint ) {
430+ if * utxo_claim_id != claim_id && !force_conflicting_utxo_spend {
431+ log_trace ! (
432+ self . logger,
433+ "Skipping UTXO {} to prevent conflicting spend" ,
434+ utxo. outpoint
435+ ) ;
436+ return None ;
437+ }
438+ }
439+ let fee_to_spend_utxo = Amount :: from_sat ( fee_for_weight (
440+ target_feerate_sat_per_1000_weight,
441+ BASE_INPUT_WEIGHT + utxo. satisfaction_weight ,
442+ ) ) ;
443+ let should_spend = if tolerate_high_network_feerates {
444+ utxo. output . value > fee_to_spend_utxo
445+ } else {
446+ utxo. output . value >= fee_to_spend_utxo * 2
447+ } ;
448+ if should_spend {
449+ Some ( ( utxo, fee_to_spend_utxo) )
450+ } else {
451+ log_trace ! (
452+ self . logger,
453+ "Skipping UTXO {} due to dust proximity after spend" ,
454+ utxo. outpoint
455+ ) ;
456+ None
457+ }
458+ } )
459+ . collect :: < Vec < _ > > ( ) ;
460+ eligible_utxos. sort_unstable_by_key ( |( utxo, _) | utxo. output . value ) ;
461+
462+ for ( utxo, fee_to_spend_utxo) in eligible_utxos {
463+ if selected_amount >= target_amount_sat + total_fees {
464+ break ;
465+ }
466+ selected_amount += utxo. output . value ;
467+ total_fees += fee_to_spend_utxo;
468+ selected_utxos. push ( utxo. clone ( ) ) ;
469+ }
470+ if selected_amount < target_amount_sat + total_fees {
471+ log_debug ! (
472+ self . logger,
473+ "Insufficient funds to meet target feerate {} sat/kW" ,
474+ target_feerate_sat_per_1000_weight
475+ ) ;
476+ return Err ( ( ) ) ;
477+ }
478+ for utxo in & selected_utxos {
479+ locked_utxos. insert ( utxo. outpoint , claim_id) ;
464480 }
465- selected_amount += utxo. output . value ;
466- total_fees += fee_to_spend_utxo;
467- selected_utxos. push ( utxo. clone ( ) ) ;
468- }
469- if selected_amount < target_amount_sat + total_fees {
470- log_debug ! (
471- self . logger,
472- "Insufficient funds to meet target feerate {} sat/kW" ,
473- target_feerate_sat_per_1000_weight
474- ) ;
475- return Err ( ( ) ) ;
476- }
477- for utxo in & selected_utxos {
478- locked_utxos. insert ( utxo. outpoint , claim_id) ;
479481 }
480- core:: mem:: drop ( locked_utxos) ;
481482
482483 let remaining_amount = selected_amount - target_amount_sat - total_fees;
483484 let change_script = self . source . get_change_script ( ) ?;
0 commit comments