From 99f2019a24738fa4f7b003d345e73813c76d393e Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 13 Sep 2022 06:49:11 +0930 Subject: [PATCH] lightningd: add jsonrpc_request_start_raw instead of NULL method. Since we want to use methodname to create id, don't overload it for a raw request. Signed-off-by: Rusty Russell --- lightningd/jsonrpc.c | 16 ++++++++-------- lightningd/jsonrpc.h | 18 ++++++++++++++++-- lightningd/plugin.c | 6 +++--- lightningd/test/run-invoice-select-inchan.c | 2 +- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index fea4de13c..765401640 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -1312,6 +1312,7 @@ void jsonrpc_notification_end(struct jsonrpc_notification *n) struct jsonrpc_request *jsonrpc_request_start_( const tal_t *ctx, const char *method, struct log *log, + bool add_header, void (*notify_cb)(const char *buffer, const jsmntok_t *methodtok, const jsmntok_t *paramtoks, @@ -1327,22 +1328,21 @@ struct jsonrpc_request *jsonrpc_request_start_( r->notify_cb = notify_cb; r->response_cb = response_cb; r->response_cb_arg = response_cb_arg; - r->method = NULL; + r->method = tal_strdup(r, method); r->stream = new_json_stream(r, NULL, log); - /* If no method is specified we don't prefill the JSON-RPC - * request with the header. This serves as an escape hatch to - * get a raw request, but get a valid request-id assigned. */ - if (method != NULL) { - r->method = tal_strdup(r, method); + /* Disabling this serves as an escape hatch for plugin code to + * get a raw request to paste into, but get a valid request-id + * assigned. */ + if (add_header) { json_object_start(r->stream, NULL); json_add_string(r->stream, "jsonrpc", "2.0"); json_add_u64(r->stream, "id", r->id); json_add_string(r->stream, "method", method); json_object_start(r->stream, "params"); - if (log) - log_debug(log, "OUT:id=%"PRIu64, r->id); } + if (log) + log_debug(log, "OUT:id=%"PRIu64, r->id); return r; } diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index 43ed29b30..6339e4c12 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -217,7 +217,7 @@ void jsonrpc_notification_end(struct jsonrpc_notification *n); #define jsonrpc_request_start(ctx, method, log, notify_cb, response_cb, response_cb_arg) \ jsonrpc_request_start_( \ - (ctx), (method), (log), \ + (ctx), (method), (log), true, \ typesafe_cb_preargs(void, void *, (notify_cb), (response_cb_arg), \ const char *buffer, \ const jsmntok_t *idtok, \ @@ -229,8 +229,22 @@ void jsonrpc_notification_end(struct jsonrpc_notification *n); const jsmntok_t *idtok), \ (response_cb_arg)) +#define jsonrpc_request_start_raw(ctx, method, log, notify_cb, response_cb, response_cb_arg) \ + jsonrpc_request_start_( \ + (ctx), (method), (log), false, \ + typesafe_cb_preargs(void, void *, (notify_cb), (response_cb_arg), \ + const char *buffer, \ + const jsmntok_t *idtok, \ + const jsmntok_t *methodtok, \ + const jsmntok_t *paramtoks), \ + typesafe_cb_preargs(void, void *, (response_cb), (response_cb_arg), \ + const char *buffer, \ + const jsmntok_t *toks, \ + const jsmntok_t *idtok), \ + (response_cb_arg)) + struct jsonrpc_request *jsonrpc_request_start_( - const tal_t *ctx, const char *method, struct log *log, + const tal_t *ctx, const char *method, struct log *log, bool add_header, void (*notify_cb)(const char *buffer, const jsmntok_t *idtok, const jsmntok_t *methodtok, diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 7abd04b7a..25a8703ba 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -1129,9 +1129,9 @@ static struct command_result *plugin_rpcmethod_dispatch(struct command *cmd, call = tal(plugin, struct plugin_rpccall); call->cmd = cmd; - req = jsonrpc_request_start(plugin, NULL, plugin->log, - plugin_notify_cb, - plugin_rpcmethod_cb, call); + req = jsonrpc_request_start_raw(plugin, cmd->json_cmd->name, plugin->log, + plugin_notify_cb, + plugin_rpcmethod_cb, call); call->request = req; call->plugin = plugin; list_add_tail(&plugin->pending_rpccalls, &call->list); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index d25a7da1b..4550e0e0f 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -491,7 +491,7 @@ void jsonrpc_request_end(struct jsonrpc_request *request UNNEEDED) { fprintf(stderr, "jsonrpc_request_end called!\n"); abort(); } /* Generated stub for jsonrpc_request_start_ */ struct jsonrpc_request *jsonrpc_request_start_( - const tal_t *ctx UNNEEDED, const char *method UNNEEDED, struct log *log UNNEEDED, + const tal_t *ctx UNNEEDED, const char *method UNNEEDED, struct log *log UNNEEDED, bool add_header UNNEEDED, void (*notify_cb)(const char *buffer UNNEEDED, const jsmntok_t *idtok UNNEEDED, const jsmntok_t *methodtok UNNEEDED,