Skip to content

Commit 57dcf68

Browse files
committedJan 13, 2023
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 <[email protected]>
1 parent cb5ee7e commit 57dcf68

File tree

4 files changed

+53
-101
lines changed

4 files changed

+53
-101
lines changed
 

‎plugins/libplugin-pay.c

+21-24
Original file line numberDiff line numberDiff line change
@@ -3284,34 +3284,31 @@ static struct command_result *direct_pay_listpeers(struct command *cmd,
32843284
const jsmntok_t *toks,
32853285
struct payment *p)
32863286
{
3287-
struct listpeers_result *r =
3288-
json_to_listpeers_result(tmpctx, buffer, toks);
3287+
struct listpeers_channel **channels = json_to_listpeers_channels(tmpctx, buffer, toks);
32893288
struct direct_pay_data *d = payment_mod_directpay_get_data(p);
32903289

3291-
if (r && tal_count(r->peers) == 1) {
3292-
struct listpeers_peer *peer = r->peers[0];
3293-
if (!peer->connected)
3294-
goto cont;
3295-
3296-
for (size_t i=0; i<tal_count(peer->channels); i++) {
3297-
struct listpeers_channel *chan = r->peers[0]->channels[i];
3298-
if (!streq(chan->state, "CHANNELD_NORMAL"))
3299-
continue;
3300-
3301-
/* Must have either a local alias for zeroconf
3302-
* channels or a final scid. */
3303-
assert(chan->alias[LOCAL] || chan->scid);
3304-
d->chan = tal(d, struct short_channel_id_dir);
3305-
if (chan->scid) {
3306-
d->chan->scid = *chan->scid;
3307-
d->chan->dir = *chan->direction;
3308-
} else {
3309-
d->chan->scid = *chan->alias[LOCAL];
3310-
d->chan->dir = 0; /* Don't care. */
3311-
}
3290+
for (size_t i=0; i<tal_count(channels); i++) {
3291+
struct listpeers_channel *chan = channels[i];
3292+
3293+
if (!chan->connected)
3294+
continue;
3295+
3296+
if (!streq(chan->state, "CHANNELD_NORMAL"))
3297+
continue;
3298+
3299+
/* Must have either a local alias for zeroconf
3300+
* channels or a final scid. */
3301+
assert(chan->alias[LOCAL] || chan->scid);
3302+
d->chan = tal(d, struct short_channel_id_dir);
3303+
if (chan->scid) {
3304+
d->chan->scid = *chan->scid;
3305+
d->chan->dir = *chan->direction;
3306+
} else {
3307+
d->chan->scid = *chan->alias[LOCAL];
3308+
d->chan->dir = 0; /* Don't care. */
33123309
}
33133310
}
3314-
cont:
3311+
33153312
direct_pay_override(p);
33163313
return command_still_pending(cmd);
33173314

‎plugins/libplugin.c

+21-57
Original file line numberDiff line numberDiff line change
@@ -1914,15 +1914,6 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
19141914
json_get_member(buffer, tok, "spendable_msat"),
19151915
*aliastok = json_get_member(buffer, tok, "alias");
19161916

1917-
if (privtok == NULL || privtok->type != JSMN_PRIMITIVE ||
1918-
statetok == NULL || statetok->type != JSMN_STRING ||
1919-
ftxidtok == NULL || ftxidtok->type != JSMN_STRING ||
1920-
(scidtok != NULL && scidtok->type != JSMN_STRING) ||
1921-
(dirtok != NULL && dirtok->type != JSMN_PRIMITIVE) ||
1922-
tmsattok == NULL ||
1923-
smsattok == NULL)
1924-
return NULL;
1925-
19261917
chan = tal(ctx, struct listpeers_channel);
19271918

19281919
json_to_bool(buffer, privtok, &chan->private);
@@ -1973,70 +1964,43 @@ static struct listpeers_channel *json_to_listpeers_channel(const tal_t *ctx,
19731964
return chan;
19741965
}
19751966

1976-
static struct listpeers_peer *json_to_listpeers_peer(const tal_t *ctx,
1977-
const char *buffer,
1978-
const jsmntok_t *tok)
1967+
/* Append channels for this peer */
1968+
static void json_add_listpeers_peer(struct listpeers_channel ***chans,
1969+
const char *buffer,
1970+
const jsmntok_t *tok)
19791971
{
1980-
struct listpeers_peer *res;
19811972
size_t i;
19821973
const jsmntok_t *iter;
19831974
const jsmntok_t *idtok = json_get_member(buffer, tok, "id"),
19841975
*conntok = json_get_member(buffer, tok, "connected"),
1985-
*netaddrtok = json_get_member(buffer, tok, "netaddr"),
19861976
*channelstok = json_get_member(buffer, tok, "channels");
1977+
bool connected;
1978+
struct node_id id;
19871979

1988-
/* Preliminary sanity checks. */
1989-
if (idtok == NULL || idtok->type != JSMN_STRING || conntok == NULL ||
1990-
conntok->type != JSMN_PRIMITIVE ||
1991-
(netaddrtok != NULL && netaddrtok->type != JSMN_ARRAY) ||
1992-
channelstok == NULL || channelstok->type != JSMN_ARRAY)
1993-
return NULL;
1994-
1995-
res = tal(ctx, struct listpeers_peer);
1996-
json_to_node_id(buffer, idtok, &res->id);
1997-
json_to_bool(buffer, conntok, &res->connected);
1980+
json_to_node_id(buffer, idtok, &id);
1981+
json_to_bool(buffer, conntok, &connected);
19981982

1999-
res->netaddr = tal_arr(res, const char *, 0);
2000-
if (netaddrtok != NULL) {
2001-
json_for_each_arr(i, iter, netaddrtok) {
2002-
tal_arr_expand(&res->netaddr,
2003-
json_strdup(res, buffer, iter));
2004-
}
2005-
}
2006-
2007-
res->channels = tal_arr(res, struct listpeers_channel *, 0);
20081983
json_for_each_arr(i, iter, channelstok) {
2009-
struct listpeers_channel *chan = json_to_listpeers_channel(res, buffer, iter);
2010-
assert(chan != NULL);
2011-
tal_arr_expand(&res->channels, chan);
1984+
struct listpeers_channel *chan = json_to_listpeers_channel(*chans, buffer, iter);
1985+
chan->id = id;
1986+
chan->connected = connected;
1987+
tal_arr_expand(chans, chan);
20121988
}
2013-
2014-
return res;
20151989
}
20161990

2017-
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx,
2018-
const char *buffer,
2019-
const jsmntok_t *toks)
1991+
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
1992+
const char *buffer,
1993+
const jsmntok_t *tok)
20201994
{
20211995
size_t i;
20221996
const jsmntok_t *iter;
2023-
struct listpeers_result *res;
2024-
const jsmntok_t *peerstok = json_get_member(buffer, toks, "peers");
2025-
2026-
if (peerstok == NULL || peerstok->type != JSMN_ARRAY)
2027-
return NULL;
2028-
2029-
res = tal(ctx, struct listpeers_result);
2030-
res->peers = tal_arr(res, struct listpeers_peer *, 0);
1997+
const jsmntok_t *peerstok = json_get_member(buffer, tok, "peers");
1998+
struct listpeers_channel **chans;
20311999

2032-
json_for_each_obj(i, iter, peerstok) {
2033-
struct listpeers_peer *p =
2034-
json_to_listpeers_peer(res, buffer, iter);
2035-
if (p == NULL)
2036-
return tal_free(res);
2037-
tal_arr_expand(&res->peers, p);
2038-
}
2039-
return res;
2000+
chans = tal_arr(ctx, struct listpeers_channel *, 0);
2001+
json_for_each_obj(i, iter, peerstok)
2002+
json_add_listpeers_peer(&chans, buffer, iter);
2003+
return chans;
20402004
}
20412005

20422006
struct createonion_response *json_to_createonion_response(const tal_t *ctx,

‎plugins/libplugin.h

+6-15
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
433433
...);
434434

435435
struct listpeers_channel {
436+
struct node_id id;
437+
bool connected;
436438
bool private;
437439
struct bitcoin_txid funding_txid;
438440
const char *state;
@@ -444,21 +446,10 @@ struct listpeers_channel {
444446
/* TODO Add fields as we need them. */
445447
};
446448

447-
struct listpeers_peer {
448-
struct node_id id;
449-
bool connected;
450-
const char **netaddr;
451-
struct feature_set *features;
452-
struct listpeers_channel **channels;
453-
};
454-
455-
struct listpeers_result {
456-
struct listpeers_peer **peers;
457-
};
458-
459-
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx,
460-
const char *buffer,
461-
const jsmntok_t *tok);
449+
/* Returns an array of listpeers_channel * */
450+
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
451+
const char *buffer,
452+
const jsmntok_t *tok);
462453

463454
struct createonion_response {
464455
u8 *onion;

‎plugins/test/run-route-overlong.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -133,11 +133,11 @@ struct createonion_response *json_to_createonion_response(const tal_t *ctx UNNEE
133133
/* Generated stub for json_to_int */
134134
bool json_to_int(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, int *num UNNEEDED)
135135
{ fprintf(stderr, "json_to_int called!\n"); abort(); }
136-
/* Generated stub for json_to_listpeers_result */
137-
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx UNNEEDED,
138-
const char *buffer UNNEEDED,
139-
const jsmntok_t *tok UNNEEDED)
140-
{ fprintf(stderr, "json_to_listpeers_result called!\n"); abort(); }
136+
/* Generated stub for json_to_listpeers_channels */
137+
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx UNNEEDED,
138+
const char *buffer UNNEEDED,
139+
const jsmntok_t *tok UNNEEDED)
140+
{ fprintf(stderr, "json_to_listpeers_channels called!\n"); abort(); }
141141
/* Generated stub for json_to_msat */
142142
bool json_to_msat(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
143143
struct amount_msat *msat UNNEEDED)

0 commit comments

Comments
 (0)
Please sign in to comment.