From 0e11176194ad7c09406f68a1a8e5fb28a2531d0f Mon Sep 17 00:00:00 2001 From: adi2011 Date: Thu, 4 Aug 2022 19:14:10 +0530 Subject: [PATCH] Plugins/chanbackup: Send message upon every update to every peer! --- plugins/chanbackup.c | 97 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) diff --git a/plugins/chanbackup.c b/plugins/chanbackup.c index f392117b51ff..7e51f8ec54c5 100644 --- a/plugins/chanbackup.c +++ b/plugins/chanbackup.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -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, @@ -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, @@ -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 */