Skip to content

Commit

Permalink
gossipd: split wire types into msgs from lightningd and msgs from per…
Browse files Browse the repository at this point in the history
…-peer daemons

This avoids some very ugly switch() statements which mixed the two,
but we also take the chance to rename 'towire_gossip_' to
'towire_gossipd_' for those inter-daemon messages; they're messages to
gossipd, not gossip messages.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Nov 21, 2018
1 parent 07b16e3 commit 5c60d7f
Show file tree
Hide file tree
Showing 17 changed files with 106 additions and 132 deletions.
2 changes: 1 addition & 1 deletion channeld/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ CHANNELD_COMMON_OBJS := \
common/version.o \
common/wire_error.o \
common/wireaddr.o \
gossipd/gen_gossip_wire.o \
gossipd/gen_gossip_peerd_wire.o \
hsmd/gen_hsm_wire.o \
lightningd/gossip_msg.o \
wire/fromwire.o \
Expand Down
36 changes: 18 additions & 18 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include <common/wire_error.h>
#include <errno.h>
#include <fcntl.h>
#include <gossipd/gen_gossip_wire.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gossip_constants.h>
#include <hsmd/gen_hsm_wire.h>
#include <inttypes.h>
Expand Down Expand Up @@ -260,15 +260,15 @@ static void send_channel_update(struct peer *peer, int disable_flag)

assert(peer->short_channel_ids[LOCAL].u64);

msg = towire_gossip_local_channel_update(NULL,
&peer->short_channel_ids[LOCAL],
disable_flag
== ROUTING_FLAGS_DISABLED,
peer->cltv_delta,
peer->conf[REMOTE].htlc_minimum_msat,
peer->fee_base,
peer->fee_per_satoshi,
advertised_htlc_max(
msg = towire_gossipd_local_channel_update(NULL,
&peer->short_channel_ids[LOCAL],
disable_flag
== ROUTING_FLAGS_DISABLED,
peer->cltv_delta,
peer->conf[REMOTE].htlc_minimum_msat,
peer->fee_base,
peer->fee_per_satoshi,
advertised_htlc_max(
peer->channel->funding_msat,
&peer->conf[LOCAL],
&peer->conf[REMOTE]));
Expand All @@ -290,10 +290,10 @@ static void make_channel_local_active(struct peer *peer)
u8 *msg;

/* Tell gossipd about local channel. */
msg = towire_gossip_local_add_channel(NULL,
&peer->short_channel_ids[LOCAL],
&peer->node_ids[REMOTE],
peer->channel->funding_msat / 1000);
msg = towire_gossipd_local_add_channel(NULL,
&peer->short_channel_ids[LOCAL],
&peer->node_ids[REMOTE],
peer->channel->funding_msat / 1000);
wire_sync_write(GOSSIP_FD, take(msg));

/* Tell gossipd and the other side what parameters we expect should
Expand Down Expand Up @@ -741,7 +741,7 @@ static u8 *master_wait_sync_reply(const tal_t *ctx,

static u8 *gossipd_wait_sync_reply(const tal_t *ctx,
struct peer *peer, const u8 *msg,
enum gossip_wire_type replytype)
enum gossip_peerd_wire_type replytype)
{
return wait_sync_reply(ctx, msg, replytype,
GOSSIP_FD, peer->from_gossipd, "gossipd");
Expand All @@ -753,10 +753,10 @@ static u8 *foreign_channel_update(const tal_t *ctx,
{
u8 *msg, *update, *channel_update;

msg = towire_gossip_get_update(NULL, scid);
msg = towire_gossipd_get_update(NULL, scid);
msg = gossipd_wait_sync_reply(tmpctx, peer, take(msg),
WIRE_GOSSIP_GET_UPDATE_REPLY);
if (!fromwire_gossip_get_update_reply(ctx, msg, &update))
WIRE_GOSSIPD_GET_UPDATE_REPLY);
if (!fromwire_gossipd_get_update_reply(ctx, msg, &update))
status_failed(STATUS_FAIL_GOSSIP_IO,
"Invalid update reply");

Expand Down
5 changes: 2 additions & 3 deletions closingd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ CLOSINGD_COMMON_OBJS := \
common/version.o \
common/wire_error.o \
common/wireaddr.o \
gossipd/gen_gossip_wire.o \
hsmd/gen_hsm_wire.o \
lightningd/gossip_msg.o
gossipd/gen_gossip_peerd_wire.o \
hsmd/gen_hsm_wire.o

closingd/gen_closing_wire.h: $(WIRE_GEN) closingd/closing_wire.csv
$(WIRE_GEN) --header $@ closing_wire_type < closingd/closing_wire.csv > $@
Expand Down
4 changes: 2 additions & 2 deletions common/read_peer_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <common/utils.h>
#include <common/wire_error.h>
#include <errno.h>
#include <gossipd/gen_gossip_wire.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <sys/select.h>
#include <wire/peer_wire.h>
#include <wire/wire_sync.h>
Expand Down Expand Up @@ -84,7 +84,7 @@ void handle_gossip_msg(int peer_fd, struct crypto_state *cs, const u8 *msg TAKES
{
u8 *gossip;

if (!fromwire_gossip_send_gossip(tmpctx, msg, &gossip)) {
if (!fromwire_gossipd_send_gossip(tmpctx, msg, &gossip)) {
status_broken("Got bad message from gossipd: %s",
tal_hex(msg, msg));
peer_failed_connection_lost();
Expand Down
11 changes: 9 additions & 2 deletions gossipd/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ gossipd-wrongdir:

default: gossipd-all

# Control daemon uses this:
# lightningd uses this:
LIGHTNINGD_GOSSIP_CONTROL_HEADERS := gossipd/gen_gossip_wire.h gossipd/gossip_constants.h
LIGHTNINGD_GOSSIP_CONTROL_SRC := gossipd/gen_gossip_wire.c
LIGHTNINGD_GOSSIP_CONTROL_OBJS := $(LIGHTNINGD_GOSSIP_CONTROL_SRC:.c=.o)

# gossipd needs these:
LIGHTNINGD_GOSSIP_HEADERS := gossipd/gen_gossip_wire.h \
gossipd/gen_gossip_peerd_wire.h \
gossipd/gen_gossip_store.h \
gossipd/gossip_store.h \
gossipd/routing.h \
Expand All @@ -23,7 +24,7 @@ LIGHTNINGD_GOSSIP_OBJS := $(LIGHTNINGD_GOSSIP_SRC:.c=.o)
# Make sure these depend on everything.
ALL_OBJS += $(LIGHTNINGD_GOSSIP_OBJS)
ALL_PROGRAMS += lightningd/lightning_gossipd
ALL_GEN_HEADERS += gossipd/gen_gossip_wire.h
ALL_GEN_HEADERS += gossipd/gen_gossip_wire.h gossipd/gen_gossip_peerd_wire.h

# For checking
LIGHTNINGD_GOSSIP_ALLSRC_NOGEN := $(filter-out gossipd/gen_%, $(LIGHTNINGD_GOSSIP_CLIENT_SRC) $(LIGHTNINGD_GOSSIP_SRC))
Expand Down Expand Up @@ -79,6 +80,12 @@ gossipd/gen_gossip_wire.h: $(WIRE_GEN) gossipd/gossip_wire.csv
gossipd/gen_gossip_wire.c: $(WIRE_GEN) gossipd/gossip_wire.csv
$(WIRE_GEN) ${@:.c=.h} gossip_wire_type < gossipd/gossip_wire.csv > $@

gossipd/gen_gossip_peerd_wire.h: $(WIRE_GEN) gossipd/gossip_peerd_wire.csv
$(WIRE_GEN) --header $@ gossip_peerd_wire_type < gossipd/gossip_peerd_wire.csv > $@

gossipd/gen_gossip_peerd_wire.c: $(WIRE_GEN) gossipd/gossip_peerd_wire.csv
$(WIRE_GEN) ${@:.c=.h} gossip_peerd_wire_type < gossipd/gossip_peerd_wire.csv > $@

gossipd/gen_gossip_store.h: $(WIRE_GEN) gossipd/gossip_store.csv
$(WIRE_GEN) --header $@ gossip_store_type < gossipd/gossip_store.csv > $@

Expand Down
4 changes: 4 additions & 0 deletions gossipd/gossip_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@
*/
#define ANNOUNCE_MIN_DEPTH 6

/* Utility function that, given a source and a destination, gives us
* the direction bit the matching channel should get */
#define get_channel_direction(from, to) (pubkey_cmp(from, to) > 0)

#endif /* LIGHTNING_GOSSIPD_GOSSIP_CONSTANTS_H */
32 changes: 32 additions & 0 deletions gossipd/gossip_peerd_wire.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Channel daemon can ask for updates for a specific channel, for sending
# errors. Must be distinct from WIRE_CHANNEL_ANNOUNCEMENT etc. gossip msgs!
gossipd_get_update,3501
gossipd_get_update,,short_channel_id,struct short_channel_id

# If channel isn't known, update will be empty.
gossipd_get_update_reply,3601
gossipd_get_update_reply,,len,u16
gossipd_get_update_reply,,update,len*u8

# Gossipd can tell channeld etc about gossip to fwd.
gossipd_send_gossip,3502
gossipd_send_gossip,,len,u16
gossipd_send_gossip,,gossip,len*u8

# Both sides have seen the funding tx being locked, but we have not
# yet reached the announcement depth. So we add the channel locally so
# we (and peer) can update it already.
gossipd_local_add_channel,3503
gossipd_local_add_channel,,short_channel_id,struct short_channel_id
gossipd_local_add_channel,,remote_node_id,struct pubkey
gossipd_local_add_channel,,satoshis,u64

# Send this channel_update.
gossipd_local_channel_update,3504
gossipd_local_channel_update,,short_channel_id,struct short_channel_id
gossipd_local_channel_update,,disable,bool
gossipd_local_channel_update,,cltv_expiry_delta,u16
gossipd_local_channel_update,,htlc_minimum_msat,u64
gossipd_local_channel_update,,fee_base_msat,u32
gossipd_local_channel_update,,fee_proportional_millionths,u32
gossipd_local_channel_update,,htlc_maximum_msat,u64
3 changes: 2 additions & 1 deletion gossipd/gossip_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <common/utils.h>
#include <errno.h>
#include <fcntl.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_store.h>
#include <gossipd/gen_gossip_wire.h>
#include <stdio.h>
Expand Down Expand Up @@ -130,7 +131,7 @@ static bool gossip_store_append(int fd, struct routing_state *rstate, const u8 *
msg = towire_gossip_store_channel_update(tmpctx, gossip_msg);
else if(t == WIRE_NODE_ANNOUNCEMENT)
msg = towire_gossip_store_node_announcement(tmpctx, gossip_msg);
else if(t == WIRE_GOSSIP_LOCAL_ADD_CHANNEL)
else if(t == WIRE_GOSSIPD_LOCAL_ADD_CHANNEL)
msg = towire_gossip_store_local_add_channel(tmpctx, gossip_msg);
else if(t == WIRE_GOSSIP_STORE_CHANNEL_DELETE)
msg = gossip_msg;
Expand Down
33 changes: 1 addition & 32 deletions gossipd/gossip_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,7 @@ gossip_get_channel_peer,,channel_id,struct short_channel_id
gossip_get_channel_peer_reply,3109
gossip_get_channel_peer_reply,,peer_id,?struct pubkey

# Channel daemon can ask for updates for a specific channel, for sending
# errors. Must be distinct from WIRE_CHANNEL_ANNOUNCEMENT etc. gossip msgs!
gossip_get_update,3012
gossip_get_update,,short_channel_id,struct short_channel_id

# If channel isn't known, update will be empty.
gossip_get_update_reply,3112
gossip_get_update_reply,,len,u16
gossip_get_update_reply,,update,len*u8

# Gossipd can tell channeld etc about gossip to fwd.
gossip_send_gossip,3016
gossip_send_gossip,,len,u16
gossip_send_gossip,,gossip,len*u8

# Both sides have seen the funding tx being locked, but we have not
# yet reached the announcement depth. So we add the channel locally so
# we (and peer) can update it already.
gossip_local_add_channel,3017
gossip_local_add_channel,,short_channel_id,struct short_channel_id
gossip_local_add_channel,,remote_node_id,struct pubkey
gossip_local_add_channel,,satoshis,u64

gossip_local_channel_update,3026
gossip_local_channel_update,,short_channel_id,struct short_channel_id
gossip_local_channel_update,,disable,bool
gossip_local_channel_update,,cltv_expiry_delta,u16
gossip_local_channel_update,,htlc_minimum_msat,u64
gossip_local_channel_update,,fee_base_msat,u32
gossip_local_channel_update,,fee_proportional_millionths,u32
gossip_local_channel_update,,htlc_maximum_msat,u64

# gossipd->master: we're closing this channel.
gossip_local_channel_close,3027
gossip_local_channel_close,,short_channel_id,struct short_channel_id

Expand Down
71 changes: 21 additions & 50 deletions gossipd/gossipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <errno.h>
#include <fcntl.h>
#include <gossipd/broadcast.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_wire.h>
#include <gossipd/routing.h>
#include <hsmd/gen_hsm_wire.h>
Expand Down Expand Up @@ -174,7 +175,7 @@ static struct peer *find_peer(struct daemon *daemon, const struct pubkey *id)

static void queue_peer_msg(struct peer *peer, const u8 *msg TAKES)
{
const u8 *send = towire_gossip_send_gossip(NULL, msg);
const u8 *send = towire_gossipd_send_gossip(NULL, msg);
if (taken(msg))
tal_free(msg);
daemon_conn_send(peer->dc, take(send));
Expand Down Expand Up @@ -1048,7 +1049,7 @@ static bool handle_get_update(struct peer *peer, const u8 *msg)
struct routing_state *rstate = peer->daemon->rstate;
int direction;

if (!fromwire_gossip_get_update(msg, &scid)) {
if (!fromwire_gossipd_get_update(msg, &scid)) {
status_broken("peer %s sent bad gossip_get_update %s",
type_to_string(tmpctx, struct pubkey, &peer->id),
tal_hex(tmpctx, msg));
Expand Down Expand Up @@ -1086,7 +1087,7 @@ static bool handle_get_update(struct peer *peer, const u8 *msg)
type_to_string(tmpctx, struct short_channel_id, &scid),
update ? "got" : "no");

msg = towire_gossip_get_update_reply(NULL, update);
msg = towire_gossipd_get_update_reply(NULL, update);
daemon_conn_send(peer->dc, take(msg));
return true;
}
Expand Down Expand Up @@ -1119,14 +1120,14 @@ static bool handle_local_channel_update(struct peer *peer, const u8 *msg)
u32 fee_proportional_millionths;
int direction;

if (!fromwire_gossip_local_channel_update(msg,
&scid,
&disable,
&cltv_expiry_delta,
&htlc_minimum_msat,
&fee_base_msat,
&fee_proportional_millionths,
&htlc_maximum_msat)) {
if (!fromwire_gossipd_local_channel_update(msg,
&scid,
&disable,
&cltv_expiry_delta,
&htlc_minimum_msat,
&fee_base_msat,
&fee_proportional_millionths,
&htlc_maximum_msat)) {
status_broken("peer %s bad local_channel_update %s",
type_to_string(tmpctx, struct pubkey, &peer->id),
tal_hex(tmpctx, msg));
Expand Down Expand Up @@ -1244,51 +1245,26 @@ static struct io_plan *peer_msg_in(struct io_conn *conn,
}

/* Must be a gossip_wire_type asking us to do something. */
switch ((enum gossip_wire_type)fromwire_peektype(msg)) {
case WIRE_GOSSIP_GET_UPDATE:
switch ((enum gossip_peerd_wire_type)fromwire_peektype(msg)) {
case WIRE_GOSSIPD_GET_UPDATE:
ok = handle_get_update(peer, msg);
goto handled_cmd;
case WIRE_GOSSIP_LOCAL_ADD_CHANNEL:
case WIRE_GOSSIPD_LOCAL_ADD_CHANNEL:
ok = handle_local_add_channel(peer->daemon->rstate, msg);
if (ok)
gossip_store_add(peer->daemon->rstate->store, msg);
goto handled_cmd;
case WIRE_GOSSIP_LOCAL_CHANNEL_UPDATE:
case WIRE_GOSSIPD_LOCAL_CHANNEL_UPDATE:
ok = handle_local_channel_update(peer, msg);
goto handled_cmd;
case WIRE_GOSSIPCTL_INIT:
case WIRE_GOSSIP_GETNODES_REQUEST:
case WIRE_GOSSIP_GETNODES_REPLY:
case WIRE_GOSSIP_GETROUTE_REQUEST:
case WIRE_GOSSIP_GETROUTE_REPLY:
case WIRE_GOSSIP_GETCHANNELS_REQUEST:
case WIRE_GOSSIP_GETCHANNELS_REPLY:
case WIRE_GOSSIP_PING:
case WIRE_GOSSIP_PING_REPLY:
case WIRE_GOSSIP_QUERY_SCIDS:
case WIRE_GOSSIP_SCIDS_REPLY:
case WIRE_GOSSIP_SEND_TIMESTAMP_FILTER:
case WIRE_GOSSIP_QUERY_CHANNEL_RANGE:
case WIRE_GOSSIP_QUERY_CHANNEL_RANGE_REPLY:
case WIRE_GOSSIP_DEV_SET_MAX_SCIDS_ENCODE_SIZE:
case WIRE_GOSSIP_GET_CHANNEL_PEER:
case WIRE_GOSSIP_GET_CHANNEL_PEER_REPLY:
case WIRE_GOSSIP_GET_INCOMING_CHANNELS:
case WIRE_GOSSIP_GET_INCOMING_CHANNELS_REPLY:
case WIRE_GOSSIP_GET_UPDATE_REPLY:
case WIRE_GOSSIP_SEND_GOSSIP:
case WIRE_GOSSIP_LOCAL_CHANNEL_CLOSE:
case WIRE_GOSSIP_GET_TXOUT:
case WIRE_GOSSIP_GET_TXOUT_REPLY:
case WIRE_GOSSIP_ROUTING_FAILURE:
case WIRE_GOSSIP_MARK_CHANNEL_UNROUTABLE:
case WIRE_GOSSIP_OUTPOINT_SPENT:
case WIRE_GOSSIP_DEV_SUPPRESS:
case WIRE_GOSSIPD_GET_UPDATE_REPLY:
case WIRE_GOSSIPD_SEND_GOSSIP:
break;
}
status_broken("peer %s: unexpected cmd of type %s",
status_broken("peer %s: unexpected cmd of type %i %s",
type_to_string(tmpctx, struct pubkey, &peer->id),
gossip_wire_type_name(fromwire_peektype(msg)));
fromwire_peektype(msg),
gossip_peerd_wire_type_name(fromwire_peektype(msg)));
return io_close(conn);

handled_cmd:
Expand Down Expand Up @@ -2179,11 +2155,6 @@ static struct io_plan *recv_req(struct io_conn *conn,
case WIRE_GOSSIP_QUERY_CHANNEL_RANGE_REPLY:
case WIRE_GOSSIP_GET_CHANNEL_PEER_REPLY:
case WIRE_GOSSIP_GET_INCOMING_CHANNELS_REPLY:
case WIRE_GOSSIP_GET_UPDATE:
case WIRE_GOSSIP_GET_UPDATE_REPLY:
case WIRE_GOSSIP_SEND_GOSSIP:
case WIRE_GOSSIP_LOCAL_ADD_CHANNEL:
case WIRE_GOSSIP_LOCAL_CHANNEL_UPDATE:
case WIRE_GOSSIP_GET_TXOUT:
break;
}
Expand Down
5 changes: 3 additions & 2 deletions gossipd/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <common/type_to_string.h>
#include <common/wire_error.h>
#include <common/wireaddr.h>
#include <gossipd/gen_gossip_peerd_wire.h>
#include <gossipd/gen_gossip_wire.h>
#include <inttypes.h>
#include <wire/gen_peer_wire.h>
Expand Down Expand Up @@ -1712,8 +1713,8 @@ bool handle_local_add_channel(struct routing_state *rstate, const u8 *msg)
struct pubkey remote_node_id;
u64 satoshis;

if (!fromwire_gossip_local_add_channel(msg, &scid, &remote_node_id,
&satoshis)) {
if (!fromwire_gossipd_local_add_channel(msg, &scid, &remote_node_id,
&satoshis)) {
status_broken("Unable to parse local_add_channel message: %s",
tal_hex(msg, msg));
return false;
Expand Down
Loading

0 comments on commit 5c60d7f

Please sign in to comment.