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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-01-12 11:45:10 +10:30
parent 300f732bbe
commit 1d8b899551
7 changed files with 44 additions and 12 deletions

View file

@ -105,7 +105,9 @@ static void channel_err_broken(struct channel *channel,
} }
void json_add_unsaved_channel(struct json_stream *response, 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 amount_msat total;
struct open_attempt *oa; struct open_attempt *oa;
@ -125,6 +127,11 @@ void json_add_unsaved_channel(struct json_stream *response,
oa = channel->open_attempt; oa = channel->open_attempt;
json_object_start(response, NULL); 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, "state", channel_state_name(channel));
json_add_string(response, "owner", channel->owner->name); json_add_string(response, "owner", channel->owner->name);
json_add_string(response, "opener", channel->opener == LOCAL ? json_add_string(response, "opener", channel->opener == LOCAL ?

View file

@ -22,7 +22,9 @@ void dualopen_tell_depth(struct subd *dualopend,
void channel_unsaved_close_conn(struct channel *channel, const char *why); void channel_unsaved_close_conn(struct channel *channel, const char *why);
void json_add_unsaved_channel(struct json_stream *response, 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, void channel_update_reserve(struct channel *channel,
struct channel_config *their_config, struct channel_config *their_config,

View file

@ -32,9 +32,12 @@
#include <wally_psbt.h> #include <wally_psbt.h>
void json_add_uncommitted_channel(struct json_stream *response, 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; struct amount_msat total, ours;
if (!uc) if (!uc)
return; return;
@ -43,6 +46,10 @@ void json_add_uncommitted_channel(struct json_stream *response,
return; return;
json_object_start(response, NULL); 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, "state", "OPENINGD");
json_add_string(response, "owner", "lightning_openingd"); json_add_string(response, "owner", "lightning_openingd");
json_add_string(response, "opener", "local"); json_add_string(response, "opener", "local");

View file

@ -12,7 +12,9 @@ struct peer_fd;
struct uncommitted_channel; struct uncommitted_channel;
void json_add_uncommitted_channel(struct json_stream *response, 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, bool peer_start_openingd(struct peer *peer,
struct peer_fd *peer_fd); struct peer_fd *peer_fd);

View file

@ -695,7 +695,9 @@ struct amount_msat channel_amount_receivable(const struct channel *channel)
static void json_add_channel(struct lightningd *ld, static void json_add_channel(struct lightningd *ld,
struct json_stream *response, const char *key, 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 channel_stats channel_stats;
struct amount_msat funding_msat; struct amount_msat funding_msat;
@ -704,6 +706,10 @@ static void json_add_channel(struct lightningd *ld,
u32 feerate; u32 feerate;
json_object_start(response, key); 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)); json_add_string(response, "state", channel_state_name(channel));
if (channel->last_tx && !invalid_last_tx(channel->last_tx)) { if (channel->last_tx && !invalid_last_tx(channel->last_tx)) {
struct bitcoin_txid txid; struct bitcoin_txid txid;
@ -1936,13 +1942,13 @@ static void json_add_peer(struct lightningd *ld,
} }
json_array_start(response, "channels"); 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) { list_for_each(&p->channels, channel, list) {
if (channel_unsaved(channel)) if (channel_unsaved(channel))
json_add_unsaved_channel(response, channel); json_add_unsaved_channel(response, channel, NULL);
else else
json_add_channel(ld, response, NULL, channel); json_add_channel(ld, response, NULL, channel, NULL);
} }
json_array_end(response); json_array_end(response);

View file

@ -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(); } { fprintf(stderr, "json_add_u64 called!\n"); abort(); }
/* Generated stub for json_add_uncommitted_channel */ /* Generated stub for json_add_uncommitted_channel */
void json_add_uncommitted_channel(struct json_stream *response UNNEEDED, 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(); } { fprintf(stderr, "json_add_uncommitted_channel called!\n"); abort(); }
/* Generated stub for json_add_unsaved_channel */ /* Generated stub for json_add_unsaved_channel */
void json_add_unsaved_channel(struct json_stream *response UNNEEDED, 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(); } { fprintf(stderr, "json_add_unsaved_channel called!\n"); abort(); }
/* Generated stub for json_array_end */ /* Generated stub for json_array_end */
void json_array_end(struct json_stream *js UNNEEDED) void json_array_end(struct json_stream *js UNNEEDED)

View file

@ -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(); } { fprintf(stderr, "json_add_u64 called!\n"); abort(); }
/* Generated stub for json_add_uncommitted_channel */ /* Generated stub for json_add_uncommitted_channel */
void json_add_uncommitted_channel(struct json_stream *response UNNEEDED, 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(); } { fprintf(stderr, "json_add_uncommitted_channel called!\n"); abort(); }
/* Generated stub for json_add_unsaved_channel */ /* Generated stub for json_add_unsaved_channel */
void json_add_unsaved_channel(struct json_stream *response UNNEEDED, 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(); } { fprintf(stderr, "json_add_unsaved_channel called!\n"); abort(); }
/* Generated stub for json_array_end */ /* Generated stub for json_array_end */
void json_array_end(struct json_stream *js UNNEEDED) void json_array_end(struct json_stream *js UNNEEDED)