55#include <common/hsm_encryption.h>
66#include <common/json_param.h>
77#include <common/json_stream.h>
8+ #include <common/memleak.h>
89#include <common/type_to_string.h>
910#include <errno.h>
1011#include <plugins/libplugin.h>
1112#include <unistd.h>
1213
1314static struct plugin * plugin ;
14- static struct plugin_timer * lost_state_timer ;
15+ static struct gossmap * global_gossmap ;
16+ static struct plugin_timer * lost_state_timer , * find_exes_timer , * peer_storage_timer ;
17+
1518/* This tells if we are already in the process of recovery. */
1619static bool recovery , already_has_peers ;
1720static void do_check_lost_peer (void * unused );
21+ static void do_check_gossip (struct command * cmd );
22+ static void do_find_peer_storage (struct command * cmd );
1823static struct node_id local_id ;
1924
2025/* List of most connected nodes on the network */
@@ -60,10 +65,81 @@ static struct command_result *after_emergency_recover(struct command *cmd,
6065 return command_still_pending (cmd );
6166}
6267
68+ static struct command_result * after_restorefrompeer (struct command * cmd ,
69+ const char * buf ,
70+ const jsmntok_t * params ,
71+ void * cb_arg UNUSED )
72+ {
73+ plugin_log (plugin , LOG_DBG , "restorefrompeer called" );
74+
75+ peer_storage_timer = plugin_timer (plugin , time_from_sec (5 ), do_find_peer_storage , cmd );
76+ return command_still_pending (cmd );
77+ }
78+
79+ static struct command_result * find_peer_storage (struct command * cmd )
80+ {
81+ peer_storage_timer = NULL ;
82+
83+ struct out_req * req ;
84+ req = jsonrpc_request_start (plugin , cmd , "restorefrompeer" ,
85+ after_restorefrompeer ,
86+ & forward_error , NULL );
87+
88+ return send_outreq (plugin , req );
89+ }
90+
91+ static void do_find_peer_storage (struct command * cmd )
92+ {
93+ find_peer_storage (cmd );
94+ return ;
95+ }
96+
97+
98+ static void do_check_gossip (struct command * cmd )
99+ {
100+ find_exes_timer = NULL ;
101+
102+ gossmap_refresh (global_gossmap , NULL );
103+
104+ plugin_log (plugin , LOG_DBG , "Finding our node in gossip" );
105+
106+ struct gossmap_node * n = gossmap_find_node (global_gossmap , & local_id );
107+
108+ if (n ) {
109+ for (size_t i = 0 ; i < n -> num_chans ; i ++ ) {
110+ int half ;
111+ struct node_id peer_id ;
112+ struct gossmap_chan * c = gossmap_nth_chan (global_gossmap , n , i , & half );
113+ struct gossmap_node * neighbour = gossmap_nth_node (global_gossmap , c , !half );
114+
115+ gossmap_node_get_id (global_gossmap , neighbour , & peer_id );
116+
117+ struct out_req * req ;
118+ req = jsonrpc_request_start (plugin ,
119+ cmd ,
120+ "connect" ,
121+ connect_success ,
122+ connect_fail ,
123+ NULL );
124+
125+ json_add_node_id (req -> js , "id" , & peer_id );
126+
127+ plugin_log (plugin , LOG_DBG , "Connecting to: %s" , type_to_string (tmpctx , struct node_id , & peer_id ));
128+ send_outreq (plugin , req );
129+
130+ }
131+
132+ peer_storage_timer = plugin_timer (plugin , time_from_sec (5 ), do_find_peer_storage , cmd );
133+ return ;
134+ }
135+
136+ find_exes_timer = plugin_timer (plugin , time_from_sec (5 ), do_check_gossip , cmd );
137+ return ;
138+ }
63139
64140static void entering_recovery_mode (struct command * cmd )
65141{
66- if (!already_has_peers ) {
142+ if (!already_has_peers ) {
67143 for (size_t i = 0 ; i < ARRAY_SIZE (nodes_for_gossip ); i ++ ) {
68144 struct out_req * req ;
69145 req = jsonrpc_request_start (plugin ,
@@ -89,6 +165,7 @@ static void entering_recovery_mode(struct command *cmd)
89165 NULL );
90166
91167 send_outreq (plugin , req_emer_recovery );
168+ find_exes_timer = plugin_timer (plugin , time_from_sec (5 ), do_check_gossip , cmd );
92169 return ;
93170}
94171
@@ -157,6 +234,7 @@ static const char *init(struct plugin *p,
157234 recovery = false;
158235 lost_state_timer = plugin_timer (plugin , time_from_sec (2 ), do_check_lost_peer , NULL );
159236 u32 num_peers ;
237+ size_t num_cupdates_rejected ;
160238
161239 /* Find number of peers */
162240 rpc_scan (p , "getinfo" ,
@@ -165,6 +243,21 @@ static const char *init(struct plugin *p,
165243 JSON_SCAN (json_to_node_id , & local_id ),
166244 JSON_SCAN (json_to_u32 , & num_peers ));
167245
246+ global_gossmap = notleak_with_children (gossmap_load (NULL ,
247+ GOSSIP_STORE_FILENAME ,
248+ & num_cupdates_rejected ));
249+
250+ if (!global_gossmap )
251+ plugin_err (p , "Could not load gossmap %s: %s" ,
252+ GOSSIP_STORE_FILENAME , strerror (errno ));
253+
254+ if (num_cupdates_rejected )
255+ plugin_log (p , LOG_DBG ,
256+ "gossmap ignored %zu channel updates" ,
257+ num_cupdates_rejected );
258+
259+ plugin_log (p , LOG_DBG , "Gossmap loaded!" );
260+
168261 already_has_peers = num_peers > 1 ? 1 : 0 ;
169262
170263 return NULL ;
0 commit comments