@@ -413,51 +413,52 @@ where
413413 tolerate_high_network_feerates : bool , target_feerate_sat_per_1000_weight : u32 ,
414414 preexisting_tx_weight : u64 , input_amount_sat : Amount , target_amount_sat : Amount ,
415415 ) -> Result < CoinSelection , ( ) > {
416- let mut locked_utxos = self . locked_utxos . lock ( ) . unwrap ( ) ;
417- let mut eligible_utxos = utxos. iter ( ) . filter_map ( |utxo| {
418- if let Some ( utxo_claim_id) = locked_utxos. get ( & utxo. outpoint ) {
419- if * utxo_claim_id != claim_id && !force_conflicting_utxo_spend {
420- log_trace ! ( self . logger, "Skipping UTXO {} to prevent conflicting spend" , utxo. outpoint) ;
421- return None ;
422- }
423- }
424- let fee_to_spend_utxo = Amount :: from_sat ( fee_for_weight (
425- target_feerate_sat_per_1000_weight, BASE_INPUT_WEIGHT + utxo. satisfaction_weight ,
426- ) ) ;
427- let should_spend = if tolerate_high_network_feerates {
428- utxo. output . value > fee_to_spend_utxo
429- } else {
430- utxo. output . value >= fee_to_spend_utxo * 2
431- } ;
432- if should_spend {
433- Some ( ( utxo, fee_to_spend_utxo) )
434- } else {
435- log_trace ! ( self . logger, "Skipping UTXO {} due to dust proximity after spend" , utxo. outpoint) ;
436- None
437- }
438- } ) . collect :: < Vec < _ > > ( ) ;
439- eligible_utxos. sort_unstable_by_key ( |( utxo, _) | utxo. output . value ) ;
440-
441416 let mut selected_amount = input_amount_sat;
442417 let mut total_fees = Amount :: from_sat ( fee_for_weight ( target_feerate_sat_per_1000_weight, preexisting_tx_weight) ) ;
443418 let mut selected_utxos = Vec :: new ( ) ;
444- for ( utxo, fee_to_spend_utxo) in eligible_utxos {
445- if selected_amount >= target_amount_sat + total_fees {
446- break ;
419+ {
420+ let mut locked_utxos = self . locked_utxos . lock ( ) . unwrap ( ) ;
421+ let mut eligible_utxos = utxos. iter ( ) . 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 ! ( self . logger, "Skipping UTXO {} to prevent conflicting spend" , utxo. outpoint) ;
425+ return None ;
426+ }
427+ }
428+ let fee_to_spend_utxo = Amount :: from_sat ( fee_for_weight (
429+ target_feerate_sat_per_1000_weight, BASE_INPUT_WEIGHT + utxo. satisfaction_weight ,
430+ ) ) ;
431+ let should_spend = if tolerate_high_network_feerates {
432+ utxo. output . value > fee_to_spend_utxo
433+ } else {
434+ utxo. output . value >= fee_to_spend_utxo * 2
435+ } ;
436+ if should_spend {
437+ Some ( ( utxo, fee_to_spend_utxo) )
438+ } else {
439+ log_trace ! ( self . logger, "Skipping UTXO {} due to dust proximity after spend" , utxo. outpoint) ;
440+ None
441+ }
442+ } ) . collect :: < Vec < _ > > ( ) ;
443+ eligible_utxos. sort_unstable_by_key ( |( utxo, _) | utxo. output . value ) ;
444+
445+ for ( utxo, fee_to_spend_utxo) in eligible_utxos {
446+ if selected_amount >= target_amount_sat + total_fees {
447+ break ;
448+ }
449+ selected_amount += utxo. output . value ;
450+ total_fees += fee_to_spend_utxo;
451+ selected_utxos. push ( utxo. clone ( ) ) ;
452+ }
453+ if selected_amount < target_amount_sat + total_fees {
454+ log_debug ! ( self . logger, "Insufficient funds to meet target feerate {} sat/kW" ,
455+ target_feerate_sat_per_1000_weight) ;
456+ return Err ( ( ) ) ;
457+ }
458+ for utxo in & selected_utxos {
459+ locked_utxos. insert ( utxo. outpoint , claim_id) ;
447460 }
448- selected_amount += utxo. output . value ;
449- total_fees += fee_to_spend_utxo;
450- selected_utxos. push ( utxo. clone ( ) ) ;
451- }
452- if selected_amount < target_amount_sat + total_fees {
453- log_debug ! ( self . logger, "Insufficient funds to meet target feerate {} sat/kW" ,
454- target_feerate_sat_per_1000_weight) ;
455- return Err ( ( ) ) ;
456- }
457- for utxo in & selected_utxos {
458- locked_utxos. insert ( utxo. outpoint , claim_id) ;
459461 }
460- core:: mem:: drop ( locked_utxos) ;
461462
462463 let remaining_amount = selected_amount - target_amount_sat - total_fees;
463464 let change_script = self . source . get_change_script ( ) ?;
0 commit comments