Skip to content

create-gossipstore.c can read scid -> satoshis csv file. The csv is i… #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7509cd9
pytest: backtrace on internal errors in subdaemons.
rustyrussell Apr 5, 2019
de21089
devtools/create-gossipstore: tool to create a gossip_store file from …
rustyrussell Apr 5, 2019
d06daf9
gossipd: use htable instead of simple array for node's channels.
rustyrussell Apr 5, 2019
c3cd344
dev: --dev-gossip-time so gossipd doesn't prune old data.
rustyrussell Apr 5, 2019
4835e25
gossipd: temporarily allow giant messages
rustyrussell Apr 5, 2019
b2b3126
gossipd: dev option to allow unknown channels.
rustyrussell Apr 5, 2019
9533126
fixup! gossipd: dev option to allow unknown channels.
rustyrussell Apr 5, 2019
f8d9425
dev-compact-store-gossip: specific RPC so we can test gossip_store re…
rustyrussell Apr 5, 2019
ad1e530
pytest: test that gossipd remembers unannounced local channels across…
rustyrussell Apr 5, 2019
cb1018a
lightningd: log IO only on actual output.
rustyrussell Apr 5, 2019
835834d
log: truncate giant IO logging.
rustyrussell Apr 5, 2019
5c4f8c4
gossipd: store local channel updates across restart, even if unannoun…
rustyrussell Apr 5, 2019
abc6487
gossipd: preserve unannounced channels across store compaction.
rustyrussell Apr 5, 2019
4f00cae
devtools/gossipwith: add option to stream from stdin.
rustyrussell Apr 5, 2019
b563009
tools/bench-gossipd.sh: rough benchmark for gossipd and the million c…
rustyrussell Apr 5, 2019
0eeb94a
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the mi…
rustyrussell Apr 5, 2019
2009e62
fixup! tools/bench-gossipd.sh: rough benchmark for gossipd and the mi…
rustyrussell Apr 5, 2019
4ebe5ce
tools/bench-gossipd.sh: don't print CSV by default.
rustyrussell Apr 5, 2019
ec31727
patch fixup-shellcheck.patch
rustyrussell Apr 5, 2019
0f683c7
create-gossipstore.c can read scid -> satoshis csv file. The csv …
nettijoe96 Apr 1, 2019
603dbc6
devtools/create-gossipstore: don't pollute output with message.
rustyrussell Apr 5, 2019
d332153
devtools/create-gossipstore: add --max option to create reduced tests…
rustyrussell Apr 5, 2019
c2736f0
patch cleanups.patch
rustyrussell Apr 5, 2019
30fc20b
added sanity check to make sure scid of csv is the same as scid in go…
nettijoe96 Apr 5, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ changes.
### Fixed

- `--bind-addr=<path>` fixed for nodes using local sockets (eg. testing).
- Unannounced local channels were forgotten for routing on restart until reconnection occurred.

### Security

Expand Down
18 changes: 15 additions & 3 deletions common/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,25 @@ static int backtrace_status(void *unused UNUSED, uintptr_t pc,
return 0;
}

static void crashdump(int sig)
void send_backtrace(const char *why)
{
/* We do stderr first, since it's most reliable. */
warnx("Fatal signal %d (version %s)", sig, version());
warnx("%s (version %s)", why, version());
if (backtrace_state)
backtrace_print(backtrace_state, 0, stderr);

/* Now send to parent. */
bt_print("FATAL SIGNAL %d (version %s)", sig, version());
bt_print("%s (version %s)", why, version());
if (backtrace_state)
backtrace_full(backtrace_state, 0, backtrace_status, NULL, NULL);
}

static void crashdump(int sig)
{
char why[100];

snprintf(why, 100, "FATAL SIGNAL %d", sig);
send_backtrace(why);

/* Probably shouldn't return. */
bt_exit();
Expand All @@ -66,6 +74,10 @@ static void crashlog_activate(void)
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}
#else
void send_backtrace(const char *why)
{
}
#endif

int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout)
Expand Down
3 changes: 3 additions & 0 deletions common/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ void daemon_setup(const char *argv0,
/* Exposed for lightningd's use. */
int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout);

/* Print a backtrace to stderr, and via backtrace_print */
void send_backtrace(const char *why);

/* Shutdown for a valgrind-clean exit (frees everything) */
void daemon_shutdown(void);

Expand Down
5 changes: 5 additions & 0 deletions common/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <ccan/fdpass/fdpass.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/tal/str/str.h>
#include <common/daemon.h>
#include <common/daemon_conn.h>
#include <common/gen_status_wire.h>
#include <common/status.h>
Expand Down Expand Up @@ -167,6 +168,10 @@ void status_failed(enum status_failreason reason, const char *fmt, ...)
str = tal_vfmt(NULL, fmt, ap);
va_end(ap);

/* Give a nice backtrace when this happens! */
if (reason == STATUS_FAIL_INTERNAL_ERROR)
send_backtrace(str);

status_send_fatal(take(towire_status_fail(NULL, reason, str)),
-1, -1);
}
Expand Down
9 changes: 7 additions & 2 deletions devtools/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
DEVTOOLS_SRC := devtools/gen_print_wire.c devtools/gen_print_onion_wire.c devtools/print_wire.c
DEVTOOLS_OBJS := $(DEVTOOLS_SRC:.c=.o)
DEVTOOLS := devtools/bolt11-cli devtools/decodemsg devtools/onion devtools/dump-gossipstore devtools/gossipwith
DEVTOOLS := devtools/bolt11-cli devtools/decodemsg devtools/onion devtools/dump-gossipstore devtools/gossipwith devtools/create-gossipstore
DEVTOOLS_TOOL_SRC := $(DEVTOOLS:=.c)
DEVTOOLS_TOOL_OBJS := $(DEVTOOLS_TOOL_SRC:.c=.o)

Expand All @@ -16,7 +16,8 @@ DEVTOOLS_COMMON_OBJS := \
common/utils.o \
common/version.o \
common/wireaddr.o \
wire/gen_onion_wire.o
wire/gen_onion_wire.o \
wire/gen_peer_wire.o

devtools-all: $(DEVTOOLS)

Expand All @@ -39,6 +40,10 @@ devtools/decodemsg: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN
devtools/dump-gossipstore: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/dump-gossipstore.o gossipd/gen_gossip_store.o

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/onion.c: ccan/config.h

devtools/onion: $(DEVTOOLS_OBJS) $(DEVTOOLS_COMMON_OBJS) $(JSMN_OBJS) $(CCAN_OBJS) $(BITCOIN_OBJS) wire/fromwire.o wire/towire.o devtools/onion.o common/sphinx.o
Expand Down
186 changes: 186 additions & 0 deletions devtools/create-gossipstore.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#include <bitcoin/short_channel_id.h>
#include <ccan/crc/crc.h>
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <common/amount.h>
#include <common/type_to_string.h>
#include <common/utils.h>
#include <fcntl.h>
#include <gossipd/gen_gossip_store.h>
#include <gossipd/gossip_store.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <wire/gen_peer_wire.h>


struct scidsat {
struct short_channel_id scid;
struct amount_sat sat;
} scidsat;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial: these two extra lines are unnecessary.

/* read scid,satoshis csv file and create return an array of scidsat pointers */
static struct scidsat *load_csv_file(FILE *scidf)
{
int n, r;
char title[15];
int i = 0;
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");

while(fscanf(scidf, "%s ,%ld\n", str, &scidsats[i].sat.satoshis) == 2 ) { /* Raw: read from file */
if (!short_channel_id_from_str(str, strlen(str), &scidsats[i].scid, 0))
err(1, "failed to make scid struct");
i++;
}
return scidsats;
}

static u64 getScid(const u8 *msg, size_t *max) {
const u8 ** cursor = &msg;
return fromwire_u64(cursor, max);
}

int main(int argc, char *argv[])
{
u8 version;
beint16_t be_inlen;
struct amount_sat sat;
bool verbose = false;
char *infile = NULL, *outfile = NULL, *csvfile = NULL, *csat = NULL;
int infd, outfd, scidi, channelsi, nodesi, updatesi, msgi = 0;
u64 scid;
short featurelen;
struct scidsat *scidsats;
unsigned max = -1U;
size_t plen;

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");
opt_register_arg("--csv", opt_set_charp, NULL, &csvfile,
"Input for 'scid, satshis' csv");
opt_register_arg("--sat", opt_set_charp, NULL, &csat,
"default satoshi value if --csv flag not present");
opt_register_arg("--max", opt_set_uintval, opt_show_uintval, &max,
"maximum number of messages to read");
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);


Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trivial: Two lines is unnecessary.

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) {
if (!parse_amount_sat(&sat, csat, strlen(csat))) {
errx(1, "Invalid satoshi amount %s", csat);
}
}
else {
err(1, "must contain either --sat xor --csvfile");
}

if (infile) {
infd = open(infile, O_RDONLY);
if (!infd)
err(1, "opening %s", infile);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This removal seems weird; if we don't specify infile infd will be undefined. Standard UNIX tools use stdin/stdout by default.


if (outfile) {
outfd = open(outfile, O_WRONLY|O_TRUNC|O_CREAT, 0666);
if (outfd < 0)
err(1, "opening %s", outfile);
} else
outfd = STDOUT_FILENO;

version = GOSSIP_STORE_VERSION;
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);
u8 *inmsg = tal_arr(NULL, u8, msglen), *outmsg;
beint32_t be_outlen;
beint32_t becsum;

if (!read_all(infd, inmsg, msglen))
err(1, "Only read partial message");

switch (fromwire_peektype(inmsg)) {
case WIRE_CHANNEL_ANNOUNCEMENT:
if (csvfile) {
sat = scidsats[scidi].sat;
msgi = 258;
featurelen = *(short *)(&inmsg[msgi]);
msgi += 2 + (int)featurelen + 32;
plen = tal_count(inmsg);
scid = getScid(&inmsg[msgi], &plen);
if (scid != scidsats[scidi].scid.u64)
err(1, "scid of message does not match scid in csv");
scidi++;
}
outmsg = towire_gossip_store_channel_announcement(inmsg, inmsg, sat);
channelsi += 1;
break;
case WIRE_CHANNEL_UPDATE:
outmsg = towire_gossip_store_channel_update(inmsg, inmsg);
updatesi += 1;
break;
case WIRE_NODE_ANNOUNCEMENT:
outmsg = towire_gossip_store_node_announcement(inmsg, inmsg);
nodesi += 1;
break;
default:
warnx("Unknown message %u (%s)", fromwire_peektype(inmsg),
wire_type_name(fromwire_peektype(inmsg)));
tal_free(inmsg);
continue;
}
if (verbose)
fprintf(stderr, "%s->%s\n",
wire_type_name(fromwire_peektype(inmsg)),
gossip_store_type_name(fromwire_peektype(outmsg)));

becsum = cpu_to_be32(crc32c(0, outmsg, tal_count(outmsg)));
be_outlen = cpu_to_be32(tal_count(outmsg));
if (!write_all(outfd, &be_outlen, sizeof(be_outlen))
|| !write_all(outfd, &becsum, sizeof(becsum))
|| !write_all(outfd, outmsg, tal_count(outmsg))) {
exit(1);
}
tal_free(inmsg);
if (--max == 0)
break;
}
fprintf(stderr, "channels %d, updates %d, nodes %d\n", channelsi, updatesi, nodesi);
if (csvfile)
tal_free(scidsats);
return 0;
}
16 changes: 16 additions & 0 deletions devtools/gossipwith.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#define io_write_ simple_write
#define io_read_ simple_read
#define io_close simple_close
static bool stream_stdin = false;

static struct io_plan *simple_write(struct io_conn *conn,
const void *data, size_t len,
Expand Down Expand Up @@ -130,6 +131,19 @@ static struct io_plan *handshake_success(struct io_conn *conn,
tal_free(sync_crypto_read(NULL, &cs, conn->fd));

/* Did they ask us to send any messages? Do so now. */
if (stream_stdin) {
beint16_t be_inlen;

while (read_all(STDIN_FILENO, &be_inlen, sizeof(be_inlen))) {
u32 msglen = be16_to_cpu(be_inlen);
u8 *msg = tal_arr(NULL, u8, msglen);

if (!read_all(STDIN_FILENO, msg, msglen))
err(1, "Only read partial message");
sync_crypto_write(&cs, conn->fd, take(msg));
}
}

while (*args) {
u8 *m = tal_hexdata(NULL, *args, strlen(*args));
if (!m)
Expand Down Expand Up @@ -172,6 +186,8 @@ int main(int argc, char *argv[])
opt_register_arg("--max-messages", opt_set_ulongval, opt_show_ulongval,
&max_messages,
"Terminate after reading this many messages (> 0)");
opt_register_noarg("--stdin", opt_set_bool, &stream_stdin,
"Stream gossip messages from stdin.");
opt_register_noarg("--help|-h", opt_usage_and_exit,
"id@addr[:port] [hex-msg-tosend...]\n"
"Connect to a lightning peer and relay gossip messages from it",
Expand Down
Loading