From 51f72c1d1d526d6ae95f62930eedcdcab46ed178 Mon Sep 17 00:00:00 2001 From: adi2011 Date: Mon, 31 Jul 2023 09:06:33 +0930 Subject: [PATCH] lightningd: Added plugin hook to notify whenever started in rocover mode. --- lightningd/lightningd.c | 53 +++++++++++++++++++++++++++ lightningd/lightningd.h | 3 ++ lightningd/options.c | 2 + lightningd/test/run-find_my_abspath.c | 11 ++++++ 4 files changed, 69 insertions(+) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 759766d61..46ba84f5f 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -50,6 +50,7 @@ #include #include #include +#include #include #include #include @@ -72,6 +73,7 @@ #include #include #include +#include #include #include #include @@ -207,6 +209,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx) * parsing, we know they're to be set to the defaults. */ ld->alias = NULL; ld->rgb = NULL; + ld->recover = NULL; list_head_init(&ld->connects); list_head_init(&ld->waitsendpay_commands); list_head_init(&ld->sendpay_commands); @@ -910,6 +913,49 @@ void lightningd_exit(struct lightningd *ld, int exit_code) io_break(ld); } +struct recover_payload { + const char *codex32secret; +}; + +static bool +recover_hook_deserialize(struct recover_payload *payload, + const char *buffer, const jsmntok_t *toks) +{ + const jsmntok_t *t_res; + + if (!toks || !buffer) + return true; + + t_res = json_get_member(buffer, toks, "result"); + + /* fail */ + if (!t_res || !json_tok_streq(buffer, t_res, "continue")) + fatal("Plugin returned an invalid response to the " + "recover hook: %s", buffer); + + /* call next hook */ + return true; +} + +static void recover_hook_final(struct recover_payload *payload STEALS) +{ + tal_steal(tmpctx, payload); +} + +static void recover_hook_serialize(struct recover_payload *payload, + struct json_stream *stream, + struct plugin *plugin) +{ + json_add_string(stream, "codex32", payload->codex32secret); +} + + +REGISTER_PLUGIN_HOOK(recover, + recover_hook_deserialize, + recover_hook_final, + recover_hook_serialize, + struct recover_payload *); + int main(int argc, char *argv[]) { struct lightningd *ld; @@ -1001,6 +1047,7 @@ int main(int argc, char *argv[]) orig_argv = notleak(tal_arr(ld, char *, argc + 1)); for (size_t i = 1; i < argc; i++) orig_argv[i] = tal_strdup(orig_argv, argv[i]); + /*~ Turn argv[0] into an absolute path (if not already) */ orig_argv[0] = path_join(orig_argv, take(path_cwd(NULL)), argv[0]); orig_argv[argc] = NULL; @@ -1220,6 +1267,12 @@ int main(int argc, char *argv[]) tal_hex(tmpctx, ld->rgb), version()); ld->state = LD_STATE_RUNNING; + if (ld->recover) { + struct recover_payload *payload = tal(NULL, struct recover_payload); + payload->codex32secret = tal_strdup(payload, + ld->recover); + plugin_hook_call_recover(ld, NULL, payload); + } /*~ If `closefrom_may_be_slow`, we limit ourselves to 4096 file * descriptors; tell the user about it as that limits the number * of channels they can have. diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 72c8e5ed5..67a9b76d8 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -271,6 +271,9 @@ struct lightningd { /* Indexes used by all the wait infra */ struct indexes indexes[NUM_WAIT_SUBSYSTEM]; + /* Contains the codex32 string used with --recover flag */ + char *recover; + #if DEVELOPER /* If we want to debug a subdaemon/plugin. */ char *dev_debug_subprocess; diff --git a/lightningd/options.c b/lightningd/options.c index f3db51d9d..7ce6ec5c7 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -1322,6 +1322,8 @@ static char *opt_set_codex32(const char *arg, struct lightningd *ld) return tal_fmt(tmpctx, "fsyncdir: %s", strerror(errno)); } close(fd); + + ld->recover = tal_strdup(ld, arg); opt_set_offline(ld); return NULL; diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 171997f86..580800cc4 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -114,6 +114,11 @@ void htlcs_notify_new_block(struct lightningd *ld UNNEEDED, u32 height UNNEEDED) void htlcs_resubmit(struct lightningd *ld UNNEEDED, struct htlc_in_map *unconnected_htlcs_in STEALS UNNEEDED) { fprintf(stderr, "htlcs_resubmit called!\n"); abort(); } +/* Generated stub for json_add_string */ +void json_add_string(struct json_stream *js UNNEEDED, + const char *fieldname UNNEEDED, + const char *str TAKES UNNEEDED) +{ fprintf(stderr, "json_add_string called!\n"); abort(); } /* Generated stub for jsonrpc_listen */ void jsonrpc_listen(struct jsonrpc *rpc UNNEEDED, struct lightningd *ld UNNEEDED) { fprintf(stderr, "jsonrpc_listen called!\n"); abort(); } @@ -170,6 +175,12 @@ struct chain_topology *new_topology(struct lightningd *ld UNNEEDED, struct logge /* Generated stub for onchaind_replay_channels */ void onchaind_replay_channels(struct lightningd *ld UNNEEDED) { fprintf(stderr, "onchaind_replay_channels called!\n"); abort(); } +/* Generated stub for plugin_hook_call_ */ +bool plugin_hook_call_(struct lightningd *ld UNNEEDED, + const struct plugin_hook *hook UNNEEDED, + const char *cmd_id TAKES UNNEEDED, + tal_t *cb_arg STEALS UNNEEDED) +{ fprintf(stderr, "plugin_hook_call_ called!\n"); abort(); } /* Generated stub for plugins_config */ bool plugins_config(struct plugins *plugins UNNEEDED) { fprintf(stderr, "plugins_config called!\n"); abort(); }