Skip to content

Commit

Permalink
Plugins/chanbackup: Add RPC for recovering from the latestscb receive…
Browse files Browse the repository at this point in the history
…d from peers.
  • Loading branch information
adi2011 authored and endothermicdev committed Feb 8, 2023
1 parent 7affaff commit e637e84
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion plugins/chanbackup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
&timestamp,
&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; i<tal_count(scb); i++) {
u8 *scb_hex = tal_arr(cmd, u8, 0);
towire_scb_chan(&scb_hex,scb[i]);
json_add_hex(req->js, 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)
Expand Down Expand Up @@ -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[])
Expand Down

0 comments on commit e637e84

Please sign in to comment.