From 95c734551514a44f1b397bba3df9232461640e0e Mon Sep 17 00:00:00 2001 From: niftynei Date: Fri, 27 Oct 2023 14:16:23 -0500 Subject: [PATCH] db, inflights: add method to remove any 'dangling' inflights When we reconnect, if we get a note from the peer that they dont know about a pending inflight, we need to be able to clean it up so we can restart/re-negotiate a new RBF etc. This adds a cleanup method to remove any inflights for a channel without a last_tx (commitment tx) --- lightningd/channel.c | 17 +++++++++++++++++ lightningd/channel.h | 3 +++ wallet/wallet.c | 12 ++++++++++++ wallet/wallet.h | 6 ++++++ 4 files changed, 38 insertions(+) diff --git a/lightningd/channel.c b/lightningd/channel.c index 9d4e16635..6a2269854 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -97,6 +97,23 @@ void delete_channel(struct channel *channel STEALS) maybe_delete_peer(peer); } +bool maybe_cleanup_last_inflight(struct channel *channel) +{ + struct channel_inflight *inflight; + inflight = channel_current_inflight(channel); + if (!inflight) + return false; + + if (inflight->last_tx) + return false; + + /* Remove from database */ + wallet_channel_inflight_cleanup_incomplete( + channel->peer->ld->wallet, channel->dbid); + tal_free(inflight); + return true; +} + void get_channel_basepoints(struct lightningd *ld, const struct node_id *peer_id, const u64 dbid, diff --git a/lightningd/channel.h b/lightningd/channel.h index 0db24af5a..0d6edb8da 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -404,6 +404,9 @@ void inflight_set_last_tx(struct channel_inflight *inflight, struct bitcoin_tx *last_tx STEALS, const struct bitcoin_signature last_sig); +/* If the last inflight has no commitment tx, remove it */ +bool maybe_cleanup_last_inflight(struct channel *channel); + /* Given a txid, find an inflight channel stub. Returns NULL if none found */ struct channel_inflight *channel_inflight_find(struct channel *channel, const struct bitcoin_txid *txid); diff --git a/wallet/wallet.c b/wallet/wallet.c index 8117942d7..ddad3b5c4 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2606,6 +2606,18 @@ void wallet_channel_close(struct wallet *w, u64 wallet_id) db_exec_prepared_v2(take(stmt)); } +void wallet_channel_inflight_cleanup_incomplete(struct wallet *w, u64 wallet_id) +{ + struct db_stmt *stmt; + + /* Delete any incomplete entries from 'inflights' */ + stmt = db_prepare_v2(w->db, + SQL("DELETE FROM channel_funding_inflights " + " WHERE channel_id=? AND last_tx IS NULL")); + db_bind_u64(stmt, wallet_id); + db_exec_prepared_v2(take(stmt)); +} + void wallet_delete_peer_if_unused(struct wallet *w, u64 peer_dbid) { struct db_stmt *stmt; diff --git a/wallet/wallet.h b/wallet/wallet.h index 8a080a1ae..2e8138d5f 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -626,6 +626,12 @@ void wallet_inflight_add(struct wallet *w, struct channel_inflight *inflight); void wallet_inflight_save(struct wallet *w, struct channel_inflight *inflight); +/** + * Remove any channel inflights that are incomplete. + */ +void wallet_channel_inflight_cleanup_incomplete(struct wallet *w, + u64 wallet_id); + /** * Remove all the inflights from a channel. Also cleans up * the channel's inflight list