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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-09-13 06:49:11 +09:30
parent ce0b765c96
commit 99f2019a24
4 changed files with 28 additions and 14 deletions

View file

@ -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);
}
return r;
}

View file

@ -217,7 +217,21 @@ 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, \
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))
#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, \
@ -230,7 +244,7 @@ void jsonrpc_notification_end(struct jsonrpc_notification *n);
(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,

View file

@ -1129,7 +1129,7 @@ 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,
req = jsonrpc_request_start_raw(plugin, cmd->json_cmd->name, plugin->log,
plugin_notify_cb,
plugin_rpcmethod_cb, call);
call->request = req;

View file

@ -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,