mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
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 <rusty@rustcorp.com.au>
This commit is contained in:
committed by
Christian Decker
parent
d7b5882f61
commit
be14b52423
@@ -1349,7 +1349,7 @@ static void gossip_refresh_network(struct daemon *daemon)
|
|||||||
|
|
||||||
nc = connection_from(n, n->channels[i]);
|
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
|
/* Connection is not public yet, so don't even
|
||||||
* try to re-announce it */
|
* try to re-announce it */
|
||||||
continue;
|
continue;
|
||||||
@@ -1826,11 +1826,10 @@ static struct io_plan *handle_disable_channel(struct io_conn *conn,
|
|||||||
}
|
}
|
||||||
|
|
||||||
chan = get_channel(daemon->rstate, &scid);
|
chan = get_channel(daemon->rstate, &scid);
|
||||||
if (!chan || !chan->connections[direction]) {
|
if (!chan) {
|
||||||
status_trace(
|
status_trace(
|
||||||
"Unable to find channel %s/%d",
|
"Unable to find channel %s",
|
||||||
type_to_string(msg, struct short_channel_id, &scid),
|
type_to_string(msg, struct short_channel_id, &scid));
|
||||||
direction);
|
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
nc = chan->connections[direction];
|
nc = chan->connections[direction];
|
||||||
|
|||||||
@@ -196,20 +196,6 @@ static void destroy_routing_channel(struct routing_channel *chan,
|
|||||||
tal_free(chan->nodes[1]);
|
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,
|
static struct node_connection *new_node_connection(struct routing_state *rstate,
|
||||||
struct routing_channel *chan,
|
struct routing_channel *chan,
|
||||||
struct node *from,
|
struct node *from,
|
||||||
@@ -237,8 +223,6 @@ static struct node_connection *new_node_connection(struct routing_state *rstate,
|
|||||||
|
|
||||||
/* Hook it into in/out arrays. */
|
/* Hook it into in/out arrays. */
|
||||||
chan->connections[idx] = c;
|
chan->connections[idx] = c;
|
||||||
|
|
||||||
tal_add_destructor2(c, destroy_node_connection, chan);
|
|
||||||
return c;
|
return c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -380,7 +364,7 @@ static void bfg_one_edge(struct node *node,
|
|||||||
/* Determine if the given node_connection is routable */
|
/* Determine if the given node_connection is routable */
|
||||||
static bool nc_is_routable(const struct node_connection *nc, time_t now)
|
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 */
|
/* 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];
|
c = chan->connections[direction];
|
||||||
|
|
||||||
/* Channel could have been pruned: re-add */
|
if (c->last_timestamp >= timestamp) {
|
||||||
if (!c) {
|
|
||||||
c = new_node_connection(rstate, chan,
|
|
||||||
chan->nodes[direction],
|
|
||||||
chan->nodes[!direction],
|
|
||||||
direction);
|
|
||||||
} else if (c->last_timestamp >= timestamp) {
|
|
||||||
SUPERVERBOSE("Ignoring outdated update.");
|
SUPERVERBOSE("Ignoring outdated update.");
|
||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
return;
|
return;
|
||||||
@@ -1164,11 +1142,7 @@ static void routing_failure_channel_out(const tal_t *disposal_context,
|
|||||||
struct routing_channel *chan,
|
struct routing_channel *chan,
|
||||||
time_t now)
|
time_t now)
|
||||||
{
|
{
|
||||||
struct node_connection *nc;
|
struct node_connection *nc = connection_from(node, chan);
|
||||||
|
|
||||||
nc = connection_from(node, chan);
|
|
||||||
if (!nc)
|
|
||||||
return;
|
|
||||||
|
|
||||||
/* BOLT #4:
|
/* BOLT #4:
|
||||||
*
|
*
|
||||||
@@ -1304,10 +1278,8 @@ void mark_channel_unroutable(struct routing_state *rstate,
|
|||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (chan->connections[0])
|
chan->connections[0]->unroutable_until = now + 20;
|
||||||
chan->connections[0]->unroutable_until = now + 20;
|
chan->connections[1]->unroutable_until = now + 20;
|
||||||
if (chan->connections[1])
|
|
||||||
chan->connections[1]->unroutable_until = now + 20;
|
|
||||||
tal_free(tmpctx);
|
tal_free(tmpctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ struct routing_channel {
|
|||||||
struct short_channel_id scid;
|
struct short_channel_id scid;
|
||||||
u8 *txout_script;
|
u8 *txout_script;
|
||||||
|
|
||||||
/* One of these might be NULL.
|
/*
|
||||||
* connections[0]->src == nodes[0] connections[0]->dst == nodes[1]
|
* connections[0]->src == nodes[0] connections[0]->dst == nodes[1]
|
||||||
* connections[1]->src == nodes[1] connections[1]->dst == nodes[0]
|
* 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);
|
int idx = (chan->nodes[1] == n);
|
||||||
|
|
||||||
assert(!chan->connections[idx] || chan->connections[idx]->src == n);
|
assert(chan->connections[idx]->src == n);
|
||||||
assert(!chan->connections[!idx] || chan->connections[!idx]->dst == n);
|
assert(chan->connections[!idx]->dst == n);
|
||||||
return chan->connections[idx];
|
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);
|
int idx = (chan->nodes[1] == n);
|
||||||
|
|
||||||
assert(!chan->connections[idx] || chan->connections[idx]->src == n);
|
assert(chan->connections[idx]->src == n);
|
||||||
assert(!chan->connections[!idx] || chan->connections[!idx]->dst == n);
|
assert(chan->connections[!idx]->dst == n);
|
||||||
return chan->connections[!idx];
|
return chan->connections[!idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user