Gather enough utxos to meet feerate, otherwise all we can. */
| 640 | |
| 641 | /* Gather enough utxos to meet feerate, otherwise all we can. */ |
| 642 | struct utxo **wallet_utxo_boost(const tal_t *ctx, |
| 643 | struct wallet *w, |
| 644 | u32 blockheight, |
| 645 | struct amount_sat excess_sats, |
| 646 | struct amount_sat output_sats_required, |
| 647 | u32 feerate_target, |
| 648 | size_t *weight, |
| 649 | bool *insufficient) |
| 650 | { |
| 651 | struct utxo **all_utxos = wallet_get_unspent_utxos(tmpctx, w); |
| 652 | struct utxo **utxos = tal_arr(ctx, struct utxo *, 0); |
| 653 | u32 feerate; |
| 654 | |
| 655 | /* Select in random order */ |
| 656 | tal_arr_randomize(all_utxos, struct utxo *); |
| 657 | |
| 658 | feerate = calc_feerate(excess_sats, output_sats_required, *weight); |
| 659 | |
| 660 | for (size_t i = 0; i < tal_count(all_utxos); i++) { |
| 661 | u32 new_feerate; |
| 662 | size_t new_weight; |
| 663 | struct amount_sat new_excess_sats; |
| 664 | /* Convenience var */ |
| 665 | struct utxo *utxo = all_utxos[i]; |
| 666 | |
| 667 | /* Are we already happy? */ |
| 668 | if (feerate >= feerate_target) { |
| 669 | log_debug(w->log, "wallet_utxo_boost: got %zu UTXOs, excess %s (needed %s), weight %zu, feerate %u >= %u", |
| 670 | tal_count(utxos), |
| 671 | fmt_amount_sat(tmpctx, excess_sats), |
| 672 | fmt_amount_sat(tmpctx, output_sats_required), |
| 673 | *weight, feerate, feerate_target); |
| 674 | if (insufficient) |
| 675 | *insufficient = false; |
| 676 | return utxos; |
| 677 | } |
| 678 | |
| 679 | /* Don't add reserved ones */ |
| 680 | if (utxo_is_reserved(utxo, blockheight)) |
| 681 | continue; |
| 682 | |
| 683 | /* Don't add csv-locked ones */ |
| 684 | if (utxo_is_csv_locked(utxo, blockheight)) |
| 685 | continue; |
| 686 | |
| 687 | /* UTXOs must be sane amounts */ |
| 688 | if (!amount_sat_add(&new_excess_sats, |
| 689 | excess_sats, utxo->amount)) |
| 690 | abort(); |
| 691 | |
| 692 | new_weight = *weight + utxo_spend_weight(utxo, 0); |
| 693 | new_feerate = calc_feerate(new_excess_sats, output_sats_required, |
| 694 | new_weight); |
| 695 | |
| 696 | /* Don't add uneconomic ones! */ |
| 697 | if (new_feerate < feerate) |
| 698 | continue; |
| 699 |
no test coverage detected