From be14b52423dd2a56a912ebc55b3cdd762611678c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 2 Mar 2018 19:29:17 +1030 Subject: [PATCH] routing: connections are now never null; simplify. Failure and pruning were the two places where a node_connection could be freed; now they both deal with entire channels, we can remove the NULL checks, and the destructor. Signed-off-by: Rusty Russell --- gossipd/gossip.c | 9 ++++----- gossipd/routing.c | 38 +++++--------------------------------- gossipd/routing.h | 10 +++++----- 3 files changed, 14 insertions(+), 43 deletions(-) diff --git a/gossipd/gossip.c b/gossipd/gossip.c index bdfdd128d..4793947e4 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -1349,7 +1349,7 @@ static void gossip_refresh_network(struct daemon *daemon) nc = connection_from(n, n->channels[i]); - if (!nc || !nc->channel_update) { + if (!nc->channel_update) { /* Connection is not public yet, so don't even * try to re-announce it */ continue; @@ -1826,11 +1826,10 @@ static struct io_plan *handle_disable_channel(struct io_conn *conn, } chan = get_channel(daemon->rstate, &scid); - if (!chan || !chan->connections[direction]) { + if (!chan) { status_trace( - "Unable to find channel %s/%d", - type_to_string(msg, struct short_channel_id, &scid), - direction); + "Unable to find channel %s", + type_to_string(msg, struct short_channel_id, &scid)); goto fail; } nc = chan->connections[direction]; diff --git a/gossipd/routing.c b/gossipd/routing.c index a7e0d9142..9a2eb945c 100644 --- a/gossipd/routing.c +++ b/gossipd/routing.c @@ -196,20 +196,6 @@ static void destroy_routing_channel(struct routing_channel *chan, tal_free(chan->nodes[1]); } -static void destroy_node_connection(struct node_connection *nc, - struct routing_channel *chan) -{ - int dir = nc->flags & 0x1; - struct node_connection *c = chan->connections[dir]; - - assert(nc == c); - chan->connections[dir] = NULL; - - /* Both sides deleted? Free channel */ - if (!chan->connections[!dir]) - tal_free(chan); -} - static struct node_connection *new_node_connection(struct routing_state *rstate, struct routing_channel *chan, struct node *from, @@ -237,8 +223,6 @@ static struct node_connection *new_node_connection(struct routing_state *rstate, /* Hook it into in/out arrays. */ chan->connections[idx] = c; - - tal_add_destructor2(c, destroy_node_connection, chan); return c; } @@ -380,7 +364,7 @@ static void bfg_one_edge(struct node *node, /* Determine if the given node_connection is routable */ static bool nc_is_routable(const struct node_connection *nc, time_t now) { - return nc && nc->active && nc->unroutable_until < now; + return nc->active && nc->unroutable_until < now; } /* riskfactor is already scaled to per-block amount */ @@ -916,13 +900,7 @@ void handle_channel_update(struct routing_state *rstate, const u8 *update) c = chan->connections[direction]; - /* Channel could have been pruned: re-add */ - if (!c) { - c = new_node_connection(rstate, chan, - chan->nodes[direction], - chan->nodes[!direction], - direction); - } else if (c->last_timestamp >= timestamp) { + if (c->last_timestamp >= timestamp) { SUPERVERBOSE("Ignoring outdated update."); tal_free(tmpctx); return; @@ -1164,11 +1142,7 @@ static void routing_failure_channel_out(const tal_t *disposal_context, struct routing_channel *chan, time_t now) { - struct node_connection *nc; - - nc = connection_from(node, chan); - if (!nc) - return; + struct node_connection *nc = connection_from(node, chan); /* BOLT #4: * @@ -1304,10 +1278,8 @@ void mark_channel_unroutable(struct routing_state *rstate, tal_free(tmpctx); return; } - if (chan->connections[0]) - chan->connections[0]->unroutable_until = now + 20; - if (chan->connections[1]) - chan->connections[1]->unroutable_until = now + 20; + chan->connections[0]->unroutable_until = now + 20; + chan->connections[1]->unroutable_until = now + 20; tal_free(tmpctx); } diff --git a/gossipd/routing.h b/gossipd/routing.h index 49a49cd28..aa9d17908 100644 --- a/gossipd/routing.h +++ b/gossipd/routing.h @@ -94,7 +94,7 @@ struct routing_channel { struct short_channel_id scid; u8 *txout_script; - /* One of these might be NULL. + /* * connections[0]->src == nodes[0] connections[0]->dst == nodes[1] * connections[1]->src == nodes[1] connections[1]->dst == nodes[0] */ @@ -124,8 +124,8 @@ static inline struct node_connection *connection_from(const struct node *n, { int idx = (chan->nodes[1] == n); - assert(!chan->connections[idx] || chan->connections[idx]->src == n); - assert(!chan->connections[!idx] || chan->connections[!idx]->dst == n); + assert(chan->connections[idx]->src == n); + assert(chan->connections[!idx]->dst == n); return chan->connections[idx]; } @@ -134,8 +134,8 @@ static inline struct node_connection *connection_to(const struct node *n, { int idx = (chan->nodes[1] == n); - assert(!chan->connections[idx] || chan->connections[idx]->src == n); - assert(!chan->connections[!idx] || chan->connections[!idx]->dst == n); + assert(chan->connections[idx]->src == n); + assert(chan->connections[!idx]->dst == n); return chan->connections[!idx]; }