forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump-gossipstore.c
143 lines (128 loc) · 4.43 KB
/
dump-gossipstore.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
#include "config.h"
#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>
/* Current versions we support */
#define GSTORE_MAJOR 0
#define GSTORE_MINOR 12
int main(int argc, char *argv[])
{
int fd;
u8 version;
struct gossip_hdr hdr;
size_t off;
bool print_deleted = false;
bool print_timestamp = false;
setup_locale();
opt_register_noarg("--print-deleted", opt_set_bool, &print_deleted,
"Print deleted entries too");
opt_register_noarg("--print-timestamps", opt_set_bool, &print_timestamp,
"Print timestamp with entries");
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 (GOSSIP_STORE_MAJOR_VERSION(version) != GSTORE_MAJOR)
errx(1, "Unsupported major gossip_version %u (expected %u)",
GOSSIP_STORE_MAJOR_VERSION(version), GSTORE_MAJOR);
/* Unsupported minor just means we might not understand all fields,
* or all flags. */
if (GOSSIP_STORE_MINOR_VERSION(version) != GSTORE_MINOR)
warnx("UNKNOWN GOSSIP minor VERSION %u (expected %u)",
GOSSIP_STORE_MINOR_VERSION(version), GSTORE_MINOR);
printf("GOSSIP VERSION %u/%u\n",
GOSSIP_STORE_MINOR_VERSION(version),
GOSSIP_STORE_MAJOR_VERSION(version));
off = 1;
while (read(fd, &hdr, sizeof(hdr)) == sizeof(hdr)) {
struct amount_sat sat;
struct short_channel_id scid;
u16 flags = be16_to_cpu(hdr.flags);
u16 msglen = be16_to_cpu(hdr.len);
u8 *msg, *inner;
bool deleted, push, ratelimit, zombie, dying;
u32 blockheight;
deleted = (flags & GOSSIP_STORE_DELETED_BIT);
push = (flags & GOSSIP_STORE_PUSH_BIT);
ratelimit = (flags & GOSSIP_STORE_RATELIMIT_BIT);
zombie = (flags & GOSSIP_STORE_ZOMBIE_BIT);
dying = (flags & GOSSIP_STORE_DYING_BIT);
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%s%s%s", off,
deleted ? "DELETED " : "",
push ? "PUSH " : "",
ratelimit ? "RATE-LIMITED " : "",
zombie ? "ZOMBIE " : "",
dying ? "DYING " : "");
if (print_timestamp)
printf("T=%u ", be32_to_cpu(hdr.timestamp));
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 if (fromwire_gossip_store_chan_dying(msg, &scid, &blockheight)) {
printf("dying channel: %s (deadline %u)\n",
type_to_string(tmpctx, struct short_channel_id,
&scid),
blockheight);
} else {
warnx("Unknown message %u",
fromwire_peektype(msg));
}
end:
off += sizeof(hdr) + msglen;
tal_free(msg);
}
return 0;
}