From b45b4eaba6ae53e929f2e39eb927a7d3ed6def26 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Oct 2016 14:00:16 +1030 Subject: [PATCH] bitcoind: explicit flag to bitcoin-cli for testnet/regtest. Three days of on and off debugging, before I realized my server was talking to a non-testnet bitcoind. There was a bitcoind on that machine running on testnet, but it uses the same dir and config, so the --bitcoin-datadir option couldn't help. This is more certain: specify whether we're testnet on every single query. Now we can skip the attempt to parse bitcoin.conf, too. Signed-off-by: Rusty Russell --- daemon/bitcoind.c | 54 +++++++-------------------------------------- daemon/bitcoind.h | 2 -- daemon/lightningd.c | 6 +++-- daemon/lightningd.h | 4 ++-- daemon/test/test.sh | 2 ++ 5 files changed, 16 insertions(+), 52 deletions(-) diff --git a/daemon/bitcoind.c b/daemon/bitcoind.c index 7184dc2ed..68f08ff5a 100644 --- a/daemon/bitcoind.c +++ b/daemon/bitcoind.c @@ -24,13 +24,17 @@ char *bitcoin_datadir; -static char **gather_args(const tal_t *ctx, const char *cmd, va_list ap) +static char **gather_args(struct lightningd_state *dstate, + const tal_t *ctx, const char *cmd, va_list ap) { size_t n = 0; - char **args = tal_arr(ctx, char *, 1); + char **args = tal_arr(ctx, char *, 3); args[n++] = BITCOIN_CLI; - tal_resize(&args, n + 1); + if (dstate->config.regtest) + args[n++] = "-regtest=1"; + else + args[n++] = tal_fmt(args, "-testnet=%u", dstate->config.testnet); if (bitcoin_datadir) { args[n++] = tal_fmt(args, "-datadir=%s", bitcoin_datadir); tal_resize(&args, n + 1); @@ -169,7 +173,7 @@ start_bitcoin_cli(struct lightningd_state *dstate, else bcli->exitstatus = NULL; va_start(ap, cmd); - bcli->args = gather_args(bcli, cmd, ap); + bcli->args = gather_args(dstate, bcli, cmd, ap); va_end(ap); list_add_tail(&dstate->bitcoin_req, &bcli->list); @@ -430,45 +434,3 @@ void bitcoind_getblockhash_(struct lightningd_state *dstate, start_bitcoin_cli(dstate, process_getblockhash, false, cb, arg, "getblockhash", str, NULL); } - -/* Make testnet/regtest status matches us. */ -void check_bitcoind_config(struct lightningd_state *dstate) -{ - void *ctx = tal(dstate, char); - char *path, *config, **lines; - size_t i; - int testnet = -1, regtest = -1; - - path = path_simplify(ctx, path_join(ctx, path_cwd(ctx), - "../.bitcoin/bitcoin.conf")); - config = grab_file(ctx, path); - if (!config) { - log_unusual(dstate->base_log, "Could not open %s to check it", - path); - goto out; - } - - lines = tal_strsplit(ctx, config, "\n", STR_NO_EMPTY); - for (i = 0; lines[i]; i++) { - char *str; - if (tal_strreg(ctx, lines[i], - "^[ \t]*testnet[ \t]*=[ \t]*([01])", &str)) - testnet = atoi(str); - else if (tal_strreg(ctx, lines[i], - "^[ \t]*regtest[ \t]*=[ \t]*([01])", &str)) - regtest = atoi(str); - } - - if (dstate->config.testnet) { - if (testnet != 1 && regtest != 1) - log_unusual(dstate->base_log, - "%s does not set testnet/regtest," - " but we are on testnet.", - path); - } else if (testnet == 1 || regtest == 1) - log_unusual(dstate->base_log, - "%s sets %s, but we are not on testnet", - path, testnet == 1 ? "testnet" : "regtest"); -out: - tal_free(ctx); -} diff --git a/daemon/bitcoind.h b/daemon/bitcoind.h index b92fdc377..cafe912ac 100644 --- a/daemon/bitcoind.h +++ b/daemon/bitcoind.h @@ -97,6 +97,4 @@ void bitcoind_getrawblock_(struct lightningd_state *dstate, struct lightningd_state *, \ struct bitcoin_block *), \ (arg)) - -void check_bitcoind_config(struct lightningd_state *dstate); #endif /* LIGHTNING_DAEMON_BITCOIND_H */ diff --git a/daemon/lightningd.c b/daemon/lightningd.c index 96d801f99..9530961b3 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -94,6 +94,9 @@ static void opt_show_s32(char buf[OPT_SHOW_LEN], const s32 *u) static void config_register_opts(struct lightningd_state *dstate) { + opt_register_noarg("--bitcoind-regtest", opt_set_bool, + &dstate->config.regtest, + "Bitcoind is in regtest mode"); opt_register_arg("--locktime-blocks", opt_set_u32, opt_show_u32, &dstate->config.locktime_blocks, "Blocks before peer can unilaterally spend funds"); @@ -162,6 +165,7 @@ static void default_config(struct config *config) { /* aka. "Dude, where's my coins?" */ config->testnet = true; + config->regtest = false; /* ~one day to catch cheating attempts. */ config->locktime_blocks = 6 * 24; @@ -353,8 +357,6 @@ int main(int argc, char *argv[]) check_config(dstate); - check_bitcoind_config(dstate); - /* Set up node ID and private key. */ secrets_init(dstate); new_node(dstate, &dstate->id); diff --git a/daemon/lightningd.h b/daemon/lightningd.h index f8d1e479f..489c1b424 100644 --- a/daemon/lightningd.h +++ b/daemon/lightningd.h @@ -11,8 +11,8 @@ /* Various adjustable things. */ struct config { - /* Are we on testnet? */ - bool testnet; + /* Are we on testnet? regtest?*/ + bool testnet, regtest; /* How long do we want them to lock up their funds? (blocks) */ u32 locktime_blocks; diff --git a/daemon/test/test.sh b/daemon/test/test.sh index c841b6fe5..1df721273 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -366,6 +366,7 @@ fi cat > $DIR1/config < $DIR2/config <