mirror of
https://github.com/ElementsProject/lightning.git
synced 2024-11-19 09:54:16 +01:00
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:
parent
cb5ee7e49c
commit
57dcf68c0b
@ -3284,34 +3284,31 @@ static struct command_result *direct_pay_listpeers(struct command *cmd,
|
||||
const jsmntok_t *toks,
|
||||
struct payment *p)
|
||||
{
|
||||
struct listpeers_result *r =
|
||||
json_to_listpeers_result(tmpctx, buffer, toks);
|
||||
struct listpeers_channel **channels = json_to_listpeers_channels(tmpctx, buffer, toks);
|
||||
struct direct_pay_data *d = payment_mod_directpay_get_data(p);
|
||||
|
||||
if (r && tal_count(r->peers) == 1) {
|
||||
struct listpeers_peer *peer = r->peers[0];
|
||||
if (!peer->connected)
|
||||
goto cont;
|
||||
for (size_t i=0; i<tal_count(channels); i++) {
|
||||
struct listpeers_channel *chan = channels[i];
|
||||
|
||||
for (size_t i=0; i<tal_count(peer->channels); i++) {
|
||||
struct listpeers_channel *chan = r->peers[0]->channels[i];
|
||||
if (!streq(chan->state, "CHANNELD_NORMAL"))
|
||||
continue;
|
||||
if (!chan->connected)
|
||||
continue;
|
||||
|
||||
/* Must have either a local alias for zeroconf
|
||||
* channels or a final scid. */
|
||||
assert(chan->alias[LOCAL] || chan->scid);
|
||||
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. */
|
||||
}
|
||||
if (!streq(chan->state, "CHANNELD_NORMAL"))
|
||||
continue;
|
||||
|
||||
/* Must have either a local alias for zeroconf
|
||||
* channels or a final scid. */
|
||||
assert(chan->alias[LOCAL] || chan->scid);
|
||||
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. */
|
||||
}
|
||||
}
|
||||
cont:
|
||||
|
||||
direct_pay_override(p);
|
||||
return command_still_pending(cmd);
|
||||
|
||||
|
@ -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,
|
||||
|
@ -433,6 +433,8 @@ void NORETURN LAST_ARG_NULL plugin_main(char *argv[],
|
||||
...);
|
||||
|
||||
struct listpeers_channel {
|
||||
struct node_id id;
|
||||
bool connected;
|
||||
bool private;
|
||||
struct bitcoin_txid funding_txid;
|
||||
const char *state;
|
||||
@ -444,21 +446,10 @@ struct listpeers_channel {
|
||||
/* TODO Add fields as we need them. */
|
||||
};
|
||||
|
||||
struct listpeers_peer {
|
||||
struct node_id id;
|
||||
bool connected;
|
||||
const char **netaddr;
|
||||
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);
|
||||
/* Returns an array of listpeers_channel * */
|
||||
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx,
|
||||
const char *buffer,
|
||||
const jsmntok_t *tok);
|
||||
|
||||
struct createonion_response {
|
||||
u8 *onion;
|
||||
|
@ -133,11 +133,11 @@ struct createonion_response *json_to_createonion_response(const tal_t *ctx UNNEE
|
||||
/* Generated stub for json_to_int */
|
||||
bool json_to_int(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, int *num UNNEEDED)
|
||||
{ fprintf(stderr, "json_to_int called!\n"); abort(); }
|
||||
/* Generated stub for json_to_listpeers_result */
|
||||
struct listpeers_result *json_to_listpeers_result(const tal_t *ctx UNNEEDED,
|
||||
const char *buffer UNNEEDED,
|
||||
const jsmntok_t *tok UNNEEDED)
|
||||
{ fprintf(stderr, "json_to_listpeers_result called!\n"); abort(); }
|
||||
/* Generated stub for json_to_listpeers_channels */
|
||||
struct listpeers_channel **json_to_listpeers_channels(const tal_t *ctx UNNEEDED,
|
||||
const char *buffer UNNEEDED,
|
||||
const jsmntok_t *tok UNNEEDED)
|
||||
{ fprintf(stderr, "json_to_listpeers_channels called!\n"); abort(); }
|
||||
/* Generated stub for json_to_msat */
|
||||
bool json_to_msat(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
|
||||
struct amount_msat *msat UNNEEDED)
|
||||
|
Loading…
Reference in New Issue
Block a user