From c3bed51b2d626449384af955fd9bba70f87cf496 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 23 Oct 2017 00:02:16 +1030 Subject: [PATCH] test_lightningd.py: make HSM seeds constant for tests. Makes it easier to compare before/after failures. Ideally, we should run under Travis both with this option and with the seed based on the entire tmp path (which is still reproducible with determination, but not fixed every run like this is). Signed-off-by: Rusty Russell --- lightningd/lightningd.c | 1 + lightningd/lightningd.h | 3 +++ lightningd/options.c | 28 ++++++++++++++++++++++++++++ tests/utils.py | 3 +++ 4 files changed, 35 insertions(+) diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 2381557e4..2921fe996 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -75,6 +75,7 @@ static struct lightningd *new_lightningd(const tal_t *ctx, htlc_in_map_init(&ld->htlcs_in); htlc_out_map_init(&ld->htlcs_out); ld->dev_disconnect_fd = -1; + ld->dev_hsm_seed = NULL; ld->log_book = log_book; ld->log = new_log(log_book, log_book, "lightningd(%u):", (int)getpid()); diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index eff6cda6a..e0c6165d5 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -113,6 +113,9 @@ struct lightningd { /* If we want to debug a subdaemon. */ const char *dev_debug_subdaemon; + /* If we want to set a specific non-random HSM seed. */ + const u8 *dev_hsm_seed; + /* If we have a --dev-disconnect file */ int dev_disconnect_fd; diff --git a/lightningd/options.c b/lightningd/options.c index 816236c00..b147ed037 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -2,12 +2,14 @@ #include #include #include +#include #include #include #include #include #include #include +#include #include #include #include @@ -20,6 +22,8 @@ #include #include #include +#include +#include #include #include #include @@ -253,6 +257,15 @@ static void config_register_opts(struct lightningd *ld) "regtest, or litecoin)"); } +static char *opt_set_hsm_seed(const char *arg, struct lightningd *ld) +{ + ld->dev_hsm_seed = tal_hexdata(ld, arg, strlen(arg)); + if (ld->dev_hsm_seed) + return NULL; + + return tal_fmt(NULL, "bad hex string '%s'", arg); +} + static void dev_register_opts(struct lightningd *ld) { opt_register_noarg("--dev-no-broadcast", opt_set_bool, @@ -266,6 +279,8 @@ static void dev_register_opts(struct lightningd *ld) "Time between gossip broadcasts in milliseconds (default: 30000)"); opt_register_arg("--dev-disconnect=", opt_subd_dev_disconnect, NULL, ld, "File containing disconnection points"); + opt_register_arg("--dev-hsm-seed=", opt_set_hsm_seed, + NULL, ld, "Hex-encoded seed for HSM"); } static const struct config testnet_config = { @@ -546,5 +561,18 @@ bool handle_opts(struct lightningd *ld, int argc, char *argv[]) errx(1, "no arguments accepted"); check_config(ld); + + if (ld->dev_hsm_seed) { + int fd; + unlink("hsm_secret"); + fd = open("hsm_secret", O_CREAT|O_WRONLY, 0400); + if (fd < 0 || + !write_all(fd, ld->dev_hsm_seed, tal_len(ld->dev_hsm_seed)) + || fsync(fd) != 0) + fatal("dev-hsm-seed: Could not write file: %s", + strerror(errno)); + close(fd); + } + return newdir; } diff --git a/tests/utils.py b/tests/utils.py index 395b86572..891a77ac6 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -225,6 +225,8 @@ class LightningD(TailableProc): TailableProc.__init__(self, lightning_dir) self.lightning_dir = lightning_dir self.port = port + # Last 32-bytes of final part of dir -> seed. + seed = (bytes(re.search('([^/]+)/*$', lightning_dir).group(1), encoding='utf-8') + bytes(32))[:32] self.cmd_line = [ 'lightningd/lightningd', '--bitcoin-datadir={}'.format(bitcoin_dir), @@ -232,6 +234,7 @@ class LightningD(TailableProc): '--port={}'.format(port), '--network=regtest', '--dev-broadcast-interval=1000', + '--dev-hsm-seed={}'.format(seed.hex()) ] self.cmd_line += ["--{}={}".format(k, v) for k, v in LIGHTNINGD_CONFIG.items()]