2019-03-18 03:40:32 +01:00
|
|
|
#include <ccan/io/io.h>
|
2019-01-02 19:23:18 +01:00
|
|
|
#include <common/memleak.h>
|
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-12-20 12:00:35 +01:00
|
|
|
#include <lightningd/plugin_hook.h>
|
2019-01-03 17:56:51 +01:00
|
|
|
#include <wallet/db.h>
|
2018-12-20 12:00:35 +01:00
|
|
|
|
2019-01-02 19:23:18 +01:00
|
|
|
/* Struct containing all the information needed to deserialize and
|
|
|
|
* dispatch an eventual plugin_hook response. */
|
|
|
|
struct plugin_hook_request {
|
|
|
|
const struct plugin_hook *hook;
|
|
|
|
void *cb_arg;
|
2019-01-03 17:56:51 +01:00
|
|
|
struct db *db;
|
2019-01-02 19:23:18 +01:00
|
|
|
};
|
|
|
|
|
2018-12-28 21:23:08 +01:00
|
|
|
static struct plugin_hook *plugin_hook_by_name(const char *name)
|
|
|
|
{
|
|
|
|
static struct plugin_hook **hooks = NULL;
|
|
|
|
static size_t num_hooks;
|
|
|
|
if (!hooks)
|
|
|
|
hooks = autodata_get(hooks, &num_hooks);
|
|
|
|
|
|
|
|
for (size_t i=0; i<num_hooks; i++)
|
|
|
|
if (streq(hooks[i]->name, name))
|
|
|
|
return hooks[i];
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool plugin_hook_register(struct plugin *plugin, const char *method)
|
|
|
|
{
|
|
|
|
struct plugin_hook *hook = plugin_hook_by_name(method);
|
|
|
|
if (!hook) {
|
|
|
|
/* No such hook name registered */
|
|
|
|
return false;
|
|
|
|
} else if (hook->plugin != NULL) {
|
|
|
|
/* Another plugin already registered for this name */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
hook->plugin = plugin;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME(cdecker): Remove dummy hook, once we have a real one */
|
|
|
|
REGISTER_PLUGIN_HOOK(hello, NULL, void *, NULL, void *, NULL, void *);
|
|
|
|
|
2019-01-02 19:23:18 +01:00
|
|
|
/**
|
|
|
|
* Callback to be passed to the jsonrpc_request.
|
|
|
|
*
|
|
|
|
* Unbundles the arguments, deserializes the response and dispatches
|
|
|
|
* it to the hook callback.
|
|
|
|
*/
|
|
|
|
static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
struct plugin_hook_request *r)
|
|
|
|
{
|
|
|
|
const jsmntok_t *resulttok = json_get_member(buffer, toks, "result");
|
2019-04-16 02:10:07 +02:00
|
|
|
void *response;
|
|
|
|
|
|
|
|
if (!resulttok)
|
|
|
|
fatal("Plugin for %s returned non-result response %.*s",
|
|
|
|
r->hook->name,
|
|
|
|
toks->end - toks->start, buffer + toks->end);
|
|
|
|
|
|
|
|
response = r->hook->deserialize_response(r, buffer, resulttok);
|
2019-01-03 17:56:51 +01:00
|
|
|
db_begin_transaction(r->db);
|
2019-01-02 19:23:18 +01:00
|
|
|
r->hook->response_cb(r->cb_arg, response);
|
2019-01-03 17:56:51 +01:00
|
|
|
db_commit_transaction(r->db);
|
2019-01-02 19:23:18 +01:00
|
|
|
tal_free(r);
|
|
|
|
}
|
|
|
|
|
2018-12-20 12:00:35 +01:00
|
|
|
void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
|
|
|
void *payload, void *cb_arg)
|
|
|
|
{
|
2019-01-02 19:23:18 +01:00
|
|
|
struct jsonrpc_request *req;
|
|
|
|
struct plugin_hook_request *ph_req;
|
|
|
|
if (hook->plugin) {
|
|
|
|
/* If we have a plugin that has registered for this
|
|
|
|
* hook, serialize and call it */
|
|
|
|
/* FIXME: technically this is a leak, but we don't
|
|
|
|
* currently have a list to store these. We might want
|
|
|
|
* to eventually to inspect in-flight requests. */
|
|
|
|
ph_req = notleak(tal(hook->plugin, struct plugin_hook_request));
|
2019-02-18 03:44:29 +01:00
|
|
|
/* FIXME: do IO logging for these! */
|
|
|
|
req = jsonrpc_request_start(NULL, hook->name, NULL,
|
2019-01-02 19:23:18 +01:00
|
|
|
plugin_hook_callback, ph_req);
|
|
|
|
ph_req->hook = hook;
|
|
|
|
ph_req->cb_arg = cb_arg;
|
2019-01-03 17:56:51 +01:00
|
|
|
ph_req->db = ld->wallet->db;
|
2019-01-02 19:23:18 +01:00
|
|
|
hook->serialize_payload(payload, req->stream);
|
|
|
|
jsonrpc_request_end(req);
|
|
|
|
plugin_request_send(hook->plugin, req);
|
|
|
|
} else {
|
|
|
|
/* If no plugin has registered for this hook, just
|
|
|
|
* call the callback with a NULL result. Saves us the
|
|
|
|
* roundtrip to the serializer and deserializer. If we
|
|
|
|
* were expecting a default response it should have
|
|
|
|
* been part of the `cb_arg`. */
|
|
|
|
hook->response_cb(cb_arg, NULL);
|
|
|
|
}
|
2018-12-20 12:00:35 +01:00
|
|
|
}
|
2019-03-18 03:40:32 +01:00
|
|
|
|
|
|
|
/* We open-code this, because it's just different and special enough to be
|
|
|
|
* annoying, and to make it clear that it's totally synchronous. */
|
|
|
|
|
|
|
|
/* Special synchronous hook for db */
|
|
|
|
static struct plugin_hook db_write_hook = { "db_write", NULL, NULL, NULL, NULL };
|
|
|
|
AUTODATA(hooks, &db_write_hook);
|
|
|
|
|
|
|
|
static void db_hook_response(const char *buffer, const jsmntok_t *toks,
|
|
|
|
const jsmntok_t *idtok,
|
|
|
|
struct plugin_hook_request *ph_req)
|
|
|
|
{
|
|
|
|
const jsmntok_t *resulttok;
|
|
|
|
bool resp;
|
|
|
|
|
|
|
|
resulttok = json_get_member(buffer, toks, "result");
|
|
|
|
if (!resulttok)
|
|
|
|
fatal("Plugin returned an invalid response to the db_write "
|
|
|
|
"hook: %s", buffer);
|
|
|
|
|
|
|
|
/* We expect result: True. Anything else we abort. */
|
|
|
|
if (!json_to_bool(buffer, resulttok, &resp))
|
|
|
|
fatal("Plugin returned an invalid result to the db_write "
|
|
|
|
"hook: %s", buffer);
|
|
|
|
|
|
|
|
/* If it fails, we must not commit to our db. */
|
|
|
|
if (!resp)
|
|
|
|
fatal("Plugin returned failed db_write: %s.", buffer);
|
|
|
|
|
|
|
|
/* We're done, exit exclusive loop. */
|
|
|
|
io_break(ph_req);
|
|
|
|
}
|
|
|
|
|
|
|
|
void plugin_hook_db_sync(struct db *db, const char **changes, const char *final)
|
|
|
|
{
|
|
|
|
const struct plugin_hook *hook = &db_write_hook;
|
|
|
|
struct jsonrpc_request *req;
|
|
|
|
struct plugin_hook_request *ph_req;
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
if (!hook->plugin)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ph_req = notleak(tal(hook->plugin, struct plugin_hook_request));
|
|
|
|
/* FIXME: do IO logging for this! */
|
|
|
|
req = jsonrpc_request_start(NULL, hook->name, NULL, db_hook_response,
|
|
|
|
ph_req);
|
|
|
|
|
|
|
|
ph_req->hook = hook;
|
|
|
|
ph_req->db = db;
|
|
|
|
|
|
|
|
json_array_start(req->stream, "writes");
|
|
|
|
for (size_t i = 0; i < tal_count(changes); i++)
|
|
|
|
json_add_string(req->stream, NULL, changes[i]);
|
|
|
|
if (final)
|
|
|
|
json_add_string(req->stream, NULL, final);
|
|
|
|
json_array_end(req->stream);
|
|
|
|
jsonrpc_request_end(req);
|
|
|
|
|
|
|
|
plugin_request_send(hook->plugin, req);
|
|
|
|
|
|
|
|
/* We can be called on way out of an io_loop, which is already breaking.
|
|
|
|
* That will make this immediately return; save the break value and call
|
|
|
|
* again, then hand it onwards. */
|
|
|
|
ret = plugin_exclusive_loop(hook->plugin);
|
|
|
|
if (ret != ph_req) {
|
|
|
|
void *ret2 = plugin_exclusive_loop(hook->plugin);
|
|
|
|
assert(ret2 == ph_req);
|
|
|
|
io_break(ret);
|
|
|
|
}
|
|
|
|
}
|