gossipd: separate init and activate.

This means gossipd is live and we can tell it things, but it won't
receive incoming connections.  The split also means that the main daemon
continues (eg. loading peers from db) while gossipd is loading from the store,
potentially speeding startup.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2018-04-30 11:38:15 +09:30
committed by Christian Decker
parent 61317859f8
commit f083a699e2
6 changed files with 58 additions and 25 deletions

View File

@@ -118,6 +118,7 @@ static unsigned gossip_msg(struct subd *gossip, const u8 *msg, const int *fds)
switch (t) {
/* These are messages we send, not them. */
case WIRE_GOSSIPCTL_INIT:
case WIRE_GOSSIPCTL_ACTIVATE:
case WIRE_GOSSIP_GETNODES_REQUEST:
case WIRE_GOSSIP_GETROUTE_REQUEST:
case WIRE_GOSSIP_GETCHANNELS_REQUEST:
@@ -139,7 +140,7 @@ static unsigned gossip_msg(struct subd *gossip, const u8 *msg, const int *fds)
case WIRE_GOSSIPCTL_PEER_IMPORTANT:
case WIRE_GOSSIPCTL_PEER_DISCONNECTED:
/* This is a reply, so never gets through to here. */
case WIRE_GOSSIPCTL_INIT_REPLY:
case WIRE_GOSSIPCTL_ACTIVATE_REPLY:
case WIRE_GOSSIP_GET_UPDATE_REPLY:
case WIRE_GOSSIP_GETNODES_REPLY:
case WIRE_GOSSIP_GETROUTE_REPLY:
@@ -175,15 +176,6 @@ static unsigned gossip_msg(struct subd *gossip, const u8 *msg, const int *fds)
return 0;
}
static void gossip_init_done(struct subd *gossip UNUSED,
const u8 *reply UNUSED,
const int *fds UNUSED,
void *unused UNUSED)
{
/* Break out of loop, so we can begin */
io_break(gossip);
}
/* Create the `gossipd` subdaemon and send the initialization
* message */
void gossip_init(struct lightningd *ld)
@@ -217,13 +209,29 @@ void gossip_init(struct lightningd *ld)
msg = towire_gossipctl_init(
tmpctx, ld->config.broadcast_interval,
&get_chainparams(ld)->genesis_blockhash, &ld->id, ld->portnum,
&get_chainparams(ld)->genesis_blockhash, &ld->id,
get_offered_global_features(tmpctx),
get_offered_local_features(tmpctx), ld->wireaddrs, ld->rgb,
ld->alias, ld->config.channel_update_interval, no_reconnect);
subd_req(ld->gossip, ld->gossip, msg, -1, 0, gossip_init_done, NULL);
subd_send_msg(ld->gossip, msg);
}
/* Wait for init done */
static void gossip_activate_done(struct subd *gossip UNUSED,
const u8 *reply UNUSED,
const int *fds UNUSED,
void *unused UNUSED)
{
/* Break out of loop, so we can begin */
io_break(gossip);
}
void gossip_activate(struct lightningd *ld)
{
const u8 *msg = towire_gossipctl_activate(NULL, ld->portnum);
subd_req(ld->gossip, ld->gossip, take(msg), -1, 0,
gossip_activate_done, NULL);
/* Wait for activate done */
io_loop(NULL, NULL);
}

View File

@@ -8,6 +8,7 @@
struct lightningd;
void gossip_init(struct lightningd *ld);
void gossip_activate(struct lightningd *ld);
void gossipd_notify_spend(struct lightningd *ld,
const struct short_channel_id *scid);

View File

@@ -328,6 +328,9 @@ int main(int argc, char *argv[])
/* Now we know our ID, we can set our color/alias if not already. */
setup_color_and_alias(ld);
/* Set up gossip daemon. */
gossip_init(ld);
/* Everything is within a transaction. */
db_begin_transaction(ld->wallet->db);
@@ -391,9 +394,9 @@ int main(int argc, char *argv[])
ld->config.poll_time,
blockheight);
/* Set up gossip daemon. Needs to be after the initialization of
/* Activate gossip daemon. Needs to be after the initialization of
* chaintopology, otherwise we may be asking for uninitialized data. */
gossip_init(ld);
gossip_activate(ld);
/* Replay transactions for all running onchainds */
onchaind_replay_channels(ld);

View File

@@ -42,6 +42,9 @@ void fatal(const char *fmt UNNEEDED, ...)
/* Generated stub for free_htlcs */
void free_htlcs(struct lightningd *ld UNNEEDED, const struct channel *channel UNNEEDED)
{ fprintf(stderr, "free_htlcs called!\n"); abort(); }
/* Generated stub for gossip_activate */
void gossip_activate(struct lightningd *ld UNNEEDED)
{ fprintf(stderr, "gossip_activate called!\n"); abort(); }
/* Generated stub for gossip_init */
void gossip_init(struct lightningd *ld UNNEEDED)
{ fprintf(stderr, "gossip_init called!\n"); abort(); }