mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-24 09:34:24 +01:00
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>
This commit is contained in:
committed by
Rusty Russell
parent
a281f4b692
commit
05ec56a968
@@ -1,12 +1,14 @@
|
||||
#include <common/memleak.h>
|
||||
#include <lightningd/jsonrpc.h>
|
||||
#include <lightningd/plugin_hook.h>
|
||||
#include <wallet/db.h>
|
||||
|
||||
/* 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;
|
||||
struct db *db;
|
||||
};
|
||||
|
||||
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");
|
||||
void *response = r->hook->deserialize_response(r, buffer, resulttok);
|
||||
db_begin_transaction(r->db);
|
||||
r->hook->response_cb(r->cb_arg, response);
|
||||
db_commit_transaction(r->db);
|
||||
tal_free(r);
|
||||
}
|
||||
|
||||
@@ -71,6 +75,7 @@ void plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
|
||||
plugin_hook_callback, ph_req);
|
||||
ph_req->hook = hook;
|
||||
ph_req->cb_arg = cb_arg;
|
||||
ph_req->db = ld->wallet->db;
|
||||
hook->serialize_payload(payload, req->stream);
|
||||
jsonrpc_request_end(req);
|
||||
plugin_request_send(hook->plugin, req);
|
||||
|
||||
Reference in New Issue
Block a user