From 8f16f6ca099303efdcb24d8eec3ab49673f6530e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 13 Jan 2018 12:30:52 +0100 Subject: [PATCH] gossip: Forget nodes that have no associated channels anymore Signed-off-by: Christian Decker --- gossipd/gossip.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/gossipd/gossip.c b/gossipd/gossip.c index 2108763a6..88c2dd7b1 100644 --- a/gossipd/gossip.c +++ b/gossipd/gossip.c @@ -1254,6 +1254,8 @@ static void gossip_prune_network(struct daemon *daemon) struct node_map_iter it; /* Anything below this highwater mark ought to be pruned */ s64 highwater = now - 2*daemon->update_channel_interval; + struct node *n; + struct node **prunednodes; /* Schedule next run now */ new_reltimer(&daemon->timers, daemon, @@ -1261,7 +1263,7 @@ static void gossip_prune_network(struct daemon *daemon) gossip_prune_network, daemon); /* Find myself in the network */ - struct node *n = node_map_get(daemon->rstate->nodes, &daemon->id.pubkey); + n = node_map_get(daemon->rstate->nodes, &daemon->id.pubkey); if (!n) { status_trace("Could not find myself in the nodemap, do we have a channel yet?"); return; @@ -1318,6 +1320,26 @@ static void gossip_prune_network(struct daemon *daemon) tal_free(nc); } } + + /* Finally remove all nodes that do not have any edges + * anymore. Accumulate into prunednodes, and delete in the + * second loop. */ + prunednodes = tal_arr(n, struct node*, 0); + for (n = node_map_first(daemon->rstate->nodes, &it); n; + n = node_map_next(daemon->rstate->nodes, &it)) { + if (tal_count(n->in) == 0 && tal_count(n->out) == 0) { + size_t count = tal_count(prunednodes); + tal_resize(&prunednodes, count + 1); + prunednodes[count] = n; + } + } + + for (size_t i=0; irstate->nodes, prunednodes[i]); + tal_free(prunednodes[i]); + } + + tal_free(prunednodes); } static struct io_plan *connection_in(struct io_conn *conn, struct daemon *daemon)