Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connectd demux part 2 #4985

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
a9e6692
lightningd: start gossipd after channels are loaded.
rustyrussell Jan 24, 2022
627404a
gossipd: infrastructure to tell lightningd about local channel updates.
rustyrussell Jan 24, 2022
5a9e8f3
gossipd: feed lightningd the channel_updates as soon as we make them.
rustyrussell Jan 24, 2022
34e1038
lightningd: tell gossipd when we use the channel_update.
rustyrussell Jan 24, 2022
24fc4d4
lightningd: use our cached channel_update for errors instead of askin…
rustyrussell Jan 24, 2022
042a6ed
channeld: keep local copy of latest channel_update for errors.
rustyrussell Jan 24, 2022
cff1d7e
channeld: send channel updates and announcements via lightningd.
rustyrussell Jan 24, 2022
4ca14eb
channeld: pause before sending initial channel_update.
rustyrussell Jan 24, 2022
6a17c89
dualopend: tell lightningd about new channel as soon as it's locked in.
rustyrussell Jan 24, 2022
95b0dec
connectd: temporarily have two fds to gossipd.
rustyrussell Jan 24, 2022
5b7cccf
gossipd: add routines to send gossip messages to and from connectd.
rustyrussell Jan 24, 2022
4b48a40
connectd: divert gossip messages directly to gossipd.
rustyrussell Jan 24, 2022
4c9e67c
gossipd: send all gossip msgs directly to connectd, not peer.
rustyrussell Jan 24, 2022
bc18acf
pytest: ignore pings when doing query_gossip.
rustyrussell Jan 29, 2022
b4c45af
connectd: handle pings and pongs.
rustyrussell Jan 29, 2022
fb2d208
connectd: handle onion messages.
rustyrussell Jan 29, 2022
de82e39
connectd: do io logging properly for msgs we make.
rustyrussell Jan 29, 2022
c7ec0a3
common: make sure we hand through peer for io logging.
rustyrussell Jan 29, 2022
8ffb126
connectd: handle custom messages.
rustyrussell Jan 29, 2022
cee59bc
channeld: simply exit if hsmd vanishes.
rustyrussell Jan 29, 2022
8d6d86d
subdaemons: remove gossipd fd from per-peer daemons.
rustyrussell Jan 29, 2022
5dea211
gossipd: don't hand out fds.
rustyrussell Jan 29, 2022
cd61786
connectd: remove second gossip fd.
rustyrussell Jan 29, 2022
9b51d5b
connectd: don't received useless peer fd if we're told to send final …
rustyrussell Jan 29, 2022
18e1dea
gossip_store: handle compacted gossip_store correctly.
rustyrussell Jan 29, 2022
c77f6ed
pytest: remove flake in test_upgrade_statickey_onchaind
rustyrussell Jan 29, 2022
e2e6fb4
pytest: make test_gossip_no_empty_announcements more robust.
rustyrussell Jan 29, 2022
1c95e02
pytest: protect against bad gossip messages from mining confirms too …
rustyrussell Jan 30, 2022
63f9344
gossipd: fix longstanding logic error in gossip_generation.
rustyrussell Jan 30, 2022
dfb78eb
lightningd: clean up subds before freeing HTLCs.
rustyrussell Jan 30, 2022
93f8a11
dual_funding: don't steal inflight field in update_channel_from_infli…
rustyrussell Jan 30, 2022
333b0e8
pytest: make test_mpp_adaptive more reliable
rustyrussell Jan 31, 2022
7530018
pytest: note unfixable test_htlc_rexmit_while_closing test.
rustyrussell Jan 31, 2022
73e325c
misc: clarifications from cdecker review.
rustyrussell Feb 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions channeld/channeld.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ static void maybe_send_stfu(struct peer *peer)
}
#endif

/* Create and send channel_update to gossipd (and maybe peer) */
/* Tell gossipd to create channel_update (then it goes into
* gossip_store, then streams out to peers, or sends it directly if
* it's a private channel) */
static void send_channel_update(struct peer *peer, int disable_flag)
{
u8 *msg;
Expand All @@ -405,7 +407,7 @@ static void send_channel_update(struct peer *peer, int disable_flag)

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

msg = towire_gossipd_local_channel_update(NULL,
msg = towire_channeld_local_channel_update(NULL,
&peer->short_channel_ids[LOCAL],
disable_flag
== ROUTING_FLAGS_DISABLED,
Expand All @@ -414,7 +416,7 @@ static void send_channel_update(struct peer *peer, int disable_flag)
peer->fee_base,
peer->fee_per_satoshi,
advertized_htlc_max(peer->channel));
wire_sync_write(peer->pps->gossip_fd, take(msg));
wire_sync_write(MASTER_FD, take(msg));
}

/**
Expand All @@ -430,22 +432,15 @@ static void send_channel_update(struct peer *peer, int disable_flag)
static void make_channel_local_active(struct peer *peer)
{
u8 *msg;
const u8 *ann;
const u8 *annfeatures = get_agreed_channelfeatures(tmpctx,
peer->our_features,
peer->their_features);

ann = private_channel_announcement(tmpctx,
&peer->short_channel_ids[LOCAL],
&peer->node_ids[LOCAL],
&peer->node_ids[REMOTE],
annfeatures);

/* Tell gossipd about local channel. */
msg = towire_gossip_store_private_channel(NULL,
peer->channel->funding_sats,
ann);
wire_sync_write(peer->pps->gossip_fd, take(msg));
/* Tell lightningd to tell gossipd about local channel. */
msg = towire_channeld_local_private_channel(NULL,
peer->channel->funding_sats,
annfeatures);
wire_sync_write(MASTER_FD, take(msg));

/* Tell gossipd and the other side what parameters we expect should
* they route through us */
Expand Down Expand Up @@ -560,9 +555,9 @@ static void announce_channel(struct peer *peer)

cannounce = create_channel_announcement(tmpctx, peer);

wire_sync_write(peer->pps->gossip_fd,
take(towire_gossipd_local_channel_announcement(NULL,
cannounce)));
wire_sync_write(MASTER_FD,
take(towire_channeld_local_channel_announcement(NULL,
cannounce)));
send_channel_update(peer, 0);
}

Expand Down Expand Up @@ -3795,6 +3790,9 @@ static void req_in(struct peer *peer, const u8 *msg)
case WIRE_CHANNELD_UPGRADED:
case WIRE_CHANNELD_PING_REPLY:
case WIRE_CHANNELD_USED_CHANNEL_UPDATE:
case WIRE_CHANNELD_LOCAL_CHANNEL_UPDATE:
case WIRE_CHANNELD_LOCAL_CHANNEL_ANNOUNCEMENT:
case WIRE_CHANNELD_LOCAL_PRIVATE_CHANNEL:
break;
}

Expand Down
21 changes: 21 additions & 0 deletions channeld/channeld_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,27 @@ msgdata,channeld_channel_update,msg,u8,len
# Tell lightningd we used the latest channel_update for an error.
msgtype,channeld_used_channel_update,1102

# Channeld: tell gossipd to make this channel_update.
msgtype,channeld_local_channel_update,1013
msgdata,channeld_local_channel_update,short_channel_id,short_channel_id,
msgdata,channeld_local_channel_update,disable,bool,
msgdata,channeld_local_channel_update,cltv_expiry_delta,u16,
msgdata,channeld_local_channel_update,htlc_minimum_msat,amount_msat,
msgdata,channeld_local_channel_update,fee_base_msat,u32,
msgdata,channeld_local_channel_update,fee_proportional_millionths,u32,
msgdata,channeld_local_channel_update,htlc_maximum_msat,amount_msat,

# Channeld: tell gossipd about our channel_announcement
msgtype,channeld_local_channel_announcement,1014
msgdata,channeld_local_channel_announcement,len,u16,
msgdata,channeld_local_channel_announcement,cannounce,u8,len

# Channeld: tell gossipd about this (as-yet) unannounced channel
msgtype,channeld_local_private_channel,1015
msgdata,channeld_local_private_channel,capacity,amount_sat,
msgdata,channeld_local_private_channel,len,u16,
msgdata,channeld_local_private_channel,features,u8,len

# Ask channeld to quiesce.
msgtype,channeld_dev_quiesce,1009
msgtype,channeld_dev_quiesce_reply,1109
Expand Down
29 changes: 11 additions & 18 deletions gossipd/gossip_generation.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include <gossipd/gossip_store.h>
#include <gossipd/gossip_store_wiregen.h>
#include <gossipd/gossipd.h>
#include <gossipd/gossipd_peerd_wiregen.h>
#include <gossipd/gossipd_wiregen.h>
#include <hsmd/hsmd_wiregen.h>
#include <wire/wire_sync.h>
Expand Down Expand Up @@ -695,11 +694,10 @@ void refresh_local_channel(struct daemon *daemon,
sign_timestamp_and_apply_update(daemon, chan, direction, take(update));
}

/* channeld asks us to update the local channel. */
bool handle_local_channel_update(struct daemon *daemon,
const struct node_id *src,
const u8 *msg)
/* channeld (via lightningd) asks us to update the local channel. */
void handle_local_channel_update(struct daemon *daemon, const u8 *msg)
{
struct node_id id;
struct short_channel_id scid;
bool disable;
u16 cltv_expiry_delta;
Expand All @@ -710,37 +708,33 @@ bool handle_local_channel_update(struct daemon *daemon,
u8 *unsigned_update;
const struct half_chan *hc;

/* FIXME: We should get scid from lightningd when setting up the
* connection, so no per-peer daemon can mess with channels other than
* its own! */
if (!fromwire_gossipd_local_channel_update(msg,
&id,
&scid,
&disable,
&cltv_expiry_delta,
&htlc_minimum,
&fee_base_msat,
&fee_proportional_millionths,
&htlc_maximum)) {
status_peer_broken(src, "bad local_channel_update %s",
tal_hex(tmpctx, msg));
return false;
master_badmsg(WIRE_GOSSIPD_LOCAL_CHANNEL_UPDATE, msg);
rustyrussell marked this conversation as resolved.
Show resolved Hide resolved
}

chan = get_channel(daemon->rstate, &scid);
/* Can theoretically happen if channel just closed. */
if (!chan) {
status_peer_debug(src, "local_channel_update for unknown %s",
status_peer_debug(&id, "local_channel_update for unknown %s",
type_to_string(tmpctx, struct short_channel_id,
&scid));
return true;
return;
}

if (!local_direction(daemon->rstate, chan, &direction)) {
status_peer_broken(src, "bad local_channel_update chan %s",
status_peer_broken(&id, "bad local_channel_update chan %s",
type_to_string(tmpctx,
struct short_channel_id,
&scid));
return false;
return;
}

unsigned_update = create_unsigned_update(tmpctx, &scid, direction,
Expand All @@ -754,7 +748,7 @@ bool handle_local_channel_update(struct daemon *daemon,
/* Ignore duplicates. */
if (is_halfchan_defined(hc)
&& !cupdate_different(daemon->rstate->gs, hc, unsigned_update))
return true;
return;

/* Too early? Defer (don't worry if it's unannounced). */
if (hc && is_chan_public(chan)) {
Expand All @@ -764,13 +758,12 @@ bool handle_local_channel_update(struct daemon *daemon,
if (now < next_time) {
defer_update(daemon, next_time - now,
chan, direction, take(unsigned_update));
return true;
return;
}
}

sign_timestamp_and_apply_update(daemon, chan, direction,
take(unsigned_update));
return true;
}

/* Take update, set/unset disabled flag (and update timestamp).
Expand Down
6 changes: 2 additions & 4 deletions gossipd/gossip_generation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ void local_enable_chan(struct daemon *daemon, const struct chan *chan, int direc
void refresh_local_channel(struct daemon *daemon,
struct chan *chan, int direction);

/* channeld asks us to update the local channel. */
bool handle_local_channel_update(struct daemon *daemon,
const struct node_id *src,
const u8 *msg);
/* channeld (via lightningd) asks us to update the local channel. */
void handle_local_channel_update(struct daemon *daemon, const u8 *msg);

/* lightningd tells us it used the last channel_update we sent. */
void handle_used_local_channel_update(struct daemon *daemon, const u8 *msg);
Expand Down
15 changes: 13 additions & 2 deletions gossipd/gossip_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,25 @@ u32 gossip_store_load(struct routing_state *rstate, struct gossip_store *gs)
}

switch (fromwire_peektype(msg)) {
case WIRE_GOSSIP_STORE_PRIVATE_CHANNEL:
if (!routing_add_private_channel(rstate, NULL, msg,
case WIRE_GOSSIP_STORE_PRIVATE_CHANNEL: {
u8 *chan_ann;
struct amount_sat sat;
if (!fromwire_gossip_store_private_channel(msg, msg,
&sat,
&chan_ann)) {
bad = "Bad private_channel";
goto badmsg;
}
rustyrussell marked this conversation as resolved.
Show resolved Hide resolved

if (!routing_add_private_channel(rstate, NULL,
sat, chan_ann,
gs->len)) {
bad = "Bad add_private_channel";
goto badmsg;
}
stats[0]++;
break;
}
rustyrussell marked this conversation as resolved.
Show resolved Hide resolved
case WIRE_GOSSIP_STORE_CHANNEL_AMOUNT:
if (!fromwire_gossip_store_channel_amount(msg,
&satoshis)) {
Expand Down
95 changes: 56 additions & 39 deletions gossipd/gossipd.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <common/ecdh_hsmd.h>
#include <common/lease_rates.h>
#include <common/memleak.h>
#include <common/private_channel_announcement.h>
#include <common/pseudorand.h>
#include <common/sphinx.h>
#include <common/status.h>
Expand Down Expand Up @@ -289,31 +290,60 @@ static u8 *handle_node_announce(struct peer *peer, const u8 *msg)
return err;
}

static bool handle_local_channel_announcement(struct daemon *daemon,
struct peer *peer,
const u8 *msg)
static void handle_local_channel_announcement(struct daemon *daemon, const u8 *msg)
{
u8 *cannouncement;
const u8 *err;
struct node_id id;
struct peer *peer;

if (!fromwire_gossipd_local_channel_announcement(msg, msg,
&cannouncement)) {
status_broken("peer %s bad local_channel_announcement %s",
type_to_string(tmpctx, struct node_id, &peer->id),
tal_hex(tmpctx, msg));
return false;
}
&id,
&cannouncement))
master_badmsg(WIRE_GOSSIPD_LOCAL_CHANNEL_ANNOUNCEMENT, msg);

/* We treat it OK even if peer has disconnected since (unlikely though!) */
peer = find_peer(daemon, &id);
if (!peer)
status_broken("Unknown peer %s for local_channel_announcement",
type_to_string(tmpctx, struct node_id, &id));

err = handle_channel_announcement_msg(daemon, peer, cannouncement);
if (err) {
status_broken("peer %s invalid local_channel_announcement %s (%s)",
type_to_string(tmpctx, struct node_id, &peer->id),
type_to_string(tmpctx, struct node_id, &id),
tal_hex(tmpctx, msg),
tal_hex(tmpctx, err));
return false;
}
}

return true;

/* channeld (via lightningd) tells us about (as-yet?) unannounce channel.
* It needs us to put it in gossip_store. */
static void handle_local_private_channel(struct daemon *daemon, const u8 *msg)
{
struct node_id id;
struct amount_sat capacity;
u8 *features;
struct short_channel_id scid;
const u8 *cannounce;

if (!fromwire_gossipd_local_private_channel(msg, msg,
&id, &capacity, &scid,
&features))
master_badmsg(WIRE_GOSSIPD_LOCAL_PRIVATE_CHANNEL, msg);

cannounce = private_channel_announcement(tmpctx,
&scid,
&daemon->id,
&id,
features);

if (!routing_add_private_channel(daemon->rstate, &id, capacity,
cannounce, 0)) {
status_peer_broken(&id, "bad add_private_channel %s",
tal_hex(tmpctx, cannounce));
}
}

/* Peer sends obsolete onion msg. */
Expand Down Expand Up @@ -645,7 +675,6 @@ static struct io_plan *peer_msg_in(struct io_conn *conn,
struct peer *peer)
{
const u8 *err;
bool ok;

/* These are messages relayed from peer */
switch ((enum peer_wire)fromwire_peektype(msg)) {
Expand Down Expand Up @@ -720,40 +749,17 @@ static struct io_plan *peer_msg_in(struct io_conn *conn,
return io_close(conn);
}

/* Must be a gossipd_peerd_wire_type asking us to do something. */
switch ((enum gossipd_peerd_wire)fromwire_peektype(msg)) {
case WIRE_GOSSIPD_LOCAL_CHANNEL_UPDATE:
ok = handle_local_channel_update(peer->daemon, &peer->id, msg);
goto handled_cmd;
case WIRE_GOSSIPD_LOCAL_CHANNEL_ANNOUNCEMENT:
ok = handle_local_channel_announcement(peer->daemon, peer, msg);
goto handled_cmd;
}

if (fromwire_peektype(msg) == WIRE_GOSSIP_STORE_PRIVATE_CHANNEL) {
ok = routing_add_private_channel(peer->daemon->rstate, peer,
msg, 0);
goto handled_cmd;
}

/* Anything else should not have been sent to us: close on it */
status_peer_broken(&peer->id, "unexpected cmd of type %i %s",
fromwire_peektype(msg),
gossipd_peerd_wire_name(fromwire_peektype(msg)));
status_peer_broken(&peer->id, "unexpected cmd of type %i",
fromwire_peektype(msg));
rustyrussell marked this conversation as resolved.
Show resolved Hide resolved
return io_close(conn);

/* Commands should always be OK. */
handled_cmd:
if (!ok)
return io_close(conn);
goto done;

/* Forwarded messages may be bad, so we have error which the per-peer
* daemon will forward to the peer. */
handled_relay:
if (err)
queue_peer_msg(peer, take(err));
done:

return daemon_conn_read_next(conn, peer->dc);
}

Expand Down Expand Up @@ -1297,6 +1303,17 @@ static struct io_plan *recv_req(struct io_conn *conn,
handle_used_local_channel_update(daemon, msg);
goto done;

case WIRE_GOSSIPD_LOCAL_CHANNEL_UPDATE:
handle_local_channel_update(daemon, msg);
goto done;

case WIRE_GOSSIPD_LOCAL_CHANNEL_ANNOUNCEMENT:
handle_local_channel_announcement(daemon, msg);
goto done;

case WIRE_GOSSIPD_LOCAL_PRIVATE_CHANNEL:
handle_local_private_channel(daemon, msg);
goto done;
#if DEVELOPER
case WIRE_GOSSIPD_DEV_SET_MAX_SCIDS_ENCODE_SIZE:
dev_set_max_scids_encode_size(daemon, msg);
Expand Down
Loading