forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossmap-compress.c
832 lines (740 loc) · 24.2 KB
/
gossmap-compress.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#include "config.h"
#include <bitcoin/privkey.h>
#include <bitcoin/pubkey.h>
#include <ccan/asort/asort.h>
#include <ccan/cast/cast.h>
#include <ccan/crc32c/crc32c.h>
#include <ccan/err/err.h>
#include <ccan/mem/mem.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/tal/str/str.h>
#include <common/bigsize.h>
#include <common/gossip_store.h>
#include <common/gossmap.h>
#include <common/setup.h>
#include <errno.h>
#include <fcntl.h>
#include <gossipd/gossip_store_wiregen.h>
#include <stdio.h>
#include <unistd.h>
#include <wire/peer_wiregen.h>
#if HAVE_ZLIB
#include <zlib.h>
#else
/* Worst... zlib... ever! */
#define gzFile int
#define gzdopen(fd, mode) (fd)
#define gzclose(outf) close(outf)
static int gzread(int fd, void *buf, size_t len)
{
if (read_all(fd, buf, len))
return len;
return 0;
}
static int gzwrite(int fd, const void *buf, size_t len)
{
if (write_all(fd, buf, len))
return len;
return 0;
}
#endif
static unsigned int verbose = 0;
/* All {numbers} are bigsize.
*
* <FILE> := <HEADER> <CHANNEL_ENDS> <CAPACITIES> <DISABLEDS> <HTLC_MINS> <HTLC_MAXS> <BASEFEES> <PROPFEES> <DELAYS>
* <HEADER> := "GOSSMAP_COMPRESSv1\0"
* <CHANNEL_ENDS> := {channel_count} {start_nodeidx}*{channel_count} {end_nodeidx}*{channel_count}
* This describes each attached channel, eg if there are two
* channels, node 0 to node 1 and node 0 to node 2, this would be:
* 2 0 0 1 2
*
* <DISABLEDS> := <DISABLED>* {channel_count*2}
* <DISABLED> := {chanidx}*2+{direction}
* Selection of disabled channels and directions, expected to only be a few. Indexes into the
* first channel_ends array. Terminated by invalid index.
*
* <CAPACITIES> := <CAPACITY_TEMPLATES> {channel_count}*{capacity_idx}
* <CAPACITY_TEMPLATES> := {capacity_count} {channel_count}*{capacity}
* This is one satoshi amount per channel.
*
* <HTLC_MINS> := <HTLC_MIN_TEMPLATES> {channel_count*2}*{htlc_min_idx}
* <HTLC_MIN_TEMPLATES> := {htlc_min_count} {htlc_min_count}*{htlc_min}
* These templates are all of the same form. A set of values, followed by
* an index into these values for each direction of each channel, in order
* 1. 0'th channel 1st direction
* 2. 0'th channel 2nd direction
* 3. 1'st channel 1st direction
* 4. 1'st channel 2nd direction
*
* <HTLC_MAXS> := <HTLC_MAX_TEMPLATES> {channel_count*2}*{htlc_max_idx}
* Note that values 0 and 1 are special: 0 == channel capacity, 1 == 0.99 * channel capacity.
* <HTLC_MAX_TEMPLATES> := {htlc_max_count} {htlc_max_count}*{htlc_max}
* <BASEFEES> := <BASEFEE_TEMPLATES> {channel_count*2}*{basefee_idx}
* <BASEFEE_TEMPLATES> := {basefee_count} {basefee_count}*{basefee}
* <PROPFEES> := <PROPFEE_TEMPLATES> {channel_count*2}*{propfee_idx}
* <PROPFEE_TEMPLATES> := {propfee_count} {propfee_count}*{propfee}
* <DELAYS> := <DELAY_TEMPLATES> {channel_count*2}*{delay_idx}
* <DELAY_TEMPLATES> := {delay_count} {delay_count}*{delay}
*/
#define GC_HEADER "GOSSMAP_COMPRESSv1"
#define GC_HEADERLEN (sizeof(GC_HEADER))
#define GOSSIP_STORE_VER ((0 << 5) | 14)
/* Backwards, we want larger first */
static int cmp_node_num_chans(struct gossmap_node *const *a,
struct gossmap_node *const *b,
void *unused)
{
return (int)(*b)->num_chans - (int)(*a)->num_chans;
}
static void write_bigsize(gzFile outf, u64 val)
{
u8 buf[BIGSIZE_MAX_LEN];
size_t len;
len = bigsize_put(buf, val);
if (gzwrite(outf, buf, len) == 0)
err(1, "Writing bigsize");
}
static u64 read_bigsize(gzFile inf)
{
u64 val;
u8 buf[BIGSIZE_MAX_LEN];
if (gzread(inf, buf, 1) != 1)
errx(1, "Reading bigsize");
switch (buf[0]) {
case 0xfd:
if (gzread(inf, buf+1, 2) != 2)
errx(1, "Reading bigsize");
break;
case 0xfe:
if (gzread(inf, buf+1, 4) != 4)
errx(1, "Reading bigsize");
break;
case 0xff:
if (gzread(inf, buf+1, 8) != 8)
errx(1, "Reading bigsize");
break;
}
if (bigsize_get(buf, sizeof(buf), &val) == 0)
errx(1, "Bad bigsize");
return val;
}
static int cmp_u64(const u64 *a,
const u64 *b,
void *unused)
{
if (*a > *b)
return 1;
else if (*a < *b)
return -1;
return 0;
}
static const u64 *deduplicate(const tal_t *ctx, const u64 *vals)
{
u64 *sorted;
u64 *dedup;
size_t n;
/* Sort and remove dups */
sorted = tal_dup_talarr(tmpctx, u64, vals);
asort(sorted, tal_count(sorted), cmp_u64, NULL);
dedup = tal_arr(ctx, u64, tal_count(sorted));
n = 0;
dedup[n++] = sorted[0];
for (size_t i = 1; i < tal_count(sorted); i++) {
if (sorted[i] == dedup[n-1])
continue;
dedup[n++] = sorted[i];
}
tal_resize(&dedup, n);
return dedup;
}
static size_t find_index(const u64 *template, u64 val)
{
for (size_t i = 0; i < tal_count(template); i++) {
if (template[i] == val)
return i;
}
abort();
}
/* All templates are of the same form. Output all the distinct values, then
* write out which one is used by each channel */
static void write_template_and_values(gzFile outf, const u64 *vals, const char *what)
{
/* Sort and remove dups */
const u64 *template = deduplicate(tmpctx, vals);
if (verbose)
printf("%zu unique %s\n", tal_count(template), what);
assert(tal_count(vals) >= tal_count(template));
/* Write template. */
write_bigsize(outf, tal_count(template));
for (size_t i = 0; i < tal_count(template); i++)
write_bigsize(outf, template[i]);
/* Tie every channel into the template. O(N^2) but who
* cares? */
for (size_t i = 0; i < tal_count(vals); i++) {
write_bigsize(outf, find_index(template, vals[i]));
}
}
static void write_bidir_perchan(gzFile outf,
struct gossmap *gossmap,
struct gossmap_chan **chans,
u64 (*get_value)(struct gossmap *,
const struct gossmap_chan *,
int),
const char *what)
{
u64 *vals = tal_arr(tmpctx, u64, tal_count(chans) * 2);
for (size_t i = 0; i < tal_count(chans); i++) {
for (size_t dir = 0; dir < 2; dir++) {
if (chans[i]->half[dir].enabled)
vals[i*2+dir] = get_value(gossmap, chans[i], dir);
else
vals[i*2+dir] = 0;
}
}
write_template_and_values(outf, vals, what);
}
static u64 get_htlc_min(struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
struct amount_msat msat;
gossmap_chan_get_update_details(gossmap, chan, dir,
NULL, NULL, NULL, NULL, NULL, NULL, &msat, NULL);
return msat.millisatoshis; /* Raw: compressed format */
}
static u64 get_htlc_max(struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
struct amount_msat msat, capacity_msat;
capacity_msat = gossmap_chan_get_capacity(gossmap, chan);
gossmap_chan_get_update_details(gossmap, chan, dir,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, &msat);
/* Special value for the common case of "max_htlc == capacity" */
if (amount_msat_eq(msat, capacity_msat)) {
return 0;
}
/* Other common case: "max_htlc == 99% capacity" */
if (amount_msat_scale(&capacity_msat, capacity_msat, 0.99)
&& amount_msat_eq(msat, capacity_msat)) {
return 1;
}
return msat.millisatoshis; /* Raw: compressed format */
}
static u64 get_basefee(struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
u32 basefee;
gossmap_chan_get_update_details(gossmap, chan, dir,
NULL, NULL, NULL, NULL, &basefee, NULL, NULL, NULL);
return basefee;
}
static u64 get_propfee(struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
u32 propfee;
gossmap_chan_get_update_details(gossmap, chan, dir,
NULL, NULL, NULL, NULL, NULL, &propfee, NULL, NULL);
return propfee;
}
static u64 get_delay(struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
return chan->half[dir].delay;
}
static void pubkey_for_node(u64 nodeidx, struct pubkey *key,
const struct pubkey **node_ids)
{
struct secret seckey;
if (nodeidx < tal_count(node_ids) && node_ids[nodeidx]) {
*key = *node_ids[nodeidx];
return;
}
memset(&seckey, 1, sizeof(seckey));
memcpy(&seckey, &nodeidx, sizeof(nodeidx));
if (!pubkey_from_secret(&seckey, key))
abort();
}
static void write_msg_to_gstore(int outfd, const u8 *msg TAKES)
{
struct gossip_hdr hdr;
hdr.flags = 0;
hdr.len = cpu_to_be16(tal_bytelen(msg));
hdr.timestamp = 0;
hdr.crc = cpu_to_be32(crc32c(0, msg, tal_bytelen(msg)));
if (!write_all(outfd, &hdr, sizeof(hdr))
|| !write_all(outfd, msg, tal_bytelen(msg))) {
err(1, "Writing gossip_store");
}
if (taken(msg))
tal_free(msg);
}
/* BOLT #7:
* 1. type: 256 (`channel_announcement`)
* 2. data:
* * [`signature`:`node_signature_1`]
* * [`signature`:`node_signature_2`]
* * [`signature`:`bitcoin_signature_1`]
* * [`signature`:`bitcoin_signature_2`]
* * [`u16`:`len`]
* * [`len*byte`:`features`]
* * [`chain_hash`:`chain_hash`]
* * [`short_channel_id`:`short_channel_id`]
* * [`point`:`node_id_1`]
* * [`point`:`node_id_2`]
* * [`point`:`bitcoin_key_1`]
* * [`point`:`bitcoin_key_2`]
*/
static void write_announce(int outfd,
size_t node1,
size_t node2,
u64 capacity,
size_t i,
const struct pubkey **node_ids)
{
struct {
secp256k1_ecdsa_signature sig;
struct bitcoin_blkid chain_hash;
} vals;
u8 *msg;
struct short_channel_id scid;
struct pubkey id1, id2;
struct node_id nodeid1, nodeid2;
memset(&vals, 0, sizeof(vals));
pubkey_for_node(node1, &id1, node_ids);
pubkey_for_node(node2, &id2, node_ids);
/* Nodes in pubkey order */
if (pubkey_cmp(&id1, &id2) < 0) {
node_id_from_pubkey(&nodeid1, &id1);
node_id_from_pubkey(&nodeid2, &id2);
} else {
node_id_from_pubkey(&nodeid1, &id2);
node_id_from_pubkey(&nodeid2, &id1);
}
/* Use i to avoid clashing scids even if two nodes have > 1 channel */
if (!mk_short_channel_id(&scid, node1, node2, i & 0xFFFF))
abort();
msg = towire_channel_announcement(NULL, &vals.sig, &vals.sig, &vals.sig, &vals.sig,
NULL, &vals.chain_hash, scid,
&nodeid1, &nodeid2,
&id1, &id1);
write_msg_to_gstore(outfd, take(msg));
msg = towire_gossip_store_channel_amount(NULL, amount_sat(capacity));
write_msg_to_gstore(outfd, take(msg));
}
/* BOLT #7:
* 1. type: 258 (`channel_update`)
* 2. data:
* * [`signature`:`signature`]
* * [`chain_hash`:`chain_hash`]
* * [`short_channel_id`:`short_channel_id`]
* * [`u32`:`timestamp`]
* * [`byte`:`message_flags`]
* * [`byte`:`channel_flags`]
* * [`u16`:`cltv_expiry_delta`]
* * [`u64`:`htlc_minimum_msat`]
* * [`u32`:`fee_base_msat`]
* * [`u32`:`fee_proportional_millionths`]
* * [`u64`:`htlc_maximum_msat`]
*/
static void write_update(int outfd,
size_t node1,
size_t node2,
size_t i,
int dir,
bool disabled,
u64 htlc_min, u64 htlc_max,
u64 basefee,
u32 propfee,
u16 delay,
const struct pubkey **node_ids)
{
struct vals {
secp256k1_ecdsa_signature sig;
struct bitcoin_blkid chain_hash;
u32 timestamp;
} vals;
u8 *msg;
u8 message_flags, channel_flags;
struct pubkey id1, id2;
struct short_channel_id scid;
memset(&vals, 0, sizeof(vals));
/* Use i to avoid clashing scids even if two nodes have > 1 channel */
if (!mk_short_channel_id(&scid, node1, node2, i & 0xFFFF))
abort();
/* If node ids are backward, dir is reversed */
pubkey_for_node(node1, &id1, node_ids);
pubkey_for_node(node2, &id2, node_ids);
if (pubkey_cmp(&id1, &id2) > 0)
dir = !dir;
/* BOLT #7:
* The `channel_flags` bitfield is used to indicate the direction of
* the channel: it identifies the node that this update originated
* from and signals various options concerning the channel. The
* following table specifies the meaning of its individual bits:
*
* | Bit Position | Name | Meaning |
* | ------------- | ----------- | -------------------------------- |
* | 0 | `direction` | Direction this update refers to. |
* | 1 | `disable` | Disable the channel. |
*
* The `message_flags` bitfield is used to provide additional details about the message:
*
* | Bit Position | Name |
* | ------------- | ---------------|
* | 0 | `must_be_one` |
* | 1 | `dont_forward` |
*/
channel_flags = dir ? 1 : 0;
if (disabled)
channel_flags |= 2;
message_flags = 1;
msg = towire_channel_update(NULL, &vals.sig, &vals.chain_hash, scid,
0, message_flags, channel_flags,
delay,
amount_msat(htlc_min),
basefee, propfee,
amount_msat(htlc_max));
write_msg_to_gstore(outfd, take(msg));
}
static const u64 *read_template(const tal_t *ctx, gzFile inf, const char *what)
{
size_t count = read_bigsize(inf);
u64 *template = tal_arr(ctx, u64, count);
for (size_t i = 0; i < count; i++)
template[i] = read_bigsize(inf);
if (verbose)
printf("%zu unique %s\n", count, what);
return template;
}
static u64 read_val(gzFile inf, const u64 *template)
{
size_t idx = read_bigsize(inf);
assert(idx < tal_count(template));
return template[idx];
}
static char *opt_add_one(unsigned int *val)
{
(*val)++;
return NULL;
}
static char *opt_node(const char *optarg, const struct pubkey ***node_ids)
{
long num;
char *endl;
struct pubkey *pk;
num = strtol(optarg, &endl, 10);
if (*endl != '=')
return tal_fmt(tmpctx, "Must be of form NUMBER '=' NODEID");
if (tal_count(*node_ids) <= num)
tal_resizez(node_ids, num + 1);
(*node_ids)[num] = pk = tal(*node_ids, struct pubkey);
if (!pubkey_from_hexstr(endl + 1, strlen(endl + 1), pk))
return tal_fmt(tmpctx, "Invalid node id '%s'", endl + 1);
return NULL;
}
static const char *get_alias(const tal_t *ctx,
const struct gossmap *gossmap,
const struct gossmap_node *n)
{
const u8 *ann = gossmap_node_get_announce(tmpctx, gossmap, n);
secp256k1_ecdsa_signature signature;
u8 *features;
u32 timestamp;
struct node_id node_id;
u8 rgb_color[3];
u8 alias[32];
u8 *addresses;
struct tlv_node_ann_tlvs *tlvs;
if (!fromwire_node_announcement(tmpctx, ann, &signature, &features, ×tamp,
&node_id, rgb_color, alias, &addresses,
&tlvs))
return "";
return tal_strndup(ctx, (const char *)alias, 32);
}
static void cupdate_bad(struct gossmap *map,
const struct short_channel_id_dir *scidd,
u16 cltv_expiry_delta,
u32 fee_base_msat,
u32 fee_proportional_millionths,
void *unused)
{
warnx("Bad cupdate for %s, ignoring (delta=%u, fee=%u/%u)",
fmt_short_channel_id_dir(tmpctx, scidd),
cltv_expiry_delta, fee_base_msat, fee_proportional_millionths);
}
int main(int argc, char *argv[])
{
int infd, outfd;
const struct pubkey **node_ids;
bool print_nodes = false;
common_setup(argv[0]);
setup_locale();
node_ids = tal_arr(tmpctx, const struct pubkey *, 0);
opt_register_noarg("--verbose|-v", opt_add_one, &verbose,
"Print details (each additional gives more!).");
opt_register_arg("--node-map=num=<nodeid>", opt_node, NULL, &node_ids,
"Map node num to <nodeid> (decompress only)");
opt_register_noarg("--output-node-map", opt_set_bool, &print_nodes,
"Output nodenumber:nodeid:alias for each node (compress only)");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"[decompress|compress] infile outfile\n"
"Compress or decompress a gossmap file",
"Print this message.");
opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc != 4)
opt_usage_exit_fail("Needs 4 arguments");
infd = open(argv[2], O_RDONLY);
if (infd < 0)
opt_usage_exit_fail(tal_fmt(tmpctx, "Cannot open %s for reading: %s",
argv[2], strerror(errno)));
outfd = open(argv[3], O_WRONLY|O_CREAT|O_TRUNC, 0666);
if (outfd < 0)
opt_usage_exit_fail(tal_fmt(tmpctx, "Cannot open %s for writing: %s",
argv[3], strerror(errno)));
if (streq(argv[1], "compress")) {
struct gossmap_node **nodes, *n;
size_t *node_to_compr_idx;
size_t node_count, channel_count;
struct gossmap_chan **chans, *c;
bool *dirs;
gzFile outf = gzdopen(outfd, "wb9");
struct gossmap *gossmap = gossmap_load_fd(tmpctx, infd, cupdate_bad, NULL, NULL);
if (!gossmap)
opt_usage_and_exit("Cannot read gossmap");
nodes = tal_arr(gossmap, struct gossmap_node *, gossmap_max_node_idx(gossmap));
for (node_count = 0, n = gossmap_first_node(gossmap);
n;
n = gossmap_next_node(gossmap, n), node_count++) {
nodes[node_count] = n;
}
tal_resize(&nodes, node_count);
if (verbose)
printf("%zu nodes\n", node_count);
/* nodes with most channels go first */
asort(nodes, tal_count(nodes), cmp_node_num_chans, NULL);
/* Create map of gossmap index to compression index */
node_to_compr_idx = tal_arr(nodes, size_t, gossmap_max_node_idx(gossmap));
for (size_t i = 0; i < tal_count(nodes); i++) {
node_to_compr_idx[gossmap_node_idx(gossmap, nodes[i])] = i;
if (print_nodes) {
struct node_id node_id;
gossmap_node_get_id(gossmap, nodes[i], &node_id);
printf("%zu:%s:%s\n",
i,
fmt_node_id(tmpctx, &node_id),
get_alias(tmpctx, gossmap, nodes[i]));
}
}
if (gzwrite(outf, GC_HEADER, GC_HEADERLEN) == 0)
err(1, "Writing header");
/* Now, output channels. First get exact count. */
for (channel_count = 0, c = gossmap_first_chan(gossmap);
c;
c = gossmap_next_chan(gossmap, c)) {
channel_count++;
}
if (verbose)
printf("%zu channels\n", channel_count);
chans = tal_arr(gossmap, struct gossmap_chan *, channel_count);
dirs = tal_arr(gossmap, bool, channel_count);
/* * <CHANNEL_ENDS> := {channel_count} {start_nodeidx}*{channel_count} {end_nodeidx}*{channel_count} */
write_bigsize(outf, channel_count);
size_t chanidx = 0;
/* We iterate nodes to get to channels. This gives us nicer ordering for compression */
for (size_t i = 0; i < tal_count(nodes); i++) {
n = nodes[i];
for (size_t j = 0; j < n->num_chans; j++) {
const struct gossmap_node *peer;
int dir;
c = gossmap_nth_chan(gossmap, n, j, &dir);
peer = gossmap_nth_node(gossmap, c, !dir);
/* Don't write if peer already wrote it! */
/* FIXME: What about self-channels? */
if (node_to_compr_idx[gossmap_node_idx(gossmap, peer)] < i)
continue;
write_bigsize(outf, node_to_compr_idx[gossmap_node_idx(gossmap, n)]);
assert(chanidx < channel_count);
dirs[chanidx] = dir;
chans[chanidx] = c;
chanidx++;
}
}
assert(chanidx == channel_count);
/* Now write out the other ends of the channels */
for (size_t i = 0; i < channel_count; i++) {
const struct gossmap_node *peer;
peer = gossmap_nth_node(gossmap, chans[i], !dirs[i]);
write_bigsize(outf, node_to_compr_idx[gossmap_node_idx(gossmap, peer)]);
}
/* <DISABLEDS> := <DISABLED>* {channel_count*2} */
/* <DISABLED> := {chanidx}*2+{direction} */
size_t num_disabled = 0;
size_t num_unknown = 0;
for (size_t i = 0; i < channel_count; i++) {
for (size_t dir = 0; dir < 2; dir++) {
if (chans[i]->cupdate_off[dir] == 0)
num_unknown++;
if (!chans[i]->half[dir].enabled) {
write_bigsize(outf, i * 2 + dir);
num_disabled++;
}
}
}
write_bigsize(outf, channel_count * 2);
if (verbose)
printf("%zu disabled channels (%zu no update)\n", num_disabled, num_unknown);
/* <CAPACITIES> := <CAPACITY_TEMPLATES> {channel_count}*{capacity_idx} */
/* <CAPACITY_TEMPLATES> := {capacity_count} {capacity_count}*{capacity} */
u64 *vals = tal_arr(chans, u64, channel_count);
for (size_t i = 0; i < channel_count; i++) {
struct amount_msat cap;
cap = gossmap_chan_get_capacity(gossmap, chans[i]);
vals[i] = cap.millisatoshis / 1000; /* Raw: compression format */
}
write_template_and_values(outf, vals, "capacities");
/* These are all of same form: one entry per direction per channel */
/* <HTLC_MINS> := <HTLC_MIN_TEMPLATES> {channel_count}*{htlc_min_idx} */
/* <HTLC_MIN_TEMPLATES> := {htlc_min_count} {htlc_min_count}*{htlc_min} */
/* <HTLC_MAXS> := <HTLC_MAX_TEMPLATES> {channel_count}*{htlc_max_idx} */
/* <HTLC_MAX_TEMPLATES> := {htlc_max_count} {htlc_max_count}*{htlc_max} */
/* <BASEFEES> := <BASEFEE_TEMPLATES> {channel_count}*{basefee_idx} */
/* <BASEFEE_TEMPLATES> := {basefee_count} {basefee_count}*{basefee} */
/* <PROPFEES> := <PROPFEE_TEMPLATES> {channel_count}*{propfee_idx} */
/* <PROPFEE_TEMPLATES> := {propfee_count} {propfee_count}*{propfee} */
/* <DELAYS> := <DELAY_TEMPLATES> {channel_count}*{delay_idx} */
/* <DELAY_TEMPLATES> := {delay_count} {delay_count}*{delay} */
write_bidir_perchan(outf, gossmap, chans, get_htlc_min, "htlc_min");
write_bidir_perchan(outf, gossmap, chans, get_htlc_max, "htlc_max");
write_bidir_perchan(outf, gossmap, chans, get_basefee, "basefee");
write_bidir_perchan(outf, gossmap, chans, get_propfee, "propfee");
write_bidir_perchan(outf, gossmap, chans, get_delay, "delay");
gzclose(outf);
} else if (streq(argv[1], "decompress")) {
char hdr[GC_HEADERLEN];
size_t channel_count, chanidx;
const u64 *template;
struct fakechan {
size_t node1, node2;
u64 capacity;
struct halffake {
u64 htlc_min, htlc_max;
u32 basefee, propfee;
u32 delay;
bool disabled;
} half[2];
} *chans;
const u8 version = GOSSIP_STORE_VER;
size_t disabled_count, node_limit;
gzFile inf = gzdopen(infd, "rb");
if (gzread(inf, hdr, sizeof(hdr)) != sizeof(hdr)
|| !memeq(hdr, sizeof(hdr), GC_HEADER, GC_HEADERLEN))
errx(1, "Not a valid compressed gossmap header");
channel_count = read_bigsize(inf);
if (verbose)
printf("%zu channels\n", channel_count);
chans = tal_arrz(tmpctx, struct fakechan, channel_count);
node_limit = 0;
for (size_t i = 0; i < channel_count; i++) {
chans[i].node1 = read_bigsize(inf);
if (chans[i].node1 >= node_limit)
node_limit = chans[i].node1 + 1;
}
for (size_t i = 0; i < channel_count; i++) {
chans[i].node2 = read_bigsize(inf);
if (chans[i].node2 >= node_limit)
node_limit = chans[i].node2 + 1;
}
/* 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, 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, node_ids);
pubkey_for_node(chans[i].node2, &id2, node_ids);
printf("Channel %zu: %s -> %s\n",
i,
fmt_pubkey(tmpctx, &id1),
fmt_pubkey(tmpctx, &id2));
}
}
disabled_count = 0;
while ((chanidx = read_bigsize(inf)) < channel_count*2) {
disabled_count++;
chans[chanidx/2].half[chanidx%2].disabled = true;
}
if (verbose)
printf("%zu disabled\n", disabled_count);
template = read_template(tmpctx, inf, "capacities");
for (size_t i = 0; i < channel_count; i++)
chans[i].capacity = read_val(inf, template);
template = read_template(tmpctx, inf, "htlc_min");
for (size_t i = 0; i < channel_count; i++) {
for (size_t dir = 0; dir < 2; dir++) {
chans[i].half[dir].htlc_min = read_val(inf, template);
}
}
template = read_template(tmpctx, inf, "htlc_max");
for (size_t i = 0; i < channel_count; i++) {
for (size_t dir = 0; dir < 2; dir++) {
u64 v = read_val(inf, template);
if (v == 0)
v = chans[i].capacity;
else if (v == 1)
v = chans[i].capacity * 0.99;
chans[i].half[dir].htlc_max = v;
}
}
template = read_template(tmpctx, inf, "basefee");
for (size_t i = 0; i < channel_count; i++) {
for (size_t dir = 0; dir < 2; dir++) {
chans[i].half[dir].basefee = read_val(inf, template);
}
}
template = read_template(tmpctx, inf, "propfee");
for (size_t i = 0; i < channel_count; i++) {
for (size_t dir = 0; dir < 2; dir++) {
chans[i].half[dir].propfee = read_val(inf, template);
}
}
template = read_template(tmpctx, inf, "delay");
for (size_t i = 0; i < channel_count; i++) {
for (size_t dir = 0; dir < 2; dir++) {
chans[i].half[dir].delay = read_val(inf, template);
}
}
/* Now write out gossmap */
if (write(outfd, &version, 1) != 1)
err(1, "Failed to write output");
for (size_t i = 0; i < channel_count; i++) {
write_announce(outfd,
chans[i].node1,
chans[i].node2,
chans[i].capacity,
i, node_ids);
for (size_t dir = 0; dir < 2; dir++) {
write_update(outfd,
chans[i].node1, chans[i].node2, i, dir,
chans[i].half[dir].disabled,
chans[i].half[dir].htlc_min,
chans[i].half[dir].htlc_max,
chans[i].half[dir].basefee,
chans[i].half[dir].propfee,
chans[i].half[dir].delay,
node_ids);
}
}
gzclose(inf);
} else
opt_usage_and_exit("Unknown command");
close(outfd);
common_shutdown();
}