2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-06-11 07:30:54 +02:00
|
|
|
#include <ccan/crc32c/crc32c.h>
|
2019-04-08 01:51:30 +02:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/opt/opt.h>
|
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2019-05-04 07:53:13 +02:00
|
|
|
#include <common/gossip_store.h>
|
2019-04-08 01:51:30 +02:00
|
|
|
#include <fcntl.h>
|
2020-08-25 04:05:45 +02:00
|
|
|
#include <gossipd/gossip_store_wiregen.h>
|
2019-04-08 01:51:30 +02:00
|
|
|
#include <inttypes.h>
|
2019-04-18 08:34:09 +02:00
|
|
|
#include <stdio.h>
|
2019-04-08 01:51:30 +02:00
|
|
|
#include <unistd.h>
|
2020-09-07 23:06:50 +02:00
|
|
|
#include <wire/peer_wire.h>
|
2019-04-08 01:51:30 +02:00
|
|
|
|
2019-04-01 19:52:08 +02:00
|
|
|
|
2019-04-18 08:34:09 +02:00
|
|
|
struct scidsat {
|
|
|
|
struct short_channel_id scid;
|
|
|
|
struct amount_sat sat;
|
2019-06-03 20:18:25 +02:00
|
|
|
};
|
2019-04-01 19:52:08 +02:00
|
|
|
|
2019-04-18 08:34:09 +02:00
|
|
|
/* read scid,satoshis csv file and create return an array of scidsat pointers */
|
|
|
|
static struct scidsat *load_csv_file(FILE *scidf)
|
2019-04-01 19:52:08 +02:00
|
|
|
{
|
2019-04-18 08:34:09 +02:00
|
|
|
int n, r;
|
|
|
|
char title[15];
|
2019-04-01 19:52:08 +02:00
|
|
|
int i = 0;
|
2019-04-18 08:34:09 +02:00
|
|
|
struct scidsat *scidsats;
|
|
|
|
/* max characters is 8 (0xffffff) + 8 for tx + 5 (0xffffff) for outputs (0xffff) + 2 (x's) */
|
|
|
|
char str[23];
|
|
|
|
|
|
|
|
if (fscanf(scidf, "%d\n", &n) != 1)
|
|
|
|
err(1, "reading number of entries from csv failed");
|
|
|
|
|
|
|
|
scidsats = tal_arr(NULL, struct scidsat, n);
|
|
|
|
r = fscanf(scidf, "%5s ,%8s\n", title, &title[6]);
|
|
|
|
if (r != 2 || strcmp(title, "scid") != 0 || strcmp(&title[6], "satoshis") != 0)
|
|
|
|
err(1, "reading 'scid ,satoshis' from csv failed");
|
|
|
|
|
2019-04-29 05:30:09 +02:00
|
|
|
while(fscanf(scidf, "%s ,%"SCNu64"\n", str, &scidsats[i].sat.satoshis) == 2 ) { /* Raw: read from file */
|
2019-09-06 08:41:41 +02:00
|
|
|
if (!short_channel_id_from_str(str, strlen(str), &scidsats[i].scid))
|
2019-04-18 08:34:09 +02:00
|
|
|
err(1, "failed to make scid struct");
|
|
|
|
i++;
|
2019-04-01 19:52:08 +02:00
|
|
|
}
|
2019-04-18 08:34:09 +02:00
|
|
|
return scidsats;
|
2019-04-01 19:52:08 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 20:18:25 +02:00
|
|
|
static void write_outmsg(int outfd, const u8 *outmsg, u32 timestamp)
|
2019-05-04 07:53:12 +02:00
|
|
|
{
|
2019-06-03 20:18:25 +02:00
|
|
|
struct gossip_hdr hdr;
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2019-06-03 20:18:25 +02:00
|
|
|
hdr.len = cpu_to_be32(tal_count(outmsg));
|
|
|
|
hdr.crc = cpu_to_be32(crc32c(timestamp, outmsg, tal_count(outmsg)));
|
|
|
|
hdr.timestamp = cpu_to_be32(timestamp);
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2019-06-03 20:18:25 +02:00
|
|
|
if (!write_all(outfd, &hdr, sizeof(hdr))
|
2019-05-04 07:53:12 +02:00
|
|
|
|| !write_all(outfd, outmsg, tal_count(outmsg)))
|
|
|
|
err(1, "Writing output");
|
|
|
|
}
|
|
|
|
|
2019-06-03 20:18:25 +02:00
|
|
|
static u32 get_update_timestamp(const u8 *msg, struct short_channel_id *scid)
|
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct bitcoin_blkid chain_hash;
|
|
|
|
u32 timestamp;
|
|
|
|
u8 u8_ignore;
|
|
|
|
u16 u16_ignore;
|
|
|
|
u32 u32_ignore;
|
2022-09-14 05:50:31 +02:00
|
|
|
struct amount_msat msat_ignore;
|
2019-06-03 20:18:25 +02:00
|
|
|
|
|
|
|
if (fromwire_channel_update(msg, &sig, &chain_hash, scid,
|
|
|
|
×tamp, &u8_ignore, &u8_ignore,
|
2022-09-14 05:50:31 +02:00
|
|
|
&u16_ignore, &msat_ignore, &u32_ignore,
|
|
|
|
&u32_ignore, &msat_ignore))
|
2019-06-03 20:18:25 +02:00
|
|
|
return timestamp;
|
|
|
|
errx(1, "Invalid channel_update");
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 get_node_announce_timestamp(const u8 *msg)
|
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
u32 timestamp;
|
|
|
|
struct node_id id;
|
|
|
|
u8 rgb_color[3], alias[32];
|
|
|
|
u8 *features, *addresses;
|
2021-06-10 19:54:08 +02:00
|
|
|
struct tlv_node_ann_tlvs *na_tlvs;
|
2019-06-03 20:18:25 +02:00
|
|
|
|
|
|
|
if (fromwire_node_announcement(tmpctx, msg, &sig, &features, ×tamp,
|
2021-06-10 19:54:08 +02:00
|
|
|
&id, rgb_color, alias, &addresses,
|
2022-03-23 00:31:14 +01:00
|
|
|
&na_tlvs))
|
2019-06-03 20:18:25 +02:00
|
|
|
return timestamp;
|
|
|
|
|
|
|
|
errx(1, "Invalid node_announcement");
|
|
|
|
}
|
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
u8 version;
|
|
|
|
beint16_t be_inlen;
|
|
|
|
struct amount_sat sat;
|
|
|
|
bool verbose = false;
|
2019-04-18 08:34:09 +02:00
|
|
|
char *infile = NULL, *outfile = NULL, *csvfile = NULL, *csat = NULL;
|
|
|
|
int infd, outfd, scidi = 0, channels = 0, nodes = 0, updates = 0;
|
2019-05-03 04:14:33 +02:00
|
|
|
struct scidsat *scidsats = NULL;
|
2019-06-03 20:18:25 +02:00
|
|
|
const u8 *last_announce = NULL;
|
2019-04-08 01:56:29 +02:00
|
|
|
unsigned max = -1U;
|
2019-04-08 01:51:30 +02:00
|
|
|
|
|
|
|
setup_locale();
|
|
|
|
|
|
|
|
opt_register_noarg("--verbose|-v", opt_set_bool, &verbose,
|
|
|
|
"Print progress to stderr");
|
|
|
|
opt_register_arg("--output|-o", opt_set_charp, NULL, &outfile,
|
|
|
|
"Send output to this file instead of stdout");
|
|
|
|
opt_register_arg("--input|-i", opt_set_charp, NULL, &infile,
|
|
|
|
"Read input from this file instead of stdin");
|
2019-04-18 08:34:09 +02:00
|
|
|
opt_register_arg("--csv", opt_set_charp, NULL, &csvfile,
|
2019-04-01 19:52:08 +02:00
|
|
|
"Input for 'scid, satshis' csv");
|
|
|
|
opt_register_arg("--sat", opt_set_charp, NULL, &csat,
|
2019-04-18 08:34:09 +02:00
|
|
|
"default satoshi value if --csv flag not present");
|
2019-04-08 01:56:29 +02:00
|
|
|
opt_register_arg("--max", opt_set_uintval, opt_show_uintval, &max,
|
|
|
|
"maximum number of messages to read");
|
2019-04-08 01:51:30 +02:00
|
|
|
opt_register_noarg("--help|-h", opt_usage_and_exit,
|
|
|
|
"Create gossip store, from be16 / input messages",
|
|
|
|
"Print this message.");
|
|
|
|
|
|
|
|
opt_parse(&argc, argv, opt_log_stderr_exit);
|
2019-04-01 19:52:08 +02:00
|
|
|
|
|
|
|
|
2019-04-18 08:34:09 +02:00
|
|
|
if (csvfile && !csat) {
|
|
|
|
FILE *scidf;
|
|
|
|
scidf = fopen(csvfile, "r");
|
|
|
|
if (!scidf)
|
|
|
|
err(1, "opening %s", csvfile);
|
|
|
|
scidsats = load_csv_file(scidf);
|
|
|
|
fclose(scidf);
|
|
|
|
} else if (csat && !csvfile) {
|
2019-04-01 19:52:08 +02:00
|
|
|
if (!parse_amount_sat(&sat, csat, strlen(csat))) {
|
|
|
|
errx(1, "Invalid satoshi amount %s", csat);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-04-18 08:34:09 +02:00
|
|
|
err(1, "must contain either --sat xor --csv");
|
2019-04-01 19:52:08 +02:00
|
|
|
}
|
2019-04-08 01:51:30 +02:00
|
|
|
|
|
|
|
if (infile) {
|
|
|
|
infd = open(infile, O_RDONLY);
|
|
|
|
if (infd < 0)
|
|
|
|
err(1, "opening %s", infile);
|
2019-04-01 19:52:08 +02:00
|
|
|
}
|
2019-05-24 11:57:30 +02:00
|
|
|
else
|
|
|
|
infd = STDIN_FILENO;
|
2019-04-08 01:51:30 +02:00
|
|
|
|
|
|
|
if (outfile) {
|
|
|
|
outfd = open(outfile, O_WRONLY|O_TRUNC|O_CREAT, 0666);
|
|
|
|
if (outfd < 0)
|
|
|
|
err(1, "opening %s", outfile);
|
|
|
|
} else
|
|
|
|
outfd = STDOUT_FILENO;
|
|
|
|
|
2022-09-14 05:50:31 +02:00
|
|
|
version = ((0 << 5) | 10);
|
2019-04-08 01:51:30 +02:00
|
|
|
if (!write_all(outfd, &version, sizeof(version)))
|
|
|
|
err(1, "Writing version");
|
|
|
|
|
|
|
|
while (read_all(infd, &be_inlen, sizeof(be_inlen))) {
|
|
|
|
u32 msglen = be16_to_cpu(be_inlen);
|
2019-05-04 07:53:12 +02:00
|
|
|
u8 *inmsg = tal_arr(NULL, u8, msglen);
|
2019-06-03 20:18:25 +02:00
|
|
|
u32 timestamp;
|
|
|
|
struct short_channel_id scid;
|
2019-04-08 01:51:30 +02:00
|
|
|
|
|
|
|
if (!read_all(infd, inmsg, msglen))
|
|
|
|
err(1, "Only read partial message");
|
|
|
|
|
2019-06-03 20:18:25 +02:00
|
|
|
if (verbose)
|
|
|
|
fprintf(stderr, "%s\n",
|
2020-08-31 03:13:25 +02:00
|
|
|
peer_wire_name(fromwire_peektype(inmsg)));
|
2019-06-03 20:18:25 +02:00
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
switch (fromwire_peektype(inmsg)) {
|
|
|
|
case WIRE_CHANNEL_ANNOUNCEMENT:
|
2019-05-03 04:14:33 +02:00
|
|
|
if (scidsats) {
|
2019-04-18 08:34:09 +02:00
|
|
|
/* We ignore these; we just want scid */
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
u8 *features;
|
|
|
|
struct bitcoin_blkid hash;
|
|
|
|
struct node_id id;
|
|
|
|
struct pubkey pubkey;
|
|
|
|
|
|
|
|
if (!fromwire_channel_announcement(inmsg,
|
|
|
|
inmsg,
|
|
|
|
&sig,
|
|
|
|
&sig,
|
|
|
|
&sig,
|
|
|
|
&sig,
|
|
|
|
&features,
|
|
|
|
&hash,
|
|
|
|
&scid,
|
|
|
|
&id,
|
|
|
|
&id,
|
|
|
|
&pubkey,
|
|
|
|
&pubkey))
|
|
|
|
errx(1, "bad channel_announcement");
|
|
|
|
if (!short_channel_id_eq(&scid, &scidsats[scidi].scid))
|
2019-06-03 20:18:25 +02:00
|
|
|
errx(1, "scid of channel_announcement does not match scid in csv");
|
|
|
|
if (last_announce)
|
|
|
|
errx(1, "Expected update before announce");
|
|
|
|
last_announce = inmsg;
|
|
|
|
inmsg = NULL;
|
2019-04-01 19:52:08 +02:00
|
|
|
}
|
2019-04-08 01:51:30 +02:00
|
|
|
break;
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
case WIRE_CHANNEL_UPDATE:
|
2019-06-03 20:18:25 +02:00
|
|
|
/* We assume update immediately follows announcement */
|
|
|
|
timestamp = get_update_timestamp(inmsg, &scid);
|
|
|
|
if (last_announce) {
|
|
|
|
if (scidsats && !short_channel_id_eq(&scid,
|
|
|
|
&scidsats[scidi].scid))
|
|
|
|
errx(1, "scid of channel_update does not match scid in csv");
|
|
|
|
|
|
|
|
/* Now we have timestamp, write out announce */
|
|
|
|
/* First write announce */
|
|
|
|
write_outmsg(outfd, last_announce, timestamp);
|
|
|
|
last_announce = tal_free(last_announce);
|
|
|
|
channels += 1;
|
|
|
|
/* Now write amount */
|
|
|
|
write_outmsg(outfd,
|
|
|
|
towire_gossip_store_channel_amount(inmsg,
|
|
|
|
scidsats ? scidsats[scidi].sat: sat),
|
|
|
|
0);
|
|
|
|
scidi++;
|
|
|
|
}
|
|
|
|
write_outmsg(outfd, inmsg, timestamp);
|
2019-04-01 19:52:08 +02:00
|
|
|
updates += 1;
|
2019-04-08 01:51:30 +02:00
|
|
|
break;
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
case WIRE_NODE_ANNOUNCEMENT:
|
2019-06-03 20:18:25 +02:00
|
|
|
timestamp = get_node_announce_timestamp(inmsg);
|
|
|
|
write_outmsg(outfd, inmsg, timestamp);
|
2019-04-01 19:52:08 +02:00
|
|
|
nodes += 1;
|
2019-04-08 01:51:30 +02:00
|
|
|
break;
|
2019-05-04 07:53:12 +02:00
|
|
|
|
2019-04-08 01:51:30 +02:00
|
|
|
default:
|
|
|
|
warnx("Unknown message %u (%s)", fromwire_peektype(inmsg),
|
2020-08-31 03:13:25 +02:00
|
|
|
peer_wire_name(fromwire_peektype(inmsg)));
|
2019-04-08 01:51:30 +02:00
|
|
|
tal_free(inmsg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tal_free(inmsg);
|
2019-04-08 01:56:29 +02:00
|
|
|
if (--max == 0)
|
|
|
|
break;
|
2019-04-08 01:51:30 +02:00
|
|
|
}
|
2019-04-08 01:56:27 +02:00
|
|
|
fprintf(stderr, "channels %d, updates %d, nodes %d\n", channels, updates, nodes);
|
2019-05-03 04:14:33 +02:00
|
|
|
tal_free(scidsats);
|
2019-04-08 01:51:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|