From 62c52fe868938e069e95b82b2bb63cefc8bdc09b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 30 Oct 2020 11:43:42 +1030 Subject: [PATCH] libplugin: add support for before and after deps on hooks. Signed-off-by: Rusty Russell --- plugins/libplugin.c | 21 +++++++++++++++++++-- plugins/libplugin.h | 2 ++ tests/plugins/test_libplugin.c | 5 +++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 681d94b28..1466a0aef 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -628,8 +628,25 @@ handle_getmanifest(struct command *getmanifest_cmd, json_array_end(params); json_array_start(params, "hooks"); - for (size_t i = 0; i < p->num_hook_subs; i++) - json_add_string(params, NULL, p->hook_subs[i].name); + for (size_t i = 0; i < p->num_hook_subs; i++) { + json_object_start(params, NULL); + json_add_string(params, "name", p->hook_subs[i].name); + if (p->hook_subs[i].before) { + json_array_start(params, "before"); + for (size_t j = 0; p->hook_subs[i].before[j]; j++) + json_add_string(params, NULL, + p->hook_subs[i].before[j]); + json_array_end(params); + } + if (p->hook_subs[i].after) { + json_array_start(params, "after"); + for (size_t j = 0; p->hook_subs[i].after[j]; j++) + json_add_string(params, NULL, + p->hook_subs[i].after[j]); + json_array_end(params); + } + json_object_end(params); + } json_array_end(params); if (p->our_features != NULL) { diff --git a/plugins/libplugin.h b/plugins/libplugin.h index 9ecb68065..684c8a41a 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -93,6 +93,8 @@ struct plugin_hook { struct command_result *(*handle)(struct command *cmd, const char *buf, const jsmntok_t *params); + /* If non-NULL, these are NULL-terminated arrays of deps */ + const char **before, **after; }; /* Return the feature set of the current lightning node */ diff --git a/tests/plugins/test_libplugin.c b/tests/plugins/test_libplugin.c index 073565696..ac2641304 100644 --- a/tests/plugins/test_libplugin.c +++ b/tests/plugins/test_libplugin.c @@ -118,9 +118,14 @@ static const struct plugin_command commands[] = { { } }; +static const char *before[] = { "dummy", NULL }; +static const char *after[] = { "dummy", NULL }; + static const struct plugin_hook hooks[] = { { "peer_connected", json_peer_connected, + before, + after } };