From 294394215eb8a40cb29326dbd823cd7ffae60a16 Mon Sep 17 00:00:00 2001 From: Joe Netti Date: Mon, 1 Apr 2019 10:52:08 -0700 Subject: [PATCH] create-gossipstore.c can read scid -> satoshis csv file. The csv is in the format scid ,satoshis where there is a black space after scid. Made a header file that contains a struct. Modified makefile. Added cmdline arg --scidfile /path/to/csv and made the constant capacity command optional. create-gossipstore prints stats at the end. --- devtools/Makefile | 2 +- devtools/create-gossipstore.c | 65 ++++++++++++++++++++++++++++++----- devtools/create-gossipstore.h | 11 ++++++ 3 files changed, 69 insertions(+), 9 deletions(-) create mode 100644 devtools/create-gossipstore.h diff --git a/devtools/Makefile b/devtools/Makefile index 73353619c..67be25869 100644 --- a/devtools/Makefile +++ b/devtools/Makefile @@ -42,7 +42,7 @@ devtools/dump-gossipstore: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) devtools/dump-gossipstore.o: gossipd/gen_gossip_store.h devtools/create-gossipstore: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/create-gossipstore.o gossipd/gen_gossip_store.o -devtools/create-gossipstore.o: gossipd/gen_gossip_store.h +devtools/create-gossipstore.o: gossipd/gen_gossip_store.h devtools/create-gossipstore.h devtools/onion.c: ccan/config.h diff --git a/devtools/create-gossipstore.c b/devtools/create-gossipstore.c index 87337e932..2b8987906 100644 --- a/devtools/create-gossipstore.c +++ b/devtools/create-gossipstore.c @@ -4,24 +4,44 @@ #include #include #include +#include #include #include #include #include #include +#include #include #include #include #include + + +struct scidsat * load_scid_file(FILE * scidfd) +{ + int n; + fscanf(scidfd, "%d\n", &n); + char title[16]; + fscanf(scidfd, "%s\n", title); + struct scidsat * scids = calloc(n, sizeof(scidsat)); + int i = 0; + while(fscanf(scidfd, "%s ,%ld\n", scids[i].scid, &scids[i].sat.satoshis) == 2 ) { + i++; + } + return scids; +} + int main(int argc, char *argv[]) { u8 version; beint16_t be_inlen; struct amount_sat sat; bool verbose = false; - char *infile = NULL, *outfile = NULL; + char *infile = NULL, *outfile = NULL, *scidfile = NULL, *csat = NULL; int infd, outfd; + FILE * scidfd; + struct scidsat * scids; setup_locale(); @@ -31,23 +51,38 @@ int main(int argc, char *argv[]) "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"); + opt_register_arg("--scidfile", opt_set_charp, NULL, &scidfile, + "Input for 'scid, satshis' csv"); + opt_register_arg("--sat", opt_set_charp, NULL, &csat, + "default satoshi value if --scidfile flag not present"); opt_register_noarg("--help|-h", opt_usage_and_exit, - "default-satoshis\n" "Create gossip store, from be16 / input messages", "Print this message."); opt_parse(&argc, argv, opt_log_stderr_exit); - if (argc != 2) - errx(1, "Need default-satoshi argument for channel amounts"); - if (!parse_amount_sat(&sat, argv[1], strlen(argv[1]))) - errx(1, "Invalid satoshi amount %s", argv[1]); + + + if (scidfile) { + scidfd = fopen(scidfile, "r"); + if (scidfd < 0) + err(1, "opening %s", scidfile); + scids = load_scid_file(scidfd); + fclose(scidfd); + } + else if (csat) { + if (!parse_amount_sat(&sat, csat, strlen(csat))) { + errx(1, "Invalid satoshi amount %s", csat); + } + } + else { + err(1, "must contain either --sat xor --scidfile"); + } if (infile) { infd = open(infile, O_RDONLY); if (infd < 0) err(1, "opening %s", infile); - } else - infd = STDIN_FILENO; + } if (outfile) { outfd = open(outfile, O_WRONLY|O_TRUNC|O_CREAT, 0666); @@ -60,6 +95,10 @@ int main(int argc, char *argv[]) if (!write_all(outfd, &version, sizeof(version))) err(1, "Writing version"); + int scidi = 0; + int channels = 0; + int nodes = 0; + int updates = 0; while (read_all(infd, &be_inlen, sizeof(be_inlen))) { u32 msglen = be16_to_cpu(be_inlen); u8 *inmsg = tal_arr(NULL, u8, msglen), *outmsg; @@ -71,13 +110,20 @@ int main(int argc, char *argv[]) switch (fromwire_peektype(inmsg)) { case WIRE_CHANNEL_ANNOUNCEMENT: + if (scidfile) { + sat = scids[scidi].sat; + scidi += 1; + } + channels += 1; outmsg = towire_gossip_store_channel_announcement(inmsg, inmsg, sat); break; case WIRE_CHANNEL_UPDATE: outmsg = towire_gossip_store_channel_update(inmsg, inmsg); + updates += 1; break; case WIRE_NODE_ANNOUNCEMENT: outmsg = towire_gossip_store_node_announcement(inmsg, inmsg); + nodes += 1; break; default: warnx("Unknown message %u (%s)", fromwire_peektype(inmsg), @@ -99,5 +145,8 @@ int main(int argc, char *argv[]) } tal_free(inmsg); } + printf("channels %d, updates %d, nodes %d\n", channels, updates, nodes); + if (scidfile) + free(scids); return 0; } diff --git a/devtools/create-gossipstore.h b/devtools/create-gossipstore.h new file mode 100644 index 000000000..d6a715048 --- /dev/null +++ b/devtools/create-gossipstore.h @@ -0,0 +1,11 @@ +#include +#include +#include + +struct scidsat { + char scid[17]; + struct amount_sat sat; +} scidsat; + +struct scidsat * load_scid_file(FILE * scidfd); +