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

@@ -1914,15 +1914,6 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
json_get_member(buffer, tok, "spendable_msat"),
*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);
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;
}
static struct listpeers_peer *json_to_listpeers_peer(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok)
/* Append channels for this peer */
static void json_add_listpeers_peer(struct listpeers_channel ***chans,
const char *buffer,
const jsmntok_t *tok)
{
struct listpeers_peer *res;
size_t i;
const jsmntok_t *iter;
const jsmntok_t *idtok = json_get_member(buffer, tok, "id"),
*conntok = json_get_member(buffer, tok, "connected"),
*netaddrtok = json_get_member(buffer, tok, "netaddr"),
*channelstok = json_get_member(buffer, tok, "channels");
bool connected;
struct node_id id;
/* Preliminary sanity checks. */
if (idtok == NULL || idtok->type != JSMN_STRING || conntok == NULL ||
conntok->type != JSMN_PRIMITIVE ||
(netaddrtok != NULL && netaddrtok->type != JSMN_ARRAY) ||
channelstok == NULL || channelstok->type != JSMN_ARRAY)
return NULL;
json_to_node_id(buffer, idtok, &id);
json_to_bool(buffer, conntok, &connected);
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) {
struct listpeers_channel *chan = json_to_listpeers_channel(res, buffer, iter);
assert(chan != NULL);
tal_arr_expand(&res->channels, chan);
struct listpeers_channel *chan = json_to_listpeers_channel(*chans, buffer, iter);
chan->id = id;
chan->connected = connected;
tal_arr_expand(chans, chan);
}
return res;
}
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx,
const char *buffer,
const jsmntok_t *toks)
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok)
{
size_t i;
const jsmntok_t *iter;
struct listpeers_result *res;
const jsmntok_t *peerstok = json_get_member(buffer, toks, "peers");
const jsmntok_t *peerstok = json_get_member(buffer, tok, "peers");
struct listpeers_channel **chans;
if (peerstok == NULL || peerstok->type != JSMN_ARRAY)
return NULL;
res = tal(ctx, struct listpeers_result);
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;
chans = tal_arr(ctx, struct listpeers_channel *, 0);
json_for_each_obj(i, iter, peerstok)
json_add_listpeers_peer(&chans, buffer, iter);
return chans;
}
struct createonion_response *json_to_createonion_response(const tal_t *ctx,