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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-10-07 14:00:16 +10:30
parent 8c8fa2cecd
commit b45b4eaba6
5 changed files with 16 additions and 52 deletions

View File

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

View File

@ -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 */

View File

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

View File

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

View File

@ -366,6 +366,7 @@ fi
cat > $DIR1/config <<EOF
disable-irc
log-level=debug
bitcoind-regtest
bitcoind-poll=1s
deadline-blocks=5
min-htlc-expiry=6
@ -376,6 +377,7 @@ EOF
cat > $DIR2/config <<EOF
disable-irc
bitcoind-regtest
log-level=debug
bitcoind-poll=1s
deadline-blocks=5