Skip to content

Commit

Permalink
gossmap: implement gossmap_random_node(), use it in gossipd.
Browse files Browse the repository at this point in the history
It's easy for gossmap, since it has access to the htable.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Nov 22, 2024
1 parent 363b721 commit 69c252e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
8 changes: 8 additions & 0 deletions common/gossmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,14 @@ struct gossmap_node *gossmap_next_node(const struct gossmap *map,
return node_iter(map, prev - map->node_arr + 1);
}

struct gossmap_node *gossmap_random_node(const struct gossmap *map)
{
ptrint_t *pi = nodeidx_htable_pick(map->nodes, pseudorand_u64(), NULL);
if (pi)
return ptrint2node(pi);
return NULL;
}

size_t gossmap_num_chans(const struct gossmap *map)
{
return chanidx_htable_count(map->channels);
Expand Down
3 changes: 3 additions & 0 deletions common/gossmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,9 @@ struct gossmap_node *gossmap_first_node(const struct gossmap *map);
struct gossmap_node *gossmap_next_node(const struct gossmap *map,
const struct gossmap_node *prev);

/* Get me a random node */
struct gossmap_node *gossmap_random_node(const struct gossmap *map);

/* Unsorted iterate through (do not add/remove channels or nodes!) */
size_t gossmap_num_chans(const struct gossmap *map);

Expand Down
35 changes: 9 additions & 26 deletions gossipd/seeker.c
Original file line number Diff line number Diff line change
Expand Up @@ -979,41 +979,24 @@ static struct node_and_addrs *get_random_node(const tal_t *ctx,
struct seeker *seeker)
{
struct gossmap *gossmap = gossmap_manage_get_gossmap(seeker->daemon->gm);
if (gossmap_num_nodes(gossmap) < 1)
return NULL;
u32 max_idx = gossmap_max_node_idx(gossmap);
if (max_idx < 1)
struct gossmap_node *node = gossmap_random_node(gossmap);

if (!node)
return NULL;

struct gossmap_node *random_node;
struct node_and_addrs *found_node = NULL;
for (int i = 0; i<20; i++) {
u32 random = pseudorand(max_idx);
random_node = gossmap_node_byidx(gossmap, random);
if (!random_node) {
continue;
}
found_node = tal(ctx, struct node_and_addrs);
struct node_and_addrs *found_node = tal(ctx, struct node_and_addrs);
found_node->id = tal(found_node, struct node_id);
gossmap_node_get_id(gossmap, random_node, found_node->id);
if (node_id_eq(found_node->id, &seeker->daemon->id)) {
found_node = tal_free(found_node);
continue;
}
gossmap_node_get_id(gossmap, node, found_node->id);
found_node->addrs =
gossmap_manage_get_node_addresses(found_node,
seeker->daemon->gm,
found_node->id);
if (!found_node->addrs || tal_count(found_node->addrs) == 0) {
found_node = tal_free(found_node);
continue;
}

break;
if (found_node->addrs && tal_count(found_node->addrs) != 0)
return found_node;
tal_free(found_node);
}

return found_node;

return NULL;
}

/* Ask lightningd for more peers if we're short on gossip streamers. */
Expand Down

0 comments on commit 69c252e

Please sign in to comment.