Skip to content

Commit

Permalink
mfc-df: only add outputs for v1 outs; go to openchannel_update if v2s
Browse files Browse the repository at this point in the history
We only have output scripts for v1 protocols after the
fundchannel_start/openchannel_init round. We need to add them before
we get into the openchannel_update rounds, however, so we do that here.
  • Loading branch information
niftynei committed Nov 23, 2020
1 parent 3d2c095 commit c90a19f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
43 changes: 36 additions & 7 deletions plugins/spender/multifundchannel.c
Expand Up @@ -1196,22 +1196,33 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
{
struct multifundchannel_destination **deck;
char *content = tal_strdup(tmpctx, "");
size_t v1_dest_count = dest_count(mfc, FUND_CHANNEL);
size_t v2_dest_count = dest_count(mfc, OPEN_CHANNEL);
size_t i, deck_i;

plugin_log(mfc->cmd->plugin, LOG_DBG,
"mfc %"PRIu64": Creating funding tx.",
mfc->id);

/* Construct a deck of destinations. */
deck = tal_arr(tmpctx, struct multifundchannel_destination *,
tal_count(mfc->destinations) + mfc->change_needed);
for (size_t i = 0; i < tal_count(mfc->destinations); ++i)
deck[i] = &mfc->destinations[i];
v1_dest_count + mfc->change_needed);

deck_i = 0;
for (i = 0; i < tal_count(mfc->destinations); i++) {
if (mfc->destinations[i].protocol == OPEN_CHANNEL)
continue;

assert(deck_i < tal_count(deck));
deck[deck_i++] = &mfc->destinations[i];
}

/* Add a NULL into the deck as a proxy for change output, if
* needed. */
if (mfc->change_needed)
deck[tal_count(mfc->destinations)] = NULL;
deck[v1_dest_count] = NULL;
/* Fisher-Yates shuffle. */
for (size_t i = tal_count(deck); i > 1; --i) {
for (i = tal_count(deck); i > 1; --i) {
size_t j = pseudorand(i);
if (j == i - 1)
continue;
Expand All @@ -1221,7 +1232,7 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
deck[i - 1] = tmp;
}

/* Now that we have the outputs shuffled, add outputs to the PSBT. */
/* Now that we have our outputs shuffled, add outputs to the PSBT. */
for (unsigned int outnum = 0; outnum < tal_count(deck); ++outnum) {
if (outnum != 0)
tal_append_fmt(&content, ", ");
Expand All @@ -1233,7 +1244,10 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
dest->funding_script,
dest->amount,
outnum);
dest->outnum = outnum;
/* The actual output index will be based on the
* serial_id if this contains any v2 outputs */
if (v2_dest_count == 0)
dest->outnum = outnum;
tal_append_fmt(&content, "%s: %s",
type_to_string(tmpctx, struct node_id,
&dest->id),
Expand All @@ -1254,6 +1268,21 @@ perform_funding_tx_finalize(struct multifundchannel_command *mfc)
}
}

if (v2_dest_count > 0) {
/* Add serial_ids to all the new outputs */
psbt_add_serials(mfc->psbt, TX_INITIATOR);

/* Now we stash the 'mfc' command, so when/if
* signature notifications start coming
* in, we'll catch them. */
register_mfc(mfc);

/* Take a side-quest to finish filling out
* the funding tx */
return perform_openchannel_update(mfc);
}

/* We've only got v1 destinations, move onward */
/* Elements requires a fee output. */
psbt_elements_normalize_fees(mfc->psbt);

Expand Down
4 changes: 1 addition & 3 deletions plugins/spender/openchannel.c
Expand Up @@ -14,21 +14,19 @@

static struct list_head mfc_commands;

/* unused for now, will return soon!
static void
destroy_mfc(struct multifundchannel_command *mfc)
{
list_del(&mfc->list);
}

static void register_mfc(struct multifundchannel_command *mfc)
void register_mfc(struct multifundchannel_command *mfc)
{
assert(mfc);

list_add_tail(&mfc_commands, &mfc->list);
tal_add_destructor(mfc, &destroy_mfc);
}
*/

static struct multifundchannel_destination *
find_dest_by_channel_id(struct channel_id *cid)
Expand Down
4 changes: 4 additions & 0 deletions plugins/spender/openchannel.h
Expand Up @@ -8,6 +8,10 @@ struct wally_psbt;
extern const struct plugin_notification openchannel_notifs[];
extern const size_t num_openchannel_notifs;

/* register_mfc - Register to listen for incoming
* peer signature notifications */
void register_mfc(struct multifundchannel_command *mfc);

struct command_result *
openchannel_init_dest(struct multifundchannel_destination *dest);

Expand Down

0 comments on commit c90a19f

Please sign in to comment.