Skip to content

Commit

Permalink
lightningd: wait for gossipd to finish initalizing before starting pl…
Browse files Browse the repository at this point in the history
…ugins.

This mainly helps our CI under valgrind, which starts a fresh instance
and immediately calls the invoice command.  This can cause the topology
plugin to try to access the gossmap file before it's created.

We can also move the gossmap reading in topology to init time.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jun 14, 2021
1 parent 9a7d49d commit a0e1020
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 16 deletions.
4 changes: 4 additions & 0 deletions gossipd/gossipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,9 @@ static struct io_plan *gossip_init(struct io_conn *conn,
daemon->connectd = daemon_conn_new(daemon, CONNECTD_FD,
connectd_req, NULL, daemon);

/* OK, we are ready. */
daemon_conn_send(daemon->master,
take(towire_gossipd_init_reply(NULL)));
return daemon_conn_read_next(conn, daemon->master);
}

Expand Down Expand Up @@ -1507,6 +1510,7 @@ static struct io_plan *recv_req(struct io_conn *conn,
return onionmsg_req(conn, daemon, msg);
/* We send these, we don't receive them */
case WIRE_GOSSIPD_PING_REPLY:
case WIRE_GOSSIPD_INIT_REPLY:
case WIRE_GOSSIPD_GET_STRIPPED_CUPDATE_REPLY:
case WIRE_GOSSIPD_GET_TXOUT:
case WIRE_GOSSIPD_DEV_MEMLEAK_REPLY:
Expand Down
2 changes: 2 additions & 0 deletions gossipd/gossipd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ msgdata,gossipd_init,dev_gossip_time,?u32,
msgdata,gossipd_init,dev_fast_gossip,bool,
msgdata,gossipd_init,dev_fast_gossip_prune,bool,

msgtype,gossipd_init_reply,3100

# In developer mode, we can mess with time.
msgtype,gossipd_dev_set_time,3001
msgdata,gossipd_dev_set_time,dev_gossip_time,u32,
Expand Down
23 changes: 22 additions & 1 deletion gossipd/gossipd_wiregen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion gossipd/gossipd_wiregen.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 18 additions & 2 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ static unsigned gossip_msg(struct subd *gossip, const u8 *msg, const int *fds)
case WIRE_GOSSIPD_SEND_ONIONMSG:
case WIRE_GOSSIPD_ADDGOSSIP:
/* This is a reply, so never gets through to here. */
case WIRE_GOSSIPD_INIT_REPLY:
case WIRE_GOSSIPD_DEV_MEMLEAK_REPLY:
case WIRE_GOSSIPD_DEV_COMPACT_STORE_REPLY:
case WIRE_GOSSIPD_GET_STRIPPED_CUPDATE_REPLY:
Expand Down Expand Up @@ -187,6 +188,16 @@ static void gossip_topology_synced(struct chain_topology *topo, void *unused)
gossip_notify_new_block(topo->ld, get_block_height(topo));
}

/* We make sure gossipd is started before plugins (which may want gossip_map) */
static void gossipd_init_done(struct subd *gossipd,
const u8 *msg,
const int *fds,
void *unused)
{
/* Break out of loop, so we can begin */
io_break(gossipd);
}

/* Create the `gossipd` subdaemon and send the initialization
* message */
void gossip_init(struct lightningd *ld, int connectd_fd)
Expand All @@ -207,7 +218,7 @@ void gossip_init(struct lightningd *ld, int connectd_fd)
gossip_topology_synced, NULL);

msg = towire_gossipd_init(
tmpctx,
NULL,
chainparams,
ld->our_features,
&ld->id,
Expand All @@ -217,7 +228,12 @@ void gossip_init(struct lightningd *ld, int connectd_fd)
IFDEV(ld->dev_gossip_time ? &ld->dev_gossip_time: NULL, NULL),
IFDEV(ld->dev_fast_gossip, false),
IFDEV(ld->dev_fast_gossip_prune, false));
subd_send_msg(ld->gossip, msg);

subd_req(ld->gossip, ld->gossip, take(msg), -1, 0,
gossipd_init_done, NULL);

/* Wait for gossipd_init_reply */
io_loop(NULL, NULL);
}

void gossipd_notify_spend(struct lightningd *ld,
Expand Down
21 changes: 9 additions & 12 deletions plugins/topology.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,15 @@
#include <wire/peer_wire.h>

/* Access via get_gossmap() */
static struct gossmap *global_gossmap;
static struct node_id local_id;
static struct plugin *plugin;

/* We load this on demand, since we can start before gossipd. */
static struct gossmap *get_gossmap(void)
{
static struct gossmap *gossmap;

if (gossmap)
gossmap_refresh(gossmap);
else {
gossmap = notleak_with_children(gossmap_load(NULL,
GOSSIP_STORE_FILENAME));
if (!gossmap)
plugin_err(plugin, "Could not load gossmap %s: %s",
GOSSIP_STORE_FILENAME, strerror(errno));
}
return gossmap;
gossmap_refresh(global_gossmap);
return global_gossmap;
}

/* Convenience global since route_score_fuzz doesn't take args. 0 to 1. */
Expand Down Expand Up @@ -677,6 +668,12 @@ static const char *init(struct plugin *p,
take(json_out_obj(NULL, NULL, NULL)),
"{id:%}", JSON_SCAN(json_to_node_id, &local_id));

global_gossmap = notleak_with_children(gossmap_load(NULL,
GOSSIP_STORE_FILENAME));
if (!global_gossmap)
plugin_err(plugin, "Could not load gossmap %s: %s",
GOSSIP_STORE_FILENAME, strerror(errno));

return NULL;
}

Expand Down

0 comments on commit a0e1020

Please sign in to comment.