libplugin: use json_stream helpers for RPC calls

This adds helpers to start and send a jsonrpc request using json_stream
in order to benefit from the helpers.

This then simplifies existing plugins RPC requests by using json_stream
helpers.
This commit is contained in:
darosior
2020-01-30 00:55:55 +01:00
committed by Rusty Russell
parent 0546728819
commit 2bff80e3de
5 changed files with 226 additions and 281 deletions

View File

@@ -27,6 +27,26 @@ enum plugin_restartability {
PLUGIN_RESTARTABLE
};
struct out_req {
/* The unique id of this request. */
u64 id;
/* The command which is why we're calling this rpc. */
struct command *cmd;
/* The request stream. */
struct json_stream *js;
/* The callback when we get a response. */
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg);
/* The callback when we get an error. */
struct command_result *(*errcb)(struct command *command,
const char *buf,
const jsmntok_t *error,
void *arg);
void *arg;
};
struct command {
u64 *id;
const char *methodname;
@@ -70,6 +90,35 @@ struct plugin_hook {
const jsmntok_t *params);
};
/* Helper to create a JSONRPC2 request stream. Send it with `send_outreq`. */
struct out_req *
jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
const char *method,
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
struct command_result *(*errcb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
void *arg);
#define jsonrpc_request_start(plugin, cmd, method, cb, errcb, arg) \
jsonrpc_request_start_((plugin), (cmd), (method), \
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))
/* Helper to create a JSONRPC2 response stream with a "result" object. */
struct json_stream *jsonrpc_stream_success(struct command *cmd);
@@ -131,38 +180,9 @@ const char *rpc_delve(const tal_t *ctx,
const struct json_out *params TAKES,
const char *guide);
/* Async rpc request.
* @cmd can be NULL if we're coming from a timer callback.
* @params can be NULL, otherwise it's an array or object.
*/
/* Send an async rpc request to lightningd. */
struct command_result *
send_outreq_(struct plugin *plugin,
struct command *cmd,
const char *method,
struct command_result *(*cb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
struct command_result *(*errcb)(struct command *command,
const char *buf,
const jsmntok_t *result,
void *arg),
void *arg,
const struct json_out *params TAKES);
#define send_outreq(plugin, cmd, method, cb, errcb, arg, params) \
send_outreq_((plugin), (cmd), (method), \
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), (params))
send_outreq(struct plugin *plugin, const struct out_req *req);
/* Callback to just forward error and close request; @cmd cannot be NULL */
struct command_result *forward_error(struct command *cmd,