From 0ad8ab98a97b0a34355b42f0da53eb553fd09c5e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 28 Sep 2023 10:24:38 +0930 Subject: [PATCH] lightningd: ensure we *always* watch channel spend. Now we're not always using the same functions to watch during dual-funding opening, we need to make sure we're watching the close (in particular, df close before the opening is confirmed). So, keep a pointer, and if it's not set in drop_to_chain, set it. Signed-off-by: Rusty Russell --- lightningd/channel.c | 2 ++ lightningd/channel.h | 3 +++ lightningd/peer_control.c | 24 +++++++++++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index 2dc3755d368d..976d690a2b0e 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -234,6 +234,7 @@ struct channel *new_unsaved_channel(struct peer *peer, channel->next_index[LOCAL] = 1; channel->next_index[REMOTE] = 1; channel->next_htlc_id = 0; + channel->funding_spend_watch = NULL; /* FIXME: remove push when v1 deprecated */ channel->push = AMOUNT_MSAT(0); channel->closing_fee_negotiation_step = 50; @@ -465,6 +466,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->next_htlc_id = next_htlc_id; channel->funding = *funding; channel->funding_sats = funding_sats; + channel->funding_spend_watch = NULL; channel->push = push; channel->our_funds = our_funds; channel->remote_channel_ready = remote_channel_ready; diff --git a/lightningd/channel.h b/lightningd/channel.h index 0c707aef3c77..6027b57565a5 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -155,6 +155,9 @@ struct channel { struct bitcoin_outpoint funding; struct amount_sat funding_sats; + /* Watch we have on funding output. */ + struct txowatch *funding_spend_watch; + /* Our original funds, in funding amount */ struct amount_sat our_funds; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 9c84fc709dc0..172aa03b550c 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -311,12 +311,28 @@ static struct bitcoin_tx *sign_and_send_last(const tal_t *ctx, return tx; } +/* FIXME: reorder! */ +static enum watch_result funding_spent(struct channel *channel, + const struct bitcoin_tx *tx, + size_t inputnum UNUSED, + const struct block *block); + void drop_to_chain(struct lightningd *ld, struct channel *channel, bool cooperative) { struct channel_inflight *inflight; const char *cmd_id; + /* If we're not already (e.g. close before channel fully open), + * make sure we're watching for the funding spend */ + if (!channel->funding_spend_watch) { + log_debug(channel->log, "Adding funding_spend_watch"); + channel->funding_spend_watch = watch_txo(channel, + ld->topology, channel, + &channel->funding, + funding_spent); + } + /* If this was triggered by a close command, get a copy of the cmd id */ cmd_id = cmd_id_from_close_command(tmpctx, ld, channel); @@ -2108,9 +2124,11 @@ void channel_watch_funding(struct lightningd *ld, struct channel *channel) type_to_string(tmpctx, struct bitcoin_txid, &channel->funding.txid)); watch_txid(channel, ld->topology, &channel->funding.txid, funding_depth_cb, channel); - watch_txo(channel, ld->topology, channel, - &channel->funding, - funding_spent); + + tal_free(channel->funding_spend_watch); + channel->funding_spend_watch = watch_txo(channel, ld->topology, channel, + &channel->funding, + funding_spent); channel_watch_wrong_funding(ld, channel); }