libplugin: support filtering of outgoing commands.

This is crude, handing a raw JSON string, but it works.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-06-20 11:23:14 +09:30
parent d2465d4498
commit 1f9ddd24b2
4 changed files with 26 additions and 2 deletions

View File

@ -325,6 +325,7 @@ struct out_req *
jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
const char *method,
const char *id_prefix,
const char *filter,
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
@ -355,6 +356,12 @@ jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
json_add_string(out->js, "jsonrpc", "2.0");
json_add_id(out->js, out->id);
json_add_string(out->js, "method", method);
if (filter) {
/* This is raw JSON, so paste, don't escape! */
size_t len = strlen(filter);
char *p = json_out_member_direct(out->js->jout, "filter", len);
memcpy(p, filter, len);
}
if (out->errcb)
json_object_start(out->js, "params");

View File

@ -103,6 +103,7 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin,
struct command *cmd,
const char *method,
const char *id_prefix,
const char *filter,
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
@ -117,7 +118,7 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin,
* "error" members. */
#define jsonrpc_request_start(plugin, cmd, method, cb, errcb, arg) \
jsonrpc_request_start_((plugin), (cmd), (method), \
json_id_prefix(tmpctx, (cmd)), \
json_id_prefix(tmpctx, (cmd)), NULL, \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \
@ -130,11 +131,25 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin,
const jsmntok_t *result), \
(arg))
#define jsonrpc_request_with_filter_start(plugin, cmd, method, filter, cb, errcb, arg) \
jsonrpc_request_start_((plugin), (cmd), (method), \
json_id_prefix(tmpctx, (cmd)), (filter), \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
typesafe_cb_preargs(struct command_result *, void *, \
(errcb), (arg), \
struct command *command, \
const char *buf, \
const jsmntok_t *result), \
(arg))
/* This variant has callbacks received whole obj, not "result" or
* "error" members. It also doesn't start params{}. */
#define jsonrpc_request_whole_object_start(plugin, cmd, method, id_prefix, cb, arg) \
jsonrpc_request_start_((plugin), (cmd), (method), (id_prefix), \
jsonrpc_request_start_((plugin), (cmd), (method), (id_prefix), NULL, \
typesafe_cb_preargs(struct command_result *, void *, \
(cb), (arg), \
struct command *command, \

View File

@ -228,6 +228,7 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin UNNEEDED,
struct command *cmd UNNEEDED,
const char *method UNNEEDED,
const char *id_prefix UNNEEDED,
const char *filter UNNEEDED,
struct command_result *(*cb)(struct command *command UNNEEDED,
const char *buf UNNEEDED,
const jsmntok_t *result UNNEEDED,

View File

@ -225,6 +225,7 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin UNNEEDED,
struct command *cmd UNNEEDED,
const char *method UNNEEDED,
const char *id_prefix UNNEEDED,
const char *filter UNNEEDED,
struct command_result *(*cb)(struct command *command UNNEEDED,
const char *buf UNNEEDED,
const jsmntok_t *result UNNEEDED,