Skip to content

Commit

Permalink
Plugins/chanbackup: Send message upon every update to every peer!
Browse files Browse the repository at this point in the history
  • Loading branch information
adi2011 committed Aug 10, 2022
1 parent d01d58e commit 0e11176
Showing 1 changed file with 95 additions and 2 deletions.
97 changes: 95 additions & 2 deletions plugins/chanbackup.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <common/json_param.h>
#include <common/json_stream.h>
#include <common/scb_wiregen.h>
#include <common/type_to_string.h>
#include <errno.h>
#include <fcntl.h>
#include <plugins/libplugin.h>
Expand Down Expand Up @@ -387,6 +388,90 @@ static struct command_result *after_send_scb(struct command *cmd,
json_add_node_id(req->js,"key",nodeid);
return send_outreq(cmd->plugin, req);
}
bool wait;
static struct command_result *after_send_scb_single(struct command *cmd,
const char *buf,
const jsmntok_t *params,
struct node_id *nodeid)
{
plugin_log(cmd->plugin, LOG_DBG, "Peer storage sent to %s", type_to_string(tmpctx,struct node_id, nodeid));
wait = false;
return command_still_pending(cmd);
}

static void send_to_peer(struct command *cmd, struct node_id *node_id, u8 *msg) {
struct out_req *req;
wait = true;
req = jsonrpc_request_start(cmd->plugin,
cmd,
"sendcustommsg",
after_send_scb_single,
&forward_error,
node_id);

json_add_node_id(req->js, "node_id", node_id);
json_add_hex(req->js, "msg", msg,
tal_bytelen(msg));

plugin_log(cmd->plugin, LOG_DBG, "sending peer storage1");
send_outreq(cmd->plugin, req);
return;
}

static struct command_result *after_listpeers(struct command *cmd,
const char *buf,
const jsmntok_t *params,
void *cb_arg UNUSED)
{
const jsmntok_t *peers, *peer, *nodeid;
size_t i;
struct stat st;
struct node_id *node_id = tal(cmd, struct node_id);
bool is_connected;

int fd = open("emergency.recover", O_RDONLY);
if (fd < 0)
plugin_err(cmd->plugin, "Opening: %s", strerror(errno));

if (stat("emergency.recover", &st) != 0)
plugin_err(cmd->plugin, "SCB file is corrupted!: %s",
strerror(errno));

u8 *scb = tal_arr(cmd, u8, st.st_size);

if (!read_all(fd, scb, tal_bytelen(scb))) {
plugin_log(cmd->plugin, LOG_DBG, "SCB file is corrupted!: %s",
strerror(errno));
return NULL;
}

u8 *serialise_scb = towire_peer_storage(cmd, scb);

if (fsync(fd) != 0) {
plugin_err(cmd->plugin, "closing: %s", strerror(errno));
}

if (close(fd) != 0) {
plugin_err(cmd->plugin, "closing: %s", strerror(errno));
}

peers = json_get_member(buf, params, "peers");
if (peers != NULL) {
json_for_each_arr(i, peer, peers) {
json_to_bool(buf, json_get_member(buf, peer, "connected"), &is_connected);
if (is_connected) {
plugin_log(cmd->plugin, LOG_DBG, "sending peer storage");
nodeid = json_get_member(buf, peer, "id");
json_to_node_id(buf, nodeid, node_id);
send_to_peer(cmd, node_id, serialise_scb);
/* Wait till message is sent to this peer. */
do {}
while(wait);
}
}
}
return notification_handled(cmd);
}

static struct command_result *after_staticbackup(struct command *cmd,
const char *buf,
Expand All @@ -395,11 +480,19 @@ static struct command_result *after_staticbackup(struct command *cmd,
{
struct scb_chan **scb_chan;
const jsmntok_t *scbs = json_get_member(buf, params, "scb");
struct out_req *req;
json_to_scb_chan(buf, scbs, &scb_chan);
plugin_log(cmd->plugin, LOG_INFORM, "Updating the SCB");

update_scb(cmd->plugin, scb_chan);
return notification_handled(cmd);

req = jsonrpc_request_start(cmd->plugin,
cmd,
"listpeers",
after_listpeers,
&forward_error,
NULL);
return send_outreq(cmd->plugin, req);
}

static struct command_result *json_state_changed(struct command *cmd,
Expand Down Expand Up @@ -740,7 +833,7 @@ int main(int argc, char *argv[])
set_feature_bit(&features->bits[INIT_FEATURE],
YOUR_PEER_STORAGE_FEATUREBIT);

plugin_main(argv, init, PLUGIN_RESTARTABLE, true, NULL,
plugin_main(argv, init, PLUGIN_STATIC, true, features,
commands, ARRAY_SIZE(commands),
notifs, ARRAY_SIZE(notifs), hooks, ARRAY_SIZE(hooks),
NULL, 0, /* Notification topics we publish */
Expand Down

0 comments on commit 0e11176

Please sign in to comment.