From 1d8b8995514b95846d74173d9317d6301694b04c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 12 Jan 2023 11:45:10 +1030 Subject: [PATCH] lightningd: prepare internal json routines for listpeerchannels. We're soon going to call json_add_unsaved_channel and json_add_uncommitted_channel from a new place, where we want the peer state directly included. Based on patch by @vincenzopalazzo. Signed-off-by: Rusty Russell --- lightningd/dual_open_control.c | 9 ++++++++- lightningd/dual_open_control.h | 4 +++- lightningd/opening_control.c | 9 ++++++++- lightningd/opening_control.h | 4 +++- lightningd/peer_control.c | 14 ++++++++++---- lightningd/test/run-invoice-select-inchan.c | 8 ++++++-- wallet/test/run-wallet.c | 8 ++++++-- 7 files changed, 44 insertions(+), 12 deletions(-) diff --git a/lightningd/dual_open_control.c b/lightningd/dual_open_control.c index c65b2bd29..428c1ede1 100644 --- a/lightningd/dual_open_control.c +++ b/lightningd/dual_open_control.c @@ -105,7 +105,9 @@ static void channel_err_broken(struct channel *channel, } void json_add_unsaved_channel(struct json_stream *response, - const struct channel *channel) + const struct channel *channel, + /* Only set for listpeerchannels */ + const struct peer *peer) { struct amount_msat total; struct open_attempt *oa; @@ -125,6 +127,11 @@ void json_add_unsaved_channel(struct json_stream *response, oa = channel->open_attempt; json_object_start(response, NULL); + /* listpeerchannels only */ + if (peer) { + json_add_node_id(response, "peer_id", &peer->id); + json_add_bool(response, "peer_connected", peer->connected == PEER_CONNECTED); + } json_add_string(response, "state", channel_state_name(channel)); json_add_string(response, "owner", channel->owner->name); json_add_string(response, "opener", channel->opener == LOCAL ? diff --git a/lightningd/dual_open_control.h b/lightningd/dual_open_control.h index 7d34d9816..63c51bc3f 100644 --- a/lightningd/dual_open_control.h +++ b/lightningd/dual_open_control.h @@ -22,7 +22,9 @@ void dualopen_tell_depth(struct subd *dualopend, void channel_unsaved_close_conn(struct channel *channel, const char *why); void json_add_unsaved_channel(struct json_stream *response, - const struct channel *channel); + const struct channel *channel, + /* Only set for listpeerchannels */ + const struct peer *peer); void channel_update_reserve(struct channel *channel, struct channel_config *their_config, diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index a64764c85..2aebaec62 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -32,9 +32,12 @@ #include void json_add_uncommitted_channel(struct json_stream *response, - const struct uncommitted_channel *uc) + const struct uncommitted_channel *uc, + /* Only set for listpeerchannels */ + const struct peer *peer) { struct amount_msat total, ours; + if (!uc) return; @@ -43,6 +46,10 @@ void json_add_uncommitted_channel(struct json_stream *response, return; json_object_start(response, NULL); + if (peer) { + json_add_node_id(response, "peer_id", &peer->id); + json_add_bool(response, "peer_connected", peer->connected == PEER_CONNECTED); + } json_add_string(response, "state", "OPENINGD"); json_add_string(response, "owner", "lightning_openingd"); json_add_string(response, "opener", "local"); diff --git a/lightningd/opening_control.h b/lightningd/opening_control.h index a8f8d982a..258735713 100644 --- a/lightningd/opening_control.h +++ b/lightningd/opening_control.h @@ -12,7 +12,9 @@ struct peer_fd; struct uncommitted_channel; void json_add_uncommitted_channel(struct json_stream *response, - const struct uncommitted_channel *uc); + const struct uncommitted_channel *uc, + /* Only set for listpeerchannels */ + const struct peer *peer); bool peer_start_openingd(struct peer *peer, struct peer_fd *peer_fd); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 76f5a768e..7b18d2cc1 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -695,7 +695,9 @@ struct amount_msat channel_amount_receivable(const struct channel *channel) static void json_add_channel(struct lightningd *ld, struct json_stream *response, const char *key, - const struct channel *channel) + const struct channel *channel, + /* Only set for listpeerchannels */ + const struct peer *peer) { struct channel_stats channel_stats; struct amount_msat funding_msat; @@ -704,6 +706,10 @@ static void json_add_channel(struct lightningd *ld, u32 feerate; json_object_start(response, key); + if (peer) { + json_add_node_id(response, "peer_id", &peer->id); + json_add_bool(response, "peer_connected", peer->connected == PEER_CONNECTED); + } json_add_string(response, "state", channel_state_name(channel)); if (channel->last_tx && !invalid_last_tx(channel->last_tx)) { struct bitcoin_txid txid; @@ -1936,13 +1942,13 @@ static void json_add_peer(struct lightningd *ld, } json_array_start(response, "channels"); - json_add_uncommitted_channel(response, p->uncommitted_channel); + json_add_uncommitted_channel(response, p->uncommitted_channel, NULL); list_for_each(&p->channels, channel, list) { if (channel_unsaved(channel)) - json_add_unsaved_channel(response, channel); + json_add_unsaved_channel(response, channel, NULL); else - json_add_channel(ld, response, NULL, channel); + json_add_channel(ld, response, NULL, channel, NULL); } json_array_end(response); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index e1aee3435..0d3dfa82c 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -445,11 +445,15 @@ void json_add_u64(struct json_stream *result UNNEEDED, const char *fieldname UNN { fprintf(stderr, "json_add_u64 called!\n"); abort(); } /* Generated stub for json_add_uncommitted_channel */ void json_add_uncommitted_channel(struct json_stream *response UNNEEDED, - const struct uncommitted_channel *uc UNNEEDED) + const struct uncommitted_channel *uc UNNEEDED, + /* Only set for listpeerchannels */ + const struct peer *peer UNNEEDED) { fprintf(stderr, "json_add_uncommitted_channel called!\n"); abort(); } /* Generated stub for json_add_unsaved_channel */ void json_add_unsaved_channel(struct json_stream *response UNNEEDED, - const struct channel *channel UNNEEDED) + const struct channel *channel UNNEEDED, + /* Only set for listpeerchannels */ + const struct peer *peer UNNEEDED) { fprintf(stderr, "json_add_unsaved_channel called!\n"); abort(); } /* Generated stub for json_array_end */ void json_array_end(struct json_stream *js UNNEEDED) diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index e470a61e5..4421c59ae 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -403,11 +403,15 @@ void json_add_u64(struct json_stream *result UNNEEDED, const char *fieldname UNN { fprintf(stderr, "json_add_u64 called!\n"); abort(); } /* Generated stub for json_add_uncommitted_channel */ void json_add_uncommitted_channel(struct json_stream *response UNNEEDED, - const struct uncommitted_channel *uc UNNEEDED) + const struct uncommitted_channel *uc UNNEEDED, + /* Only set for listpeerchannels */ + const struct peer *peer UNNEEDED) { fprintf(stderr, "json_add_uncommitted_channel called!\n"); abort(); } /* Generated stub for json_add_unsaved_channel */ void json_add_unsaved_channel(struct json_stream *response UNNEEDED, - const struct channel *channel UNNEEDED) + const struct channel *channel UNNEEDED, + /* Only set for listpeerchannels */ + const struct peer *peer UNNEEDED) { fprintf(stderr, "json_add_unsaved_channel called!\n"); abort(); } /* Generated stub for json_array_end */ void json_array_end(struct json_stream *js UNNEEDED)