From c6de4138e65cc71384c5f80a04ea0cdce1c0b509 Mon Sep 17 00:00:00 2001 From: niftynei Date: Mon, 23 Nov 2020 18:54:56 -0600 Subject: [PATCH] open_command: keep them local to channel Since this all stays in dualopend/dual_open_control, we can hold onto the openchannel_signed command to wait for a response here locally. Previously we were splitting across the channeld/openingd boundary. --- lightningd/channel.c | 1 + lightningd/channel.h | 3 ++ lightningd/dual_open_control.c | 29 ++++++++-------- lightningd/lightningd.c | 1 - lightningd/lightningd.h | 2 -- lightningd/peer_control.c | 62 ---------------------------------- lightningd/peer_control.h | 19 ----------- 7 files changed, 19 insertions(+), 98 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index f8d82142e..05691df5d 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -206,6 +206,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->dbid = dbid; channel->error = NULL; channel->htlc_timeout = NULL; + channel->openchannel_signed_cmd = NULL; if (their_shachain) channel->their_shachain = *their_shachain; else { diff --git a/lightningd/channel.h b/lightningd/channel.h index fb4a1b008..6abd49442 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -157,6 +157,9 @@ struct channel { /* Last known state_change cause */ enum state_change state_change_cause; + + /* Outstanding command for this channel, v2 only */ + struct command *openchannel_signed_cmd; }; struct channel *new_channel(struct peer *peer, u64 dbid, diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index daa376c2b..e40fff567 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -1051,23 +1051,22 @@ static void sendfunding_done(struct bitcoind *bitcoind UNUSED, const struct wally_tx *wtx = cs->wtx; struct json_stream *response; struct bitcoin_txid txid; - struct open_command *oc; struct amount_sat unused; int num_utxos; + struct command *cmd = channel->openchannel_signed_cmd; + channel->openchannel_signed_cmd = NULL; - oc = find_open_command(ld, channel); - if (!oc && channel->opener == LOCAL) { + if (!cmd && channel->opener == LOCAL) log_broken(channel->log, "No outstanding command for channel %s," " funding sent was success? %d", type_to_string(tmpctx, struct channel_id, &channel->cid), success); - } if (!success) { - if (oc) - was_pending(command_fail(oc->cmd, + if (cmd) + was_pending(command_fail(cmd, FUNDING_BROADCAST_FAIL, "Error broadcasting funding " "tx: %s. Unsent tx discarded " @@ -1090,24 +1089,24 @@ static void sendfunding_done(struct bitcoind *bitcoind UNUSED, num_utxos = wallet_extract_owned_outputs(ld->wallet, wtx, NULL, &unused); - if (num_utxos) { + if (num_utxos) wallet_transaction_add(ld->wallet, wtx, 0, 0); - } - if (oc) { - response = json_stream_success(oc->cmd); + if (cmd) { + response = json_stream_success(cmd); wally_txid(wtx, &txid); json_add_hex_talarr(response, "tx", linearize_wtx(tmpctx, wtx)); json_add_txid(response, "txid", &txid); json_add_string(response, "channel_id", type_to_string(tmpctx, struct channel_id, &channel->cid)); - was_pending(command_success(oc->cmd, response)); + was_pending(command_success(cmd, response)); } tal_free(cs); } + static void send_funding_tx(struct channel *channel, const struct wally_tx *wtx TAKES) { @@ -1211,6 +1210,10 @@ json_openchannel_signed(struct command *cmd, return command_fail(cmd, LIGHTNINGD, "Already have a finalized PSBT for " "this channel"); + if (channel->openchannel_signed_cmd) + return command_fail(cmd, LIGHTNINGD, + "Already sent sigs, waiting for" + " peer's"); /* Verify that the psbt's txid matches that of the * funding txid for this channel */ @@ -1247,9 +1250,6 @@ json_openchannel_signed(struct command *cmd, wallet_channel_save(cmd->ld->wallet, channel); channel_watch_funding(cmd->ld, channel); - /* Return when the transaction is broadcast */ - register_open_command(cmd->ld, cmd, channel); - /* Send our tx_sigs to the peer */ subd_send_msg(channel->owner, take(towire_dualopend_send_tx_sigs(NULL, channel->psbt))); @@ -1260,6 +1260,7 @@ json_openchannel_signed(struct command *cmd, send_funding_tx(channel, take(wtx)); } + channel->openchannel_signed_cmd = tal_steal(channel, cmd); return command_still_pending(cmd); } diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index c7f7f2e60..ff3df6bb3 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -196,7 +196,6 @@ static struct lightningd *new_lightningd(const tal_t *ctx) list_head_init(&ld->waitsendpay_commands); list_head_init(&ld->sendpay_commands); list_head_init(&ld->close_commands); - list_head_init(&ld->open_commands); list_head_init(&ld->ping_commands); list_head_init(&ld->waitblockheight_commands); diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 9f83f27ac..afcac17d6 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -179,8 +179,6 @@ struct lightningd { struct list_head sendpay_commands; /* Outstanding close commands. */ struct list_head close_commands; - /* Outstanding openchannel_signed commands. */ - struct list_head open_commands; /* Outstanding ping commands. */ struct list_head ping_commands; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index f533bb19d..d31b2791c 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -323,68 +323,6 @@ register_close_command(struct lightningd *ld, &close_command_timeout, cc); } -/* Destroy the open command structure in reaction to the - * channel being destroyed. */ -static void -destroy_open_command_on_channel_destroy(struct channel *_ UNUSED, - struct open_command *oc) -{ - /* The oc has the command as parent, so resolving the - * command destroys the oc and triggers destroy_open_command. - * Clear the oc->channel first so that we will not try to - * remove a destructor. */ - oc->channel = NULL; - was_pending(command_fail(oc->cmd, LIGHTNINGD, - "Channel forgotten before open concluded.")); -} - -/* Destroy the open command structure. */ -static void -destroy_open_command(struct open_command *oc) -{ - list_del(&oc->list); - /* If destroy_close_command_on_channel_destroy was - * triggered beforehand, it will have cleared - * the channel field, preventing us from removing it - * from an already-destroyed channel. */ - if (!oc->channel) - return; - tal_del_destructor2(oc->channel, - &destroy_open_command_on_channel_destroy, - oc); -} - -struct open_command *find_open_command(struct lightningd *ld, - const struct channel *channel) -{ - struct open_command *oc, *n; - - list_for_each_safe (&ld->open_commands, oc, n, list) { - if (oc->channel != channel) - continue; - return oc; - } - - return NULL; -} - -void register_open_command(struct lightningd *ld, - struct command *cmd, - struct channel *channel) -{ - struct open_command *oc; - assert(channel); - - oc = tal(cmd, struct open_command); - list_add_tail(&ld->open_commands, &oc->list); - oc->cmd = cmd; - oc->channel = channel; - tal_add_destructor(oc, &destroy_open_command); - tal_add_destructor2(channel, - &destroy_open_command_on_channel_destroy, - oc); -} - static bool invalid_last_tx(const struct bitcoin_tx *tx) { /* This problem goes back further, but was discovered just before the diff --git a/lightningd/peer_control.h b/lightningd/peer_control.h index 85a5a4528..ffdeeb599 100644 --- a/lightningd/peer_control.h +++ b/lightningd/peer_control.h @@ -52,15 +52,6 @@ struct peer { #endif }; -struct open_command { - /* Inside struct lightningd open_commands. */ - struct list_node list; - /* Command structure. This is the parent of the open command. */ - struct command *cmd; - /* Channel being opened. */ - struct channel *channel; -}; - struct peer *find_peer_by_dbid(struct lightningd *ld, u64 dbid); struct peer *new_peer(struct lightningd *ld, u64 dbid, @@ -99,16 +90,6 @@ void channel_watch_funding(struct lightningd *ld, struct channel *channel); struct amount_msat channel_amount_receivable(const struct channel *channel); -/* Find the open command that was registered for this channel */ -struct open_command *find_open_command(struct lightningd *ld, - const struct channel *channel); - -/* Save an `openchannel_signed` command */ -void register_open_command(struct lightningd *ld, - struct command *cmd, - struct channel *channel); - - /* Pull peers, channels and HTLCs from db, and wire them up. * Returns any HTLCs we have to resubmit via htlcs_resubmit. */ struct htlc_in_map *load_channels_from_wallet(struct lightningd *ld);