From 5dc8cb12bea3ef13ecf5ce5e4b4964734125acd1 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 10 Mar 2017 21:20:43 +1030 Subject: [PATCH] lightningd/gossip: convert to subd. Signed-off-by: Rusty Russell --- lightningd/gossip/Makefile | 23 ++++------- lightningd/gossip/gossip.c | 29 ++++++++------ lightningd/gossip/gossip_control_wire_csv | 16 -------- ...gossip_status_wire_csv => gossip_wire.csv} | 25 +++++++++--- lightningd/gossip_control.c | 39 +++++++++++-------- lightningd/lightningd.h | 2 +- lightningd/peer_control.c | 25 ++++++------ 7 files changed, 81 insertions(+), 78 deletions(-) delete mode 100644 lightningd/gossip/gossip_control_wire_csv rename lightningd/gossip/{gossip_status_wire_csv => gossip_wire.csv} (63%) diff --git a/lightningd/gossip/Makefile b/lightningd/gossip/Makefile index b2a5d20a1..47224f3f2 100644 --- a/lightningd/gossip/Makefile +++ b/lightningd/gossip/Makefile @@ -7,10 +7,8 @@ lightningd/gossip-wrongdir: default: lightningd/gossip-all # Control daemon uses this: -LIGHTNINGD_GOSSIP_CONTROL_HEADERS := lightningd/gossip/gen_gossip_control_wire.h \ - lightningd/gossip/gen_gossip_status_wire.h -LIGHTNINGD_GOSSIP_CONTROL_SRC := lightningd/gossip/gen_gossip_control_wire.c \ - lightningd/gossip/gen_gossip_status_wire.c +LIGHTNINGD_GOSSIP_CONTROL_HEADERS := lightningd/gossip/gen_gossip_wire.h +LIGHTNINGD_GOSSIP_CONTROL_SRC := lightningd/gossip/gen_gossip_wire.c LIGHTNINGD_GOSSIP_CONTROL_OBJS := $(LIGHTNINGD_GOSSIP_CONTROL_SRC:.c=.o) # These should eventually be migrated to the lightningd directory, after @@ -19,8 +17,7 @@ LIGHTNINGD_GOSSIP_LEGACY_HEADERS := daemon/routing.h daemon/broadcast.h \ daemon/log.h daemon/pseudorand.h daemon/timeout.h # lightningd/gossip needs these: -LIGHTNINGD_GOSSIP_HEADERS := lightningd/gossip/gen_gossip_control_wire.h \ - lightningd/gossip/gen_gossip_status_wire.h \ +LIGHTNINGD_GOSSIP_HEADERS := lightningd/gossip/gen_gossip_wire.h \ $(LIGHTNINGD_GOSSIP_LEGACY_HEADERS) LIGHTNINGD_GOSSIP_SRC := lightningd/gossip/gossip.c \ $(LIGHTNINGD_GOSSIP_HEADERS:.h=.c) @@ -42,17 +39,11 @@ lightningd/gossip-all: lightningd/lightningd_gossip $(LIGHTNINGD_GOSSIP_CLIENT_O lightningd/lightningd_gossip: $(LIGHTNINGD_GOSSIP_OBJS) $(CORE_OBJS) $(CORE_TX_OBJS) $(BITCOIN_OBJS) $(WIRE_OBJS) $(CCAN_OBJS) $(LIGHTNINGD_OLD_LIB_OBJS) $(LIGHTNINGD_LIB_OBJS) $(LIBBASE58_OBJS) libsecp256k1.a libsodium.a libwallycore.a $(CC) $(CFLAGS) -o $@ $^ $(LDLIBS) -lightningd/gossip/gen_gossip_control_wire.h: $(WIRE_GEN) lightningd/gossip/gossip_control_wire_csv - $(WIRE_GEN) --header $@ gossip_control_wire_type < lightningd/gossip/gossip_control_wire_csv > $@ +lightningd/gossip/gen_gossip_wire.h: $(WIRE_GEN) lightningd/gossip/gossip_wire.csv + $(WIRE_GEN) --header $@ gossip_wire_type < lightningd/gossip/gossip_wire.csv > $@ -lightningd/gossip/gen_gossip_control_wire.c: $(WIRE_GEN) lightningd/gossip/gossip_control_wire_csv - $(WIRE_GEN) ${@:.c=.h} gossip_control_wire_type < lightningd/gossip/gossip_control_wire_csv > $@ - -lightningd/gossip/gen_gossip_status_wire.h: $(WIRE_GEN) lightningd/gossip/gossip_status_wire_csv - $(WIRE_GEN) --header $@ gossip_status_wire_type < lightningd/gossip/gossip_status_wire_csv > $@ - -lightningd/gossip/gen_gossip_status_wire.c: $(WIRE_GEN) lightningd/gossip/gossip_status_wire_csv - $(WIRE_GEN) ${@:.c=.h} gossip_status_wire_type < lightningd/gossip/gossip_status_wire_csv > $@ +lightningd/gossip/gen_gossip_wire.c: $(WIRE_GEN) lightningd/gossip/gossip_wire.csv + $(WIRE_GEN) ${@:.c=.h} gossip_wire_type < lightningd/gossip/gossip_wire.csv > $@ check-source: $(LIGHTNINGD_GOSSIP_ALLSRC_NOGEN:%=check-src-include-order/%) $(LIGHTNINGD_GOSSIP_ALLHEADERS_NOGEN:%=check-hdr-include-order/%) check-source-bolt: $(LIGHTNINGD_GOSSIP_SRC:%=bolt-check/%) $(LIGHTNINGD_GOSSIP_HEADERS:%=bolt-check/%) diff --git a/lightningd/gossip/gossip.c b/lightningd/gossip/gossip.c index eba07f20c..66627f042 100644 --- a/lightningd/gossip/gossip.c +++ b/lightningd/gossip/gossip.c @@ -17,8 +17,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -343,9 +342,9 @@ static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon, tal_steal(daemon, peer); io_close_taken_fd(peer->conn); - out = towire_gossipctl_release_peer_response(msg, - unique_id, - &peer->pcs.cs); + out = towire_gossipctl_release_peer_reply(msg, + unique_id, + &peer->pcs.cs); return io_write_wire(conn, out, release_peer_fd, peer); } } @@ -355,11 +354,10 @@ static struct io_plan *release_peer(struct io_conn *conn, struct daemon *daemon, static struct io_plan *recv_req(struct io_conn *conn, struct daemon *daemon) { - enum gossip_control_wire_type t = fromwire_peektype(daemon->msg_in); + enum gossip_wire_type t = fromwire_peektype(daemon->msg_in); status_trace("req: type %s len %zu", - gossip_control_wire_type_name(t), - tal_count(daemon->msg_in)); + gossip_wire_type_name(t), tal_count(daemon->msg_in)); switch (t) { case WIRE_GOSSIPCTL_NEW_PEER: @@ -367,7 +365,15 @@ static struct io_plan *recv_req(struct io_conn *conn, struct daemon *daemon) case WIRE_GOSSIPCTL_RELEASE_PEER: return release_peer(conn, daemon, daemon->msg_in); - case WIRE_GOSSIPCTL_RELEASE_PEER_RESPONSE: + case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY: + case WIRE_GOSSIPSTATUS_INIT_FAILED: + case WIRE_GOSSIPSTATUS_BAD_NEW_PEER_REQUEST: + case WIRE_GOSSIPSTATUS_BAD_RELEASE_REQUEST: + case WIRE_GOSSIPSTATUS_BAD_REQUEST: + case WIRE_GOSSIPSTATUS_FDPASS_FAILED: + case WIRE_GOSSIPSTATUS_PEER_BAD_MSG: + case WIRE_GOSSIPSTATUS_PEER_READY: + case WIRE_GOSSIPSTATUS_PEER_NONGOSSIP: break; } @@ -399,9 +405,8 @@ int main(int argc, char *argv[]) timers_init(&daemon->timers, time_mono()); daemon->msg_in = NULL; - /* Stdout == status, stdin == requests */ - status_setup(STDOUT_FILENO); - + /* stdin == control */ + status_setup(STDIN_FILENO); io_new_conn(NULL, STDIN_FILENO, next_req_in, daemon); for (;;) { diff --git a/lightningd/gossip/gossip_control_wire_csv b/lightningd/gossip/gossip_control_wire_csv deleted file mode 100644 index aa8ed7761..000000000 --- a/lightningd/gossip/gossip_control_wire_csv +++ /dev/null @@ -1,16 +0,0 @@ -#include - -# These take an fd, but have no response -# (if it is to move onto a channel, we get a status msg). -gossipctl_new_peer,1 -gossipctl_new_peer,0,unique_id,8 -gossipctl_new_peer,8,crypto_state,144,struct crypto_state - -# Tell it to release a peer which has initialized. -gossipctl_release_peer,2 -gossipctl_release_peer,0,unique_id,8 - -# This releases the peer and returns the cryptostate (followed by fd) -gossipctl_release_peer_response,102 -gossipctl_release_peer_response,0,unique_id,8 -gossipctl_release_peer_response,8,crypto_state,144,struct crypto_state diff --git a/lightningd/gossip/gossip_status_wire_csv b/lightningd/gossip/gossip_wire.csv similarity index 63% rename from lightningd/gossip/gossip_status_wire_csv rename to lightningd/gossip/gossip_wire.csv index 8eba3b60f..8cf74da74 100644 --- a/lightningd/gossip/gossip_status_wire_csv +++ b/lightningd/gossip/gossip_wire.csv @@ -1,5 +1,3 @@ -#include - # These are fatal. gossipstatus_init_failed,0x8000 gossipstatus_bad_new_peer_request,0x8001 @@ -8,11 +6,28 @@ gossipstatus_bad_request,0x8003 gossipstatus_fdpass_failed,0x8004 # Peers can give a bad message, we close their fd, but no harm done. -gossipstatus_peer_bad_msg,100 +gossipstatus_peer_bad_msg,1000 gossipstatus_peer_bad_msg,0,unique_id,8 gossipstatus_peer_bad_msg,8,len,2 gossipstatus_peer_bad_msg,10,err,len,u8 +#include + +# These take an fd, but have no response +# (if it is to move onto a channel, we get a status msg). +gossipctl_new_peer,1 +gossipctl_new_peer,0,unique_id,8 +gossipctl_new_peer,8,crypto_state,144,struct crypto_state + +# Tell it to release a peer which has initialized. +gossipctl_release_peer,2 +gossipctl_release_peer,0,unique_id,8 + +# This releases the peer and returns the cryptostate (followed by fd) +gossipctl_release_peer_reply,102 +gossipctl_release_peer_reply,0,unique_id,8 +gossipctl_release_peer_reply,8,crypto_state,144,struct crypto_state + # This is where we save a peer's features. #gossipstatus_peer_features,1 #gossipstatus_peer_features,0,unique_id,8 @@ -22,11 +37,11 @@ gossipstatus_peer_bad_msg,10,err,len,u8 #gossipstatus_peer_features,12+gflen,localfeatures,lflen # Peer init handshake complete (now you can release_peer if you want) -gossipstatus_peer_ready,2 +gossipstatus_peer_ready,3 gossipstatus_peer_ready,0,unique_id,8 # Peer can send non-gossip packet (usually an open_channel) (followed by fd) -gossipstatus_peer_nongossip,3 +gossipstatus_peer_nongossip,4 gossipstatus_peer_nongossip,0,unique_id,8 gossipstatus_peer_nongossip,10,crypto_state,144,struct crypto_state gossipstatus_peer_nongossip,154,len,2 diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 6c3d898fb..e1e6f4516 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -1,6 +1,7 @@ #include "gossip_control.h" #include "lightningd.h" #include "peer_control.h" +#include "subd.h" #include "subdaemon.h" #include #include @@ -8,11 +9,10 @@ #include #include #include -#include -#include +#include #include -static void gossip_finished(struct subdaemon *gossip, int status) +static void gossip_finished(struct subd *gossip, int status) { if (WIFEXITED(status)) errx(1, "Gossip failed (exit status %i), exiting.", @@ -20,7 +20,7 @@ static void gossip_finished(struct subdaemon *gossip, int status) errx(1, "Gossip failed (signal %u), exiting.", WTERMSIG(status)); } -static void peer_bad_message(struct subdaemon *gossip, const u8 *msg) +static void peer_bad_message(struct subd *gossip, const u8 *msg) { u64 unique_id; struct peer *peer; @@ -38,11 +38,11 @@ static void peer_bad_message(struct subdaemon *gossip, const u8 *msg) type_to_string(msg, struct pubkey, peer->id), tal_hex(msg, msg)); peer_set_condition(peer, "Bad message %s during gossip phase", - gossip_status_wire_type_name(fromwire_peektype(msg))); + gossip_wire_type_name(fromwire_peektype(msg))); tal_free(peer); } -static void peer_nongossip(struct subdaemon *gossip, const u8 *msg, int fd) +static void peer_nongossip(struct subd *gossip, const u8 *msg, int fd) { u64 unique_id; struct peer *peer; @@ -58,7 +58,8 @@ static void peer_nongossip(struct subdaemon *gossip, const u8 *msg, int fd) if (!peer) fatal("Gossip gave bad peerid %"PRIu64, unique_id); - if (peer->owner != gossip) + /* FIXME! */ + if (peer->owner != (struct subdaemon *)gossip) fatal("Gossip gave bad peerid %"PRIu64" (owner %s)", unique_id, peer->owner ? peer->owner->name : "(none)"); @@ -72,7 +73,7 @@ static void peer_nongossip(struct subdaemon *gossip, const u8 *msg, int fd) peer_accept_open(peer, &cs, inner); } -static void peer_ready(struct subdaemon *gossip, const u8 *msg) +static void peer_ready(struct subd *gossip, const u8 *msg) { u64 unique_id; struct peer *peer; @@ -102,10 +103,10 @@ static void peer_ready(struct subdaemon *gossip, const u8 *msg) peer_set_condition(peer, "Exchanging gossip"); } -static enum subdaemon_status gossip_status(struct subdaemon *gossip, - const u8 *msg, int fd) +static enum subd_msg_ret gossip_msg(struct subd *gossip, + const u8 *msg, int fd) { - enum gossip_status_wire_type t = fromwire_peektype(msg); + enum gossip_wire_type t = fromwire_peektype(msg); switch (t) { /* We don't get told about fatal errors. */ @@ -114,28 +115,32 @@ static enum subdaemon_status gossip_status(struct subdaemon *gossip, case WIRE_GOSSIPSTATUS_BAD_REQUEST: case WIRE_GOSSIPSTATUS_FDPASS_FAILED: case WIRE_GOSSIPSTATUS_BAD_RELEASE_REQUEST: + /* These are messages we send, not them. */ + case WIRE_GOSSIPCTL_NEW_PEER: + case WIRE_GOSSIPCTL_RELEASE_PEER: + /* This is a reply, so never gets through to here. */ + case WIRE_GOSSIPCTL_RELEASE_PEER_REPLY: break; case WIRE_GOSSIPSTATUS_PEER_BAD_MSG: peer_bad_message(gossip, msg); break; case WIRE_GOSSIPSTATUS_PEER_NONGOSSIP: if (fd == -1) - return STATUS_NEED_FD; + return SUBD_NEED_FD; peer_nongossip(gossip, msg, fd); break; case WIRE_GOSSIPSTATUS_PEER_READY: peer_ready(gossip, msg); break; } - return STATUS_COMPLETE; + return SUBD_COMPLETE; } void gossip_init(struct lightningd *ld) { - ld->gossip = new_subdaemon(ld, ld, "lightningd_gossip", NULL, - gossip_status_wire_type_name, - gossip_control_wire_type_name, - gossip_status, gossip_finished, -1); + ld->gossip = new_subd(ld, ld, "lightningd_gossip", NULL, + gossip_wire_type_name, + gossip_msg, gossip_finished, -1); if (!ld->gossip) err(1, "Could not subdaemon gossip"); } diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 15a261fb0..9d1547ae9 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -27,7 +27,7 @@ struct lightningd { struct subd *hsm; /* Daemon looking after peers during init / before channel. */ - struct subdaemon *gossip; + struct subd *gossip; /* All peers we're tracking. */ struct list_head peers; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index f5bb58771..190de5bc3 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -19,8 +19,7 @@ #include #include #include -#include -#include +#include #include #include #include @@ -138,13 +137,15 @@ static void handshake_succeeded(struct subdaemon *hs, const u8 *msg, subdaemon_req(peer->owner, take(towire_handshake_exit_req(msg)), -1, NULL, NULL, NULL); - peer->owner = peer->ld->gossip; + /* FIXME! */ + peer->owner = (struct subdaemon *)peer->ld->gossip; tal_steal(peer->owner, peer); peer_set_condition(peer, "Beginning gossip"); /* Tell gossip to handle it now. */ msg = towire_gossipctl_new_peer(msg, peer->unique_id, &cs); - subdaemon_req(peer->ld->gossip, msg, peer->fd, &peer->fd, NULL, NULL); + subd_send_msg(peer->ld->gossip, msg); + subd_send_fd(peer->ld->gossip, peer->fd); /* Peer struct longer owns fd. */ peer->fd = -1; @@ -934,7 +935,7 @@ void peer_accept_open(struct peer *peer, } /* Peer has been released from gossip. Start opening. */ -static void gossip_peer_released(struct subdaemon *gossip, +static bool gossip_peer_released(struct subd *gossip, const u8 *resp, struct funding_channel *fc) { @@ -945,8 +946,8 @@ static void gossip_peer_released(struct subdaemon *gossip, u8 *msg; fc->cs = tal(fc, struct crypto_state); - if (!fromwire_gossipctl_release_peer_response(resp, NULL, &id, fc->cs)) - fatal("Gossup daemon gave invalid response %s", + if (!fromwire_gossipctl_release_peer_reply(resp, NULL, &id, fc->cs)) + fatal("Gossup daemon gave invalid reply %s", tal_hex(gossip, resp)); if (id != fc->peer->unique_id) @@ -965,7 +966,7 @@ static void gossip_peer_released(struct subdaemon *gossip, strerror(errno)); peer_set_condition(fc->peer, "Failed to subdaemon opening"); tal_free(fc->peer); - return; + return true; } /* They took our fd. */ fc->peer->fd = -1; @@ -992,6 +993,7 @@ static void gossip_peer_released(struct subdaemon *gossip, 15000, max_minimum_depth); subdaemon_req(fc->peer->owner, take(msg), -1, NULL, opening_gen_funding, fc); + return true; } static void json_fund_channel(struct command *cmd, @@ -1016,7 +1018,8 @@ static void json_fund_channel(struct command *cmd, command_fail(cmd, "Could not find peer with that peerid"); return; } - if (fc->peer->owner != ld->gossip) { + /* FIXME! */ + if (fc->peer->owner != (struct subdaemon *)ld->gossip) { command_fail(cmd, "Peer not ready for connection"); return; } @@ -1040,8 +1043,8 @@ static void json_fund_channel(struct command *cmd, /* Tie this fc lifetime (and hence utxo release) to the peer */ tal_steal(fc->peer, fc); tal_add_destructor(fc, fail_fundchannel_command); - subdaemon_req(ld->gossip, msg, -1, &fc->peer->fd, - gossip_peer_released, fc); + subd_req(ld->gossip, msg, -1, &fc->peer->fd, + gossip_peer_released, fc); } static const struct json_command fund_channel_command = {