@@ -516,22 +516,23 @@ int main(int argc, char *argv[])
516516
517517 opt_parse (& argc , argv , opt_log_stderr_exit );
518518 if (argc != 4 )
519- opt_usage_and_exit ("Needs 4 arguments" );
519+ opt_usage_exit_fail ("Needs 4 arguments" );
520520
521521 infd = open (argv [2 ], O_RDONLY );
522522 if (infd < 0 )
523- opt_usage_and_exit (tal_fmt (tmpctx , "Cannot open %s for reading: %s" ,
524- argv [2 ], strerror (errno )));
523+ opt_usage_exit_fail (tal_fmt (tmpctx , "Cannot open %s for reading: %s" ,
524+ argv [2 ], strerror (errno )));
525525 outfd = open (argv [3 ], O_WRONLY |O_CREAT |O_TRUNC , 0666 );
526526 if (outfd < 0 )
527- opt_usage_and_exit (tal_fmt (tmpctx , "Cannot open %s for writing: %s" ,
528- argv [3 ], strerror (errno )));
527+ opt_usage_exit_fail (tal_fmt (tmpctx , "Cannot open %s for writing: %s" ,
528+ argv [3 ], strerror (errno )));
529529
530530 if (streq (argv [1 ], "compress" )) {
531531 struct gossmap_node * * nodes , * n ;
532532 size_t * node_to_compr_idx ;
533533 size_t node_count , channel_count ;
534534 struct gossmap_chan * * chans , * c ;
535+ bool * dirs ;
535536 gzFile outf = gzdopen (outfd , "wb9" );
536537
537538 struct gossmap * gossmap = gossmap_load_fd (tmpctx , infd , NULL , NULL , NULL );
@@ -569,27 +570,42 @@ int main(int argc, char *argv[])
569570 if (verbose )
570571 printf ("%zu channels\n" , channel_count );
571572 chans = tal_arr (gossmap , struct gossmap_chan * , channel_count );
573+ dirs = tal_arr (gossmap , bool , channel_count );
572574
573575 /* * <CHANNEL_ENDS> := {channel_count} {start_nodeidx}*{channel_count} {end_nodeidx}*{channel_count} */
574576 write_bigsize (outf , channel_count );
575577 size_t chanidx = 0 ;
576578 /* We iterate nodes to get to channels. This gives us nicer ordering for compression */
577- for (size_t wanted_dir = 0 ; wanted_dir < 2 ; wanted_dir ++ ) {
578- for (n = gossmap_first_node (gossmap ); n ; n = gossmap_next_node (gossmap , n )) {
579- for (size_t i = 0 ; i < n -> num_chans ; i ++ ) {
580- int dir ;
581- c = gossmap_nth_chan (gossmap , n , i , & dir );
582- if (dir != wanted_dir )
583- continue ;
584-
585- write_bigsize (outf ,
586- node_to_compr_idx [gossmap_node_idx (gossmap , n )]);
587- /* First time reflects channel index for reader */
588- if (wanted_dir == 0 )
589- chans [chanidx ++ ] = c ;
590- }
579+ for (size_t i = 0 ; i < tal_count (nodes ); i ++ ) {
580+ n = nodes [i ];
581+ for (size_t j = 0 ; j < n -> num_chans ; j ++ ) {
582+ const struct gossmap_node * peer ;
583+ int dir ;
584+ c = gossmap_nth_chan (gossmap , n , j , & dir );
585+
586+ peer = gossmap_nth_node (gossmap , c , !dir );
587+ /* Don't write if peer already wrote it! */
588+ /* FIXME: What about self-channels? */
589+ if (node_to_compr_idx [gossmap_node_idx (gossmap , peer )] < i )
590+ continue ;
591+
592+ write_bigsize (outf , node_to_compr_idx [gossmap_node_idx (gossmap , n )]);
593+
594+ assert (chanidx < channel_count );
595+ dirs [chanidx ] = dir ;
596+ chans [chanidx ] = c ;
597+ chanidx ++ ;
591598 }
592599 }
600+ assert (chanidx == channel_count );
601+
602+ /* Now write out the other ends of the channels */
603+ for (size_t i = 0 ; i < channel_count ; i ++ ) {
604+ const struct gossmap_node * peer ;
605+
606+ peer = gossmap_nth_node (gossmap , chans [i ], !dirs [i ]);
607+ write_bigsize (outf , node_to_compr_idx [gossmap_node_idx (gossmap , peer )]);
608+ }
593609
594610 /* <DISABLEDS> := <DISABLED>* {channel_count*2} */
595611 /* <DISABLED> := {chanidx}*2+{direction} */
0 commit comments