mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 15:44:21 +01:00
libplugin: support filters.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
#include <ccan/tal/path/path.h>
|
#include <ccan/tal/path/path.h>
|
||||||
#include <ccan/tal/str/str.h>
|
#include <ccan/tal/str/str.h>
|
||||||
#include <common/daemon.h>
|
#include <common/daemon.h>
|
||||||
|
#include <common/json_filter.h>
|
||||||
#include <common/json_parse_simple.h>
|
#include <common/json_parse_simple.h>
|
||||||
#include <common/json_stream.h>
|
#include <common/json_stream.h>
|
||||||
#include <common/memleak.h>
|
#include <common/memleak.h>
|
||||||
@@ -132,10 +133,9 @@ struct command_result *command_done(void)
|
|||||||
return &complete;
|
return &complete;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Don't ask for _filter, we will crash! */
|
|
||||||
struct json_filter **command_filter_ptr(struct command *cmd)
|
struct json_filter **command_filter_ptr(struct command *cmd)
|
||||||
{
|
{
|
||||||
return NULL;
|
return &cmd->filter;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ld_send(struct plugin *plugin, struct json_stream *stream)
|
static void ld_send(struct plugin *plugin, struct json_stream *stream)
|
||||||
@@ -261,6 +261,8 @@ struct json_stream *jsonrpc_stream_success(struct command *cmd)
|
|||||||
struct json_stream *js = jsonrpc_stream_start(cmd);
|
struct json_stream *js = jsonrpc_stream_start(cmd);
|
||||||
|
|
||||||
json_object_start(js, "result");
|
json_object_start(js, "result");
|
||||||
|
if (cmd->filter)
|
||||||
|
json_stream_attach_filter(js, cmd->filter);
|
||||||
return js;
|
return js;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,6 +275,7 @@ struct json_stream *jsonrpc_stream_fail(struct command *cmd,
|
|||||||
json_object_start(js, "error");
|
json_object_start(js, "error");
|
||||||
json_add_primitive_fmt(js, "code", "%d", code);
|
json_add_primitive_fmt(js, "code", "%d", code);
|
||||||
json_add_string(js, "message", err);
|
json_add_string(js, "message", err);
|
||||||
|
cmd->filter = tal_free(cmd->filter);
|
||||||
|
|
||||||
return js;
|
return js;
|
||||||
}
|
}
|
||||||
@@ -302,6 +305,14 @@ static struct command_result *command_complete(struct command *cmd,
|
|||||||
struct command_result *command_finished(struct command *cmd,
|
struct command_result *command_finished(struct command *cmd,
|
||||||
struct json_stream *response)
|
struct json_stream *response)
|
||||||
{
|
{
|
||||||
|
/* Detach filter before it complains about closing object it never saw */
|
||||||
|
if (cmd->filter) {
|
||||||
|
const char *err = json_stream_detach_filter(tmpctx, response);
|
||||||
|
if (err)
|
||||||
|
json_add_string(response, "warning_parameter_filter",
|
||||||
|
err);
|
||||||
|
}
|
||||||
|
|
||||||
/* "result" or "error" object */
|
/* "result" or "error" object */
|
||||||
json_object_end(response);
|
json_object_end(response);
|
||||||
|
|
||||||
@@ -1435,11 +1446,12 @@ void plugin_set_memleak_handler(struct plugin *plugin,
|
|||||||
static void ld_command_handle(struct plugin *plugin,
|
static void ld_command_handle(struct plugin *plugin,
|
||||||
const jsmntok_t *toks)
|
const jsmntok_t *toks)
|
||||||
{
|
{
|
||||||
const jsmntok_t *methtok, *paramstok;
|
const jsmntok_t *methtok, *paramstok, *filtertok;
|
||||||
struct command *cmd;
|
struct command *cmd;
|
||||||
|
|
||||||
methtok = json_get_member(plugin->buffer, toks, "method");
|
methtok = json_get_member(plugin->buffer, toks, "method");
|
||||||
paramstok = json_get_member(plugin->buffer, toks, "params");
|
paramstok = json_get_member(plugin->buffer, toks, "params");
|
||||||
|
filtertok = json_get_member(plugin->buffer, toks, "filter");
|
||||||
|
|
||||||
if (!methtok || !paramstok)
|
if (!methtok || !paramstok)
|
||||||
plugin_err(plugin, "Malformed JSON-RPC notification missing "
|
plugin_err(plugin, "Malformed JSON-RPC notification missing "
|
||||||
@@ -1450,6 +1462,7 @@ static void ld_command_handle(struct plugin *plugin,
|
|||||||
cmd = tal(plugin, struct command);
|
cmd = tal(plugin, struct command);
|
||||||
cmd->plugin = plugin;
|
cmd->plugin = plugin;
|
||||||
cmd->usage_only = false;
|
cmd->usage_only = false;
|
||||||
|
cmd->filter = NULL;
|
||||||
cmd->methodname = json_strdup(cmd, plugin->buffer, methtok);
|
cmd->methodname = json_strdup(cmd, plugin->buffer, methtok);
|
||||||
cmd->id = json_get_id(cmd, plugin->buffer, toks);
|
cmd->id = json_get_id(cmd, plugin->buffer, toks);
|
||||||
|
|
||||||
@@ -1510,6 +1523,13 @@ static void ld_command_handle(struct plugin *plugin,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (filtertok) {
|
||||||
|
/* On error, this fails cmd */
|
||||||
|
if (parse_filter(cmd, "filter", plugin->buffer, filtertok)
|
||||||
|
!= NULL)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < plugin->num_commands; i++) {
|
for (size_t i = 0; i < plugin->num_commands; i++) {
|
||||||
if (streq(cmd->methodname, plugin->commands[i].name)) {
|
if (streq(cmd->methodname, plugin->commands[i].name)) {
|
||||||
plugin->commands[i].handle(cmd,
|
plugin->commands[i].handle(cmd,
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ struct command {
|
|||||||
const char *methodname;
|
const char *methodname;
|
||||||
bool usage_only;
|
bool usage_only;
|
||||||
struct plugin *plugin;
|
struct plugin *plugin;
|
||||||
|
/* Optional output field filter. */
|
||||||
|
struct json_filter *filter;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Create an array of these, one for each command you support. */
|
/* Create an array of these, one for each command you support. */
|
||||||
|
|||||||
@@ -2854,10 +2854,10 @@ def test_field_filter(node_factory, chainparams):
|
|||||||
"fallbacks": {'type': True}})
|
"fallbacks": {'type': True}})
|
||||||
assert dec['warning_parameter_filter'] == '.fallbacks is an array'
|
assert dec['warning_parameter_filter'] == '.fallbacks is an array'
|
||||||
|
|
||||||
# Plugins ignore filters!
|
# C plugins implement filters!
|
||||||
res = l1.rpc.call('decode', {'string': inv['bolt11']},
|
res = l1.rpc.call('decode', {'string': inv['bolt11']},
|
||||||
filter={"currency": True})
|
filter={"currency": True})
|
||||||
assert 'type' in res
|
assert res == {"currency": chainparams['bip173_prefix']}
|
||||||
|
|
||||||
|
|
||||||
def test_checkmessage_pubkey_not_found(node_factory):
|
def test_checkmessage_pubkey_not_found(node_factory):
|
||||||
|
|||||||
Reference in New Issue
Block a user