From 3d2c0951d57e2bbcd925e4b1e7ef72b8e51c8e48 Mon Sep 17 00:00:00 2001 From: niftynei Date: Thu, 22 Oct 2020 14:17:11 -0500 Subject: [PATCH] mfc-df: rework how openchannel_update works, callbacks --- plugins/spender/multifundchannel.c | 5 +-- plugins/spender/multifundchannel.h | 3 ++ plugins/spender/openchannel.c | 68 ++++++++++++++++++++++++++---- plugins/spender/openchannel.h | 4 ++ 4 files changed, 67 insertions(+), 13 deletions(-) diff --git a/plugins/spender/multifundchannel.c b/plugins/spender/multifundchannel.c index 74f320b1c..7106f71b8 100644 --- a/plugins/spender/multifundchannel.c +++ b/plugins/spender/multifundchannel.c @@ -1191,9 +1191,6 @@ inadvertently leak important privacy data in the order of its arguments, so we shuffle the outputs. */ -static struct command_result * -perform_fundchannel_complete(struct multifundchannel_command *mfc); - static struct command_result * perform_funding_tx_finalize(struct multifundchannel_command *mfc) { @@ -1305,7 +1302,7 @@ the transaction. static void fundchannel_complete_dest(struct multifundchannel_destination *dest); -static struct command_result * +struct command_result * perform_fundchannel_complete(struct multifundchannel_command *mfc) { unsigned int i; diff --git a/plugins/spender/multifundchannel.h b/plugins/spender/multifundchannel.h index 80f3a9dd7..56cd13b3e 100644 --- a/plugins/spender/multifundchannel.h +++ b/plugins/spender/multifundchannel.h @@ -253,6 +253,9 @@ mfc_finished(struct multifundchannel_command *, struct json_stream *response); struct command_result * after_channel_start(struct multifundchannel_command *mfc); +struct command_result * +perform_fundchannel_complete(struct multifundchannel_command *mfc); + struct command_result * redo_multifundchannel(struct multifundchannel_command *mfc, const char *failing_method); diff --git a/plugins/spender/openchannel.c b/plugins/spender/openchannel.c index 74b309000..e10e93d9b 100644 --- a/plugins/spender/openchannel.c +++ b/plugins/spender/openchannel.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -677,9 +678,53 @@ static void json_peer_sigs(struct command *cmd, *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~* */ -/* We call it circularly, til finished */ static struct command_result * -perform_openchannel_update(struct multifundchannel_command *mfc); +funding_transaction_established(struct multifundchannel_command *mfc) +{ + /* Elements requires a fee output. */ + /* FIXME: v2 on liquid */ + psbt_elements_normalize_fees(mfc->psbt); + + /* Generate the TXID. */ + mfc->txid = tal(mfc, struct bitcoin_txid); + psbt_txid(NULL, mfc->psbt, mfc->txid, NULL); + plugin_log(mfc->cmd->plugin, LOG_DBG, + "mfc %"PRIu64": funding tx %s", + mfc->id, + type_to_string(tmpctx, struct bitcoin_txid, + mfc->txid)); + + /* If all we've got is v2 destinations, we're just waiting + * for all of our peers to send us their sigs. + * That callback triggers separately, so we just return + * a 'still pending' here */ + if (dest_count(mfc, FUND_CHANNEL) == 0) + return command_still_pending(mfc->cmd); + + /* For any v1 destination, we need to update the destination + * outnum with the correct outnum on the now-known + * funding transaction */ + for (size_t i = 0; i < tal_count(mfc->destinations); i++) { + struct multifundchannel_destination *dest; + if (mfc->destinations[i].protocol == OPEN_CHANNEL) + continue; + + dest = &mfc->destinations[i]; + dest->outnum = mfc->psbt->num_outputs; + for (size_t j = 0; j < mfc->psbt->num_outputs; j++) { + if (memeq(dest->funding_script, + tal_bytelen(dest->funding_script), + mfc->psbt->tx->outputs[j].script, + mfc->psbt->tx->outputs[j].script_len)) + dest->outnum = j; + } + if (dest->outnum == mfc->psbt->num_outputs) + abort(); + assert(dest->outnum < mfc->psbt->num_outputs); + } + + return perform_fundchannel_complete(mfc); +} static struct command_result * openchannel_update_returned(struct multifundchannel_destination *dest) @@ -830,7 +875,7 @@ openchannel_update_dest(struct multifundchannel_destination *dest) send_outreq(cmd->plugin, req); } -static struct command_result * +struct command_result * perform_openchannel_update(struct multifundchannel_command *mfc) { size_t i, ready_count = 0; @@ -848,8 +893,6 @@ perform_openchannel_update(struct multifundchannel_command *mfc) return redo_multifundchannel(mfc, "openchannel_update"); - /* If any *one* is secured or signed, they should all - * be done. */ if (dest->state == MULTIFUNDCHANNEL_SECURED || dest->state == MULTIFUNDCHANNEL_SIGNED) { ready_count++; @@ -860,16 +903,19 @@ perform_openchannel_update(struct multifundchannel_command *mfc) dest->state == MULTIFUNDCHANNEL_STARTED); } - // FIXME: how many is the total count here? - if (ready_count == tal_count(mfc->destinations)) { - return command_still_pending(mfc->cmd); - } + /* Check if we can stop doing this and move to the next + * phase */ + if (ready_count == dest_count(mfc, OPEN_CHANNEL)) + return funding_transaction_established(mfc); /* Then, we update the parent with every node's result */ for (i = 0; i < tal_count(mfc->destinations); i++) { struct multifundchannel_destination *dest; dest = &mfc->destinations[i]; + if (dest->protocol == FUND_CHANNEL) + continue; + if (!update_parent_psbt(mfc, dest, dest->psbt, dest->updated_psbt, &mfc->psbt)) { @@ -892,6 +938,10 @@ perform_openchannel_update(struct multifundchannel_command *mfc) struct multifundchannel_destination *dest; dest = &mfc->destinations[i]; + /* We don't *have* psbts for v1 destinations */ + if (dest->protocol == FUND_CHANNEL) + continue; + if (!update_node_psbt(mfc, mfc->psbt, &dest->psbt)) { fail_destination(dest, "Unable to node PSBT" " with parent PSBT"); diff --git a/plugins/spender/openchannel.h b/plugins/spender/openchannel.h index 16ea40df6..d44e93a4b 100644 --- a/plugins/spender/openchannel.h +++ b/plugins/spender/openchannel.h @@ -13,4 +13,8 @@ openchannel_init_dest(struct multifundchannel_destination *dest); void openchannel_init(struct plugin *p, const char *b, const jsmntok_t *t); + +struct command_result * +perform_openchannel_update(struct multifundchannel_command *mfc); + #endif /* LIGHTNING_PLUGINS_SPENDER_OPENCHANNEL_H */