Skip to content

Commit

Permalink
lightningd: make listconfigs return version as a notification.
Browse files Browse the repository at this point in the history
Good for testing: make test_libplugin call it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Sep 14, 2020
1 parent 6abd7a6 commit af22c25
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
9 changes: 5 additions & 4 deletions doc/lightning-listconfigs.7

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions doc/lightning-listconfigs.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@ EXAMPLE JSON REQUEST
RETURN VALUE
------------

A notification is given with the version number (currently only if
`allow-deprecated-apis` is false).

On success, an object is returned with members reflecting the
corresponding lightningd-config(5) options which were specified in
the configuration file(s) and command line.

Additional members include:

- *# version*: A string that represents the version of node.
- *plugins*: A array that represents the non-important plugin registered. Each object contains the following members:
- *path*: A string that represents the path of plugin.
- *name*: A string that represents the name of plugin.
Expand All @@ -49,7 +51,6 @@ EXAMPLE JSON RESPONSE

```json
{
"# version": "v0.9.0-1",
"lightning-dir": "/media/vincent/Maxtor/sanboxTestWrapperRPC/lightning_dir_dev",
"network": "testnet",
"allow-deprecated-apis": true,
Expand Down
18 changes: 18 additions & 0 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,24 @@ static void json_command_malformed(struct json_connection *jcon,
json_stream_close(js, NULL);
}

void json_notify_fmt(struct command *cmd, const char *fmt, ...)
{
va_list ap;
struct json_stream *js = json_stream_raw_for_cmd(cmd);

va_start(ap, fmt);
json_object_start(js, NULL);
json_add_string(js, "jsonrpc", "2.0");
json_add_string(js, "method", "notify");
json_object_start(js, "params");
json_add_string(js, "message", tal_vfmt(tmpctx, fmt, ap));
json_object_end(js);
json_object_end(js);

json_stream_double_cr(js);
json_stream_flush(js);
}

struct json_stream *json_stream_raw_for_cmd(struct command *cmd)
{
struct json_stream *js;
Expand Down
4 changes: 4 additions & 0 deletions lightningd/jsonrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ static inline void fixme_ignore(const struct command_result *res)
{
}

/* Notifier to the caller. */
void json_notify_fmt(struct command *cmd, const char *fmt, ...)
PRINTF_FMT(2, 3);

/* FIXME: For the few cases where return value is indeterminate */
struct command_result *command_its_complicated(const char *why);

Expand Down
6 changes: 2 additions & 4 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -1366,10 +1366,8 @@ static struct command_result *json_listconfigs(struct command *cmd,
NULL))
return command_param_failed();

if (!configtok) {
response = json_stream_success(cmd);
json_add_string(response, "# version", version());
}
if (!configtok)
json_notify_fmt(cmd, "version=%s", version());

for (i = 0; i < opt_count; i++) {
unsigned int len;
Expand Down
4 changes: 2 additions & 2 deletions tests/plugins/test_libplugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ static struct command_result *json_testrpc(struct command *cmd,
if (!param(cmd, buf, params, NULL))
return command_param_failed();

req = jsonrpc_request_start(cmd->plugin, cmd, "getinfo", testrpc_cb,
req = jsonrpc_request_start(cmd->plugin, cmd, "listconfigs", testrpc_cb,
testrpc_cb, NULL);
return send_outreq(cmd->plugin, req);
}
Expand All @@ -104,7 +104,7 @@ static const struct plugin_command commands[] = { {
{
"testrpc",
"utils",
"Makes a simple getinfo call, to test rpc socket.",
"Makes a simple listconfigs call, to test rpc socket.",
"",
json_testrpc,
},
Expand Down
4 changes: 2 additions & 2 deletions tests/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1169,7 +1169,7 @@ def test_libplugin(node_factory):
l1.daemon.wait_for_log("{} connected".format(l2.info["id"]))

# Test RPC calls FIXME: test concurrent ones ?
assert l1.rpc.call("testrpc") == l1.rpc.getinfo()
assert l1.rpc.call("testrpc") == l1.rpc.listconfigs()

# Make sure deprecated options nor commands are mentioned.
with pytest.raises(RpcError, match=r'Command "testrpc-deprecated" is deprecated'):
Expand Down Expand Up @@ -1202,7 +1202,7 @@ def test_libplugin_deprecated(node_factory):

assert l1.rpc.call("helloworld") == "hello test_opt depr"
l1.rpc.help('testrpc-deprecated')
assert l1.rpc.call("testrpc-deprecated") == l1.rpc.getinfo()
assert l1.rpc.call("testrpc-deprecated") == l1.rpc.listconfigs()


@unittest.skipIf(
Expand Down

0 comments on commit af22c25

Please sign in to comment.