Browse Source

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.
ppa-0.6.1
Christian Decker 8 years ago
committed by Rusty Russell
parent
commit
7793bd1b9d
  1. 14
      daemon/routing.c
  2. 5
      daemon/routing.h
  3. 22
      lightningd/channel/channel.c

14
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,12 +677,10 @@ 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);
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);

5
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 */

22
lightningd/channel/channel.c

@ -8,6 +8,7 @@
#include <ccan/structeq/structeq.h>
#include <ccan/take/take.h>
#include <ccan/time/time.h>
#include <daemon/routing.h>
#include <errno.h>
#include <inttypes.h>
#include <lightningd/channel.h>
@ -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);

Loading…
Cancel
Save