From 7793bd1b9d61ddc9e0b08a01396e851b7704c01e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 22 Mar 2017 16:46:48 +0100 Subject: [PATCH] gossip: Consolidated direction bit computation The direction bit was computed in several spots and was inconsistent in some cases. Now we compute it just in routing, and once when starting up `channeld`, this avoids recomputing it all over the place. --- daemon/routing.c | 16 +++++++--------- daemon/routing.h | 5 ++++- lightningd/channel/channel.c | 22 +++++++++------------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/daemon/routing.c b/daemon/routing.c index 2e8321b95..6de38a603 100644 --- a/daemon/routing.c +++ b/daemon/routing.c @@ -258,7 +258,7 @@ struct node_connection *add_connection(struct routing_state *rstate, c->active = true; c->last_timestamp = 0; memset(&c->short_channel_id, 0, sizeof(c->short_channel_id)); - c->flags = pubkey_cmp(from, to) > 0; + c->flags = get_channel_direction(from, to); return c; } @@ -508,11 +508,11 @@ char *opt_add_route(const char *arg, struct lightningd_state *dstate) bool add_channel_direction(struct routing_state *rstate, const struct pubkey *from, const struct pubkey *to, - const int direction, const struct short_channel_id *short_channel_id, const u8 *announcement) { struct node_connection *c = get_connection(rstate, from, to); + u16 direction = get_channel_direction(from, to); if (c){ /* Do not clobber connections added otherwise */ memcpy(&c->short_channel_id, short_channel_id, @@ -677,13 +677,11 @@ void handle_channel_announcement( short_channel_id.outnum ); - forward |= add_channel_direction(rstate, &node_id_1, - &node_id_2, 0, &short_channel_id, - serialized); - forward |= add_channel_direction(rstate, &node_id_2, - &node_id_1, 1, &short_channel_id, - serialized); - if (!forward){ + forward |= add_channel_direction(rstate, &node_id_1, &node_id_2, + &short_channel_id, serialized); + forward |= add_channel_direction(rstate, &node_id_2, &node_id_1, + &short_channel_id, serialized); + if (!forward) { log_debug(rstate->base_log, "Not forwarding channel_announcement"); tal_free(tmpctx); return; diff --git a/daemon/routing.h b/daemon/routing.h index 9dcb57b41..b52fe99b7 100644 --- a/daemon/routing.h +++ b/daemon/routing.h @@ -158,7 +158,6 @@ char *opt_add_route(const char *arg, struct lightningd_state *dstate); bool add_channel_direction(struct routing_state *rstate, const struct pubkey *from, const struct pubkey *to, - const int direction, const struct short_channel_id *short_channel_id, const u8 *announcement); @@ -176,4 +175,8 @@ struct route_hop *get_route(tal_t *ctx, struct routing_state *rstate, const struct pubkey *destination, const u32 msatoshi, double riskfactor); +/* Utility function that, given a source and a destination, gives us + * the direction bit the matching channel should get */ +#define get_channel_direction(from, to) (pubkey_cmp(from, to) > 0) + #endif /* LIGHTNING_DAEMON_ROUTING_H */ diff --git a/lightningd/channel/channel.c b/lightningd/channel/channel.c index 8663f39b3..ada5aa80a 100644 --- a/lightningd/channel/channel.c +++ b/lightningd/channel/channel.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,9 @@ struct peer { struct short_channel_id short_channel_ids[NUM_SIDES]; secp256k1_ecdsa_signature announcement_node_sigs[NUM_SIDES]; secp256k1_ecdsa_signature announcement_bitcoin_sigs[NUM_SIDES]; + + /* Which direction of the channel do we control? */ + u16 channel_direction; }; static struct io_plan *gossip_client_recv(struct io_conn *conn, @@ -97,17 +101,6 @@ static void send_announcement_signatures(struct peer *peer) tal_free(tmpctx); } -/* The direction bit is 0 if our local node-id is lexicographically - * smaller than the remote node-id. */ -static int get_direction_bit(struct peer *peer) -{ - u8 local_der[33], remote_der[33]; - /* Find out in which order we have to list the endpoints */ - pubkey_to_der(local_der, &peer->node_ids[LOCAL]); - pubkey_to_der(remote_der, &peer->node_ids[REMOTE]); - return memcmp(local_der, remote_der, sizeof(local_der)) < 0; -} - static void send_channel_update(struct peer *peer, bool disabled) { tal_t *tmpctx = tal_tmpctx(peer); @@ -118,7 +111,7 @@ static void send_channel_update(struct peer *peer, bool disabled) secp256k1_ecdsa_signature *sig = talz(tmpctx, secp256k1_ecdsa_signature); - flags = get_direction_bit(peer) | (disabled << 1); + flags = peer->channel_direction | (disabled << 1); cupdate = towire_channel_update( tmpctx, sig, &peer->short_channel_ids[LOCAL], timestamp, flags, 36, 1, 10, peer->channel->view[LOCAL].feerate_per_kw); @@ -136,7 +129,7 @@ static void send_channel_announcement(struct peer *peer) int first, second; u8 *cannounce, *features = tal_arr(peer, u8, 0); - if (get_direction_bit(peer) == 1) { + if (peer->channel_direction == 0) { first = LOCAL; second = REMOTE; } else { @@ -271,6 +264,9 @@ static void init_channel(struct peer *peer, const u8 *msg) &points[LOCAL], &points[REMOTE], am_funder ? LOCAL : REMOTE); + peer->channel_direction = get_channel_direction( + &peer->node_ids[LOCAL], &peer->node_ids[REMOTE]); + /* OK, now we can process peer messages. */ io_set_finish(io_new_conn(peer, PEER_FD, setup_peer_conn, peer), peer_conn_broken, peer);