Skip to content

Commit

Permalink
plugins/recover: Add a plugin to detect we need recovery and it autom…
Browse files Browse the repository at this point in the history
…atically handles everything.
  • Loading branch information
adi2011 committed Nov 8, 2023
1 parent 0af6f48 commit 11e31f5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions plugins/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ chanbackup
commando
sql
cln-renepay
recover
8 changes: 7 additions & 1 deletion plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ PLUGIN_SPENDER_HEADER := \
plugins/spender/openchannel.h
PLUGIN_SPENDER_OBJS := $(PLUGIN_SPENDER_SRC:.c=.o)

PLUGIN_RECOVER_SRC := plugins/recover.c
PLUGIN_RECOVER_OBJS := $(PLUGIN_RECOVER_SRC:.c=.o)

PLUGIN_FUNDER_SRC := \
plugins/funder.c \
plugins/funder_policy.c
Expand All @@ -78,7 +81,8 @@ PLUGIN_ALL_SRC := \
$(PLUGIN_OFFERS_SRC) \
$(PLUGIN_PAY_LIB_SRC) \
$(PLUGIN_PAY_SRC) \
$(PLUGIN_SPENDER_SRC)
$(PLUGIN_SPENDER_SRC) \
$(PLUGIN_RECOVER_SRC)

PLUGIN_ALL_HEADER := \
$(PLUGIN_PAY_HEADER) \
Expand All @@ -99,6 +103,7 @@ C_PLUGINS := \
plugins/keysend \
plugins/offers \
plugins/pay \
plugins/recover \
plugins/txprepare \
plugins/cln-renepay \
plugins/spenderp
Expand Down Expand Up @@ -215,6 +220,7 @@ plugins/fetchinvoice: $(PLUGIN_FETCHINVOICE_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_CO

plugins/funder: bitcoin/psbt.o common/psbt_open.o $(PLUGIN_FUNDER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS)

plugins/recover: $(PLUGIN_RECOVER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS)
# This covers all the low-level list RPCs which return simple arrays
SQL_LISTRPCS := listchannels listforwards listhtlcs listinvoices listnodes listoffers listpeers listpeerchannels listclosedchannels listtransactions listsendpays bkpr-listaccountevents bkpr-listincome
SQL_LISTRPCS_SCHEMAS := $(foreach l,$(SQL_LISTRPCS),doc/schemas/$l.schema.json)
Expand Down
56 changes: 56 additions & 0 deletions plugins/recover.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "config.h"
#include <ccan/array_size/array_size.h>
#include <common/features.h>
#include <common/hsm_encryption.h>
#include <common/json_param.h>
#include <common/json_stream.h>
#include <plugins/libplugin.h>

static bool peer_backup;

static struct command_result *json_we_lost_state(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{

plugin_log(cmd->plugin, LOG_DBG, "RECEIVED WE LOST DATA");

return notification_handled(cmd);
}


static const char *init(struct plugin *p,
const char *buf UNUSED,
const jsmntok_t *config UNUSED)
{
u8 *features;

/* Figure out if they specified --experimental-peer-storage */
rpc_scan(p, "getinfo",
take(json_out_obj(NULL, NULL, NULL)),
"{our_features:{init:%}}",
JSON_SCAN_TAL(tmpctx, json_tok_bin_from_hex, &features));
peer_backup = feature_offered(features, OPT_WANT_PEER_BACKUP_STORAGE);

plugin_log(p, LOG_DBG, "Recover Plugin Initialised!");

return NULL;
}

static const struct plugin_notification notifs[] = {
{
"we_lost_state",
json_we_lost_state,
}
};

int main(int argc, char *argv[])
{
setup_locale();

plugin_main(argv, init, PLUGIN_STATIC, true, NULL,
NULL, 0,
notifs, ARRAY_SIZE(notifs), NULL, 0,
NULL, 0, /* Notification topics we publish */
NULL);
}

0 comments on commit 11e31f5

Please sign in to comment.