plugins/libplugin: flatten return from json_to_listpeers_result.

Instead of returning a peers -> channels heirarchy, return (as callers
want!) a flat array of channels.

This is actually most of the transition work to make them work with
listpeerchannels.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-01-12 11:48:10 +10:30
parent cb5ee7e49c
commit 57dcf68c0b
4 changed files with 51 additions and 99 deletions

View File

@ -3284,34 +3284,31 @@ static struct command_result *direct_pay_listpeers(struct command *cmd,
const jsmntok_t *toks, const jsmntok_t *toks,
struct payment *p) struct payment *p)
{ {
struct listpeers_result *r = struct listpeers_channel **channels = json_to_listpeers_channels(tmpctx, buffer, toks);
json_to_listpeers_result(tmpctx, buffer, toks);
struct direct_pay_data *d = payment_mod_directpay_get_data(p); struct direct_pay_data *d = payment_mod_directpay_get_data(p);
if (r && tal_count(r->peers) == 1) { for (size_t i=0; i<tal_count(channels); i++) {
struct listpeers_peer *peer = r->peers[0]; struct listpeers_channel *chan = channels[i];
if (!peer->connected)
goto cont;
for (size_t i=0; i<tal_count(peer->channels); i++) { if (!chan->connected)
struct listpeers_channel *chan = r->peers[0]->channels[i]; continue;
if (!streq(chan->state, "CHANNELD_NORMAL"))
continue;
/* Must have either a local alias for zeroconf if (!streq(chan->state, "CHANNELD_NORMAL"))
* channels or a final scid. */ continue;
assert(chan->alias[LOCAL] || chan->scid);
d->chan = tal(d, struct short_channel_id_dir); /* Must have either a local alias for zeroconf
if (chan->scid) { * channels or a final scid. */
d->chan->scid = *chan->scid; assert(chan->alias[LOCAL] || chan->scid);
d->chan->dir = *chan->direction; d->chan = tal(d, struct short_channel_id_dir);
} else { if (chan->scid) {
d->chan->scid = *chan->alias[LOCAL]; d->chan->scid = *chan->scid;
d->chan->dir = 0; /* Don't care. */ d->chan->dir = *chan->direction;
} } else {
d->chan->scid = *chan->alias[LOCAL];
d->chan->dir = 0; /* Don't care. */
} }
} }
cont:
direct_pay_override(p); direct_pay_override(p);
return command_still_pending(cmd); return command_still_pending(cmd);

View File

@ -1914,15 +1914,6 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
json_get_member(buffer, tok, "spendable_msat"), json_get_member(buffer, tok, "spendable_msat"),
*aliastok = json_get_member(buffer, tok, "alias"); *aliastok = json_get_member(buffer, tok, "alias");
if (privtok == NULL || privtok->type != JSMN_PRIMITIVE ||
statetok == NULL || statetok->type != JSMN_STRING ||
ftxidtok == NULL || ftxidtok->type != JSMN_STRING ||
(scidtok != NULL && scidtok->type != JSMN_STRING) ||
(dirtok != NULL && dirtok->type != JSMN_PRIMITIVE) ||
tmsattok == NULL ||
smsattok == NULL)
return NULL;
chan = tal(ctx, struct listpeers_channel); chan = tal(ctx, struct listpeers_channel);
json_to_bool(buffer, privtok, &chan->private); json_to_bool(buffer, privtok, &chan->private);
@ -1973,70 +1964,43 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
return chan; return chan;
} }
static struct listpeers_peer *json_to_listpeers_peer(const tal_t *ctx, /* Append channels for this peer */
const char *buffer, static void json_add_listpeers_peer(struct listpeers_channel ***chans,
const jsmntok_t *tok) const char *buffer,
const jsmntok_t *tok)
{ {
struct listpeers_peer *res;
size_t i; size_t i;
const jsmntok_t *iter; const jsmntok_t *iter;
const jsmntok_t *idtok = json_get_member(buffer, tok, "id"), const jsmntok_t *idtok = json_get_member(buffer, tok, "id"),
*conntok = json_get_member(buffer, tok, "connected"), *conntok = json_get_member(buffer, tok, "connected"),
*netaddrtok = json_get_member(buffer, tok, "netaddr"),
*channelstok = json_get_member(buffer, tok, "channels"); *channelstok = json_get_member(buffer, tok, "channels");
bool connected;
struct node_id id;
/* Preliminary sanity checks. */ json_to_node_id(buffer, idtok, &id);
if (idtok == NULL || idtok->type != JSMN_STRING || conntok == NULL || json_to_bool(buffer, conntok, &connected);
conntok->type != JSMN_PRIMITIVE ||
(netaddrtok != NULL && netaddrtok->type != JSMN_ARRAY) ||
channelstok == NULL || channelstok->type != JSMN_ARRAY)
return NULL;
res = tal(ctx, struct listpeers_peer);
json_to_node_id(buffer, idtok, &res->id);
json_to_bool(buffer, conntok, &res->connected);
res->netaddr = tal_arr(res, const char *, 0);
if (netaddrtok != NULL) {
json_for_each_arr(i, iter, netaddrtok) {
tal_arr_expand(&res->netaddr,
json_strdup(res, buffer, iter));
}
}
res->channels = tal_arr(res, struct listpeers_channel *, 0);
json_for_each_arr(i, iter, channelstok) { json_for_each_arr(i, iter, channelstok) {
struct listpeers_channel *chan = json_to_listpeers_channel(res, buffer, iter); struct listpeers_channel *chan = json_to_listpeers_channel(*chans, buffer, iter);
assert(chan != NULL); chan->id = id;
tal_arr_expand(&res->channels, chan); chan->connected = connected;
tal_arr_expand(chans, chan);
} }
return res;
} }
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx, struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
const char *buffer, const char *buffer,
const jsmntok_t *toks) const jsmntok_t *tok)
{ {
size_t i; size_t i;
const jsmntok_t *iter; const jsmntok_t *iter;
struct listpeers_result *res; const jsmntok_t *peerstok = json_get_member(buffer, tok, "peers");
const jsmntok_t *peerstok = json_get_member(buffer, toks, "peers"); struct listpeers_channel **chans;
if (peerstok == NULL || peerstok->type != JSMN_ARRAY) chans = tal_arr(ctx, struct listpeers_channel *, 0);
return NULL; json_for_each_obj(i, iter, peerstok)
json_add_listpeers_peer(&chans, buffer, iter);
res = tal(ctx, struct listpeers_result); return chans;
res->peers = tal_arr(res, struct listpeers_peer *, 0);
json_for_each_obj(i, iter, peerstok) {
struct listpeers_peer *p =
json_to_listpeers_peer(res, buffer, iter);
if (p == NULL)
return tal_free(res);
tal_arr_expand(&res->peers, p);
}
return res;
} }
struct createonion_response *json_to_createonion_response(const tal_t *ctx, struct createonion_response *json_to_createonion_response(const tal_t *ctx,

View File

@ -433,6 +433,8 @@ void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
...); ...);
struct listpeers_channel { struct listpeers_channel {
struct node_id id;
bool connected;
bool private; bool private;
struct bitcoin_txid funding_txid; struct bitcoin_txid funding_txid;
const char *state; const char *state;
@ -444,21 +446,10 @@ struct listpeers_channel {
/* TODO Add fields as we need them. */ /* TODO Add fields as we need them. */
}; };
struct listpeers_peer { /* Returns an array of listpeers_channel * */
struct node_id id; struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
bool connected; const char *buffer,
const char **netaddr; const jsmntok_t *tok);
struct feature_set *features;
struct listpeers_channel **channels;
};
struct listpeers_result {
struct listpeers_peer **peers;
};
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok);
struct createonion_response { struct createonion_response {
u8 *onion; u8 *onion;

View File

@ -133,11 +133,11 @@ struct createonion_response *json_to_createonion_response(const tal_t *ctx UNNEE
/* Generated stub for json_to_int */ /* Generated stub for json_to_int */
bool json_to_int(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, int *num UNNEEDED) bool json_to_int(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, int *num UNNEEDED)
{ fprintf(stderr, "json_to_int called!\n"); abort(); } { fprintf(stderr, "json_to_int called!\n"); abort(); }
/* Generated stub for json_to_listpeers_result */ /* Generated stub for json_to_listpeers_channels */
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx UNNEEDED, struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx UNNEEDED,
const char *buffer UNNEEDED, const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED) const jsmntok_t *tok UNNEEDED)
{ fprintf(stderr, "json_to_listpeers_result called!\n"); abort(); } { fprintf(stderr, "json_to_listpeers_channels called!\n"); abort(); }
/* Generated stub for json_to_msat */ /* Generated stub for json_to_msat */
bool json_to_msat(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool json_to_msat(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct amount_msat *msat UNNEEDED) struct amount_msat *msat UNNEEDED)