Skip to content

Commit

Permalink
Add optional state filter to listpeerchannels per issue ElementsProje…
Browse files Browse the repository at this point in the history
  • Loading branch information
kandycoder committed Aug 24, 2021
1 parent 4572dfa commit aa5ef99
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
29 changes: 29 additions & 0 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,35 @@ const char *channel_state_str(enum channel_state state)
return "unknown";
}

char *channel_state_option(const char *arg, enum channel_state *opt)
{
for (size_t i=0; enum_channel_state_names[i].v; i++) {
if (streq(arg, enum_channel_state_names[i].name)) {
*opt = enum_channel_state_names[i].v;
return NULL;
}
}
return tal_fmt(NULL, "'%s' is not a valid channel state", arg);
}

struct command_result *
param_channel_state(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
enum channel_state **opt)
{
char *opt_str, *err;

*opt = tal(cmd, enum channel_state);
opt_str = tal_strndup(cmd, buffer + tok->start,
tok->end - tok->start);

err = channel_state_option(opt_str, *opt);
if (err)
return command_fail_badparam(cmd, name, buffer, tok, err);

return NULL;
}

struct channel *peer_unsaved_channel(struct peer *peer)
{
struct channel *channel;
Expand Down
7 changes: 7 additions & 0 deletions lightningd/channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,13 @@ u32 channel_last_funding_feerate(const struct channel *channel);

void delete_channel(struct channel *channel STEALS);

/* Convert a command-line option to a state name */
char *channel_state_option(const char *arg, enum channel_state *opt);
struct command_result *
param_channel_state(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
enum channel_state **opt);

const char *channel_state_name(const struct channel *channel);
const char *channel_state_str(enum channel_state state);

Expand Down
48 changes: 34 additions & 14 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -1614,38 +1614,58 @@ static const struct json_command listpeers_command = {
AUTODATA(json_command, &listpeers_command);


static void json_add_peerchannels(struct lightningd *ld,
struct json_stream *response,
struct peer *peer,
enum channel_state *state,
const enum log_level *ll)
{
struct channel *channel;

if (!state)
json_add_uncommitted_channel(response, peer->uncommitted_channel);

list_for_each(&peer->channels, channel, list) {
if (!state || channel->state == *state) {
if (channel_unsaved(channel))
json_add_unsaved_channel(response, channel);
else
json_add_channel(ld, response, NULL, channel);
}
}

if (ll)
json_add_log(response, ld->log_book, &peer->id, *ll);
}

static struct command_result *json_listpeerchannels(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
enum log_level *ll;
struct node_id *peer_id;
enum channel_state *state;
struct peer *peer;
struct channel *channel;
struct json_stream *response;

if (!param(cmd, buffer, params,
p_req("id", param_node_id, &peer_id),
p_opt("id", param_node_id, &peer_id),
p_opt("state", param_channel_state, &state),
p_opt("level", param_loglevel, &ll),
NULL))
return command_param_failed();

peer = peer_by_id(cmd->ld, peer_id);
if (!peer) {
return command_fail(cmd, LIGHTNINGD,
"Could not find peer with that id");
}

response = json_stream_success(cmd);
json_array_start(response, "channels");
json_add_uncommitted_channel(response, peer->uncommitted_channel);

list_for_each(&peer->channels, channel, list) {
if (channel_unsaved(channel))
json_add_unsaved_channel(response, channel);
else
json_add_channel(cmd->ld, response, NULL, channel);
if (peer_id) {
peer = peer_by_id(cmd->ld, peer_id);
if (peer)
json_add_peerchannels(cmd->ld, response, peer, state, ll);
} else {
list_for_each(&cmd->ld->peers, peer, list)
json_add_peerchannels(cmd->ld, response, peer, state, ll);
}

json_array_end(response);
Expand Down
5 changes: 5 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,11 @@ struct txowatch *watch_txo(const tal_t *ctx UNNEEDED,
size_t input_num UNNEEDED,
const struct block *block))
{ fprintf(stderr, "watch_txo called!\n"); abort(); }
struct command_result *
param_channel_state(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
enum channel_state **opt)
{ fprintf(stderr, "param_channel_state called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

#if DEVELOPER
Expand Down

0 comments on commit aa5ef99

Please sign in to comment.