From 5d5b9c6812f86646929e5655b2b211637d88f9a9 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 12 Jan 2023 11:49:10 +1030 Subject: [PATCH] libplugin: don't return unopened channels from json_to_listpeers_channels(). This way we always have an SCID and a direction. Signed-off-by: Rusty Russell --- plugins/libplugin-pay.c | 3 +-- plugins/libplugin.c | 16 ++++++++-------- plugins/libplugin.h | 3 ++- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index d47430aab..02bacca9c 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -3302,11 +3302,10 @@ static struct command_result *direct_pay_listpeers(struct command *cmd, d->chan = tal(d, struct short_channel_id_dir); if (chan->scid) { d->chan->scid = *chan->scid; - d->chan->dir = *chan->direction; } else { d->chan->scid = *chan->alias[LOCAL]; - d->chan->dir = 0; /* Don't care. */ } + d->chan->dir = chan->direction; } direct_pay_override(p); diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 19d9b3f25..cebee85c6 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -1925,14 +1925,6 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx, json_to_short_channel_id(buffer, scidtok, chan->scid); } else { chan->scid = NULL; - chan->direction = NULL; - } - - if (dirtok != NULL) { - chan->direction = tal(chan, int); - json_to_int(buffer, dirtok, chan->direction); - } else { - chan->direction = NULL; } if (aliastok != NULL) { @@ -1958,6 +1950,12 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx, chan->alias[REMOTE] = NULL; } + /* If we catch a channel during opening, these might not be set. + * It's not a real channel (yet), so ignore it! */ + if (!chan->scid && !chan->alias[LOCAL]) + return tal_free(chan); + + json_to_int(buffer, dirtok, &chan->direction); json_to_msat(buffer, tmsattok, &chan->total_msat); json_to_msat(buffer, smsattok, &chan->spendable_msat); @@ -1982,6 +1980,8 @@ static void json_add_listpeers_peer(struct listpeers_channel ***chans, json_for_each_arr(i, iter, channelstok) { struct listpeers_channel *chan = json_to_listpeers_channel(*chans, buffer, iter); + if (!chan) + continue; chan->id = id; chan->connected = connected; tal_arr_expand(chans, chan); diff --git a/plugins/libplugin.h b/plugins/libplugin.h index bd74f5827..a846317f1 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -438,9 +438,10 @@ struct listpeers_channel { bool private; struct bitcoin_txid funding_txid; const char *state; + /* scid or alias[LOCAL] is always non-NULL */ struct short_channel_id *alias[NUM_SIDES]; struct short_channel_id *scid; - int *direction; + int direction; struct amount_msat total_msat; struct amount_msat spendable_msat; /* TODO Add fields as we need them. */