From 29157735fb60004d721e84f8d411824f73417d64 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 2 Jun 2022 01:39:53 +0200 Subject: [PATCH] channeld: Track the funding depth while awaiting lockin We used to agree up on the `minimum_depth` with the peer, thus when they told us that the funding locked we'd be sure we either have a scid or we'd trigger the state transition when we do. However if we had a scid, and we got a funding_locked we'd trust them not to have sent it early. Now we explicitly track the depth in the channel while waiting for the funding to confirm. Changelog-Fixed: channeld: Enforce our own `minimum_depth` beyond just confirming --- lightningd/channel.c | 1 + lightningd/channel.h | 6 ++++++ lightningd/channel_control.c | 5 ++++- 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lightningd/channel.c b/lightningd/channel.c index e0bffb768..92c6f809d 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -430,6 +430,7 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->channel_flags = channel_flags; channel->our_config = *our_config; channel->minimum_depth = minimum_depth; + channel->depth = 0; channel->next_index[LOCAL] = next_index_local; channel->next_index[REMOTE] = next_index_remote; channel->next_htlc_id = next_htlc_id; diff --git a/lightningd/channel.h b/lightningd/channel.h index afd72e4a7..fbe86a63f 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -119,6 +119,12 @@ struct channel { /* Minimum funding depth (specified by us if they fund). */ u32 minimum_depth; + /* Depth of the funding TX as reported by the txout + * watch. Only used while we're in state + * CHANNELD_AWAITING_LOCKING, afterwards the watches do not + * trigger anymore. */ + u32 depth; + /* Tracking commitment transaction numbers. */ u64 next_index[NUM_SIDES]; u64 next_htlc_id; diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index eafed96ba..9c908b586 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -256,7 +256,9 @@ static void peer_got_funding_locked(struct channel *channel, const u8 *msg) /* Remember that we got the lockin */ wallet_channel_save(channel->peer->ld->wallet, channel); - lockin_complete(channel); + + if (channel->depth >= channel->minimum_depth) + lockin_complete(channel); } static void peer_got_announcement(struct channel *channel, const u8 *msg) @@ -805,6 +807,7 @@ bool channel_tell_depth(struct lightningd *ld, const char *txidstr; txidstr = type_to_string(tmpctx, struct bitcoin_txid, txid); + channel->depth = depth; if (!channel->owner) { log_debug(channel->log,