Skip to content

Commit

Permalink
plugins: remove now-unused single-hook infrastructure.
Browse files Browse the repository at this point in the history
Should have really let @mschmook do this, as he did all the work to
make it possible!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Apr 7, 2021
1 parent aa9786e commit d36565f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 69 deletions.
34 changes: 10 additions & 24 deletions lightningd/plugin_hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ struct plugin_hook *plugin_hook_register(struct plugin *plugin, const char *meth
if (hook->hooks == NULL)
hook->hooks = notleak(tal_arr(NULL, struct hook_instance *, 0));

/* If this is a single type hook and we have a plugin registered we
* must fail this attempt to add the plugin to the hook. */
if (hook->type == PLUGIN_HOOK_SINGLE && tal_count(hook->hooks) > 0)
return NULL;

/* Ensure we don't register the same plugin multple times. */
for (size_t i=0; i<tal_count(hook->hooks); i++)
if (hook->hooks[i]->plugin == plugin)
Expand Down Expand Up @@ -187,16 +182,14 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
r->hook->name, toks->end - toks->start,
buffer + toks->start);

if (r->hook->type == PLUGIN_HOOK_CHAIN) {
db_begin_transaction(db);
if (!r->hook->deserialize_cb(r->cb_arg, buffer,
resulttok)) {
tal_free(r->cb_arg);
db_commit_transaction(db);
goto cleanup;
}
in_transaction = true;
db_begin_transaction(db);
if (!r->hook->deserialize_cb(r->cb_arg, buffer,
resulttok)) {
tal_free(r->cb_arg);
db_commit_transaction(db);
goto cleanup;
}
in_transaction = true;
} else {
/* plugin died */
resulttok = NULL;
Expand All @@ -212,10 +205,7 @@ static void plugin_hook_callback(const char *buffer, const jsmntok_t *toks,
/* We optimize for the case where we already called deserialize_cb */
if (!in_transaction)
db_begin_transaction(db);
if (r->hook->type == PLUGIN_HOOK_CHAIN)
r->hook->final_cb(r->cb_arg);
else
r->hook->single_response_cb(r->cb_arg, buffer, resulttok);
r->hook->final_cb(r->cb_arg);
db_commit_transaction(db);

cleanup:
Expand Down Expand Up @@ -282,10 +272,7 @@ bool plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
* roundtrip to the serializer and deserializer. If we
* were expecting a default response it should have
* been part of the `cb_arg`. */
if (hook->type == PLUGIN_HOOK_CHAIN)
hook->final_cb(cb_arg);
else
hook->single_response_cb(cb_arg, NULL, NULL);
hook->final_cb(cb_arg);
return true;
}
}
Expand All @@ -294,8 +281,7 @@ bool plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook,
* 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", PLUGIN_HOOK_CHAIN, NULL,
NULL, NULL};
static struct plugin_hook db_write_hook = {"db_write", NULL, NULL};
AUTODATA(hooks, &db_write_hook);

/* A `db_write` for one particular plugin hook. */
Expand Down
45 changes: 0 additions & 45 deletions lightningd/plugin_hook.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@
*
* - `serialize_payload` which takes a payload of type `cb_arg_type`
* and serializes it into the given `json_stream`. `
*
* For single-plugin hooks:
* - `single_response_cb` is called once the plugin has responded (or with
* buffer == NULL if there's no plugin). In addition an arbitrary
* additional argument of type `cb_arg_type` can be passed along
* that may contain any additional context necessary. It must free
* or otherwise take ownership of the cb_arg_type argument.
*
* For chained-plugin hooks:
* - `deserialize_cb` is called for each plugin, if it returns true the
* next one is called, otherwise the cb_arg_type argument is free.
* - If all `deserialize_cb` return true, `final_cb` is called. It must free
Expand All @@ -49,24 +40,9 @@
* that all the provided functions for serialization, deserialization
* and callback have the correct type.
*/

enum plugin_hook_type {
PLUGIN_HOOK_SINGLE,
PLUGIN_HOOK_CHAIN,
};

struct plugin_hook {
const char *name;

/* Which type of plugin is this? It'll determine how many plugins can
* register this hook, and how the hooks are called. */
enum plugin_hook_type type;

/* For PLUGIN_HOOK_SINGLE hooks */
void (*single_response_cb)(void *arg,
const char *buffer, const jsmntok_t *toks);

/* For PLUGIN_HOOK_CHAIN hooks: */
/* Returns false if we should stop iterating (and free arg). */
bool (*deserialize_cb)(void *arg,
const char *buffer, const jsmntok_t *toks);
Expand Down Expand Up @@ -114,31 +90,10 @@ bool plugin_hook_continue(void *arg, const char *buffer, const jsmntok_t *toks);
* response_cb function accepts the deserialized response format and
* an arbitrary extra argument used to maintain context.
*/
#define REGISTER_SINGLE_PLUGIN_HOOK(name, response_cb, \
serialize_payload, cb_arg_type) \
struct plugin_hook name##_hook_gen = { \
stringify(name), \
PLUGIN_HOOK_SINGLE, \
typesafe_cb_cast( \
void (*)(void *STEALS, const char *, const jsmntok_t *), \
void (*)(cb_arg_type STEALS, const char *, const jsmntok_t *), \
response_cb), \
NULL, NULL, \
typesafe_cb_cast(void (*)(void *, struct json_stream *), \
void (*)(cb_arg_type, struct json_stream *), \
serialize_payload), \
NULL, /* .plugins */ \
}; \
AUTODATA(hooks, &name##_hook_gen); \
PLUGIN_HOOK_CALL_DEF(name, cb_arg_type)


#define REGISTER_PLUGIN_HOOK(name, deserialize_cb, final_cb, \
serialize_payload, cb_arg_type) \
struct plugin_hook name##_hook_gen = { \
stringify(name), \
PLUGIN_HOOK_CHAIN, \
NULL, \
typesafe_cb_cast( \
bool (*)(void *, const char *, const jsmntok_t *), \
bool (*)(cb_arg_type, const char *, const jsmntok_t *), \
Expand Down

0 comments on commit d36565f

Please sign in to comment.