From fe9f391a932cc587e29f8a1c2ab643fc61af6ea4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 23 Mar 2022 06:57:30 +1030 Subject: [PATCH] connectd: tell lightningd the channel_id when we give it the active peer. Now we always have it (either extracted from an unsolicited message, or told to us by lightningd when it tells us it wants to talk), we can always send it. Signed-off-by: Rusty Russell --- connectd/connectd_wire.csv | 2 +- connectd/multiplex.c | 25 ++++++++++++++++----- lightningd/peer_control.c | 12 +++++----- lightningd/test/run-invoice-select-inchan.c | 2 +- wallet/test/run-wallet.c | 2 +- 5 files changed, 28 insertions(+), 15 deletions(-) diff --git a/connectd/connectd_wire.csv b/connectd/connectd_wire.csv index 39506d8ae..5b0950f93 100644 --- a/connectd/connectd_wire.csv +++ b/connectd/connectd_wire.csv @@ -86,7 +86,7 @@ msgdata,connectd_peer_make_active,channel_id,channel_id, msgtype,connectd_peer_active,2005 msgdata,connectd_peer_active,id,node_id, msgdata,connectd_peer_active,msgtype,?u16, -msgdata,connectd_peer_active,channel_id,?channel_id, +msgdata,connectd_peer_active,channel_id,channel_id, # master -> connectd: peer no longer wanted, you can disconnect. msgtype,connectd_discard_peer,2015 diff --git a/connectd/multiplex.c b/connectd/multiplex.c index dbc6014e6..da6f5f346 100644 --- a/connectd/multiplex.c +++ b/connectd/multiplex.c @@ -402,7 +402,9 @@ void send_custommsg(struct daemon *daemon, const u8 *msg) } /* FIXME: fwd decl */ -static struct subd *multiplex_subd_setup(struct peer *peer, int *fd_for_subd); +static struct subd *multiplex_subd_setup(struct peer *peer, + const struct channel_id *channel_id, + int *fd_for_subd); static struct subd *activate_peer(struct peer *peer, const enum peer_wire *type, @@ -415,7 +417,7 @@ static struct subd *activate_peer(struct peer *peer, /* If it wasn't active before, it is now! */ peer->active = true; - subd = multiplex_subd_setup(peer, &fd_for_subd); + subd = multiplex_subd_setup(peer, channel_id, &fd_for_subd); if (!subd) return NULL; @@ -816,11 +818,18 @@ static struct io_plan *read_body_from_peer_done(struct io_conn *peer_conn, if (!subd) { struct channel_id channel_id; enum peer_wire t = fromwire_peektype(decrypted); - bool has_channel_id = extract_channel_id(decrypted, &channel_id); + + if (!extract_channel_id(decrypted, &channel_id)) { + send_warning(peer, "Unrecognized message %s: %s", + peer_wire_name(t), + tal_hex(tmpctx, decrypted)); + tal_free(decrypted); + io_wake(peer->peer_outq); + return read_hdr_from_peer(peer_conn, peer); + } status_peer_debug(&peer->id, "Activating for message %s", peer_wire_name(t)); - subd = activate_peer(peer, &t, - has_channel_id ? &channel_id : NULL); + subd = activate_peer(peer, &t, &channel_id); if (!subd) return io_close(peer_conn); } @@ -915,7 +924,9 @@ void close_peer_conn(struct peer *peer) msg_wake(peer->peer_outq); } -static struct subd *multiplex_subd_setup(struct peer *peer, int *fd_for_subd) +static struct subd *multiplex_subd_setup(struct peer *peer, + const struct channel_id *channel_id, + int *fd_for_subd) { int fds[2]; struct subd *subd; @@ -929,6 +940,8 @@ static struct subd *multiplex_subd_setup(struct peer *peer, int *fd_for_subd) subd = tal(peer->subds, struct subd); subd->peer = peer; subd->outq = msg_queue_new(subd, false); + subd->channel_id = *channel_id; + subd->temporary_channel_id = NULL; /* This sets subd->conn inside subd_conn_init */ io_new_conn(peer, fds[0], subd_conn_init, subd); /* When conn dies, subd is freed. */ diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 99a6e84be..2237d9f36 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1237,7 +1237,7 @@ void peer_active(struct lightningd *ld, const u8 *msg, int fd) struct node_id id; u16 *msgtype; struct channel *channel; - struct channel_id *channel_id; + struct channel_id channel_id; struct peer *peer; bool dual_fund; u8 *error; @@ -1342,8 +1342,8 @@ void peer_active(struct lightningd *ld, const u8 *msg, int fd) /* It's possible that they want to reestablish a channel, but * it's closed? */ - if (*msgtype == WIRE_CHANNEL_REESTABLISH && channel_id) { - channel = find_channel_by_id(peer, channel_id); + if (*msgtype == WIRE_CHANNEL_REESTABLISH) { + channel = find_channel_by_id(peer, &channel_id); if (channel && channel_closed(channel)) { log_debug(channel->log, "Reestablish on %s channel: using channeld to reply", @@ -1351,12 +1351,12 @@ void peer_active(struct lightningd *ld, const u8 *msg, int fd) peer_start_channeld(channel, peer_fd, NULL, true, true); return; } else { - const u8 *err = towire_errorfmt(tmpctx, channel_id, + const u8 *err = towire_errorfmt(tmpctx, &channel_id, "Unknown channel for reestablish"); log_peer_debug(ld->log, &peer->id, "Reestablish on UNKNOWN channel %s", type_to_string(tmpctx, struct channel_id, - channel_id)); + &channel_id)); /* Unless we're shutting down, tell connectd to send err */ if (ld->connectd) subd_send_msg(ld->connectd, @@ -1374,7 +1374,7 @@ void peer_active(struct lightningd *ld, const u8 *msg, int fd) channel = new_unsaved_channel(peer, peer->ld->config.fee_base, peer->ld->config.fee_per_satoshi); - channel->cid = *channel_id; + channel->cid = channel_id; peer_start_dualopend(peer, peer_fd, channel); } else { peer->uncommitted_channel = new_uncommitted_channel(peer); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 8240e771b..7e7a0f738 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -209,7 +209,7 @@ bool fromwire_channel_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, bool fromwire_channeld_dev_memleak_reply(const void *p UNNEEDED, bool *leak UNNEEDED) { fprintf(stderr, "fromwire_channeld_dev_memleak_reply called!\n"); abort(); } /* Generated stub for fromwire_connectd_peer_active */ -bool fromwire_connectd_peer_active(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, u16 **msgtype UNNEEDED, struct channel_id **channel_id UNNEEDED) +bool fromwire_connectd_peer_active(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, u16 **msgtype UNNEEDED, struct channel_id *channel_id UNNEEDED) { fprintf(stderr, "fromwire_connectd_peer_active called!\n"); abort(); } /* Generated stub for fromwire_connectd_peer_connected */ bool fromwire_connectd_peer_connected(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, struct wireaddr_internal *addr UNNEEDED, struct wireaddr **remote_addr UNNEEDED, bool *incoming UNNEEDED, u8 **features UNNEEDED) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 62b6c2296..1f05d6fff 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -153,7 +153,7 @@ bool fromwire_channeld_offer_htlc_reply(const tal_t *ctx UNNEEDED, const void *p bool fromwire_channeld_sending_commitsig(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u64 *commitnum UNNEEDED, struct penalty_base **pbase UNNEEDED, struct fee_states **fee_states UNNEEDED, struct height_states **blockheight_states UNNEEDED, struct changed_htlc **changed UNNEEDED, struct bitcoin_signature *commit_sig UNNEEDED, struct bitcoin_signature **htlc_sigs UNNEEDED) { fprintf(stderr, "fromwire_channeld_sending_commitsig called!\n"); abort(); } /* Generated stub for fromwire_connectd_peer_active */ -bool fromwire_connectd_peer_active(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, u16 **msgtype UNNEEDED, struct channel_id **channel_id UNNEEDED) +bool fromwire_connectd_peer_active(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, u16 **msgtype UNNEEDED, struct channel_id *channel_id UNNEEDED) { fprintf(stderr, "fromwire_connectd_peer_active called!\n"); abort(); } /* Generated stub for fromwire_connectd_peer_connected */ bool fromwire_connectd_peer_connected(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct node_id *id UNNEEDED, struct wireaddr_internal *addr UNNEEDED, struct wireaddr **remote_addr UNNEEDED, bool *incoming UNNEEDED, u8 **features UNNEEDED)