2018-06-04 06:20:25 +02:00
|
|
|
#include <ccan/asort/asort.h>
|
2017-10-23 06:12:38 +02:00
|
|
|
#include <ccan/build_assert/build_assert.h>
|
2018-02-08 02:24:46 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/container_of/container_of.h>
|
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
2018-02-23 01:00:00 +01:00
|
|
|
#include <ccan/crypto/siphash24/siphash24.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/list/list.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/noerr/noerr.h>
|
2017-03-07 02:08:20 +01:00
|
|
|
#include <ccan/take/take.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <ccan/timer/timer.h>
|
2018-04-23 16:36:16 +02:00
|
|
|
#include <common/bech32.h>
|
|
|
|
#include <common/bech32_util.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/cryptomsg.h>
|
|
|
|
#include <common/daemon_conn.h>
|
2018-06-28 03:34:47 +02:00
|
|
|
#include <common/decode_short_channel_ids.h>
|
2018-03-13 16:42:55 +01:00
|
|
|
#include <common/features.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/ping.h>
|
2018-04-25 14:39:38 +02:00
|
|
|
#include <common/pseudorand.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2018-01-08 11:01:09 +01:00
|
|
|
#include <common/subdaemon.h>
|
2017-08-28 18:04:01 +02:00
|
|
|
#include <common/timeout.h>
|
2017-08-28 18:03:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <common/wire_error.h>
|
2017-10-23 06:17:38 +02:00
|
|
|
#include <common/wireaddr.h>
|
2018-07-24 08:18:58 +02:00
|
|
|
#include <connectd/gen_connect_gossip_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/broadcast.h>
|
|
|
|
#include <gossipd/gen_gossip_wire.h>
|
2018-05-10 01:18:23 +02:00
|
|
|
#include <gossipd/gossip.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <gossipd/routing.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <hsmd/client.h>
|
2017-11-24 15:47:14 +01:00
|
|
|
#include <hsmd/gen_hsm_client_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <inttypes.h>
|
2017-03-12 13:39:23 +01:00
|
|
|
#include <lightningd/gossip_msg.h>
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
#include <netdb.h>
|
|
|
|
#include <netinet/in.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
#include <sodium/randombytes.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-05-07 06:29:21 +02:00
|
|
|
#include <sys/un.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wire/gen_peer_wire.h>
|
|
|
|
#include <wire/wire_io.h>
|
2017-11-24 15:47:14 +01:00
|
|
|
#include <wire/wire_sync.h>
|
2018-06-04 06:28:02 +02:00
|
|
|
#include <zlib.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-10-11 11:58:50 +02:00
|
|
|
#define HSM_FD 3
|
2018-07-24 08:18:58 +02:00
|
|
|
#define CONNECTD_FD 4
|
2017-10-11 11:58:50 +02:00
|
|
|
|
2018-06-04 06:28:02 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
static u32 max_scids_encode_bytes = -1U;
|
|
|
|
#endif
|
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
struct local_update {
|
|
|
|
/* daemon->local_updates */
|
|
|
|
struct list_node list;
|
|
|
|
|
|
|
|
/* Because we're handed to a one-arg timer */
|
|
|
|
struct daemon *daemon;
|
|
|
|
|
|
|
|
/* Which channel this is */
|
|
|
|
struct short_channel_id scid;
|
|
|
|
|
|
|
|
/* Which direction we own */
|
|
|
|
int direction;
|
|
|
|
|
|
|
|
u16 cltv_delta;
|
|
|
|
u64 htlc_minimum_msat;
|
|
|
|
u32 fee_base_msat, fee_proportional_millionths;
|
|
|
|
bool disable;
|
|
|
|
};
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct daemon {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* Who am I? */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2018-04-26 06:51:01 +02:00
|
|
|
/* Peers we have directly or indirectly: id is unique */
|
2017-01-10 06:08:33 +01:00
|
|
|
struct list_head peers;
|
2017-03-19 21:28:29 +01:00
|
|
|
|
|
|
|
/* Connection to main daemon. */
|
|
|
|
struct daemon_conn master;
|
2017-02-01 15:49:01 +01:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* Connection to connect daemon. */
|
|
|
|
struct daemon_conn connectd;
|
|
|
|
|
2017-02-01 15:49:01 +01:00
|
|
|
/* Routing information */
|
|
|
|
struct routing_state *rstate;
|
2017-02-04 16:28:35 +01:00
|
|
|
|
|
|
|
struct timers timers;
|
2017-04-24 14:31:26 +02:00
|
|
|
|
|
|
|
u32 broadcast_interval;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* Global features to list in node_announcement. */
|
|
|
|
u8 *globalfeatures;
|
2017-11-24 15:03:22 +01:00
|
|
|
|
|
|
|
u8 alias[33];
|
|
|
|
u8 rgb[3];
|
2018-05-07 06:29:22 +02:00
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* What we can actually announce. */
|
2018-05-07 06:29:22 +02:00
|
|
|
struct wireaddr *announcable;
|
2018-01-04 12:40:46 +01:00
|
|
|
|
|
|
|
/* To make sure our node_announcement timestamps increase */
|
|
|
|
u32 last_announce_timestamp;
|
2018-04-25 14:37:38 +02:00
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
/* Unapplied local updates waiting for their timers. */
|
|
|
|
struct list_head local_updates;
|
2017-12-06 07:15:06 +01:00
|
|
|
};
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct peer {
|
|
|
|
struct daemon *daemon;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* daemon->peers */
|
|
|
|
struct list_node list;
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* The ID of the peer (not necessarily unique, in transit!) */
|
|
|
|
struct pubkey id;
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
bool gossip_queries_feature, initial_routing_sync_feature;
|
|
|
|
|
2017-02-01 15:49:01 +01:00
|
|
|
/* High water mark for the staggered broadcast */
|
|
|
|
u64 broadcast_index;
|
2017-04-12 18:10:10 +02:00
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
/* Timestamp range to filter gossip by */
|
|
|
|
u32 gossip_timestamp_min, gossip_timestamp_max;
|
|
|
|
|
2018-06-04 06:19:25 +02:00
|
|
|
/* Are there outstanding queries on short_channel_ids? */
|
|
|
|
const struct short_channel_id *scid_queries;
|
|
|
|
size_t scid_query_idx;
|
|
|
|
|
2018-06-04 06:20:25 +02:00
|
|
|
/* Are there outstanding node_announcements from scid_queries? */
|
|
|
|
struct pubkey *scid_query_nodes;
|
|
|
|
size_t scid_query_nodes_idx;
|
|
|
|
|
2018-06-04 06:27:25 +02:00
|
|
|
/* If this is NULL, we're syncing gossip now. */
|
|
|
|
struct oneshot *gossip_timer;
|
2017-03-09 14:24:32 +01:00
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
/* How many query responses are we expecting? */
|
|
|
|
size_t num_scid_queries_outstanding;
|
|
|
|
|
2018-07-24 02:26:43 +02:00
|
|
|
/* How many pongs are we expecting? */
|
|
|
|
size_t num_pings_outstanding;
|
|
|
|
|
2018-06-04 06:28:02 +02:00
|
|
|
/* Map of outstanding channel_range requests. */
|
|
|
|
u8 *query_channel_blocks;
|
|
|
|
u32 first_channel_range;
|
|
|
|
struct short_channel_id *query_channel_scids;
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* FIXME: Doesn't need to be a pointer. */
|
2017-12-06 07:15:06 +01:00
|
|
|
struct daemon_conn *remote;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
/* FIXME: Reorder */
|
2018-07-24 08:18:58 +02:00
|
|
|
static void peer_disable_channels(struct daemon *daemon, struct node *node);
|
2017-03-11 14:45:54 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
static void destroy_peer(struct peer *peer)
|
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
struct node *node;
|
2018-04-25 14:39:38 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
list_del_from(&peer->daemon->peers, &peer->list);
|
2018-07-24 08:18:58 +02:00
|
|
|
node = get_node(peer->daemon->rstate, &peer->id);
|
|
|
|
if (node)
|
|
|
|
peer_disable_channels(peer->daemon, node);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct peer *find_peer(struct daemon *daemon, const struct pubkey *id)
|
|
|
|
{
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
list_for_each(&daemon->peers, peer, list)
|
|
|
|
if (pubkey_eq(&peer->id, id))
|
|
|
|
return peer;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
static u8 *encode_short_channel_ids_start(const tal_t *ctx)
|
|
|
|
{
|
|
|
|
u8 *encoded = tal_arr(tmpctx, u8, 0);
|
2018-06-04 06:28:02 +02:00
|
|
|
towire_u8(&encoded, SHORTIDS_ZLIB);
|
2018-06-04 06:22:25 +02:00
|
|
|
return encoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void encode_add_short_channel_id(u8 **encoded,
|
|
|
|
const struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
towire_short_channel_id(encoded, scid);
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:28:02 +02:00
|
|
|
static u8 *zencode_scids(const tal_t *ctx, const u8 *scids, size_t len)
|
|
|
|
{
|
|
|
|
u8 *z;
|
|
|
|
int err;
|
|
|
|
unsigned long compressed_len = len;
|
|
|
|
|
|
|
|
/* Prefer to fail if zlib makes it larger */
|
|
|
|
z = tal_arr(ctx, u8, len);
|
|
|
|
err = compress2(z, &compressed_len, scids, len, Z_BEST_COMPRESSION);
|
|
|
|
if (err == Z_OK) {
|
|
|
|
status_trace("short_ids compressed %zu into %lu",
|
|
|
|
len, compressed_len);
|
|
|
|
tal_resize(&z, compressed_len);
|
|
|
|
return z;
|
|
|
|
}
|
|
|
|
status_trace("short_ids compress %zu returned %i:"
|
|
|
|
" not compresssing", len, err);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
static bool encode_short_channel_ids_end(u8 **encoded, size_t max_bytes)
|
|
|
|
{
|
2018-06-04 06:28:02 +02:00
|
|
|
u8 *z;
|
|
|
|
|
|
|
|
switch ((enum scid_encode_types)(*encoded)[0]) {
|
|
|
|
case SHORTIDS_ZLIB:
|
|
|
|
z = zencode_scids(tmpctx, *encoded + 1, tal_len(*encoded) - 1);
|
|
|
|
if (z) {
|
|
|
|
tal_resize(encoded, 1 + tal_len(z));
|
|
|
|
memcpy((*encoded) + 1, z, tal_len(z));
|
|
|
|
goto check_length;
|
|
|
|
}
|
|
|
|
(*encoded)[0] = SHORTIDS_UNCOMPRESSED;
|
|
|
|
/* Fall thru */
|
|
|
|
case SHORTIDS_UNCOMPRESSED:
|
|
|
|
goto check_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Unknown short_ids encoding %u", (*encoded)[0]);
|
|
|
|
|
|
|
|
check_length:
|
2018-06-04 06:28:02 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
if (tal_len(*encoded) > max_scids_encode_bytes)
|
|
|
|
return false;
|
|
|
|
#endif
|
2018-06-04 06:22:25 +02:00
|
|
|
return tal_len(*encoded) <= max_bytes;
|
|
|
|
}
|
|
|
|
|
2018-03-08 04:24:14 +01:00
|
|
|
static void queue_peer_msg(struct peer *peer, const u8 *msg TAKES)
|
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
const u8 *send = towire_gossip_send_gossip(NULL, msg);
|
|
|
|
if (taken(msg))
|
|
|
|
tal_free(msg);
|
|
|
|
daemon_conn_send(peer->remote, take(send));
|
2018-03-08 04:24:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:27:25 +02:00
|
|
|
static void wake_gossip_out(struct peer *peer)
|
|
|
|
{
|
|
|
|
/* If we were waiting, we're not any more */
|
|
|
|
peer->gossip_timer = tal_free(peer->gossip_timer);
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* Notify the daemon_conn-write loop */
|
|
|
|
daemon_conn_wake(peer->remote);
|
2018-06-04 06:27:25 +02:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void peer_error(struct peer *peer, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
status_trace("peer %s: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
tal_vfmt(tmpctx, fmt, ap));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
/* Send error: we'll close after writing this. */
|
|
|
|
va_start(ap, fmt);
|
2018-03-08 04:24:14 +01:00
|
|
|
queue_peer_msg(peer, take(towire_errorfmtv(peer, NULL, fmt, ap)));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
static void setup_gossip_range(struct peer *peer)
|
|
|
|
{
|
|
|
|
u8 *msg;
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!peer->gossip_queries_feature)
|
2018-06-04 06:26:25 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* Tell it to start gossip! (And give us everything!) */
|
|
|
|
msg = towire_gossip_timestamp_filter(peer,
|
|
|
|
&peer->daemon->rstate->chain_hash,
|
|
|
|
0, UINT32_MAX);
|
|
|
|
queue_peer_msg(peer, take(msg));
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:44:55 +01:00
|
|
|
static bool nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2017-11-24 15:47:14 +01:00
|
|
|
/* Create a node_announcement with the given signature. It may be NULL
|
|
|
|
* in the case we need to create a provisional announcement for the
|
|
|
|
* HSM to sign. This is typically called twice: once with the dummy
|
|
|
|
* signature to get it signed and a second time to build the full
|
|
|
|
* packet with the signature. The timestamp is handed in since that is
|
|
|
|
* the only thing that may change between the dummy creation and the
|
|
|
|
* call with a signature.*/
|
|
|
|
static u8 *create_node_announcement(const tal_t *ctx, struct daemon *daemon,
|
|
|
|
secp256k1_ecdsa_signature *sig,
|
|
|
|
u32 timestamp)
|
2017-03-11 14:45:54 +01:00
|
|
|
{
|
2017-11-24 15:47:14 +01:00
|
|
|
u8 *addresses = tal_arr(ctx, u8, 0);
|
|
|
|
u8 *announcement;
|
|
|
|
size_t i;
|
|
|
|
if (!sig) {
|
|
|
|
sig = tal(ctx, secp256k1_ecdsa_signature);
|
|
|
|
memset(sig, 0, sizeof(*sig));
|
|
|
|
}
|
2018-05-07 06:29:22 +02:00
|
|
|
for (i = 0; i < tal_count(daemon->announcable); i++)
|
|
|
|
towire_wireaddr(&addresses, &daemon->announcable[i]);
|
2017-11-24 15:47:14 +01:00
|
|
|
|
|
|
|
announcement =
|
2018-07-24 08:17:40 +02:00
|
|
|
towire_node_announcement(ctx, sig, daemon->globalfeatures, timestamp,
|
2017-11-24 15:47:14 +01:00
|
|
|
&daemon->id, daemon->rgb, daemon->alias,
|
|
|
|
addresses);
|
|
|
|
return announcement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void send_node_announcement(struct daemon *daemon)
|
|
|
|
{
|
|
|
|
u32 timestamp = time_now().ts.tv_sec;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
2018-03-08 05:10:26 +01:00
|
|
|
u8 *msg, *nannounce, *err;
|
2018-01-04 12:40:46 +01:00
|
|
|
|
|
|
|
/* Timestamps must move forward, or announce will be ignored! */
|
|
|
|
if (timestamp <= daemon->last_announce_timestamp)
|
|
|
|
timestamp = daemon->last_announce_timestamp + 1;
|
|
|
|
daemon->last_announce_timestamp = timestamp;
|
|
|
|
|
|
|
|
nannounce = create_node_announcement(tmpctx, daemon, NULL, timestamp);
|
2017-11-24 15:47:14 +01:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
if (!wire_sync_write(HSM_FD, take(towire_hsm_node_announcement_sig_req(NULL, nannounce))))
|
2017-11-24 15:47:14 +01:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "Could not write to HSM: %s", strerror(errno));
|
|
|
|
|
|
|
|
msg = wire_sync_read(tmpctx, HSM_FD);
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_node_announcement_sig_reply(msg, &sig))
|
2017-11-24 15:47:14 +01:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "HSM returned an invalid node_announcement sig");
|
|
|
|
|
|
|
|
/* We got the signature for out provisional node_announcement back
|
|
|
|
* from the HSM, create the real announcement and forward it to
|
|
|
|
* gossipd so it can take care of forwarding it. */
|
2018-03-15 07:10:22 +01:00
|
|
|
nannounce = create_node_announcement(NULL, daemon, &sig, timestamp);
|
2018-03-25 18:30:47 +02:00
|
|
|
err = handle_node_announcement(daemon->rstate, take(nannounce));
|
2018-03-08 05:10:26 +01:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"rejected own node announcement: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, err));
|
2017-11-24 15:47:14 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:15:25 +02:00
|
|
|
/* Should we announce our own node? */
|
2018-06-04 06:38:39 +02:00
|
|
|
static void maybe_send_own_node_announce(struct daemon *daemon)
|
2018-06-04 06:15:25 +02:00
|
|
|
{
|
|
|
|
if (!daemon->rstate->local_channel_announced)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* FIXME: We may not need to retransmit here, if previous still valid. */
|
|
|
|
send_node_announcement(daemon);
|
|
|
|
daemon->rstate->local_channel_announced = false;
|
|
|
|
}
|
|
|
|
|
2018-05-07 12:13:16 +02:00
|
|
|
/**
|
|
|
|
* Handle an incoming gossip message
|
|
|
|
*
|
|
|
|
* Returns wire formatted error if handling failed. The error contains the
|
|
|
|
* details of the failures. The caller is expected to return the error to the
|
|
|
|
* peer, or drop the error if the message did not come from a peer.
|
|
|
|
*/
|
2018-05-18 07:49:08 +02:00
|
|
|
static u8 *handle_gossip_msg(struct daemon *daemon, const u8 *msg,
|
|
|
|
const char *source)
|
2017-11-24 15:47:14 +01:00
|
|
|
{
|
2018-03-18 14:57:15 +01:00
|
|
|
struct routing_state *rstate = daemon->rstate;
|
2017-03-11 14:45:54 +01:00
|
|
|
int t = fromwire_peektype(msg);
|
2018-03-08 05:10:26 +01:00
|
|
|
u8 *err;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2017-03-11 14:45:54 +01:00
|
|
|
switch(t) {
|
2018-01-04 12:40:58 +01:00
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT: {
|
|
|
|
const struct short_channel_id *scid;
|
|
|
|
/* If it's OK, tells us the short_channel_id to lookup */
|
2018-03-25 18:30:47 +02:00
|
|
|
err = handle_channel_announcement(rstate, msg, &scid);
|
2018-03-08 05:10:31 +01:00
|
|
|
if (err)
|
2018-03-18 14:57:15 +01:00
|
|
|
return err;
|
2018-03-08 05:10:31 +01:00
|
|
|
else if (scid)
|
2018-03-18 14:57:15 +01:00
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-08 05:10:26 +01:00
|
|
|
take(towire_gossip_get_txout(NULL,
|
2018-01-04 12:40:58 +01:00
|
|
|
scid)));
|
2017-03-11 14:45:54 +01:00
|
|
|
break;
|
2018-01-04 12:40:58 +01:00
|
|
|
}
|
2017-03-11 14:45:54 +01:00
|
|
|
|
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
2018-03-25 18:30:47 +02:00
|
|
|
err = handle_node_announcement(rstate, msg);
|
2018-03-08 05:10:26 +01:00
|
|
|
if (err)
|
2018-03-18 14:57:15 +01:00
|
|
|
return err;
|
2017-03-11 14:45:54 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WIRE_CHANNEL_UPDATE:
|
2018-05-18 07:49:08 +02:00
|
|
|
err = handle_channel_update(rstate, msg, source);
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err)
|
2018-03-18 14:57:15 +01:00
|
|
|
return err;
|
2018-06-04 06:15:25 +02:00
|
|
|
/* In case we just announced a new local channel. */
|
2018-06-04 06:38:39 +02:00
|
|
|
maybe_send_own_node_announce(daemon);
|
2017-03-11 14:45:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-03-18 14:57:15 +01:00
|
|
|
|
|
|
|
/* All good, no error to report */
|
|
|
|
return NULL;
|
2017-03-11 14:45:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:21:25 +02:00
|
|
|
static void handle_query_short_channel_ids(struct peer *peer, u8 *msg)
|
|
|
|
{
|
2018-06-04 06:38:42 +02:00
|
|
|
struct routing_state *rstate = peer->daemon->rstate;
|
2018-06-04 06:21:25 +02:00
|
|
|
struct bitcoin_blkid chain;
|
|
|
|
u8 *encoded;
|
|
|
|
struct short_channel_id *scids;
|
|
|
|
|
|
|
|
if (!fromwire_query_short_channel_ids(tmpctx, msg, &chain, &encoded)) {
|
|
|
|
peer_error(peer, "Bad query_short_channel_ids %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&rstate->chain_hash, &chain)) {
|
2018-06-04 06:21:25 +02:00
|
|
|
status_trace("%s sent query_short_channel_ids chainhash %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid, &chain));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if it has not sent `reply_short_channel_ids_end` to a
|
|
|
|
* previously received `query_short_channel_ids` from this
|
|
|
|
* sender:
|
|
|
|
* - MAY fail the connection.
|
|
|
|
*/
|
|
|
|
if (peer->scid_queries || peer->scid_query_nodes) {
|
2018-06-04 06:38:42 +02:00
|
|
|
peer_error(peer, "Bad concurrent query_short_channel_ids");
|
2018-06-04 06:21:25 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scids = decode_short_ids(tmpctx, encoded);
|
|
|
|
if (!scids) {
|
|
|
|
peer_error(peer, "Bad query_short_channel_ids encoding %s",
|
|
|
|
tal_hex(tmpctx, encoded));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - MUST respond to each known `short_channel_id` with a
|
|
|
|
* `channel_announcement` and the latest `channel_update`s for each end
|
|
|
|
* - SHOULD NOT wait for the next outgoing gossip flush to send
|
2018-06-04 06:21:25 +02:00
|
|
|
* these.
|
|
|
|
*/
|
|
|
|
peer->scid_queries = tal_steal(peer, scids);
|
|
|
|
peer->scid_query_idx = 0;
|
|
|
|
peer->scid_query_nodes = tal_arr(peer, struct pubkey, 0);
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* Notify the daemon_conn-write loop */
|
|
|
|
daemon_conn_wake(peer->remote);
|
2018-06-04 06:21:25 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
static void handle_gossip_timestamp_filter(struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
u32 first_timestamp, timestamp_range;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_timestamp_filter(msg, &chain_hash,
|
|
|
|
&first_timestamp,
|
|
|
|
×tamp_range)) {
|
|
|
|
peer_error(peer, "Bad gossip_timestamp_filter %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain_hash)) {
|
2018-06-04 06:26:25 +02:00
|
|
|
status_trace("%s sent gossip_timestamp_filter chainhash %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid,
|
|
|
|
&chain_hash));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* First time, start gossip sync immediately. */
|
|
|
|
if (peer->gossip_timestamp_min > peer->gossip_timestamp_max)
|
2018-06-04 06:27:25 +02:00
|
|
|
wake_gossip_out(peer);
|
2018-06-04 06:26:25 +02:00
|
|
|
|
|
|
|
/* FIXME: We don't index by timestamp, so this forces a brute
|
|
|
|
* search! */
|
|
|
|
peer->gossip_timestamp_min = first_timestamp;
|
|
|
|
peer->gossip_timestamp_max = first_timestamp + timestamp_range - 1;
|
|
|
|
if (peer->gossip_timestamp_max < peer->gossip_timestamp_min)
|
|
|
|
peer->gossip_timestamp_max = UINT32_MAX;
|
|
|
|
peer->broadcast_index = 0;
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:28:02 +02:00
|
|
|
static void reply_channel_range(struct peer *peer,
|
|
|
|
u32 first_blocknum, u32 number_of_blocks,
|
|
|
|
const u8 *encoded)
|
|
|
|
{
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - For each `reply_channel_range`:
|
|
|
|
* - MUST set with `chain_hash` equal to that of `query_channel_range`,
|
|
|
|
* - MUST encode a `short_channel_id` for every open channel it
|
|
|
|
* knows in blocks `first_blocknum` to `first_blocknum` plus
|
|
|
|
* `number_of_blocks` minus one.
|
|
|
|
* - MUST limit `number_of_blocks` to the maximum number of blocks
|
|
|
|
* whose results could fit in `encoded_short_ids`
|
|
|
|
* - if does not maintain up-to-date channel information for
|
|
|
|
* `chain_hash`:
|
|
|
|
* - MUST set `complete` to 0.
|
|
|
|
* - otherwise:
|
|
|
|
* - SHOULD set `complete` to 1.
|
|
|
|
*/
|
|
|
|
u8 *msg = towire_reply_channel_range(NULL,
|
|
|
|
&peer->daemon->rstate->chain_hash,
|
|
|
|
first_blocknum,
|
|
|
|
number_of_blocks,
|
|
|
|
1, encoded);
|
|
|
|
queue_peer_msg(peer, take(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void queue_channel_ranges(struct peer *peer,
|
|
|
|
u32 first_blocknum, u32 number_of_blocks)
|
|
|
|
{
|
|
|
|
struct routing_state *rstate = peer->daemon->rstate;
|
|
|
|
u8 *encoded = encode_short_channel_ids_start(tmpctx);
|
|
|
|
struct short_channel_id scid;
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* 1. type: 264 (`reply_channel_range`) (`gossip_queries`)
|
|
|
|
* 2. data:
|
|
|
|
* * [`32`:`chain_hash`]
|
|
|
|
* * [`4`:`first_blocknum`]
|
|
|
|
* * [`4`:`number_of_blocks`]
|
|
|
|
* * [`1`:`complete`]
|
|
|
|
* * [`2`:`len`]
|
|
|
|
* * [`len`:`encoded_short_ids`]
|
|
|
|
*/
|
|
|
|
const size_t reply_overhead = 32 + 4 + 4 + 1 + 2;
|
|
|
|
const size_t max_encoded_bytes = 65535 - 2 - reply_overhead;
|
|
|
|
|
|
|
|
/* Avoid underflow: we don't use block 0 anyway */
|
|
|
|
if (first_blocknum == 0)
|
|
|
|
mk_short_channel_id(&scid, 1, 0, 0);
|
|
|
|
else
|
|
|
|
mk_short_channel_id(&scid, first_blocknum, 0, 0);
|
|
|
|
scid.u64--;
|
|
|
|
|
|
|
|
while (uintmap_after(&rstate->chanmap, &scid.u64)) {
|
|
|
|
u32 blocknum = short_channel_id_blocknum(&scid);
|
|
|
|
if (blocknum >= first_blocknum + number_of_blocks)
|
|
|
|
break;
|
|
|
|
|
|
|
|
encode_add_short_channel_id(&encoded, &scid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encode_short_channel_ids_end(&encoded, max_encoded_bytes)) {
|
|
|
|
reply_channel_range(peer, first_blocknum, number_of_blocks,
|
|
|
|
encoded);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It wouldn't all fit: divide in half */
|
|
|
|
/* We assume we can always send one block! */
|
|
|
|
if (number_of_blocks <= 1) {
|
|
|
|
/* We always assume we can send 1 blocks worth */
|
|
|
|
status_broken("Could not fit scids for single block %u",
|
|
|
|
first_blocknum);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
status_debug("queue_channel_ranges full: splitting %u+%u and %u+%u",
|
|
|
|
first_blocknum,
|
|
|
|
number_of_blocks / 2,
|
|
|
|
first_blocknum + number_of_blocks / 2,
|
|
|
|
number_of_blocks - number_of_blocks / 2);
|
|
|
|
queue_channel_ranges(peer, first_blocknum, number_of_blocks / 2);
|
|
|
|
queue_channel_ranges(peer, first_blocknum + number_of_blocks / 2,
|
|
|
|
number_of_blocks - number_of_blocks / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handle_query_channel_range(struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
u32 first_blocknum, number_of_blocks;
|
|
|
|
|
|
|
|
if (!fromwire_query_channel_range(msg, &chain_hash,
|
|
|
|
&first_blocknum, &number_of_blocks)) {
|
|
|
|
peer_error(peer, "Bad query_channel_range %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain_hash)) {
|
2018-06-04 06:28:02 +02:00
|
|
|
status_trace("%s sent query_channel_range chainhash %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct bitcoin_blkid,
|
|
|
|
&chain_hash));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_blocknum + number_of_blocks < first_blocknum) {
|
|
|
|
peer_error(peer, "query_channel_range overflow %u+%u",
|
|
|
|
first_blocknum, number_of_blocks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
queue_channel_ranges(peer, first_blocknum, number_of_blocks);
|
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void handle_ping(struct peer *peer, u8 *ping)
|
2017-04-12 18:10:10 +02:00
|
|
|
{
|
|
|
|
u8 *pong;
|
|
|
|
|
2018-07-24 02:26:43 +02:00
|
|
|
if (!check_ping_make_pong(NULL, ping, &pong)) {
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer_error(peer, "Bad ping");
|
|
|
|
return;
|
2017-04-12 18:10:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pong)
|
2018-07-24 02:26:43 +02:00
|
|
|
queue_peer_msg(peer, take(pong));
|
2017-04-12 18:10:10 +02:00
|
|
|
}
|
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
static void handle_pong(struct peer *peer, const u8 *pong)
|
2017-04-12 20:20:48 +02:00
|
|
|
{
|
2018-07-24 02:26:43 +02:00
|
|
|
const char *err = got_pong(pong, &peer->num_pings_outstanding);
|
2017-04-12 20:20:48 +02:00
|
|
|
|
2018-02-22 11:01:52 +01:00
|
|
|
if (err) {
|
|
|
|
peer_error(peer, "%s", err);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
return;
|
2017-04-12 20:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_send(&peer->daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_ping_reply(NULL, true,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
tal_len(pong))));
|
2017-04-12 20:20:48 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
static void handle_reply_short_channel_ids_end(struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
struct bitcoin_blkid chain;
|
|
|
|
u8 complete;
|
|
|
|
|
|
|
|
if (!fromwire_reply_short_channel_ids_end(msg, &chain, &complete)) {
|
|
|
|
peer_error(peer, "Bad reply_short_channel_ids_end %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain)) {
|
2018-06-04 06:22:25 +02:00
|
|
|
peer_error(peer, "reply_short_channel_ids_end for bad chain: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer->num_scid_queries_outstanding == 0) {
|
|
|
|
peer_error(peer, "unexpected reply_short_channel_ids_end: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->num_scid_queries_outstanding--;
|
|
|
|
msg = towire_gossip_scids_reply(msg, true, complete);
|
|
|
|
daemon_conn_send(&peer->daemon->master, take(msg));
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:28:02 +02:00
|
|
|
static void handle_reply_channel_range(struct peer *peer, u8 *msg)
|
|
|
|
{
|
|
|
|
struct bitcoin_blkid chain;
|
|
|
|
u8 complete;
|
|
|
|
u32 first_blocknum, number_of_blocks;
|
|
|
|
u8 *encoded, *p;
|
|
|
|
struct short_channel_id *scids;
|
|
|
|
size_t n;
|
|
|
|
|
|
|
|
if (!fromwire_reply_channel_range(tmpctx, msg, &chain, &first_blocknum,
|
|
|
|
&number_of_blocks, &complete,
|
|
|
|
&encoded)) {
|
|
|
|
peer_error(peer, "Bad reply_channel_range %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:30:02 +02:00
|
|
|
if (!bitcoin_blkid_eq(&peer->daemon->rstate->chain_hash, &chain)) {
|
2018-06-04 06:28:02 +02:00
|
|
|
peer_error(peer, "reply_channel_range for bad chain: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!peer->query_channel_blocks) {
|
|
|
|
peer_error(peer, "reply_channel_range without query: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (first_blocknum + number_of_blocks < first_blocknum) {
|
|
|
|
peer_error(peer, "reply_channel_range invalid %u+%u",
|
|
|
|
first_blocknum, number_of_blocks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
scids = decode_short_ids(tmpctx, encoded);
|
|
|
|
if (!scids) {
|
|
|
|
peer_error(peer, "Bad reply_channel_range encoding %s",
|
|
|
|
tal_hex(tmpctx, encoded));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = first_blocknum - peer->first_channel_range;
|
|
|
|
if (first_blocknum < peer->first_channel_range
|
|
|
|
|| n + number_of_blocks > tal_count(peer->query_channel_blocks)) {
|
|
|
|
peer_error(peer, "reply_channel_range invalid %u+%u for query %u+%u",
|
|
|
|
first_blocknum, number_of_blocks,
|
|
|
|
peer->first_channel_range,
|
|
|
|
tal_count(peer->query_channel_blocks));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = memchr(peer->query_channel_blocks + n, 1, number_of_blocks);
|
|
|
|
if (p) {
|
|
|
|
peer_error(peer, "reply_channel_range %u+%u already have block %zu",
|
|
|
|
first_blocknum, number_of_blocks,
|
|
|
|
peer->first_channel_range + (p - peer->query_channel_blocks));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark these blocks received */
|
|
|
|
memset(peer->query_channel_blocks + n, 1, number_of_blocks);
|
|
|
|
|
|
|
|
/* Add scids */
|
|
|
|
n = tal_count(peer->query_channel_scids);
|
|
|
|
tal_resize(&peer->query_channel_scids, n + tal_count(scids));
|
|
|
|
memcpy(peer->query_channel_scids + n, scids, tal_len(scids));
|
|
|
|
|
|
|
|
status_debug("peer %s reply_channel_range %u+%u (of %u+%zu) %zu scids",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
first_blocknum, number_of_blocks,
|
|
|
|
peer->first_channel_range,
|
|
|
|
tal_count(peer->query_channel_blocks),
|
|
|
|
tal_count(scids));
|
|
|
|
|
|
|
|
/* Still more to go? */
|
|
|
|
if (memchr(peer->query_channel_blocks, 0,
|
|
|
|
tal_count(peer->query_channel_blocks)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* All done, send reply */
|
|
|
|
msg = towire_gossip_query_channel_range_reply(NULL,
|
|
|
|
first_blocknum,
|
|
|
|
number_of_blocks,
|
|
|
|
complete,
|
|
|
|
peer->query_channel_scids);
|
|
|
|
daemon_conn_send(&peer->daemon->master, take(msg));
|
|
|
|
peer->query_channel_scids = tal_free(peer->query_channel_scids);
|
|
|
|
peer->query_channel_blocks = tal_free(peer->query_channel_blocks);
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* We keep a simple array of node ids while we're sending channel info */
|
|
|
|
static void append_query_node(struct peer *peer, const struct pubkey *id)
|
2017-10-25 11:18:05 +02:00
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
size_t n;
|
|
|
|
n = tal_count(peer->scid_query_nodes);
|
|
|
|
tal_resize(&peer->scid_query_nodes, n+1);
|
|
|
|
peer->scid_query_nodes[n] = *id;
|
2017-10-25 11:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
/* Arbitrary ordering function of pubkeys.
|
|
|
|
*
|
|
|
|
* Note that we could use memcmp() here: even if they had somehow different
|
|
|
|
* bitwise representations for the same key, we copied them all from struct
|
|
|
|
* node which should make them unique. Even if not (say, a node vanished
|
|
|
|
* and reappeared) we'd just end up sending two node_announcement for the
|
|
|
|
* same node.
|
|
|
|
*/
|
|
|
|
static int pubkey_order(const struct pubkey *k1, const struct pubkey *k2,
|
|
|
|
void *unused UNUSED)
|
2017-10-25 11:18:05 +02:00
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
return pubkey_cmp(k1, k2);
|
2017-10-25 11:18:05 +02:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
static void uniquify_node_ids(struct pubkey **ids)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2018-07-24 08:18:58 +02:00
|
|
|
size_t dst, src;
|
2018-06-04 06:20:25 +02:00
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - MUST follow with any `node_announcement`s for each
|
|
|
|
* `channel_announcement`
|
|
|
|
*
|
|
|
|
* - SHOULD avoid sending duplicate `node_announcements` in
|
|
|
|
* response to a single `query_short_channel_ids`.
|
|
|
|
*/
|
|
|
|
asort(*ids, tal_count(*ids), pubkey_order, NULL);
|
|
|
|
|
|
|
|
for (dst = 0, src = 0; src < tal_count(*ids); src++) {
|
|
|
|
if (dst && pubkey_eq(&(*ids)[dst-1], &(*ids)[src]))
|
|
|
|
continue;
|
|
|
|
(*ids)[dst++] = (*ids)[src];
|
|
|
|
}
|
|
|
|
tal_resize(ids, dst);
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:19:25 +02:00
|
|
|
static bool create_next_scid_reply(struct peer *peer)
|
|
|
|
{
|
|
|
|
struct routing_state *rstate = peer->daemon->rstate;
|
|
|
|
size_t i, num;
|
|
|
|
bool sent = false;
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* - MUST respond to each known `short_channel_id` with a
|
|
|
|
* `channel_announcement` and the latest `channel_update`s for
|
2018-06-04 06:19:25 +02:00
|
|
|
* each end
|
2018-06-17 12:13:44 +02:00
|
|
|
* - SHOULD NOT wait for the next outgoing gossip flush
|
|
|
|
* to send these.
|
2018-06-04 06:19:25 +02:00
|
|
|
*/
|
|
|
|
num = tal_count(peer->scid_queries);
|
|
|
|
for (i = peer->scid_query_idx; !sent && i < num; i++) {
|
|
|
|
struct chan *chan;
|
|
|
|
|
|
|
|
chan = get_channel(rstate, &peer->scid_queries[i]);
|
2018-06-04 06:22:25 +02:00
|
|
|
if (!chan || !is_chan_public(chan))
|
2018-06-04 06:19:25 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
queue_peer_msg(peer, chan->channel_announce);
|
|
|
|
if (chan->half[0].channel_update)
|
|
|
|
queue_peer_msg(peer, chan->half[0].channel_update);
|
|
|
|
if (chan->half[1].channel_update)
|
2018-06-15 08:37:35 +02:00
|
|
|
queue_peer_msg(peer, chan->half[1].channel_update);
|
2018-06-04 06:20:25 +02:00
|
|
|
|
|
|
|
/* Record node ids for later transmission of node_announcement */
|
|
|
|
append_query_node(peer, &chan->nodes[0]->id);
|
|
|
|
append_query_node(peer, &chan->nodes[1]->id);
|
2018-06-04 06:19:25 +02:00
|
|
|
sent = true;
|
|
|
|
}
|
2018-06-04 06:20:25 +02:00
|
|
|
|
|
|
|
/* Just finished channels? Remove duplicate nodes. */
|
|
|
|
if (peer->scid_query_idx != num && i == num)
|
|
|
|
uniquify_node_ids(&peer->scid_query_nodes);
|
2018-06-04 06:19:25 +02:00
|
|
|
peer->scid_query_idx = i;
|
|
|
|
|
2018-06-04 06:20:25 +02:00
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - MUST follow with any `node_announcement`s for each
|
|
|
|
* `channel_announcement`
|
|
|
|
* - SHOULD avoid sending duplicate `node_announcements` in response
|
|
|
|
* to a single `query_short_channel_ids`.
|
|
|
|
*/
|
|
|
|
num = tal_count(peer->scid_query_nodes);
|
|
|
|
for (i = peer->scid_query_nodes_idx; !sent && i < num; i++) {
|
|
|
|
const struct node *n;
|
|
|
|
|
|
|
|
n = get_node(rstate, &peer->scid_query_nodes[i]);
|
2018-06-08 08:31:49 +02:00
|
|
|
if (!n || !n->node_announcement || !n->node_announcement_index)
|
2018-06-04 06:20:25 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
queue_peer_msg(peer, n->node_announcement);
|
|
|
|
sent = true;
|
|
|
|
}
|
|
|
|
peer->scid_query_nodes_idx = i;
|
|
|
|
|
2018-06-04 06:19:25 +02:00
|
|
|
/* All finished? */
|
2018-06-04 06:20:25 +02:00
|
|
|
if (peer->scid_queries && peer->scid_query_nodes_idx == num) {
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - MUST follow these responses with
|
|
|
|
* `reply_short_channel_ids_end`.
|
|
|
|
* - if does not maintain up-to-date channel information for
|
|
|
|
* `chain_hash`:
|
|
|
|
* - MUST set `complete` to 0.
|
|
|
|
* - otherwise:
|
|
|
|
* - SHOULD set `complete` to 1.
|
|
|
|
*/
|
2018-06-04 06:19:25 +02:00
|
|
|
u8 *end = towire_reply_short_channel_ids_end(peer,
|
|
|
|
&rstate->chain_hash,
|
|
|
|
true);
|
|
|
|
queue_peer_msg(peer, take(end));
|
|
|
|
peer->scid_queries = tal_free(peer->scid_queries);
|
2018-06-04 06:20:25 +02:00
|
|
|
peer->scid_query_idx = 0;
|
|
|
|
peer->scid_query_nodes = tal_free(peer->scid_query_nodes);
|
|
|
|
peer->scid_query_nodes_idx = 0;
|
2018-06-04 06:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
/* If we're supposed to be sending gossip, do so now. */
|
|
|
|
static bool maybe_queue_gossip(struct peer *peer)
|
|
|
|
{
|
|
|
|
const u8 *next;
|
|
|
|
|
2018-06-04 06:27:25 +02:00
|
|
|
if (peer->gossip_timer)
|
2018-06-04 06:26:25 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
next = next_broadcast(peer->daemon->rstate->broadcasts,
|
|
|
|
peer->gossip_timestamp_min,
|
|
|
|
peer->gossip_timestamp_max,
|
|
|
|
&peer->broadcast_index);
|
|
|
|
|
|
|
|
if (next) {
|
|
|
|
queue_peer_msg(peer, next);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Gossip is drained. Wait for next timer. */
|
2018-06-04 06:27:25 +02:00
|
|
|
peer->gossip_timer
|
|
|
|
= new_reltimer(&peer->daemon->timers, peer,
|
|
|
|
time_from_msec(peer->daemon->broadcast_interval),
|
|
|
|
wake_gossip_out, peer);
|
2018-06-04 06:26:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-28 23:52:32 +01:00
|
|
|
static void handle_get_update(struct peer *peer, const u8 *msg)
|
|
|
|
{
|
2018-03-02 09:59:16 +01:00
|
|
|
struct short_channel_id scid;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2017-11-28 23:52:32 +01:00
|
|
|
const u8 *update;
|
2018-03-13 16:40:55 +01:00
|
|
|
struct routing_state *rstate = peer->daemon->rstate;
|
2017-11-28 23:52:32 +01:00
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
if (!fromwire_gossip_get_update(msg, &scid)) {
|
2017-11-28 23:52:32 +01:00
|
|
|
status_trace("peer %s sent bad gossip_get_update %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
tal_hex(tmpctx, msg));
|
2017-11-28 23:52:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-13 16:40:55 +01:00
|
|
|
chan = get_channel(rstate, &scid);
|
2018-03-02 09:59:16 +01:00
|
|
|
if (!chan) {
|
|
|
|
status_unusual("peer %s scid %s: unknown channel",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-03-02 09:59:16 +01:00
|
|
|
&scid));
|
2017-11-28 23:52:32 +01:00
|
|
|
update = NULL;
|
2018-03-02 09:59:16 +01:00
|
|
|
} else {
|
2018-05-10 14:22:37 +02:00
|
|
|
/* We want the update that comes from our end. */
|
2018-03-02 09:59:16 +01:00
|
|
|
if (pubkey_eq(&chan->nodes[0]->id, &peer->daemon->id))
|
2018-05-10 14:22:37 +02:00
|
|
|
update = chan->half[0].channel_update;
|
2018-03-02 09:59:16 +01:00
|
|
|
else if (pubkey_eq(&chan->nodes[1]->id, &peer->daemon->id))
|
2018-05-10 14:22:37 +02:00
|
|
|
update = chan->half[1].channel_update;
|
2018-03-02 09:59:16 +01:00
|
|
|
else {
|
|
|
|
status_unusual("peer %s scid %s: not our channel?",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey,
|
2018-03-02 09:59:16 +01:00
|
|
|
&peer->id),
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx,
|
2018-03-02 09:59:16 +01:00
|
|
|
struct short_channel_id,
|
|
|
|
&scid));
|
2018-03-02 09:59:17 +01:00
|
|
|
update = NULL;
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
2017-11-28 23:52:32 +01:00
|
|
|
}
|
2018-03-02 09:59:16 +01:00
|
|
|
status_trace("peer %s schanid %s: %s update",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid),
|
2018-03-02 09:59:16 +01:00
|
|
|
update ? "got" : "no");
|
2017-11-28 23:52:32 +01:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
msg = towire_gossip_get_update_reply(NULL, update);
|
2017-12-06 07:15:06 +01:00
|
|
|
daemon_conn_send(peer->remote, take(msg));
|
2017-11-28 23:52:32 +01:00
|
|
|
}
|
|
|
|
|
2018-05-21 06:35:40 +02:00
|
|
|
static u8 *create_channel_update(const tal_t *ctx,
|
|
|
|
struct routing_state *rstate,
|
|
|
|
const struct chan *chan,
|
|
|
|
int direction,
|
|
|
|
bool disable,
|
|
|
|
u16 cltv_expiry_delta,
|
|
|
|
u64 htlc_minimum_msat,
|
|
|
|
u32 fee_base_msat,
|
|
|
|
u32 fee_proportional_millionths)
|
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature dummy_sig;
|
|
|
|
u8 *update, *msg;
|
|
|
|
u32 timestamp = time_now().ts.tv_sec;
|
|
|
|
u16 flags;
|
|
|
|
|
|
|
|
/* So valgrind doesn't complain */
|
|
|
|
memset(&dummy_sig, 0, sizeof(dummy_sig));
|
|
|
|
|
|
|
|
/* Don't send duplicate timestamps. */
|
|
|
|
if (is_halfchan_defined(&chan->half[direction])
|
|
|
|
&& timestamp == chan->half[direction].last_timestamp)
|
|
|
|
timestamp++;
|
|
|
|
|
|
|
|
flags = direction;
|
|
|
|
if (disable)
|
|
|
|
flags |= ROUTING_FLAGS_DISABLED;
|
|
|
|
|
|
|
|
update = towire_channel_update(tmpctx, &dummy_sig,
|
|
|
|
&rstate->chain_hash,
|
|
|
|
&chan->scid,
|
|
|
|
timestamp,
|
|
|
|
flags, cltv_expiry_delta,
|
|
|
|
htlc_minimum_msat,
|
|
|
|
fee_base_msat,
|
|
|
|
fee_proportional_millionths);
|
|
|
|
|
|
|
|
if (!wire_sync_write(HSM_FD,
|
|
|
|
towire_hsm_cupdate_sig_req(tmpctx, update))) {
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO, "Writing cupdate_sig_req: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = wire_sync_read(tmpctx, HSM_FD);
|
|
|
|
if (!msg || !fromwire_hsm_cupdate_sig_reply(ctx, msg, &update)) {
|
|
|
|
status_failed(STATUS_FAIL_HSM_IO,
|
|
|
|
"Reading cupdate_sig_req: %s",
|
|
|
|
strerror(errno));
|
|
|
|
}
|
|
|
|
|
|
|
|
return update;
|
|
|
|
}
|
|
|
|
|
2018-07-02 22:54:12 +02:00
|
|
|
/* Return true if the only change would be the timestamp. */
|
|
|
|
static bool update_redundant(const struct half_chan *hc,
|
|
|
|
bool disable, u16 cltv_delta, u64 htlc_minimum_msat,
|
|
|
|
u32 fee_base_msat, u32 fee_proportional_millionths)
|
|
|
|
{
|
|
|
|
if (!is_halfchan_defined(hc))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return !(hc->flags & ROUTING_FLAGS_DISABLED) == !disable
|
|
|
|
&& hc->delay == cltv_delta
|
|
|
|
&& hc->htlc_minimum_msat == htlc_minimum_msat
|
|
|
|
&& hc->base_fee == fee_base_msat
|
|
|
|
&& hc->proportional_fee == fee_proportional_millionths;
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
static struct local_update *find_local_update(struct daemon *daemon,
|
|
|
|
const struct short_channel_id *scid)
|
2018-05-21 06:35:40 +02:00
|
|
|
{
|
2018-07-03 13:30:36 +02:00
|
|
|
struct local_update *i;
|
2018-05-21 06:35:40 +02:00
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
list_for_each(&daemon->local_updates, i, list) {
|
2018-07-08 15:52:23 +02:00
|
|
|
if (short_channel_id_eq(scid, &i->scid))
|
2018-07-03 13:30:36 +02:00
|
|
|
return i;
|
2018-05-21 06:35:40 +02:00
|
|
|
}
|
2018-07-03 13:30:36 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Frees local_update */
|
|
|
|
static void apply_delayed_local_update(struct local_update *local_update)
|
|
|
|
{
|
|
|
|
struct chan *chan;
|
|
|
|
const struct half_chan *hc;
|
|
|
|
u8 *cupdate, *err;
|
2018-05-21 06:35:40 +02:00
|
|
|
|
|
|
|
/* Can theoretically happen if channel just closed. */
|
2018-07-03 13:30:36 +02:00
|
|
|
chan = get_channel(local_update->daemon->rstate, &local_update->scid);
|
2018-05-21 06:35:40 +02:00
|
|
|
if (!chan) {
|
2018-07-03 13:30:36 +02:00
|
|
|
status_trace("Delayed local_channel_update for unknown %s",
|
2018-05-21 06:35:40 +02:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-07-03 13:30:36 +02:00
|
|
|
&local_update->scid));
|
|
|
|
tal_free(local_update);
|
2018-05-21 06:35:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
/* Convenience variable */
|
|
|
|
hc = &chan->half[local_update->direction];
|
2018-05-21 06:35:40 +02:00
|
|
|
|
2018-07-02 22:54:12 +02:00
|
|
|
/* Avoid redundant updates on public channels: on non-public channels
|
|
|
|
* we'd need to consider pending updates, so don't bother. */
|
|
|
|
if (is_chan_public(chan)
|
2018-07-03 13:30:36 +02:00
|
|
|
&& update_redundant(hc,
|
|
|
|
local_update->disable,
|
|
|
|
local_update->cltv_delta,
|
|
|
|
local_update->htlc_minimum_msat,
|
|
|
|
local_update->fee_base_msat,
|
|
|
|
local_update->fee_proportional_millionths)) {
|
2018-07-02 22:54:12 +02:00
|
|
|
status_trace("Suppressing redundant channel update for %s:(%u) %s %"PRIu64"/%u vs %u/%u",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
2018-07-03 13:30:36 +02:00
|
|
|
&local_update->scid),
|
|
|
|
local_update->direction,
|
|
|
|
is_halfchan_defined(hc)
|
|
|
|
? (hc->flags & ROUTING_FLAGS_DISABLED ? "DISABLED" : "ACTIVE")
|
2018-07-02 22:54:12 +02:00
|
|
|
: "UNDEFINED",
|
2018-07-03 13:30:36 +02:00
|
|
|
hc->last_timestamp,
|
2018-07-02 22:54:12 +02:00
|
|
|
(u32)time_now().ts.tv_sec,
|
2018-07-03 13:30:36 +02:00
|
|
|
hc->flags,
|
|
|
|
local_update->disable);
|
|
|
|
tal_free(local_update);
|
2018-07-02 22:54:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
cupdate = create_channel_update(tmpctx, local_update->daemon->rstate,
|
|
|
|
chan, local_update->direction,
|
|
|
|
local_update->disable,
|
|
|
|
local_update->cltv_delta,
|
|
|
|
local_update->htlc_minimum_msat,
|
|
|
|
local_update->fee_base_msat,
|
|
|
|
local_update->fee_proportional_millionths);
|
2018-05-21 06:35:40 +02:00
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
err = handle_channel_update(local_update->daemon->rstate, cupdate,
|
|
|
|
"apply_delayed_local_update");
|
2018-05-21 06:35:40 +02:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Rejected local channel update %s: %s",
|
|
|
|
tal_hex(tmpctx, cupdate),
|
|
|
|
tal_hex(tmpctx, err));
|
|
|
|
|
|
|
|
/* We always tell peer, even if it's not public yet */
|
2018-07-03 13:30:36 +02:00
|
|
|
if (!is_chan_public(chan)) {
|
|
|
|
struct peer *peer = find_peer(local_update->daemon,
|
|
|
|
&chan->nodes[!local_update
|
|
|
|
->direction]->id);
|
|
|
|
if (peer)
|
|
|
|
queue_peer_msg(peer, take(cupdate));
|
|
|
|
}
|
|
|
|
|
|
|
|
status_trace("Channel update for %s(%u)%s",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&local_update->scid),
|
|
|
|
local_update->direction,
|
|
|
|
is_chan_public(chan) ? "" : " (private)");
|
2018-06-04 06:15:25 +02:00
|
|
|
|
|
|
|
/* That channel_update might trigger our first channel_announcement */
|
2018-07-03 13:30:36 +02:00
|
|
|
maybe_send_own_node_announce(local_update->daemon);
|
|
|
|
tal_free(local_update);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_local_update(struct local_update *local_update)
|
|
|
|
{
|
|
|
|
list_del_from(&local_update->daemon->local_updates,
|
|
|
|
&local_update->list);
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
static void queue_local_update(struct daemon *daemon,
|
|
|
|
struct local_update *local_update)
|
|
|
|
{
|
|
|
|
/* Free any old unapplied update. */
|
|
|
|
tal_free(find_local_update(daemon, &local_update->scid));
|
|
|
|
|
|
|
|
list_add_tail(&daemon->local_updates, &local_update->list);
|
|
|
|
tal_add_destructor(local_update, destroy_local_update);
|
|
|
|
|
|
|
|
/* Delay 1/4 a broadcast interval */
|
|
|
|
new_reltimer(&daemon->timers, local_update,
|
|
|
|
time_from_msec(daemon->broadcast_interval/4),
|
|
|
|
apply_delayed_local_update, local_update);
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:30:36 +02:00
|
|
|
static void handle_local_channel_update(struct peer *peer, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct chan *chan;
|
|
|
|
struct local_update *local_update;
|
|
|
|
const struct pubkey *my_id = &peer->daemon->rstate->local_id;
|
|
|
|
|
|
|
|
local_update = tal(peer->daemon, struct local_update);
|
|
|
|
local_update->daemon = peer->daemon;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_local_channel_update(msg,
|
|
|
|
&local_update->scid,
|
|
|
|
&local_update->disable,
|
|
|
|
&local_update->cltv_delta,
|
|
|
|
&local_update->htlc_minimum_msat,
|
|
|
|
&local_update->fee_base_msat,
|
|
|
|
&local_update->fee_proportional_millionths)) {
|
|
|
|
status_broken("peer %s bad local_channel_update %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
tal_free(local_update);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Can theoretically happen if channel just closed. */
|
|
|
|
chan = get_channel(peer->daemon->rstate, &local_update->scid);
|
|
|
|
if (!chan) {
|
|
|
|
status_trace("peer %s local_channel_update for unknown %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&local_update->scid));
|
|
|
|
tal_free(local_update);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pubkey_eq(&chan->nodes[0]->id, my_id))
|
|
|
|
local_update->direction = 0;
|
|
|
|
else if (pubkey_eq(&chan->nodes[1]->id, my_id))
|
|
|
|
local_update->direction = 1;
|
|
|
|
else {
|
|
|
|
status_broken("peer %s bad local_channel_update for non-local %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&local_update->scid));
|
|
|
|
tal_free(local_update);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
queue_local_update(peer->daemon, local_update);
|
2018-05-21 06:35:40 +02:00
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
/**
|
2018-02-27 21:21:07 +01:00
|
|
|
* owner_msg_in - Called by the `peer->remote` upon receiving a
|
2017-03-11 15:31:17 +01:00
|
|
|
* message
|
|
|
|
*/
|
|
|
|
static struct io_plan *owner_msg_in(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
2017-03-09 16:56:04 +01:00
|
|
|
{
|
2017-12-06 07:15:06 +01:00
|
|
|
struct peer *peer = dc->ctx;
|
2018-03-18 14:57:15 +01:00
|
|
|
u8 *msg = dc->msg_in, *err;
|
2017-03-09 16:56:04 +01:00
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
int type = fromwire_peektype(msg);
|
|
|
|
if (type == WIRE_CHANNEL_ANNOUNCEMENT || type == WIRE_CHANNEL_UPDATE ||
|
|
|
|
type == WIRE_NODE_ANNOUNCEMENT) {
|
2018-05-18 07:49:08 +02:00
|
|
|
err = handle_gossip_msg(peer->daemon, dc->msg_in, "subdaemon");
|
2018-03-18 14:57:15 +01:00
|
|
|
if (err)
|
|
|
|
queue_peer_msg(peer, take(err));
|
2018-06-04 06:21:25 +02:00
|
|
|
} else if (type == WIRE_QUERY_SHORT_CHANNEL_IDS) {
|
|
|
|
handle_query_short_channel_ids(peer, dc->msg_in);
|
2018-06-04 06:22:25 +02:00
|
|
|
} else if (type == WIRE_REPLY_SHORT_CHANNEL_IDS_END) {
|
|
|
|
handle_reply_short_channel_ids_end(peer, dc->msg_in);
|
2018-06-04 06:26:25 +02:00
|
|
|
} else if (type == WIRE_GOSSIP_TIMESTAMP_FILTER) {
|
|
|
|
handle_gossip_timestamp_filter(peer, dc->msg_in);
|
2017-11-28 23:52:32 +01:00
|
|
|
} else if (type == WIRE_GOSSIP_GET_UPDATE) {
|
|
|
|
handle_get_update(peer, dc->msg_in);
|
2017-12-15 23:20:13 +01:00
|
|
|
} else if (type == WIRE_GOSSIP_LOCAL_ADD_CHANNEL) {
|
2018-05-30 20:29:17 +02:00
|
|
|
gossip_store_add(peer->daemon->rstate->store, dc->msg_in);
|
2018-04-21 11:56:54 +02:00
|
|
|
handle_local_add_channel(peer->daemon->rstate, dc->msg_in);
|
2018-05-21 06:35:40 +02:00
|
|
|
} else if (type == WIRE_GOSSIP_LOCAL_CHANNEL_UPDATE) {
|
|
|
|
handle_local_channel_update(peer, dc->msg_in);
|
2018-06-04 06:28:02 +02:00
|
|
|
} else if (type == WIRE_QUERY_CHANNEL_RANGE) {
|
|
|
|
handle_query_channel_range(peer, dc->msg_in);
|
|
|
|
} else if (type == WIRE_REPLY_CHANNEL_RANGE) {
|
|
|
|
handle_reply_channel_range(peer, dc->msg_in);
|
2018-07-24 02:26:43 +02:00
|
|
|
} else if (type == WIRE_PING) {
|
|
|
|
handle_ping(peer, dc->msg_in);
|
|
|
|
} else if (type == WIRE_PONG) {
|
|
|
|
handle_pong(peer, dc->msg_in);
|
2017-12-15 23:20:13 +01:00
|
|
|
} else {
|
2018-03-08 04:16:34 +01:00
|
|
|
status_broken("peer %s: send us unknown msg of type %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &peer->id),
|
2018-03-08 04:16:34 +01:00
|
|
|
gossip_wire_type_name(type));
|
|
|
|
return io_close(conn);
|
2017-03-11 15:31:17 +01:00
|
|
|
}
|
2017-12-15 23:20:13 +01:00
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
return daemon_conn_read_next(conn, dc);
|
2017-03-09 16:56:04 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
static void free_peer(struct io_conn *conn, struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
struct peer *peer = dc->ctx;
|
|
|
|
|
|
|
|
tal_free(peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *connectd_new_peer(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct peer *peer = tal(conn, struct peer);
|
|
|
|
int fds[2];
|
|
|
|
|
|
|
|
if (!fromwire_gossip_new_peer(msg, &peer->id,
|
|
|
|
&peer->gossip_queries_feature,
|
|
|
|
&peer->initial_routing_sync_feature)) {
|
|
|
|
status_broken("Bad new_peer msg from connectd: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (socketpair(AF_LOCAL, SOCK_STREAM, 0, fds) != 0) {
|
|
|
|
status_broken("Failed to create socketpair: %s",
|
|
|
|
strerror(errno));
|
|
|
|
daemon_conn_send(&daemon->connectd,
|
|
|
|
take(towire_gossip_new_peer_reply(NULL, false)));
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We might not have noticed old peer is dead; kill it now. */
|
|
|
|
tal_free(find_peer(daemon, &peer->id));
|
|
|
|
|
|
|
|
peer->daemon = daemon;
|
|
|
|
peer->remote = tal(peer, struct daemon_conn);
|
|
|
|
daemon_conn_init(peer, peer->remote, fds[0], owner_msg_in, free_peer);
|
|
|
|
peer->remote->msg_queue_cleared_cb = nonlocal_dump_gossip;
|
|
|
|
|
|
|
|
peer->scid_queries = NULL;
|
|
|
|
peer->scid_query_idx = 0;
|
|
|
|
peer->scid_query_nodes = NULL;
|
|
|
|
peer->scid_query_nodes_idx = 0;
|
|
|
|
peer->num_scid_queries_outstanding = 0;
|
|
|
|
peer->query_channel_blocks = NULL;
|
|
|
|
peer->num_pings_outstanding = 0;
|
|
|
|
peer->gossip_timer = NULL;
|
|
|
|
|
|
|
|
list_add_tail(&peer->daemon->peers, &peer->list);
|
|
|
|
tal_add_destructor(peer, destroy_peer);
|
|
|
|
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - if the `gossip_queries` feature is negotiated:
|
|
|
|
* - MUST NOT relay any gossip messages unless explicitly requested.
|
|
|
|
*/
|
|
|
|
if (peer->gossip_queries_feature) {
|
|
|
|
peer->broadcast_index = UINT64_MAX;
|
|
|
|
/* Nothing in this range */
|
|
|
|
peer->gossip_timestamp_min = UINT32_MAX;
|
|
|
|
peer->gossip_timestamp_max = 0;
|
|
|
|
} else {
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* - upon receiving an `init` message with the
|
|
|
|
* `initial_routing_sync` flag set to 1:
|
|
|
|
* - SHOULD send gossip messages for all known channels and
|
|
|
|
* nodes, as if they were just received.
|
|
|
|
* - if the `initial_routing_sync` flag is set to 0, OR if the
|
|
|
|
* initial sync was completed:
|
|
|
|
* - SHOULD resume normal operation, as specified in the
|
|
|
|
* following [Rebroadcasting](#rebroadcasting) section.
|
|
|
|
*/
|
|
|
|
peer->gossip_timestamp_min = 0;
|
|
|
|
peer->gossip_timestamp_max = UINT32_MAX;
|
|
|
|
if (peer->initial_routing_sync_feature)
|
|
|
|
peer->broadcast_index = 0;
|
|
|
|
else
|
|
|
|
peer->broadcast_index
|
|
|
|
= peer->daemon->rstate->broadcasts->next_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the gossip flowing. */
|
|
|
|
wake_gossip_out(peer);
|
|
|
|
|
|
|
|
setup_gossip_range(peer);
|
|
|
|
|
|
|
|
/* Reply with success, and the new fd */
|
|
|
|
daemon_conn_send(&daemon->connectd,
|
|
|
|
take(towire_gossip_new_peer_reply(NULL, true)));
|
|
|
|
daemon_conn_send_fd(&daemon->connectd, fds[1]);
|
|
|
|
|
|
|
|
done:
|
|
|
|
return daemon_conn_read_next(conn, &daemon->connectd);
|
|
|
|
}
|
|
|
|
|
2017-03-11 15:31:17 +01:00
|
|
|
/**
|
|
|
|
* nonlocal_dump_gossip - catch the nonlocal peer up with the latest gossip.
|
|
|
|
*
|
2018-02-27 21:21:07 +01:00
|
|
|
* Registered as `msg_queue_cleared_cb` by the `peer->remote`.
|
2017-03-11 15:31:17 +01:00
|
|
|
*/
|
2018-03-13 16:44:55 +01:00
|
|
|
static bool nonlocal_dump_gossip(struct io_conn *conn, struct daemon_conn *dc)
|
2017-03-10 13:06:51 +01:00
|
|
|
{
|
2017-12-06 07:15:06 +01:00
|
|
|
struct peer *peer = dc->ctx;
|
2017-03-11 15:31:17 +01:00
|
|
|
|
2018-06-04 06:19:25 +02:00
|
|
|
/* Do we have scid query replies to send? */
|
|
|
|
if (create_next_scid_reply(peer))
|
|
|
|
return true;
|
|
|
|
|
2018-06-04 06:26:25 +02:00
|
|
|
/* Otherwise queue any gossip we want to send */
|
|
|
|
return maybe_queue_gossip(peer);
|
2017-03-10 13:06:51 +01:00
|
|
|
}
|
|
|
|
|
2017-03-15 13:46:29 +01:00
|
|
|
static struct io_plan *getroute_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
u8 *msg)
|
2017-03-15 11:36:52 +01:00
|
|
|
{
|
2017-03-15 13:46:29 +01:00
|
|
|
struct pubkey source, destination;
|
2018-04-09 15:31:23 +02:00
|
|
|
u64 msatoshi;
|
|
|
|
u32 final_cltv;
|
2017-03-15 13:46:29 +01:00
|
|
|
u16 riskfactor;
|
|
|
|
u8 *out;
|
|
|
|
struct route_hop *hops;
|
2018-02-15 14:37:04 +01:00
|
|
|
double fuzz;
|
2018-02-23 01:00:00 +01:00
|
|
|
struct siphash_seed seed;
|
2017-03-15 13:46:29 +01:00
|
|
|
|
2018-02-26 03:32:58 +01:00
|
|
|
fromwire_gossip_getroute_request(msg,
|
2018-02-15 14:37:04 +01:00
|
|
|
&source, &destination,
|
|
|
|
&msatoshi, &riskfactor, &final_cltv,
|
2018-02-26 03:32:58 +01:00
|
|
|
&fuzz, &seed);
|
2018-04-09 15:31:23 +02:00
|
|
|
status_trace("Trying to find a route from %s to %s for %"PRIu64" msatoshi",
|
2017-03-15 13:46:29 +01:00
|
|
|
pubkey_to_hexstr(tmpctx, &source),
|
|
|
|
pubkey_to_hexstr(tmpctx, &destination), msatoshi);
|
|
|
|
|
|
|
|
hops = get_route(tmpctx, daemon->rstate, &source, &destination,
|
2018-02-16 04:50:51 +01:00
|
|
|
msatoshi, 1, final_cltv,
|
2018-02-23 01:00:00 +01:00
|
|
|
fuzz, &seed);
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-15 13:46:29 +01:00
|
|
|
out = towire_gossip_getroute_reply(msg, hops);
|
|
|
|
daemon_conn_send(&daemon->master, out);
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2018-03-02 03:27:30 +01:00
|
|
|
static void append_half_channel(struct gossip_getchannels_entry **entries,
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct chan *chan,
|
2018-03-04 03:26:56 +01:00
|
|
|
int idx)
|
2018-03-02 03:27:30 +01:00
|
|
|
{
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct half_chan *c = &chan->half[idx];
|
2018-03-02 03:27:30 +01:00
|
|
|
struct gossip_getchannels_entry *e;
|
|
|
|
size_t n;
|
|
|
|
|
2018-05-10 16:00:38 +02:00
|
|
|
if (!is_halfchan_defined(c))
|
2018-03-02 09:59:17 +01:00
|
|
|
return;
|
|
|
|
|
2018-03-02 03:27:30 +01:00
|
|
|
n = tal_count(*entries);
|
|
|
|
tal_resize(entries, n+1);
|
|
|
|
e = &(*entries)[n];
|
|
|
|
|
2018-03-04 03:26:56 +01:00
|
|
|
e->source = chan->nodes[idx]->id;
|
|
|
|
e->destination = chan->nodes[!idx]->id;
|
2018-03-05 23:52:26 +01:00
|
|
|
e->satoshis = chan->satoshis;
|
2018-03-02 03:27:30 +01:00
|
|
|
e->flags = c->flags;
|
2018-05-10 16:00:38 +02:00
|
|
|
e->public = is_chan_public(chan);
|
2018-03-04 03:26:56 +01:00
|
|
|
e->short_channel_id = chan->scid;
|
2018-05-10 16:00:38 +02:00
|
|
|
e->last_update_timestamp = c->last_timestamp;
|
|
|
|
e->base_fee_msat = c->base_fee;
|
|
|
|
e->fee_per_millionth = c->proportional_fee;
|
|
|
|
e->delay = c->delay;
|
2018-03-02 03:27:30 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
static void append_channel(struct gossip_getchannels_entry **entries,
|
2018-03-04 03:26:59 +01:00
|
|
|
const struct chan *chan)
|
2018-03-02 09:59:16 +01:00
|
|
|
{
|
2018-03-04 03:26:56 +01:00
|
|
|
append_half_channel(entries, chan, 0);
|
|
|
|
append_half_channel(entries, chan, 1);
|
2018-03-02 09:59:16 +01:00
|
|
|
}
|
|
|
|
|
2017-03-22 13:30:09 +01:00
|
|
|
static struct io_plan *getchannels_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
u8 *msg)
|
|
|
|
{
|
|
|
|
u8 *out;
|
|
|
|
struct gossip_getchannels_entry *entries;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2018-01-16 20:44:32 +01:00
|
|
|
struct short_channel_id *scid;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
fromwire_gossip_getchannels_request(msg, msg, &scid);
|
2017-03-22 13:30:09 +01:00
|
|
|
|
2018-03-02 03:27:30 +01:00
|
|
|
entries = tal_arr(tmpctx, struct gossip_getchannels_entry, 0);
|
2018-03-02 09:59:16 +01:00
|
|
|
if (scid) {
|
|
|
|
chan = get_channel(daemon->rstate, scid);
|
|
|
|
if (chan)
|
|
|
|
append_channel(&entries, chan);
|
|
|
|
} else {
|
|
|
|
u64 idx;
|
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
for (chan = uintmap_first(&daemon->rstate->chanmap, &idx);
|
2018-03-02 09:59:16 +01:00
|
|
|
chan;
|
2018-03-04 03:26:59 +01:00
|
|
|
chan = uintmap_after(&daemon->rstate->chanmap, &idx)) {
|
2018-03-02 09:59:16 +01:00
|
|
|
append_channel(&entries, chan);
|
2017-03-22 13:30:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
out = towire_gossip_getchannels_reply(NULL, entries);
|
2017-03-22 13:30:09 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(out));
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
static void append_node(const struct gossip_getnodes_entry ***nodes,
|
2018-06-22 01:52:57 +02:00
|
|
|
const struct pubkey *nodeid,
|
2018-06-22 01:53:57 +02:00
|
|
|
const u8 *gfeatures,
|
2018-06-22 01:52:57 +02:00
|
|
|
/* If non-NULL, contains more information */
|
2018-01-16 20:44:32 +01:00
|
|
|
const struct node *n)
|
|
|
|
{
|
2018-02-08 02:24:46 +01:00
|
|
|
struct gossip_getnodes_entry *new;
|
2018-01-16 20:44:32 +01:00
|
|
|
size_t num_nodes = tal_count(*nodes);
|
2018-02-08 02:24:46 +01:00
|
|
|
|
|
|
|
new = tal(*nodes, struct gossip_getnodes_entry);
|
2018-06-22 01:52:57 +02:00
|
|
|
new->nodeid = *nodeid;
|
2018-06-22 01:53:57 +02:00
|
|
|
new->global_features = tal_dup_arr(*nodes, u8, gfeatures,
|
|
|
|
tal_len(gfeatures), 0);
|
2018-06-22 01:52:57 +02:00
|
|
|
if (!n || n->last_timestamp < 0) {
|
|
|
|
new->last_timestamp = -1;
|
2018-02-08 02:24:46 +01:00
|
|
|
new->addresses = NULL;
|
|
|
|
} else {
|
2018-06-22 01:52:57 +02:00
|
|
|
new->last_timestamp = n->last_timestamp;
|
2018-02-08 02:24:46 +01:00
|
|
|
new->addresses = n->addresses;
|
|
|
|
new->alias = n->alias;
|
|
|
|
memcpy(new->color, n->rgb_color, 3);
|
2018-01-16 20:44:32 +01:00
|
|
|
}
|
2018-02-08 02:24:46 +01:00
|
|
|
tal_resize(nodes, num_nodes + 1);
|
|
|
|
(*nodes)[num_nodes] = new;
|
2018-01-16 20:44:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *getnodes(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2017-03-12 13:39:23 +01:00
|
|
|
{
|
2017-03-16 05:05:26 +01:00
|
|
|
u8 *out;
|
2017-03-12 13:39:23 +01:00
|
|
|
struct node *n;
|
2018-02-08 02:24:46 +01:00
|
|
|
const struct gossip_getnodes_entry **nodes;
|
2018-07-09 13:17:49 +02:00
|
|
|
struct pubkey *id;
|
2017-03-12 13:39:23 +01:00
|
|
|
|
2018-07-09 13:17:49 +02:00
|
|
|
fromwire_gossip_getnodes_request(tmpctx, msg, &id);
|
2018-01-16 20:44:32 +01:00
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
nodes = tal_arr(tmpctx, const struct gossip_getnodes_entry *, 0);
|
2018-07-09 13:17:49 +02:00
|
|
|
if (id) {
|
|
|
|
n = get_node(daemon->rstate, id);
|
|
|
|
if (n)
|
2018-07-24 08:18:59 +02:00
|
|
|
append_node(&nodes, id, n->gfeatures, n);
|
2018-01-16 20:44:32 +01:00
|
|
|
} else {
|
|
|
|
struct node_map_iter i;
|
|
|
|
n = node_map_first(daemon->rstate->nodes, &i);
|
|
|
|
while (n != NULL) {
|
2018-07-24 08:18:59 +02:00
|
|
|
append_node(&nodes, &n->id, n->gfeatures, n);
|
2018-01-16 20:44:32 +01:00
|
|
|
n = node_map_next(daemon->rstate->nodes, &i);
|
|
|
|
}
|
2017-03-12 13:39:23 +01:00
|
|
|
}
|
2018-03-15 07:10:22 +01:00
|
|
|
out = towire_gossip_getnodes_reply(NULL, nodes);
|
2017-03-19 21:28:29 +01:00
|
|
|
daemon_conn_send(&daemon->master, take(out));
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
2017-03-12 13:39:23 +01:00
|
|
|
}
|
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
#if DEVELOPER
|
2017-04-12 20:20:48 +02:00
|
|
|
static struct io_plan *ping_req(struct io_conn *conn, struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct pubkey id;
|
2017-04-12 20:20:48 +02:00
|
|
|
u16 num_pong_bytes, len;
|
|
|
|
struct peer *peer;
|
|
|
|
u8 *ping;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_ping(msg, &id, &num_pong_bytes, &len))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIP_PING, msg);
|
2017-04-12 20:20:48 +02:00
|
|
|
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
peer = find_peer(daemon, &id);
|
|
|
|
if (!peer) {
|
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_ping_reply(NULL, false, 0)));
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
goto out;
|
|
|
|
}
|
2017-04-12 20:20:48 +02:00
|
|
|
|
|
|
|
ping = make_ping(peer, num_pong_bytes, len);
|
|
|
|
if (tal_len(ping) > 65535)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "Oversize ping");
|
2017-04-12 20:20:48 +02:00
|
|
|
|
2018-07-24 02:26:43 +02:00
|
|
|
queue_peer_msg(peer, take(ping));
|
2017-04-12 20:20:48 +02:00
|
|
|
status_trace("sending ping expecting %sresponse",
|
|
|
|
num_pong_bytes >= 65532 ? "no " : "");
|
|
|
|
|
|
|
|
/* BOLT #1:
|
|
|
|
*
|
2018-06-17 12:13:44 +02:00
|
|
|
* A node receiving a `ping` message:
|
|
|
|
*...
|
|
|
|
* - if `num_pong_bytes` is less than 65532:
|
|
|
|
* - MUST respond by sending a `pong` message, with `byteslen` equal
|
|
|
|
* to `num_pong_bytes`.
|
|
|
|
* - otherwise (`num_pong_bytes` is **not** less than 65532):
|
|
|
|
* - MUST ignore the `ping`.
|
2017-04-12 20:20:48 +02:00
|
|
|
*/
|
|
|
|
if (num_pong_bytes >= 65532)
|
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_ping_reply(NULL, true, 0)));
|
2017-04-12 20:20:48 +02:00
|
|
|
else
|
2018-07-24 02:26:43 +02:00
|
|
|
peer->num_pings_outstanding++;
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
|
|
|
out:
|
2017-04-12 20:20:48 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
static struct io_plan *query_scids_req(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
struct short_channel_id *scids;
|
|
|
|
struct peer *peer;
|
|
|
|
u8 *encoded;
|
|
|
|
/* BOLT #7:
|
|
|
|
*
|
|
|
|
* 1. type: 261 (`query_short_channel_ids`) (`gossip_queries`)
|
|
|
|
* 2. data:
|
|
|
|
* * [`32`:`chain_hash`]
|
|
|
|
* * [`2`:`len`]
|
|
|
|
* * [`len`:`encoded_short_ids`]
|
|
|
|
*/
|
|
|
|
const size_t reply_overhead = 32 + 2;
|
|
|
|
const size_t max_encoded_bytes = 65535 - 2 - reply_overhead;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_query_scids(msg, msg, &id, &scids))
|
|
|
|
master_badmsg(WIRE_GOSSIP_QUERY_SCIDS, msg);
|
|
|
|
|
|
|
|
peer = find_peer(daemon, &id);
|
|
|
|
if (!peer) {
|
|
|
|
status_broken("query_scids: unknown peer %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!peer->gossip_queries_feature) {
|
2018-06-04 06:22:25 +02:00
|
|
|
status_broken("query_scids: no gossip_query support in peer %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
encoded = encode_short_channel_ids_start(tmpctx);
|
|
|
|
for (size_t i = 0; i < tal_count(scids); i++)
|
|
|
|
encode_add_short_channel_id(&encoded, &scids[i]);
|
|
|
|
|
|
|
|
if (!encode_short_channel_ids_end(&encoded, max_encoded_bytes)) {
|
|
|
|
status_broken("query_short_channel_ids: %zu is too many",
|
|
|
|
tal_count(scids));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = towire_query_short_channel_ids(NULL, &daemon->rstate->chain_hash,
|
|
|
|
encoded);
|
|
|
|
queue_peer_msg(peer, take(msg));
|
|
|
|
peer->num_scid_queries_outstanding++;
|
|
|
|
|
|
|
|
status_trace("sending query for %zu scids", tal_count(scids));
|
|
|
|
out:
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
daemon_conn_send(&daemon->master,
|
|
|
|
take(towire_gossip_scids_reply(NULL, false, false)));
|
|
|
|
goto out;
|
|
|
|
}
|
2018-06-04 06:28:02 +02:00
|
|
|
|
|
|
|
static struct io_plan *send_timestamp_filter(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
u32 first, range;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_send_timestamp_filter(msg, &id, &first, &range))
|
|
|
|
master_badmsg(WIRE_GOSSIP_SEND_TIMESTAMP_FILTER, msg);
|
|
|
|
|
|
|
|
peer = find_peer(daemon, &id);
|
|
|
|
if (!peer) {
|
|
|
|
status_broken("send_timestamp_filter: unknown peer %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!peer->gossip_queries_feature) {
|
2018-06-04 06:28:02 +02:00
|
|
|
status_broken("send_timestamp_filter: no gossip_query support in peer %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = towire_gossip_timestamp_filter(NULL, &daemon->rstate->chain_hash,
|
|
|
|
first, range);
|
|
|
|
queue_peer_msg(peer, take(msg));
|
|
|
|
out:
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-06-04 06:28:02 +02:00
|
|
|
|
|
|
|
static struct io_plan *query_channel_range(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
u32 first_blocknum, number_of_blocks;
|
|
|
|
struct peer *peer;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_query_channel_range(msg, &id, &first_blocknum,
|
|
|
|
&number_of_blocks))
|
|
|
|
master_badmsg(WIRE_GOSSIP_QUERY_SCIDS, msg);
|
|
|
|
|
|
|
|
peer = find_peer(daemon, &id);
|
|
|
|
if (!peer) {
|
|
|
|
status_broken("query_channel_range: unknown peer %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
if (!peer->gossip_queries_feature) {
|
2018-06-04 06:28:02 +02:00
|
|
|
status_broken("query_channel_range: no gossip_query support in peer %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer->query_channel_blocks) {
|
|
|
|
status_broken("query_channel_range: previous query active");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_debug("sending query_channel_range for blocks %u+%u",
|
|
|
|
first_blocknum, number_of_blocks);
|
|
|
|
msg = towire_query_channel_range(NULL, &daemon->rstate->chain_hash,
|
|
|
|
first_blocknum, number_of_blocks);
|
|
|
|
queue_peer_msg(peer, take(msg));
|
|
|
|
peer->first_channel_range = first_blocknum;
|
|
|
|
/* This uses 8 times as much as it needs to, but it's only for dev */
|
|
|
|
peer->query_channel_blocks = tal_arrz(peer, u8, number_of_blocks);
|
|
|
|
peer->query_channel_scids = tal_arr(peer, struct short_channel_id, 0);
|
|
|
|
|
|
|
|
out:
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
daemon_conn_send(&daemon->master,
|
|
|
|
take(towire_gossip_query_channel_range_reply(NULL,
|
|
|
|
0, 0,
|
|
|
|
false,
|
|
|
|
NULL)));
|
|
|
|
goto out;
|
|
|
|
}
|
2018-06-04 06:28:02 +02:00
|
|
|
|
|
|
|
static struct io_plan *dev_set_max_scids_encode_size(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
if (!fromwire_gossip_dev_set_max_scids_encode_size(msg,
|
|
|
|
&max_scids_encode_bytes))
|
|
|
|
master_badmsg(WIRE_GOSSIP_DEV_SET_MAX_SCIDS_ENCODE_SIZE, msg);
|
|
|
|
|
2018-07-02 07:23:56 +02:00
|
|
|
status_trace("Set max_scids_encode_bytes to %u", max_scids_encode_bytes);
|
2018-06-04 06:28:02 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-06-04 06:22:25 +02:00
|
|
|
#endif /* DEVELOPER */
|
|
|
|
|
2018-01-09 20:00:05 +01:00
|
|
|
static void gossip_send_keepalive_update(struct routing_state *rstate,
|
2018-05-21 06:36:17 +02:00
|
|
|
const struct chan *chan,
|
|
|
|
const struct half_chan *hc)
|
2018-01-09 20:00:05 +01:00
|
|
|
{
|
2018-05-21 06:36:17 +02:00
|
|
|
u8 *update, *err;
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-05-21 06:36:17 +02:00
|
|
|
/* Generate a new update, with up to date timestamp */
|
|
|
|
update = create_channel_update(tmpctx, rstate, chan,
|
|
|
|
hc->flags & ROUTING_FLAGS_DIRECTION,
|
|
|
|
false,
|
|
|
|
hc->delay,
|
|
|
|
hc->htlc_minimum_msat,
|
|
|
|
hc->base_fee,
|
|
|
|
hc->proportional_fee);
|
2018-01-09 20:00:05 +01:00
|
|
|
|
|
|
|
status_trace("Sending keepalive channel_update for %s",
|
2018-05-21 06:36:17 +02:00
|
|
|
type_to_string(tmpctx, struct short_channel_id,
|
|
|
|
&chan->scid));
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-05-18 07:49:08 +02:00
|
|
|
err = handle_channel_update(rstate, update, "keepalive");
|
2018-03-08 05:10:33 +01:00
|
|
|
if (err)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"rejected keepalive channel_update: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, err));
|
2018-01-09 20:00:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
static void gossip_refresh_network(struct daemon *daemon)
|
2018-01-09 16:29:10 +01:00
|
|
|
{
|
2018-01-09 20:00:05 +01:00
|
|
|
u64 now = time_now().ts.tv_sec;
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Anything below this highwater mark could be pruned if not refreshed */
|
|
|
|
s64 highwater = now - daemon->rstate->prune_timeout / 2;
|
2018-01-13 12:30:52 +01:00
|
|
|
struct node *n;
|
2018-01-09 21:51:45 +01:00
|
|
|
|
2018-01-09 16:29:10 +01:00
|
|
|
/* Schedule next run now */
|
|
|
|
new_reltimer(&daemon->timers, daemon,
|
2018-03-02 09:59:17 +01:00
|
|
|
time_from_sec(daemon->rstate->prune_timeout/4),
|
|
|
|
gossip_refresh_network, daemon);
|
2018-01-09 16:29:10 +01:00
|
|
|
|
2018-01-09 20:00:05 +01:00
|
|
|
/* Find myself in the network */
|
2018-02-27 21:16:43 +01:00
|
|
|
n = get_node(daemon->rstate, &daemon->id);
|
2018-01-13 14:15:05 +01:00
|
|
|
if (n) {
|
|
|
|
/* Iterate through all outgoing connection and check whether
|
|
|
|
* it's time to re-announce */
|
2018-03-04 03:26:59 +01:00
|
|
|
for (size_t i = 0; i < tal_count(n->chans); i++) {
|
|
|
|
struct half_chan *hc = half_chan_from(n, n->chans[i]);
|
2018-03-02 09:59:16 +01:00
|
|
|
|
2018-05-10 16:00:38 +02:00
|
|
|
if (!is_halfchan_defined(hc)) {
|
2018-05-10 14:22:37 +02:00
|
|
|
/* Connection is not announced yet, so don't even
|
2018-01-13 14:15:05 +01:00
|
|
|
* try to re-announce it */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-03-04 03:26:59 +01:00
|
|
|
if (hc->last_timestamp > highwater) {
|
2018-01-13 14:15:05 +01:00
|
|
|
/* No need to send a keepalive update message */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-05-10 16:00:38 +02:00
|
|
|
if (!is_halfchan_enabled(hc)) {
|
2018-01-13 14:15:05 +01:00
|
|
|
/* Only send keepalives for active connections */
|
|
|
|
continue;
|
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-05-21 06:36:17 +02:00
|
|
|
gossip_send_keepalive_update(daemon->rstate, n->chans[i],
|
|
|
|
hc);
|
2018-01-09 20:00:05 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-09 21:51:45 +01:00
|
|
|
|
2018-03-02 09:59:17 +01:00
|
|
|
route_prune(daemon->rstate);
|
2018-01-09 16:29:10 +01:00
|
|
|
}
|
2018-01-09 20:00:05 +01:00
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
static void gossip_disable_outgoing_halfchan(struct daemon *daemon,
|
2018-05-29 13:36:57 +02:00
|
|
|
struct chan *chan)
|
2018-05-28 19:04:04 +02:00
|
|
|
{
|
|
|
|
u8 direction;
|
|
|
|
struct half_chan *hc;
|
2018-07-03 13:43:45 +02:00
|
|
|
u16 flags;
|
|
|
|
u32 timestamp;
|
2018-05-28 19:04:04 +02:00
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
2018-07-03 13:43:45 +02:00
|
|
|
struct local_update *local_update;
|
|
|
|
struct routing_state *rstate = daemon->rstate;
|
2018-05-28 19:04:04 +02:00
|
|
|
|
|
|
|
direction = pubkey_eq(&chan->nodes[0]->id, &rstate->local_id)?0:1;
|
|
|
|
assert(chan);
|
|
|
|
hc = &chan->half[direction];
|
|
|
|
|
|
|
|
if (!is_halfchan_defined(hc))
|
|
|
|
return;
|
|
|
|
|
|
|
|
status_trace("Disabling channel %s/%d, active %d -> %d",
|
|
|
|
type_to_string(tmpctx, struct short_channel_id, &chan->scid),
|
|
|
|
direction, is_halfchan_enabled(hc), 0);
|
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
local_update = tal(daemon, struct local_update);
|
|
|
|
local_update->daemon = daemon;
|
|
|
|
local_update->direction = direction;
|
|
|
|
|
2018-05-28 19:04:04 +02:00
|
|
|
if (!fromwire_channel_update(
|
2018-07-03 13:43:45 +02:00
|
|
|
hc->channel_update, &sig, &chain_hash,
|
|
|
|
&local_update->scid, ×tamp, &flags,
|
|
|
|
&local_update->cltv_delta,
|
|
|
|
&local_update->htlc_minimum_msat,
|
|
|
|
&local_update->fee_base_msat,
|
|
|
|
&local_update->fee_proportional_millionths)) {
|
2018-05-28 19:04:04 +02:00
|
|
|
status_failed(
|
|
|
|
STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Unable to parse previously accepted channel_update");
|
|
|
|
}
|
|
|
|
|
|
|
|
timestamp = time_now().ts.tv_sec;
|
|
|
|
if (timestamp <= hc->last_timestamp)
|
|
|
|
timestamp = hc->last_timestamp + 1;
|
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
local_update->disable = true;
|
2018-05-28 19:04:04 +02:00
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
queue_local_update(daemon, local_update);
|
2018-05-28 19:04:04 +02:00
|
|
|
}
|
|
|
|
|
2018-05-29 13:36:57 +02:00
|
|
|
/**
|
|
|
|
* Disable both directions of a local channel.
|
|
|
|
*
|
|
|
|
* Disables both directions of a local channel as a result of a close or lost
|
|
|
|
* connection. A disabling `channel_update` will be queued for the outgoing
|
|
|
|
* direction as well. We can't do that for the incoming direction, so we just
|
|
|
|
* locally flip the flag, and the other endpoint should take care of publicly
|
|
|
|
* disabling it with a `channel_update`.
|
|
|
|
*
|
|
|
|
* It is important to disable the incoming edge as well since we might otherwise
|
|
|
|
* return that edge as a `contact_point` as part of an invoice.
|
|
|
|
*/
|
2018-07-03 13:43:45 +02:00
|
|
|
static void gossip_disable_local_channel(struct daemon *daemon,
|
2018-05-29 13:36:57 +02:00
|
|
|
struct chan *chan)
|
|
|
|
{
|
2018-07-03 13:43:45 +02:00
|
|
|
struct routing_state *rstate = daemon->rstate;
|
|
|
|
|
2018-05-29 13:36:57 +02:00
|
|
|
assert(pubkey_eq(&rstate->local_id, &chan->nodes[0]->id) ||
|
|
|
|
pubkey_eq(&rstate->local_id, &chan->nodes[1]->id));
|
|
|
|
|
|
|
|
chan->half[0].flags |= ROUTING_FLAGS_DISABLED;
|
|
|
|
chan->half[1].flags |= ROUTING_FLAGS_DISABLED;
|
2018-07-03 13:43:45 +02:00
|
|
|
gossip_disable_outgoing_halfchan(daemon, chan);
|
2018-05-29 13:36:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-28 19:10:25 +02:00
|
|
|
static void gossip_disable_local_channels(struct daemon *daemon)
|
|
|
|
{
|
|
|
|
struct node *local_node =
|
|
|
|
get_node(daemon->rstate, &daemon->rstate->local_id);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* We don't have a local_node, so we don't have any channels yet
|
|
|
|
* either */
|
|
|
|
if (!local_node)
|
|
|
|
return;
|
|
|
|
|
2018-05-29 13:36:57 +02:00
|
|
|
for (i = 0; i < tal_count(local_node->chans); i++)
|
2018-07-03 13:43:45 +02:00
|
|
|
gossip_disable_local_channel(daemon,
|
2018-05-29 13:36:57 +02:00
|
|
|
local_node->chans[i]);
|
2018-05-28 19:10:25 +02:00
|
|
|
}
|
|
|
|
|
2017-04-24 14:31:26 +02:00
|
|
|
/* Parse an incoming gossip init message and assign config variables
|
|
|
|
* to the daemon.
|
|
|
|
*/
|
|
|
|
static struct io_plan *gossip_init(struct daemon_conn *master,
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
2017-04-24 14:31:26 +02:00
|
|
|
{
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2018-03-02 09:59:17 +01:00
|
|
|
u32 update_channel_interval;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2018-01-09 15:52:21 +01:00
|
|
|
if (!fromwire_gossipctl_init(
|
2018-02-20 21:59:09 +01:00
|
|
|
daemon, msg, &daemon->broadcast_interval, &chain_hash,
|
2018-04-30 04:08:15 +02:00
|
|
|
&daemon->id, &daemon->globalfeatures,
|
2018-07-24 08:18:58 +02:00
|
|
|
daemon->rgb,
|
|
|
|
daemon->alias, &update_channel_interval,
|
|
|
|
&daemon->announcable)) {
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIPCTL_INIT, msg);
|
2017-04-24 14:31:26 +02:00
|
|
|
}
|
2018-03-02 09:59:17 +01:00
|
|
|
/* Prune time is twice update time */
|
|
|
|
daemon->rstate = new_routing_state(daemon, &chain_hash, &daemon->id,
|
2018-07-24 08:18:58 +02:00
|
|
|
update_channel_interval * 2);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
|
2018-04-30 04:08:15 +02:00
|
|
|
/* Load stored gossip messages */
|
|
|
|
gossip_store_load(daemon->rstate, daemon->rstate->store);
|
2018-01-09 16:29:10 +01:00
|
|
|
|
2018-05-28 19:10:25 +02:00
|
|
|
/* Now disable all local channels, they can't be connected yet. */
|
|
|
|
gossip_disable_local_channels(daemon);
|
|
|
|
|
2018-01-09 16:29:10 +01:00
|
|
|
new_reltimer(&daemon->timers, daemon,
|
2018-03-02 09:59:17 +01:00
|
|
|
time_from_sec(daemon->rstate->prune_timeout/4),
|
|
|
|
gossip_refresh_network, daemon);
|
2018-01-09 16:29:10 +01:00
|
|
|
|
2018-04-30 04:08:15 +02:00
|
|
|
return daemon_conn_read_next(master->conn, master);
|
|
|
|
}
|
|
|
|
|
2017-04-30 23:49:15 +02:00
|
|
|
static struct io_plan *resolve_channel_req(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
2018-03-04 03:26:59 +01:00
|
|
|
struct chan *chan;
|
2017-05-05 08:41:44 +02:00
|
|
|
struct pubkey *keys;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_resolve_channel_request(msg, &scid))
|
2017-09-12 06:55:52 +02:00
|
|
|
master_badmsg(WIRE_GOSSIP_RESOLVE_CHANNEL_REQUEST, msg);
|
2017-05-05 08:41:44 +02:00
|
|
|
|
2018-03-02 09:59:16 +01:00
|
|
|
chan = get_channel(daemon->rstate, &scid);
|
|
|
|
if (!chan) {
|
2017-06-06 01:47:10 +02:00
|
|
|
status_trace("Failed to resolve channel %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid));
|
2017-05-05 08:41:44 +02:00
|
|
|
keys = NULL;
|
2017-04-30 23:49:15 +02:00
|
|
|
} else {
|
2017-05-05 08:41:44 +02:00
|
|
|
keys = tal_arr(msg, struct pubkey, 2);
|
2018-03-02 09:59:16 +01:00
|
|
|
keys[0] = chan->nodes[0]->id;
|
|
|
|
keys[1] = chan->nodes[1]->id;
|
2017-06-06 01:47:10 +02:00
|
|
|
status_trace("Resolved channel %s %s<->%s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct short_channel_id, &scid),
|
|
|
|
type_to_string(tmpctx, struct pubkey, &keys[0]),
|
|
|
|
type_to_string(tmpctx, struct pubkey, &keys[1]));
|
2017-04-30 23:49:15 +02:00
|
|
|
}
|
2017-05-05 08:41:44 +02:00
|
|
|
daemon_conn_send(&daemon->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_gossip_resolve_channel_reply(NULL, keys)));
|
2017-04-30 23:49:15 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:43:45 +02:00
|
|
|
static void peer_disable_channels(struct daemon *daemon, struct node *node)
|
2018-05-28 13:27:21 +02:00
|
|
|
{
|
|
|
|
struct chan *c;
|
|
|
|
size_t i;
|
|
|
|
for (i=0; i<tal_count(node->chans); i++) {
|
|
|
|
c = node->chans[i];
|
2018-07-03 13:43:45 +02:00
|
|
|
if (pubkey_eq(&other_node(node, c)->id,
|
|
|
|
&daemon->rstate->local_id))
|
|
|
|
gossip_disable_local_channel(daemon, c);
|
2018-05-28 13:27:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
static struct io_plan *handle_txout_reply(struct io_conn *conn,
|
|
|
|
struct daemon *daemon, const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
|
|
|
u8 *outscript;
|
2018-03-05 23:25:00 +01:00
|
|
|
u64 satoshis;
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-03-05 23:25:00 +01:00
|
|
|
if (!fromwire_gossip_get_txout_reply(msg, msg, &scid, &satoshis, &outscript))
|
2018-01-04 12:40:58 +01:00
|
|
|
master_badmsg(WIRE_GOSSIP_GET_TXOUT_REPLY, msg);
|
|
|
|
|
2018-06-04 06:15:25 +02:00
|
|
|
handle_pending_cannouncement(daemon->rstate, &scid, satoshis, outscript);
|
2018-06-04 06:38:39 +02:00
|
|
|
maybe_send_own_node_announce(daemon);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-01-18 00:32:36 +01:00
|
|
|
static struct io_plan *handle_routing_failure(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey erring_node;
|
|
|
|
struct short_channel_id erring_channel;
|
|
|
|
u16 failcode;
|
2018-01-21 01:36:41 +01:00
|
|
|
u8 *channel_update;
|
2018-01-18 00:32:36 +01:00
|
|
|
|
2018-01-21 01:36:41 +01:00
|
|
|
if (!fromwire_gossip_routing_failure(msg,
|
2018-02-20 21:59:09 +01:00
|
|
|
msg,
|
2018-01-18 00:32:36 +01:00
|
|
|
&erring_node,
|
|
|
|
&erring_channel,
|
2018-01-21 01:36:41 +01:00
|
|
|
&failcode,
|
|
|
|
&channel_update))
|
2018-01-18 00:32:36 +01:00
|
|
|
master_badmsg(WIRE_GOSSIP_ROUTING_FAILURE, msg);
|
|
|
|
|
|
|
|
routing_failure(daemon->rstate,
|
|
|
|
&erring_node,
|
|
|
|
&erring_channel,
|
2018-01-21 01:36:41 +01:00
|
|
|
(enum onion_type) failcode,
|
|
|
|
channel_update);
|
2018-01-18 00:32:36 +01:00
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-02-06 16:32:06 +01:00
|
|
|
static struct io_plan *
|
|
|
|
handle_mark_channel_unroutable(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id channel;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_gossip_mark_channel_unroutable(msg, &channel))
|
2018-02-06 16:32:06 +01:00
|
|
|
master_badmsg(WIRE_GOSSIP_MARK_CHANNEL_UNROUTABLE, msg);
|
|
|
|
|
|
|
|
mark_channel_unroutable(daemon->rstate, &channel);
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
2018-01-23 22:13:19 +01:00
|
|
|
|
2018-03-26 20:10:03 +02:00
|
|
|
static struct io_plan *handle_outpoint_spent(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
2018-03-28 12:14:01 +02:00
|
|
|
struct short_channel_id scid;
|
|
|
|
struct chan *chan;
|
|
|
|
struct routing_state *rstate = daemon->rstate;
|
|
|
|
if (!fromwire_gossip_outpoint_spent(msg, &scid))
|
|
|
|
master_badmsg(WIRE_GOSSIP_ROUTING_FAILURE, msg);
|
|
|
|
|
|
|
|
chan = get_channel(rstate, &scid);
|
|
|
|
if (chan) {
|
|
|
|
status_trace(
|
|
|
|
"Deleting channel %s due to the funding outpoint being "
|
|
|
|
"spent",
|
|
|
|
type_to_string(msg, struct short_channel_id, &scid));
|
|
|
|
/* Freeing is sufficient since everything else is allocated off
|
|
|
|
* of the channel and the destructor takes care of unregistering
|
|
|
|
* the channel */
|
|
|
|
tal_free(chan);
|
2018-03-28 12:54:09 +02:00
|
|
|
gossip_store_add_channel_delete(rstate->store, &scid);
|
2018-03-28 12:14:01 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 20:10:03 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2018-05-26 15:19:24 +02:00
|
|
|
/**
|
|
|
|
* Disable both directions of a channel due to an imminent close.
|
|
|
|
*
|
|
|
|
* We'll leave it to handle_outpoint_spent to delete the channel from our view
|
|
|
|
* once the close gets confirmed. This avoids having strange states in which the
|
|
|
|
* channel is list in our peer list but won't be returned when listing public
|
|
|
|
* channels. This does not send out updates since that's triggered by the peer
|
|
|
|
* connection closing.
|
|
|
|
*/
|
|
|
|
static struct io_plan *handle_local_channel_close(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct short_channel_id scid;
|
|
|
|
struct chan *chan;
|
|
|
|
struct routing_state *rstate = daemon->rstate;
|
|
|
|
if (!fromwire_gossip_local_channel_close(msg, &scid))
|
|
|
|
master_badmsg(WIRE_GOSSIP_ROUTING_FAILURE, msg);
|
|
|
|
|
|
|
|
chan = get_channel(rstate, &scid);
|
2018-05-29 13:36:57 +02:00
|
|
|
if (chan)
|
2018-07-03 13:43:45 +02:00
|
|
|
gossip_disable_local_channel(daemon, chan);
|
2018-05-26 15:19:24 +02:00
|
|
|
return daemon_conn_read_next(conn, &daemon->master);
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:28:29 +01:00
|
|
|
static struct io_plan *recv_req(struct io_conn *conn, struct daemon_conn *master)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-03-19 21:28:29 +01:00
|
|
|
struct daemon *daemon = container_of(master, struct daemon, master);
|
|
|
|
enum gossip_wire_type t = fromwire_peektype(master->msg_in);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
switch (t) {
|
2017-04-24 14:31:26 +02:00
|
|
|
case WIRE_GOSSIPCTL_INIT:
|
|
|
|
return gossip_init(master, daemon, master->msg_in);
|
|
|
|
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REQUEST:
|
2018-01-16 20:44:32 +01:00
|
|
|
return getnodes(conn, daemon, daemon->master.msg_in);
|
2017-03-12 13:39:23 +01:00
|
|
|
|
2017-03-15 11:36:52 +01:00
|
|
|
case WIRE_GOSSIP_GETROUTE_REQUEST:
|
2017-03-15 13:46:29 +01:00
|
|
|
return getroute_req(conn, daemon, daemon->master.msg_in);
|
2017-03-15 11:36:52 +01:00
|
|
|
|
2017-03-22 13:30:09 +01:00
|
|
|
case WIRE_GOSSIP_GETCHANNELS_REQUEST:
|
|
|
|
return getchannels_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2017-04-30 23:49:15 +02:00
|
|
|
case WIRE_GOSSIP_RESOLVE_CHANNEL_REQUEST:
|
|
|
|
return resolve_channel_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2018-01-04 12:40:58 +01:00
|
|
|
case WIRE_GOSSIP_GET_TXOUT_REPLY:
|
2018-01-04 12:40:58 +01:00
|
|
|
return handle_txout_reply(conn, daemon, master->msg_in);
|
2018-01-04 12:40:58 +01:00
|
|
|
|
2018-01-18 00:32:36 +01:00
|
|
|
case WIRE_GOSSIP_ROUTING_FAILURE:
|
|
|
|
return handle_routing_failure(conn, daemon, master->msg_in);
|
|
|
|
|
2018-02-06 16:32:06 +01:00
|
|
|
case WIRE_GOSSIP_MARK_CHANNEL_UNROUTABLE:
|
|
|
|
return handle_mark_channel_unroutable(conn, daemon, master->msg_in);
|
|
|
|
|
2018-03-26 20:10:03 +02:00
|
|
|
case WIRE_GOSSIP_OUTPOINT_SPENT:
|
|
|
|
return handle_outpoint_spent(conn, daemon, master->msg_in);
|
2018-05-26 15:19:24 +02:00
|
|
|
|
|
|
|
case WIRE_GOSSIP_LOCAL_CHANNEL_CLOSE:
|
|
|
|
return handle_local_channel_close(conn, daemon, master->msg_in);
|
2018-03-05 17:16:20 +01:00
|
|
|
|
2018-06-04 06:22:25 +02:00
|
|
|
#if DEVELOPER
|
|
|
|
case WIRE_GOSSIP_PING:
|
|
|
|
return ping_req(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
|
|
|
case WIRE_GOSSIP_QUERY_SCIDS:
|
|
|
|
return query_scids_req(conn, daemon, daemon->master.msg_in);
|
2018-06-04 06:28:02 +02:00
|
|
|
|
|
|
|
case WIRE_GOSSIP_SEND_TIMESTAMP_FILTER:
|
|
|
|
return send_timestamp_filter(conn, daemon, daemon->master.msg_in);
|
2018-06-04 06:28:02 +02:00
|
|
|
|
|
|
|
case WIRE_GOSSIP_QUERY_CHANNEL_RANGE:
|
|
|
|
return query_channel_range(conn, daemon, daemon->master.msg_in);
|
|
|
|
|
2018-06-04 06:28:02 +02:00
|
|
|
case WIRE_GOSSIP_DEV_SET_MAX_SCIDS_ENCODE_SIZE:
|
|
|
|
return dev_set_max_scids_encode_size(conn, daemon,
|
|
|
|
daemon->master.msg_in);
|
2018-06-04 06:22:25 +02:00
|
|
|
#else
|
|
|
|
case WIRE_GOSSIP_PING:
|
|
|
|
case WIRE_GOSSIP_QUERY_SCIDS:
|
2018-06-04 06:28:02 +02:00
|
|
|
case WIRE_GOSSIP_SEND_TIMESTAMP_FILTER:
|
2018-06-04 06:28:02 +02:00
|
|
|
case WIRE_GOSSIP_QUERY_CHANNEL_RANGE:
|
2018-06-04 06:28:02 +02:00
|
|
|
case WIRE_GOSSIP_DEV_SET_MAX_SCIDS_ENCODE_SIZE:
|
2018-06-04 06:22:25 +02:00
|
|
|
break;
|
|
|
|
#endif /* !DEVELOPER */
|
|
|
|
|
2017-12-15 15:16:42 +01:00
|
|
|
/* We send these, we don't receive them */
|
2017-03-12 13:39:23 +01:00
|
|
|
case WIRE_GOSSIP_GETNODES_REPLY:
|
2017-03-15 11:36:52 +01:00
|
|
|
case WIRE_GOSSIP_GETROUTE_REPLY:
|
2017-03-22 13:30:09 +01:00
|
|
|
case WIRE_GOSSIP_GETCHANNELS_REPLY:
|
2017-04-12 20:20:48 +02:00
|
|
|
case WIRE_GOSSIP_PING_REPLY:
|
2018-06-04 06:22:25 +02:00
|
|
|
case WIRE_GOSSIP_SCIDS_REPLY:
|
2018-06-04 06:28:02 +02:00
|
|
|
case WIRE_GOSSIP_QUERY_CHANNEL_RANGE_REPLY:
|
2017-04-30 23:49:15 +02:00
|
|
|
case WIRE_GOSSIP_RESOLVE_CHANNEL_REPLY:
|
2017-11-28 23:52:32 +01:00
|
|
|
case WIRE_GOSSIP_GET_UPDATE:
|
|
|
|
case WIRE_GOSSIP_GET_UPDATE_REPLY:
|
2017-12-11 04:33:16 +01:00
|
|
|
case WIRE_GOSSIP_SEND_GOSSIP:
|
2017-12-15 15:16:42 +01:00
|
|
|
case WIRE_GOSSIP_LOCAL_ADD_CHANNEL:
|
2018-05-21 06:35:40 +02:00
|
|
|
case WIRE_GOSSIP_LOCAL_CHANNEL_UPDATE:
|
2018-01-04 12:40:58 +01:00
|
|
|
case WIRE_GOSSIP_GET_TXOUT:
|
2017-12-15 15:16:42 +01:00
|
|
|
break;
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-09-12 06:55:52 +02:00
|
|
|
/* Master shouldn't give bad requests. */
|
|
|
|
status_failed(STATUS_FAIL_MASTER_IO, "%i: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
t, tal_hex(tmpctx, master->msg_in));
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct io_plan *connectd_get_address(struct io_conn *conn,
|
|
|
|
struct daemon *daemon,
|
|
|
|
const u8 *msg)
|
|
|
|
{
|
|
|
|
struct pubkey id;
|
|
|
|
struct node *node;
|
|
|
|
const struct wireaddr *addrs;
|
|
|
|
|
|
|
|
if (!fromwire_gossip_get_addrs(msg, &id)) {
|
|
|
|
status_broken("Bad gossip_get_addrs msg from connectd: %s",
|
|
|
|
tal_hex(tmpctx, msg));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
node = get_node(daemon->rstate, &id);
|
|
|
|
if (node)
|
|
|
|
addrs = node->addresses;
|
|
|
|
else
|
|
|
|
addrs = NULL;
|
|
|
|
|
|
|
|
daemon_conn_send(&daemon->connectd,
|
|
|
|
take(towire_gossip_get_addrs_reply(NULL, addrs)));
|
|
|
|
return daemon_conn_read_next(conn, &daemon->connectd);
|
|
|
|
}
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
static struct io_plan *connectd_req(struct io_conn *conn,
|
|
|
|
struct daemon_conn *connectd)
|
|
|
|
{
|
|
|
|
struct daemon *daemon = container_of(connectd, struct daemon, connectd);
|
|
|
|
enum connect_gossip_wire_type t = fromwire_peektype(connectd->msg_in);
|
|
|
|
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_GOSSIP_NEW_PEER:
|
|
|
|
return connectd_new_peer(conn, daemon, connectd->msg_in);
|
|
|
|
|
2018-07-24 08:18:58 +02:00
|
|
|
case WIRE_GOSSIP_GET_ADDRS:
|
|
|
|
return connectd_get_address(conn, daemon, connectd->msg_in);
|
|
|
|
|
|
|
|
/* We send these, don't receive them. */
|
2018-07-24 08:18:58 +02:00
|
|
|
case WIRE_GOSSIP_NEW_PEER_REPLY:
|
2018-07-24 08:18:58 +02:00
|
|
|
case WIRE_GOSSIP_GET_ADDRS_REPLY:
|
2018-07-24 08:18:58 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_broken("Bad msg from connectd: %s",
|
|
|
|
tal_hex(tmpctx, connectd->msg_in));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
#ifndef TESTING
|
2018-02-21 16:06:07 +01:00
|
|
|
static void master_gone(struct io_conn *unused UNUSED, struct daemon_conn *dc UNUSED)
|
2017-06-06 05:08:42 +02:00
|
|
|
{
|
|
|
|
/* Can't tell master, it's gone. */
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
struct daemon *daemon;
|
|
|
|
|
2018-01-08 11:01:09 +01:00
|
|
|
subdaemon_setup(argc, argv);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
daemon = tal(NULL, struct daemon);
|
|
|
|
list_head_init(&daemon->peers);
|
2018-07-03 13:30:36 +02:00
|
|
|
list_head_init(&daemon->local_updates);
|
2017-02-04 16:28:35 +01:00
|
|
|
timers_init(&daemon->timers, time_mono());
|
2017-04-24 14:31:26 +02:00
|
|
|
daemon->broadcast_interval = 30000;
|
2018-01-04 12:40:46 +01:00
|
|
|
daemon->last_announce_timestamp = 0;
|
2018-07-24 08:18:58 +02:00
|
|
|
|
2017-03-10 11:50:43 +01:00
|
|
|
/* stdin == control */
|
2017-06-06 05:08:42 +02:00
|
|
|
daemon_conn_init(daemon, &daemon->master, STDIN_FILENO, recv_req,
|
|
|
|
master_gone);
|
2017-03-19 21:32:44 +01:00
|
|
|
status_setup_async(&daemon->master);
|
gossipd: rewrite to do the handshake internally.
Now the flow is much simpler from a lightningd POV:
1. If we want to connect to a peer, just send gossipd `gossipctl_reach_peer`.
2. Every new peer, gossipd hands up to lightningd, with global/local features
and the peer fd and a gossip fd using `gossip_peer_connected`
3. If lightningd doesn't want it, it just hands the peerfd and global/local
features back to gossipd using `gossipctl_handle_peer`
4. If a peer sends a non-gossip msg (eg `open_channel`) the gossipd sends
it up using `gossip_peer_nongossip`.
5. If lightningd wants to fund a channel, it simply calls `release_channel`.
Notes:
* There's no more "unique_id": we use the peer id.
* For the moment, we don't ask gossipd when we're told to list peers, so
connected peers without a channel don't appear in the JSON getpeers API.
* We add a `gossipctl_peer_addrhint` for the moment, so you can connect to
a specific ip/port, but using other sources is a TODO.
* We now (correctly) only give up on reaching a peer after we exchange init
messages, which changes the test_disconnect case.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2017-10-11 12:09:49 +02:00
|
|
|
hsm_setup(HSM_FD);
|
2018-07-24 08:18:58 +02:00
|
|
|
daemon_conn_init(daemon, &daemon->connectd, CONNECTD_FD, connectd_req,
|
|
|
|
NULL);
|
2017-04-12 08:12:15 +02:00
|
|
|
|
|
|
|
/* When conn closes, everything is freed. */
|
|
|
|
tal_steal(daemon->master.conn, daemon);
|
|
|
|
|
2017-02-04 16:28:35 +01:00
|
|
|
for (;;) {
|
|
|
|
struct timer *expired = NULL;
|
|
|
|
io_loop(&daemon->timers, &expired);
|
|
|
|
|
|
|
|
if (!expired) {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
timer_expired(daemon, expired);
|
|
|
|
}
|
|
|
|
}
|
2018-03-29 04:06:45 +02:00
|
|
|
daemon_shutdown();
|
2017-01-10 06:08:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|