lightningd/gossip: convert to subd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-03-10 21:20:43 +10:30
parent 7f406ea80d
commit 5dc8cb12be
7 changed files with 81 additions and 78 deletions

View File

@@ -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/%)

View File

@@ -17,8 +17,7 @@
#include <inttypes.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/debug.h>
#include <lightningd/gossip/gen_gossip_control_wire.h>
#include <lightningd/gossip/gen_gossip_status_wire.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <secp256k1_ecdh.h>
#include <sodium/randombytes.h>
#include <status.h>
@@ -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 (;;) {

View File

@@ -1,16 +0,0 @@
#include <lightningd/cryptomsg.h>
# 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

View File

@@ -1,5 +1,3 @@
#include <lightningd/cryptomsg.h>
# 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 <lightningd/cryptomsg.h>
# 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
1 #include <lightningd/cryptomsg.h> # These are fatal.
#include <lightningd/cryptomsg.h>
# These are fatal.
1 gossipstatus_init_failed,0x8000 # These are fatal.
2 gossipstatus_bad_new_peer_request,0x8001 gossipstatus_init_failed,0x8000
3 gossipstatus_bad_release_request,0x8002 gossipstatus_bad_new_peer_request,0x8001
6 # Peers can give a bad message, we close their fd, but no harm done. gossipstatus_fdpass_failed,0x8004
7 gossipstatus_peer_bad_msg,100 # Peers can give a bad message, we close their fd, but no harm done.
8 gossipstatus_peer_bad_msg,0,unique_id,8 gossipstatus_peer_bad_msg,1000
9 gossipstatus_peer_bad_msg,8,len,2 gossipstatus_peer_bad_msg,0,unique_id,8
10 gossipstatus_peer_bad_msg,10,err,len,u8 gossipstatus_peer_bad_msg,8,len,2
11 # This is where we save a peer's features. gossipstatus_peer_bad_msg,10,err,len,u8
12 #gossipstatus_peer_features,1 #include <lightningd/cryptomsg.h>
13 #gossipstatus_peer_features,0,unique_id,8 # These take an fd, but have no response
14 # (if it is to move onto a channel, we get a status msg).
15 gossipctl_new_peer,1
16 gossipctl_new_peer,0,unique_id,8
17 gossipctl_new_peer,8,crypto_state,144,struct crypto_state
18 # Tell it to release a peer which has initialized.
19 gossipctl_release_peer,2
20 gossipctl_release_peer,0,unique_id,8
21 # This releases the peer and returns the cryptostate (followed by fd)
22 gossipctl_release_peer_reply,102
23 gossipctl_release_peer_reply,0,unique_id,8
24 gossipctl_release_peer_reply,8,crypto_state,144,struct crypto_state
25 # This is where we save a peer's features.
26 #gossipstatus_peer_features,1
27 #gossipstatus_peer_features,0,unique_id,8
28 #gossipstatus_peer_features,8,gflen,2
29 #gossipstatus_peer_features,10,globalfeatures,gflen
30 #gossipstatus_peer_features,10+gflen,lflen,2
31 #gossipstatus_peer_features,8,gflen,2 #gossipstatus_peer_features,12+gflen,localfeatures,lflen
32 #gossipstatus_peer_features,10,globalfeatures,gflen # Peer init handshake complete (now you can release_peer if you want)
33 #gossipstatus_peer_features,10+gflen,lflen,2 gossipstatus_peer_ready,3
37 gossipstatus_peer_ready,0,unique_id,8 gossipstatus_peer_nongossip,0,unique_id,8
38 # Peer can send non-gossip packet (usually an open_channel) (followed by fd) gossipstatus_peer_nongossip,10,crypto_state,144,struct crypto_state
39 gossipstatus_peer_nongossip,3 gossipstatus_peer_nongossip,154,len,2
40 gossipstatus_peer_nongossip,0,unique_id,8 gossipstatus_peer_nongossip,156,msg,len,u8
41 gossipstatus_peer_nongossip,10,crypto_state,144,struct crypto_state
42 gossipstatus_peer_nongossip,154,len,2
43 gossipstatus_peer_nongossip,156,msg,len,u8
44
45
46
47

View File

@@ -1,6 +1,7 @@
#include "gossip_control.h"
#include "lightningd.h"
#include "peer_control.h"
#include "subd.h"
#include "subdaemon.h"
#include <ccan/err/err.h>
#include <ccan/take/take.h>
@@ -8,11 +9,10 @@
#include <daemon/log.h>
#include <inttypes.h>
#include <lightningd/cryptomsg.h>
#include <lightningd/gossip/gen_gossip_control_wire.h>
#include <lightningd/gossip/gen_gossip_status_wire.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <wire/gen_peer_wire.h>
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");
}

View File

@@ -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;

View File

@@ -19,8 +19,7 @@
#include <lightningd/channel/gen_channel_control_wire.h>
#include <lightningd/channel/gen_channel_status_wire.h>
#include <lightningd/funding_tx.h>
#include <lightningd/gossip/gen_gossip_control_wire.h>
#include <lightningd/gossip/gen_gossip_status_wire.h>
#include <lightningd/gossip/gen_gossip_wire.h>
#include <lightningd/handshake/gen_handshake_control_wire.h>
#include <lightningd/handshake/gen_handshake_status_wire.h>
#include <lightningd/hsm/gen_hsm_wire.h>
@@ -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 = {