Skip to content

Commit 79d0872

Browse files
committed
wallet: slightly neaten fundpsbt code.
We can keep a single array of 'already considered' utxos, with the same result as Tony's patch prior. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent a536777 commit 79d0872

File tree

1 file changed

+7
-18
lines changed

1 file changed

+7
-18
lines changed

wallet/reservation.c

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ static struct command_result *json_fundpsbt(struct command *cmd,
498498
const jsmntok_t *params)
499499
{
500500
struct utxo **utxos;
501+
const struct utxo **excluded;
501502
u32 *feerate_per_kw;
502503
u32 *minconf, *weight, *min_witness_weight;
503504
struct amount_sat *amount, input, diff, change;
@@ -537,8 +538,8 @@ static struct command_result *json_fundpsbt(struct command *cmd,
537538
/* We keep adding until we meet their output requirements. */
538539
utxos = tal_arr(cmd, struct utxo *, 0);
539540

540-
/* We seperate out UTXOs to exclude if they are uneconomical */
541-
struct utxo **uneconomical_utxos = tal_arr(cmd, struct utxo *, 0);
541+
/* Either uneconomical at this feerate, or already included. */
542+
excluded = tal_arr(cmd, const struct utxo *, 0);
542543

543544
input = AMOUNT_SAT(0);
544545
while (!inputs_sufficient(input, *amount, *feerate_per_kw, *weight,
@@ -547,35 +548,23 @@ static struct command_result *json_fundpsbt(struct command *cmd,
547548
struct amount_sat fee;
548549
u32 utxo_weight;
549550

550-
551-
/* Merge the two lists for exclusion */
552-
struct utxo **all_excluded = tal_arr(cmd, struct utxo *, 0);
553-
for(size_t i = 0; i < tal_count(utxos); i++) {
554-
tal_arr_expand(&all_excluded, utxos[i]);
555-
}
556-
for(size_t i = 0; i < tal_count(uneconomical_utxos); i++) {
557-
tal_arr_expand(&all_excluded, uneconomical_utxos[i]);
558-
}
559-
560551
utxo = wallet_find_utxo(utxos, cmd->ld->wallet,
561552
current_height,
562553
&diff,
563554
*feerate_per_kw,
564555
maxheight,
565556
*nonwrapped,
566-
cast_const2(const struct utxo **, all_excluded));
567-
tal_free(all_excluded);
557+
excluded);
568558

569559
if (utxo) {
560+
tal_arr_expand(&excluded, utxo);
570561
utxo_weight = utxo_spend_weight(utxo,
571562
*min_witness_weight);
572563
fee = amount_tx_fee(*feerate_per_kw, utxo_weight);
573564

574565
/* Uneconomic to add this utxo, skip it */
575-
if (!all && amount_sat_greater_eq(fee, utxo->amount)){
576-
tal_arr_expand(&uneconomical_utxos, utxo);
566+
if (!all && amount_sat_greater_eq(fee, utxo->amount))
577567
continue;
578-
}
579568

580569
tal_arr_expand(&utxos, utxo);
581570

@@ -613,7 +602,7 @@ static struct command_result *json_fundpsbt(struct command *cmd,
613602
&diff));
614603
}
615604

616-
tal_free(uneconomical_utxos);
605+
tal_free(excluded);
617606

618607
if (all) {
619608
/* We need to afford one non-dust output, at least. */

0 commit comments

Comments
 (0)