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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2017-10-23 00:02:16 +10:30
committed by Christian Decker
parent 4c9f7542b2
commit c3bed51b2d
4 changed files with 35 additions and 0 deletions

View File

@@ -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());

View File

@@ -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;

View File

@@ -2,12 +2,14 @@
#include <bitcoin/chainparams.h>
#include <ccan/err/err.h>
#include <ccan/opt/opt.h>
#include <ccan/read_write_all/read_write_all.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/grab_file/grab_file.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/version.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <lightningd/bitcoind.h>
#include <lightningd/chaintopology.h>
@@ -20,6 +22,8 @@
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/types.h>
#include <unistd.h>
#include <wire/wire.h>
@@ -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=<filename>", opt_subd_dev_disconnect,
NULL, ld, "File containing disconnection points");
opt_register_arg("--dev-hsm-seed=<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;
}

View File

@@ -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()]