Skip to content

Commit

Permalink
lightningd: ensure we *always* watch channel spend.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell committed Sep 28, 2023
1 parent 18e01c5 commit 0ad8ab9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
24 changes: 21 additions & 3 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 0ad8ab9

Please sign in to comment.