Skip to content

Commit

Permalink
devtools/gossmap-compress: allow setting the nodeid explicitly for ge…
Browse files Browse the repository at this point in the history
…nerated nodes.

This lets us make gossip which contains "real" nodes.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Aug 4, 2024
1 parent 439289b commit 71af5d9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 16 deletions.
51 changes: 39 additions & 12 deletions devtools/gossmap-compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,16 @@ static u64 get_delay(struct gossmap *gossmap,
return chan->half[dir].delay;
}

static void pubkey_for_node(size_t nodeidx, struct pubkey *key)
static void pubkey_for_node(size_t nodeidx, struct pubkey *key,
const struct pubkey *node_ids)
{
struct secret seckey;

if (nodeidx < tal_count(node_ids)) {
*key = node_ids[nodeidx];
return;
}

memset(&seckey, 1, sizeof(seckey));
memcpy(&seckey, &nodeidx, sizeof(nodeidx));
if (!pubkey_from_secret(&seckey, key))
Expand Down Expand Up @@ -324,7 +330,8 @@ static void write_announce(int outfd,
size_t node1,
size_t node2,
u64 capacity,
size_t i)
size_t i,
const struct pubkey *node_ids)
{
struct {
secp256k1_ecdsa_signature sig;
Expand All @@ -336,8 +343,8 @@ static void write_announce(int outfd,
struct node_id nodeid1, nodeid2;

memset(&vals, 0, sizeof(vals));
pubkey_for_node(node1, &id1);
pubkey_for_node(node2, &id2);
pubkey_for_node(node1, &id1, node_ids);
pubkey_for_node(node2, &id2, node_ids);

/* Nodes in pubkey order */
if (pubkey_cmp(&id1, &id2) < 0) {
Expand Down Expand Up @@ -385,7 +392,8 @@ static void write_update(int outfd,
u64 htlc_min, u64 htlc_max,
u64 basefee,
u32 propfee,
u16 delay)
u16 delay,
const struct pubkey *node_ids)
{
struct vals {
secp256k1_ecdsa_signature sig;
Expand All @@ -404,8 +412,8 @@ static void write_update(int outfd,
abort();

/* If node ids are backward, dir is reversed */
pubkey_for_node(node1, &id1);
pubkey_for_node(node2, &id2);
pubkey_for_node(node1, &id1, node_ids);
pubkey_for_node(node2, &id2, node_ids);
if (pubkey_cmp(&id1, &id2) > 0)
dir = !dir;

Expand Down Expand Up @@ -467,14 +475,32 @@ static char *opt_add_one(unsigned int *val)
return NULL;
}

static char *opt_nodes(const char *optarg, struct pubkey **node_ids)
{
char **ids = tal_strsplit(tmpctx, optarg, ",", STR_EMPTY_OK);

for (size_t i = 0; ids[i]; i++) {
struct pubkey n;
if (!pubkey_from_hexstr(ids[i], strlen(ids[i]), &n))
return tal_fmt(tmpctx, "Invalid node id '%s'", ids[i]);
tal_arr_expand(node_ids, n);
}
return NULL;
}

int main(int argc, char *argv[])
{
int infd, outfd;
struct pubkey *node_ids;

common_setup(argv[0]);
setup_locale();

node_ids = tal_arr(tmpctx, struct pubkey, 0);
opt_register_noarg("--verbose|-v", opt_add_one, &verbose,
"Print details (each additional gives more!).");
opt_register_arg("--nodes", opt_nodes, NULL, &node_ids,
"Comma separated node ids to give first nodes.");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"[decompress|compress] infile outfile"
"Compress or decompress a gossmap file",
Expand Down Expand Up @@ -643,15 +669,15 @@ int main(int argc, char *argv[])
/* Useful so they can map their ids back to node ids. */
for (size_t i = 0; i < node_limit; i++) {
struct pubkey node_id;
pubkey_for_node(i, &node_id);
pubkey_for_node(i, &node_id, node_ids);
printf("%s\n", fmt_pubkey(tmpctx, &node_id));
}

if (verbose >= 2) {
for (size_t i = 0; i < channel_count; i++) {
struct pubkey id1, id2;
pubkey_for_node(chans[i].node1, &id1);
pubkey_for_node(chans[i].node2, &id2);
pubkey_for_node(chans[i].node1, &id1, node_ids);
pubkey_for_node(chans[i].node2, &id2, node_ids);
printf("Channel %zu: %s -> %s\n",
i,
fmt_pubkey(tmpctx, &id1),
Expand Down Expand Up @@ -715,7 +741,7 @@ int main(int argc, char *argv[])
chans[i].node1,
chans[i].node2,
chans[i].capacity,
i);
i, node_ids);
for (size_t dir = 0; dir < 2; dir++) {
write_update(outfd,
chans[i].node1, chans[i].node2, i, dir,
Expand All @@ -724,7 +750,8 @@ int main(int argc, char *argv[])
chans[i].half[dir].htlc_max,
chans[i].half[dir].basefee,
chans[i].half[dir].propfee,
chans[i].half[dir].delay);
chans[i].half[dir].delay,
node_ids);
}
}
gzclose(inf);
Expand Down
6 changes: 3 additions & 3 deletions plugins/askrene/askrene.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static fp16_t *get_capacities(const tal_t *ctx,
* channels. This wouldn't be right if we looped back through ourselves,
* but we won't. */
/* FIXME: We could cache this until gossmap changes... */
static void add_free_source(struct command *cmd,
static void add_free_source(struct plugin *plugin,
struct gossmap *gossmap,
struct gossmap_localmods *localmods,
const struct node_id *source)
Expand Down Expand Up @@ -206,7 +206,7 @@ static void add_free_source(struct command *cmd,
/* Keep enabled flag */
c->half[dir].enabled,
dir))
plugin_err(cmd->plugin, "Could not zero fee on local %s",
plugin_err(plugin, "Could not zero fee on local %s",
fmt_short_channel_id(tmpctx, scid));
}
}
Expand Down Expand Up @@ -262,7 +262,7 @@ static const char *get_routes(struct command *cmd,
}

if (have_layer(layers, "auto.sourcefree"))
add_free_source(cmd, askrene->gossmap, localmods, source);
add_free_source(cmd->plugin, askrene->gossmap, localmods, source);

/* Clear scids with reservations, too, so we don't have to look up
* all the time! */
Expand Down
3 changes: 2 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def __init__(self, node1, node2, capacity_sats=1000000, forward=None, reverse=No
self.half = [forward, reverse]


def generate_gossip_store(channels):
def generate_gossip_store(channels, nodeids=[]):
"""Returns a gossip store file with the given channels in it, and a map of node labels -> ids
"""
nodes = []
Expand Down Expand Up @@ -550,6 +550,7 @@ def write_dumb_template(outf, channels, propname, illegalvals=[]):

outfile = tempfile.NamedTemporaryFile(prefix='gossip-store-')
nodeids = subprocess.check_output(['devtools/gossmap-compress',
'--nodes={}'.format(','.join(nodeids)),
'decompress',
cfile.name,
outfile.name]).decode('utf-8').splitlines()
Expand Down

0 comments on commit 71af5d9

Please sign in to comment.