mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
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:
parent
8c8fa2cecd
commit
b45b4eaba6
@ -24,13 +24,17 @@
|
|||||||
|
|
||||||
char *bitcoin_datadir;
|
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;
|
size_t n = 0;
|
||||||
char **args = tal_arr(ctx, char *, 1);
|
char **args = tal_arr(ctx, char *, 3);
|
||||||
|
|
||||||
args[n++] = BITCOIN_CLI;
|
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) {
|
if (bitcoin_datadir) {
|
||||||
args[n++] = tal_fmt(args, "-datadir=%s", bitcoin_datadir);
|
args[n++] = tal_fmt(args, "-datadir=%s", bitcoin_datadir);
|
||||||
tal_resize(&args, n + 1);
|
tal_resize(&args, n + 1);
|
||||||
@ -169,7 +173,7 @@ start_bitcoin_cli(struct lightningd_state *dstate,
|
|||||||
else
|
else
|
||||||
bcli->exitstatus = NULL;
|
bcli->exitstatus = NULL;
|
||||||
va_start(ap, cmd);
|
va_start(ap, cmd);
|
||||||
bcli->args = gather_args(bcli, cmd, ap);
|
bcli->args = gather_args(dstate, bcli, cmd, ap);
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
|
|
||||||
list_add_tail(&dstate->bitcoin_req, &bcli->list);
|
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,
|
start_bitcoin_cli(dstate, process_getblockhash, false, cb, arg,
|
||||||
"getblockhash", str, NULL);
|
"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);
|
|
||||||
}
|
|
||||||
|
@ -97,6 +97,4 @@ void bitcoind_getrawblock_(struct lightningd_state *dstate,
|
|||||||
struct lightningd_state *, \
|
struct lightningd_state *, \
|
||||||
struct bitcoin_block *), \
|
struct bitcoin_block *), \
|
||||||
(arg))
|
(arg))
|
||||||
|
|
||||||
void check_bitcoind_config(struct lightningd_state *dstate);
|
|
||||||
#endif /* LIGHTNING_DAEMON_BITCOIND_H */
|
#endif /* LIGHTNING_DAEMON_BITCOIND_H */
|
||||||
|
@ -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)
|
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,
|
opt_register_arg("--locktime-blocks", opt_set_u32, opt_show_u32,
|
||||||
&dstate->config.locktime_blocks,
|
&dstate->config.locktime_blocks,
|
||||||
"Blocks before peer can unilaterally spend funds");
|
"Blocks before peer can unilaterally spend funds");
|
||||||
@ -162,6 +165,7 @@ static void default_config(struct config *config)
|
|||||||
{
|
{
|
||||||
/* aka. "Dude, where's my coins?" */
|
/* aka. "Dude, where's my coins?" */
|
||||||
config->testnet = true;
|
config->testnet = true;
|
||||||
|
config->regtest = false;
|
||||||
|
|
||||||
/* ~one day to catch cheating attempts. */
|
/* ~one day to catch cheating attempts. */
|
||||||
config->locktime_blocks = 6 * 24;
|
config->locktime_blocks = 6 * 24;
|
||||||
@ -353,8 +357,6 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
check_config(dstate);
|
check_config(dstate);
|
||||||
|
|
||||||
check_bitcoind_config(dstate);
|
|
||||||
|
|
||||||
/* Set up node ID and private key. */
|
/* Set up node ID and private key. */
|
||||||
secrets_init(dstate);
|
secrets_init(dstate);
|
||||||
new_node(dstate, &dstate->id);
|
new_node(dstate, &dstate->id);
|
||||||
|
@ -11,8 +11,8 @@
|
|||||||
|
|
||||||
/* Various adjustable things. */
|
/* Various adjustable things. */
|
||||||
struct config {
|
struct config {
|
||||||
/* Are we on testnet? */
|
/* Are we on testnet? regtest?*/
|
||||||
bool testnet;
|
bool testnet, regtest;
|
||||||
|
|
||||||
/* How long do we want them to lock up their funds? (blocks) */
|
/* How long do we want them to lock up their funds? (blocks) */
|
||||||
u32 locktime_blocks;
|
u32 locktime_blocks;
|
||||||
|
@ -366,6 +366,7 @@ fi
|
|||||||
cat > $DIR1/config <<EOF
|
cat > $DIR1/config <<EOF
|
||||||
disable-irc
|
disable-irc
|
||||||
log-level=debug
|
log-level=debug
|
||||||
|
bitcoind-regtest
|
||||||
bitcoind-poll=1s
|
bitcoind-poll=1s
|
||||||
deadline-blocks=5
|
deadline-blocks=5
|
||||||
min-htlc-expiry=6
|
min-htlc-expiry=6
|
||||||
@ -376,6 +377,7 @@ EOF
|
|||||||
|
|
||||||
cat > $DIR2/config <<EOF
|
cat > $DIR2/config <<EOF
|
||||||
disable-irc
|
disable-irc
|
||||||
|
bitcoind-regtest
|
||||||
log-level=debug
|
log-level=debug
|
||||||
bitcoind-poll=1s
|
bitcoind-poll=1s
|
||||||
deadline-blocks=5
|
deadline-blocks=5
|
||||||
|
Loading…
Reference in New Issue
Block a user