From e637e843e7e0ab5a60e87f04bbf78d1e64989342 Mon Sep 17 00:00:00 2001 From: adi2011 Date: Thu, 2 Feb 2023 20:31:25 +1030 Subject: [PATCH] Plugins/chanbackup: Add RPC for recovering from the latestscb received from peers. --- plugins/chanbackup.c | 72 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 1 deletion(-) diff --git a/plugins/chanbackup.c b/plugins/chanbackup.c index bd861a2b0..ca480433e 100644 --- a/plugins/chanbackup.c +++ b/plugins/chanbackup.c @@ -258,7 +258,7 @@ static struct command_result *json_emergencyrecover(struct command *cmd, if (version != VERSION) { plugin_err(cmd->plugin, - "Incompatible version, Contact the admin!"); + "Incompatible SCB file version on disk, contact the admin!"); } req = jsonrpc_request_start(cmd->plugin, cmd, "recoverchannel", @@ -665,6 +665,67 @@ static struct command_result *handle_your_peer_storage(struct command *cmd, } } +static struct command_result *after_latestscb(struct command *cmd, + const u8 *res, + void *cb_arg UNUSED) +{ + u64 version; + u32 timestamp; + struct scb_chan **scb; + struct json_stream *response; + struct out_req *req; + + if (tal_bytelen(res) == 0) { + response = jsonrpc_stream_success(cmd); + + json_add_string(response, "result", + "No backup received from peers"); + return command_finished(cmd, response); + } + + if (!fromwire_static_chan_backup(cmd, + res, + &version, + ×tamp, + &scb)) { + plugin_err(cmd->plugin, "Corrupted SCB on disk!"); + } + + if (version != VERSION) { + plugin_err(cmd->plugin, + "Incompatible version, Contact the admin!"); + } + + req = jsonrpc_request_start(cmd->plugin, cmd, "recoverchannel", + after_recover_rpc, + &forward_error, NULL); + + json_array_start(req->js, "scb"); + for (size_t i=0; ijs, NULL, scb_hex, tal_bytelen(scb_hex)); + } + json_array_end(req->js); + + return send_outreq(cmd->plugin, req); + +} + +static struct command_result *json_restorefrompeer(struct command *cmd, + const char *buf, + const jsmntok_t *params) +{ + if (!param(cmd, buf, params, NULL)) + return command_param_failed(); + + return jsonrpc_get_datastore_binary(cmd->plugin, + cmd, + "chanbackup/latestscb", + after_latestscb, + NULL); +} + static const char *init(struct plugin *p, const char *buf UNUSED, const jsmntok_t *config UNUSED) @@ -719,6 +780,15 @@ static const struct plugin_command commands[] = { { "returns stub channel-id's on completion", json_emergencyrecover, }, + { + "restorefrompeer", + "recovery", + "Checks if i have got a backup from a peer, and if so, will stub " + "those channels in the database and if is successful, will return " + "list of channels that have been successfully stubbed", + "return channel-id's on completion", + json_restorefrompeer, + }, }; int main(int argc, char *argv[])