elements: use normalization for elements fee output

This will update the fee output if it exists, rather than unilaterally
adding a new one.

Also, if the fee output already exists, we should make sure that it
doesn't interfere with the outnums of the other outputs
This commit is contained in:
niftynei 2020-09-09 20:38:05 -05:00 committed by Rusty Russell
parent eab7fcf8e5
commit 81a7a6742a

View File

@ -1362,9 +1362,10 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
/* Funding outpoint. */ /* Funding outpoint. */
struct multifundchannel_destination *dest; struct multifundchannel_destination *dest;
dest = deck[outnum]; dest = deck[outnum];
(void) psbt_append_output(mfc->psbt, (void) psbt_insert_output(mfc->psbt,
dest->funding_script, dest->funding_script,
dest->amount); dest->amount,
outnum);
dest->outnum = outnum; dest->outnum = outnum;
tal_append_fmt(&content, "%s: %s", tal_append_fmt(&content, "%s: %s",
type_to_string(tmpctx, struct node_id, type_to_string(tmpctx, struct node_id,
@ -1375,9 +1376,10 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
} else { } else {
/* Change output. */ /* Change output. */
assert(mfc->change_needed); assert(mfc->change_needed);
(void) psbt_append_output(mfc->psbt, (void) psbt_insert_output(mfc->psbt,
mfc->change_scriptpubkey, mfc->change_scriptpubkey,
mfc->change_amount); mfc->change_amount,
outnum);
tal_append_fmt(&content, "change: %s", tal_append_fmt(&content, "change: %s",
type_to_string(tmpctx, type_to_string(tmpctx,
struct amount_sat, struct amount_sat,
@ -1386,30 +1388,7 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
} }
/* Elements requires a fee output. */ /* Elements requires a fee output. */
if (chainparams->is_elements) { psbt_elements_normalize_fees(mfc->psbt);
struct amount_sat inputs = AMOUNT_SAT(0);
struct amount_sat outputs = AMOUNT_SAT(0);
struct amount_sat fees;
for (size_t i = 0; i < mfc->psbt->num_inputs; ++i)
if (!amount_sat_add(&inputs,
inputs,
psbt_input_get_amount(mfc->psbt,
i)))
plugin_err(mfc->cmd->plugin,
"Overflow while adding inputs");
for (size_t i = 0; i < mfc->psbt->num_outputs; ++i)
if (!amount_sat_add(&outputs,
outputs,
psbt_output_get_amount(mfc->psbt,
i)))
plugin_err(mfc->cmd->plugin,
"Overflow while adding outputs");
if (!amount_sat_sub(&fees, inputs, outputs))
fees = AMOUNT_SAT(0);
/* If there is any fee at all, add the fee output. */
if (!amount_sat_eq(fees, AMOUNT_SAT(0)))
(void) psbt_append_output(mfc->psbt, NULL, fees);
}
/* Generate the TXID. */ /* Generate the TXID. */
mfc->txid = tal(mfc, struct bitcoin_txid); mfc->txid = tal(mfc, struct bitcoin_txid);