Skip to content

Commit aa5ef99

Browse files
committed
Add optional state filter to listpeerchannels per issue ElementsProject#4734
1 parent 4572dfa commit aa5ef99

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

lightningd/channel.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,35 @@ const char *channel_state_str(enum channel_state state)
491491
return "unknown";
492492
}
493493

494+
char *channel_state_option(const char *arg, enum channel_state *opt)
495+
{
496+
for (size_t i=0; enum_channel_state_names[i].v; i++) {
497+
if (streq(arg, enum_channel_state_names[i].name)) {
498+
*opt = enum_channel_state_names[i].v;
499+
return NULL;
500+
}
501+
}
502+
return tal_fmt(NULL, "'%s' is not a valid channel state", arg);
503+
}
504+
505+
struct command_result *
506+
param_channel_state(struct command *cmd, const char *name,
507+
const char *buffer, const jsmntok_t *tok,
508+
enum channel_state **opt)
509+
{
510+
char *opt_str, *err;
511+
512+
*opt = tal(cmd, enum channel_state);
513+
opt_str = tal_strndup(cmd, buffer + tok->start,
514+
tok->end - tok->start);
515+
516+
err = channel_state_option(opt_str, *opt);
517+
if (err)
518+
return command_fail_badparam(cmd, name, buffer, tok, err);
519+
520+
return NULL;
521+
}
522+
494523
struct channel *peer_unsaved_channel(struct peer *peer)
495524
{
496525
struct channel *channel;

lightningd/channel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,13 @@ u32 channel_last_funding_feerate(const struct channel *channel);
332332

333333
void delete_channel(struct channel *channel STEALS);
334334

335+
/* Convert a command-line option to a state name */
336+
char *channel_state_option(const char *arg, enum channel_state *opt);
337+
struct command_result *
338+
param_channel_state(struct command *cmd, const char *name,
339+
const char *buffer, const jsmntok_t *tok,
340+
enum channel_state **opt);
341+
335342
const char *channel_state_name(const struct channel *channel);
336343
const char *channel_state_str(enum channel_state state);
337344

lightningd/peer_control.c

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,38 +1614,58 @@ static const struct json_command listpeers_command = {
16141614
AUTODATA(json_command, &listpeers_command);
16151615

16161616

1617+
static void json_add_peerchannels(struct lightningd *ld,
1618+
struct json_stream *response,
1619+
struct peer *peer,
1620+
enum channel_state *state,
1621+
const enum log_level *ll)
1622+
{
1623+
struct channel *channel;
1624+
1625+
if (!state)
1626+
json_add_uncommitted_channel(response, peer->uncommitted_channel);
1627+
1628+
list_for_each(&peer->channels, channel, list) {
1629+
if (!state || channel->state == *state) {
1630+
if (channel_unsaved(channel))
1631+
json_add_unsaved_channel(response, channel);
1632+
else
1633+
json_add_channel(ld, response, NULL, channel);
1634+
}
1635+
}
1636+
1637+
if (ll)
1638+
json_add_log(response, ld->log_book, &peer->id, *ll);
1639+
}
1640+
16171641
static struct command_result *json_listpeerchannels(struct command *cmd,
16181642
const char *buffer,
16191643
const jsmntok_t *obj UNNEEDED,
16201644
const jsmntok_t *params)
16211645
{
16221646
enum log_level *ll;
16231647
struct node_id *peer_id;
1648+
enum channel_state *state;
16241649
struct peer *peer;
1625-
struct channel *channel;
16261650
struct json_stream *response;
16271651

16281652
if (!param(cmd, buffer, params,
1629-
p_req("id", param_node_id, &peer_id),
1653+
p_opt("id", param_node_id, &peer_id),
1654+
p_opt("state", param_channel_state, &state),
16301655
p_opt("level", param_loglevel, &ll),
16311656
NULL))
16321657
return command_param_failed();
16331658

1634-
peer = peer_by_id(cmd->ld, peer_id);
1635-
if (!peer) {
1636-
return command_fail(cmd, LIGHTNINGD,
1637-
"Could not find peer with that id");
1638-
}
1639-
16401659
response = json_stream_success(cmd);
16411660
json_array_start(response, "channels");
1642-
json_add_uncommitted_channel(response, peer->uncommitted_channel);
16431661

1644-
list_for_each(&peer->channels, channel, list) {
1645-
if (channel_unsaved(channel))
1646-
json_add_unsaved_channel(response, channel);
1647-
else
1648-
json_add_channel(cmd->ld, response, NULL, channel);
1662+
if (peer_id) {
1663+
peer = peer_by_id(cmd->ld, peer_id);
1664+
if (peer)
1665+
json_add_peerchannels(cmd->ld, response, peer, state, ll);
1666+
} else {
1667+
list_for_each(&cmd->ld->peers, peer, list)
1668+
json_add_peerchannels(cmd->ld, response, peer, state, ll);
16491669
}
16501670

16511671
json_array_end(response);

lightningd/test/run-invoice-select-inchan.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,11 @@ struct txowatch *watch_txo(const tal_t *ctx UNNEEDED,
861861
size_t input_num UNNEEDED,
862862
const struct block *block))
863863
{ fprintf(stderr, "watch_txo called!\n"); abort(); }
864+
struct command_result *
865+
param_channel_state(struct command *cmd, const char *name,
866+
const char *buffer, const jsmntok_t *tok,
867+
enum channel_state **opt)
868+
{ fprintf(stderr, "param_channel_state called!\n"); abort(); }
864869
/* AUTOGENERATED MOCKS END */
865870

866871
#if DEVELOPER

0 commit comments

Comments
 (0)