Browse Source

plugin: Plugin hook callbacks need to be wrapped in a DB transaction

We therefore keep a reference to the DB and will wrap and unwrap when
a hook returns.

Notice that this might cause behavior changes when moving logic into a
hook callback, since the continuation runs in a different transaction
than the event that triggered the hook in the first place. Should not
matter too much, since we don't use DB rollbacks at the moment, but
it's something to keep in mind.

Signed-off-by: Christian Decker <decker.christian@gmail.com>
plugin-timeout-inc
Christian Decker 6 years ago
committed by Rusty Russell
parent
commit
05ec56a968
  1. 5
      lightningd/plugin_hook.c

5
lightningd/plugin_hook.c

@ -1,12 +1,14 @@
#include <common/memleak.h> #include <common/memleak.h>
#include <lightningd/jsonrpc.h> #include <lightningd/jsonrpc.h>
#include <lightningd/plugin_hook.h> #include <lightningd/plugin_hook.h>
#include <wallet/db.h>
/* Struct containing all the information needed to deserialize and /* Struct containing all the information needed to deserialize and
* dispatch an eventual plugin_hook response. */ * dispatch an eventual plugin_hook response. */
struct plugin_hook_request { struct plugin_hook_request {
const struct plugin_hook *hook; const struct plugin_hook *hook;
void *cb_arg; void *cb_arg;
struct db *db;
}; };
static struct plugin_hook *plugin_hook_by_name(const char *name) static struct plugin_hook *plugin_hook_by_name(const char *name)
@ -51,7 +53,9 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
{ {
const jsmntok_t *resulttok = json_get_member(buffer, toks, "result"); const jsmntok_t *resulttok = json_get_member(buffer, toks, "result");
void *response = r->hook->deserialize_response(r, buffer, resulttok); void *response = r->hook->deserialize_response(r, buffer, resulttok);
db_begin_transaction(r->db);
r->hook->response_cb(r->cb_arg, response); r->hook->response_cb(r->cb_arg, response);
db_commit_transaction(r->db);
tal_free(r); tal_free(r);
} }
@ -71,6 +75,7 @@ void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
plugin_hook_callback, ph_req); plugin_hook_callback, ph_req);
ph_req->hook = hook; ph_req->hook = hook;
ph_req->cb_arg = cb_arg; ph_req->cb_arg = cb_arg;
ph_req->db = ld->wallet->db;
hook->serialize_payload(payload, req->stream); hook->serialize_payload(payload, req->stream);
jsonrpc_request_end(req); jsonrpc_request_end(req);
plugin_request_send(hook->plugin, req); plugin_request_send(hook->plugin, req);

Loading…
Cancel
Save