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