Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Exclude uneconomical UTXOs from fundchannel
Browse files Browse the repository at this point in the history
  • Loading branch information
TonyGiorgio authored and gkrizek committed Sep 11, 2023
1 parent 98ad467 commit 8196ace
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions wallet/reservation.c
Original file line number Diff line number Diff line change
Expand Up @@ -537,28 +537,45 @@ static struct command_result *json_fundpsbt(struct command *cmd,
/* We keep adding until we meet their output requirements. */
utxos = tal_arr(cmd, struct utxo *, 0);

/* We seperate out UTXOs to exclude if they are uneconomical */
struct utxo **uneconomical_utxos = tal_arr(cmd, struct utxo *, 0);

input = AMOUNT_SAT(0);
while (!inputs_sufficient(input, *amount, *feerate_per_kw, *weight,
&diff)) {
struct utxo *utxo;
struct amount_sat fee;
u32 utxo_weight;


/* Merge the two lists for exclusion */
struct utxo **all_excluded = tal_arr(cmd, struct utxo *, 0);
for(size_t i = 0; i < tal_count(utxos); i++) {
tal_arr_expand(&all_excluded, utxos[i]);
}
for(size_t i = 0; i < tal_count(uneconomical_utxos); i++) {
tal_arr_expand(&all_excluded, uneconomical_utxos[i]);
}

utxo = wallet_find_utxo(utxos, cmd->ld->wallet,
current_height,
&diff,
*feerate_per_kw,
maxheight,
*nonwrapped,
cast_const2(const struct utxo **, utxos));
cast_const2(const struct utxo **, all_excluded));
tal_free(all_excluded);

if (utxo) {
utxo_weight = utxo_spend_weight(utxo,
*min_witness_weight);
fee = amount_tx_fee(*feerate_per_kw, utxo_weight);

/* Uneconomic to add this utxo, skip it */
if (!all && amount_sat_greater_eq(fee, utxo->amount))
if (!all && amount_sat_greater_eq(fee, utxo->amount)){
tal_arr_expand(&uneconomical_utxos, utxo);
continue;
}

tal_arr_expand(&utxos, utxo);

Expand Down Expand Up @@ -596,6 +613,8 @@ static struct command_result *json_fundpsbt(struct command *cmd,
&diff));
}

tal_free(uneconomical_utxos);

if (all) {
/* We need to afford one non-dust output, at least. */
if (!inputs_sufficient(input, AMOUNT_SAT(0),
Expand Down

0 comments on commit 8196ace

Please sign in to comment.