From c5df5324b22c407601fbfb9795f85467d1d936de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADckolas=20Goline?= Date: Mon, 13 Jul 2026 21:35:00 -0300 Subject: [PATCH 1/2] lightningd: reject a channel that reuses an existing funding outpoint A v1 channel_id is derived solely from the funding outpoint, so two channels funded by the same outpoint would share a channel_id. When a peer completes a second funding using an outpoint we already have a channel for, refuse it and drop the connection instead of committing a second channel with a colliding channel_id. Add find_channel_by_funding_outpoint() to look up an existing channel by its funding outpoint. --- lightningd/channel.c | 12 ++++++++++++ lightningd/channel.h | 5 +++++ lightningd/opening_control.c | 9 +++++++++ 3 files changed, 26 insertions(+) diff --git a/lightningd/channel.c b/lightningd/channel.c index 1cd40871ed2d..692d81980150 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -879,6 +879,18 @@ struct channel *find_channel_by_id(const struct peer *peer, return NULL; } +struct channel *find_channel_by_funding_outpoint(const struct peer *peer, + const struct bitcoin_outpoint *outpoint) +{ + struct channel *c; + + list_for_each(&peer->channels, c, list) { + if (bitcoin_outpoint_eq(&c->funding, outpoint)) + return c; + } + return NULL; +} + struct channel *find_channel_by_scid(const struct peer *peer, struct short_channel_id scid) { diff --git a/lightningd/channel.h b/lightningd/channel.h index 46add63f51a4..8e3a499ad09c 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -918,6 +918,11 @@ struct channel *channel_by_cid(struct lightningd *ld, struct channel *find_channel_by_id(const struct peer *peer, const struct channel_id *cid); +/* Find a channel with this funding outpoint within peer (an outpoint + * funds at most one channel). */ +struct channel *find_channel_by_funding_outpoint(const struct peer *peer, + const struct bitcoin_outpoint *outpoint); + /* Find this channel within peer */ struct channel *find_channel_by_scid(const struct peer *peer, struct short_channel_id scid); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 74a3277d051e..6cc3d82f1e61 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -533,6 +533,15 @@ static void opening_fundee_finished(struct subd *openingd, derive_channel_id(&cid, &funding); + /* A funding outpoint funds at most one channel; don't accept a second + * channel reusing one we already have. Drop the connection so the + * peer's open fails cleanly instead of waiting for funding_signed. */ + if (find_channel_by_funding_outpoint(uc->peer, &funding)) { + force_peer_disconnect(ld, uc->peer, + "Funding outpoint already in use"); + return; + } + /* old_remote_per_commit not valid yet, copy valid one. */ channel_info.old_remote_per_commit = channel_info.remote_per_commit; From 472a69fbb949185008da3875d9a27e2ee332e8bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADckolas=20Goline?= Date: Mon, 13 Jul 2026 21:35:04 -0300 Subject: [PATCH 2/2] connectd: handle an already-connected subd gracefully peer_connect_subd asserted that the subd had no connection yet. Replace that assertion with defensive handling: if a connection is already attached for this channel_id, keep it and close the incoming fd. This avoids aborting connectd in the unexpected case where a second fd arrives for a subd that already has one. --- connectd/multiplex.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/connectd/multiplex.c b/connectd/multiplex.c index 1776d541e7c6..1051645f8cec 100644 --- a/connectd/multiplex.c +++ b/connectd/multiplex.c @@ -1729,7 +1729,16 @@ void peer_connect_subd(struct daemon *daemon, const u8 *msg, int fd) fmt_node_id(tmpctx, &id))); } - assert(!subd->conn); + /* We only keep one connection per channel_id. If one is already + * attached for this channel_id, drop this fd rather than replacing + * it. */ + if (subd->conn) { + status_peer_debug(&id, + "Already have a subd for channel_id %s: ignoring", + fmt_channel_id(tmpctx, &channel_id)); + close(fd); + return; + } /* This sets subd->conn inside subd_conn_init, and reparents subd! */ io_new_conn(peer, fd, subd_conn_init, subd);