plugin: Map results back to the incoming JSON-RPC request

The final step in the JSON-RPC passthrough: map the result we got from
the plugin back to the original request we got from the client.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
Christian Decker
2018-11-26 19:54:06 +01:00
committed by Rusty Russell
parent 7c93cf00bb
commit e625fd7e82
2 changed files with 24 additions and 4 deletions

View File

@@ -19,6 +19,9 @@
/* Developer error in the parameters to param() call */
#define PARAM_DEV_ERROR -2
/* Plugin returned an error */
#define PLUGIN_ERROR -3
/* Errors from `pay`, `sendpay`, or `waitsendpay` commands */
#define PAY_IN_PROGRESS 200
#define PAY_RHASH_ALREADY_USED 201

View File

@@ -9,6 +9,7 @@
#include <common/memleak.h>
#include <errno.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc_errors.h>
#include <lightningd/lightningd.h>
#include <signal.h>
#include <unistd.h>
@@ -417,10 +418,26 @@ static void plugin_rpcmethod_destroy(struct json_command *cmd,
static void plugin_rpcmethod_cb(const struct plugin_request *req,
struct plugin_rpc_request *rpc_req)
{
// Parse
// Extract results or error
// Construct reply
// Return result with appropriate return code.
struct json_stream *response;
const jsmntok_t *res;
assert(req->resulttok || req->errortok);
if (req->errortok) {
res = req->errortok;
command_fail(rpc_req->cmd, PLUGIN_ERROR, "%.*s",
res->end - res->start, req->response + res->start);
tal_free(rpc_req);
return;
}
res = req->resulttok;
response = json_stream_success(rpc_req->cmd);
json_add_member(response, NULL, "%.*s", json_tok_len(res),
json_tok_contents(req->response, res));
command_success(rpc_req->cmd, response);
tal_free(rpc_req);
}
static void plugin_rpcmethod_dispatch(struct command *cmd, const char *buffer,