core-lightning/devtools/dump-gossipstore.c
Rusty Russell 7401b26824 cleanup: remove unneeded includes in C files.
Before:
 Ten builds, laptop -j5, no ccache:

```
real	0m36.686000-38.956000(38.608+/-0.65)s
user	2m32.864000-42.253000(40.7545+/-2.7)s
sys	0m16.618000-18.316000(17.8531+/-0.48)s
```

 Ten builds, laptop -j5, ccache (warm):

```
real	0m8.212000-8.577000(8.39989+/-0.13)s
user	0m12.731000-13.212000(12.9751+/-0.17)s
sys	0m3.697000-3.902000(3.83722+/-0.064)s
```

After:
 Ten builds, laptop -j5, no ccache: 8% faster

```
real	0m33.802000-35.773000(35.468+/-0.54)s
user	2m19.073000-27.754000(26.2542+/-2.3)s
sys	0m15.784000-17.173000(16.7165+/-0.37)s
```

 Ten builds, laptop -j5, ccache (warm): 1% faster

```
real	0m8.200000-8.485000(8.30138+/-0.097)s
user	0m12.485000-13.100000(12.7344+/-0.19)s
sys	0m3.702000-3.889000(3.78787+/-0.056)s
```

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-09-17 09:43:22 +09:30

114 lines
3.2 KiB
C

#include <ccan/crc32c/crc32c.h>
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <common/gossip_store.h>
#include <common/type_to_string.h>
#include <fcntl.h>
#include <gossipd/gossip_store_wiregen.h>
#include <stdio.h>
#include <unistd.h>
#include <wire/peer_wire.h>
int main(int argc, char *argv[])
{
int fd;
u8 version;
struct gossip_hdr hdr;
size_t off;
bool print_deleted = false;
setup_locale();
opt_register_noarg("--print-deleted", opt_set_bool, &print_deleted,
"Print deleted entries too");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"[<gossip_store>]"
"Dump all gossip messages in the store",
"Print this message.");
opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc > 2)
opt_usage_and_exit("Too many arguments");
if (argc == 2) {
fd = open(argv[1], O_RDONLY);
if (fd < 0)
err(1, "Opening %s", argv[1]);
} else
fd = STDIN_FILENO;
if (read(fd, &version, sizeof(version)) != sizeof(version))
errx(1, "Empty file");
if (version != GOSSIP_STORE_VERSION)
warnx("UNSUPPORTED GOSSIP VERSION %u (expected %u)",
version, GOSSIP_STORE_VERSION);
printf("GOSSIP VERSION %u\n", version);
off = 1;
while (read(fd, &hdr, sizeof(hdr)) == sizeof(hdr)) {
struct amount_sat sat;
struct short_channel_id scid;
u32 msglen = be32_to_cpu(hdr.len);
u8 *msg, *inner;
bool deleted, push;
deleted = (msglen & GOSSIP_STORE_LEN_DELETED_BIT);
push = (msglen & GOSSIP_STORE_LEN_PUSH_BIT);
msglen &= GOSSIP_STORE_LEN_MASK;
msg = tal_arr(NULL, u8, msglen);
if (read(fd, msg, msglen) != msglen)
errx(1, "%zu: Truncated file?", off);
if (be32_to_cpu(hdr.crc)
!= crc32c(be32_to_cpu(hdr.timestamp), msg, msglen))
warnx("Checksum verification failed");
printf("%zu: %s%s", off,
deleted ? "DELETED " : "",
push ? "PUSH " : "");
if (deleted && !print_deleted) {
printf("\n");
goto end;
}
if (fromwire_gossip_store_channel_amount(msg, &sat)) {
printf("channel_amount: %s\n",
type_to_string(tmpctx, struct amount_sat, &sat));
} else if (fromwire_peektype(msg) == WIRE_CHANNEL_ANNOUNCEMENT) {
printf("t=%u channel_announcement: %s\n",
be32_to_cpu(hdr.timestamp),
tal_hex(msg, msg));
} else if (fromwire_peektype(msg) == WIRE_CHANNEL_UPDATE) {
printf("t=%u channel_update: %s\n",
be32_to_cpu(hdr.timestamp),
tal_hex(msg, msg));
} else if (fromwire_peektype(msg) == WIRE_NODE_ANNOUNCEMENT) {
printf("t=%u node_announcement: %s\n",
be32_to_cpu(hdr.timestamp),
tal_hex(msg, msg));
} else if (fromwire_gossip_store_private_channel(msg, msg, &sat,
&inner)) {
printf("private channel_announcement: %s %s\n",
type_to_string(tmpctx, struct amount_sat, &sat),
tal_hex(msg, inner));
} else if (fromwire_gossip_store_private_update(msg, msg,
&inner)) {
printf("private channel_update: %s\n",
tal_hex(msg, inner));
} else if (fromwire_gossip_store_delete_chan(msg, &scid)) {
printf("delete channel: %s\n",
type_to_string(tmpctx, struct short_channel_id,
&scid));
} else {
warnx("Unknown message %u",
fromwire_peektype(msg));
}
end:
off += sizeof(hdr) + msglen;
tal_free(msg);
}
return 0;
}