From cd649329a99d094dae1bc97231653deb9a7be565 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 22 Sep 2016 11:19:21 +0930 Subject: [PATCH] config: different defaults for mainnet and testnet. In particular, remove fee ranges on testnet (too unreliable) and accept a single confirm. Signed-off-by: Rusty Russell --- daemon/configdir.c | 63 --------------- daemon/configdir.h | 2 - daemon/lightningd.c | 186 +++++++++++++++++++++++++++++++++++++------- daemon/test/test.sh | 27 ++++--- 4 files changed, 173 insertions(+), 105 deletions(-) diff --git a/daemon/configdir.c b/daemon/configdir.c index 10cf6656acfa..8bfa5c973999 100644 --- a/daemon/configdir.c +++ b/daemon/configdir.c @@ -1,7 +1,6 @@ #include "configdir.h" #include "log.h" #include -#include #include #include #include @@ -31,65 +30,3 @@ void configdir_register_opts(const tal_t *ctx, rpc_filename, "Set JSON-RPC socket (or /dev/tty)"); } - -/* FIXME: make this nicer! */ -static void config_log_stderr_exit(const char *fmt, ...) -{ - char *msg; - va_list ap; - - va_start(ap, fmt); - - /* This is the format we expect: mangle it to remove '--'. */ - if (streq(fmt, "%s: %.*s: %s")) { - const char *argv0 = va_arg(ap, const char *); - unsigned int len = va_arg(ap, unsigned int); - const char *arg = va_arg(ap, const char *); - const char *problem = va_arg(ap, const char *); - - msg = tal_fmt(NULL, "%s line %s: %.*s: %s", - argv0, arg+strlen(arg)+1, len-2, arg+2, problem); - } else { - msg = tal_vfmt(NULL, fmt, ap); - } - va_end(ap); - - fatal("%s", msg); -} - -/* We turn the config file into cmdline arguments. */ -void opt_parse_from_config(const tal_t *ctx) -{ - char *contents, **lines; - char **argv; - int i, argc; - - contents = grab_file(ctx, "config"); - /* Doesn't have to exist. */ - if (!contents) { - if (errno != ENOENT) - fatal("Opening and reading config: %s", - strerror(errno)); - return; - } - - lines = tal_strsplit(contents, contents, "\r\n", STR_NO_EMPTY); - - /* We have to keep argv around, since opt will point into it */ - argv = tal_arr(ctx, char *, argc = 1); - argv[0] = "lightning config file"; - - for (i = 0; i < tal_count(lines) - 1; i++) { - if (strstarts(lines[i], "#")) - continue; - /* Only valid forms are "foo" and "foo=bar" */ - tal_resize(&argv, argc+1); - /* Stash line number after nul. */ - argv[argc++] = tal_fmt(argv, "--%s%c%u", lines[i], 0, i+1); - } - tal_resize(&argv, argc+1); - argv[argc] = NULL; - - opt_parse(&argc, argv, config_log_stderr_exit); - tal_free(contents); -} diff --git a/daemon/configdir.h b/daemon/configdir.h index 57cdc2b86f16..4e2d4c9f138c 100644 --- a/daemon/configdir.h +++ b/daemon/configdir.h @@ -6,6 +6,4 @@ void configdir_register_opts(const tal_t *ctx, char **config_dir, char **rpc_filename); -void opt_parse_from_config(const tal_t *ctx); - #endif /* LIGHTNING_DAEMON_CONFIGDIR_H */ diff --git a/daemon/lightningd.c b/daemon/lightningd.c index 46038a39bc52..18b2e4870f67 100644 --- a/daemon/lightningd.c +++ b/daemon/lightningd.c @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -29,6 +30,11 @@ #include #include +/* FIXME: Put into ccan/time. */ +#define TIME_FROM_SEC(sec) { { .tv_nsec = 0, .tv_sec = sec } } +#define TIME_FROM_MSEC(msec) \ + { { .tv_nsec = ((msec) % 1000) * 1000000, .tv_sec = (msec) / 1000 } } + static char *opt_set_u64(const char *arg, u64 *u) { char *endp; @@ -160,22 +166,77 @@ static void dev_register_opts(struct lightningd_state *dstate) opt_register_noarg("--dev-no-routefail", opt_set_bool, &dstate->dev_never_routefail, opt_hidden); } - -static void default_config(struct config *config) -{ - config->regtest = false; +static const struct config testnet_config = { + /* 6 blocks to catch cheating attempts. */ + .locktime_blocks = 6, + + /* They can have up to 3 days. */ + .locktime_max = 3 * 6 * 24, + + /* We're fairly trusting, under normal circumstances. */ + .anchor_confirms = 1, + + /* More than 10 confirms seems overkill. */ + .anchor_confirms_max = 10, + + /* At some point, you've got to let it go... */ + /* BOLT #onchain: + * + * Outputs... are considered *irrevocably resolved* once they + * are included in a block at least 100 deep on the most-work + * blockchain. 100 blocks is far greater than the longest + * known bitcoin fork, and the same value used to wait for + * confirmations of miner's rewards[1]. + */ + .forever_confirms = 10, + + /* Testnet fees are crazy, allow infinite feerange. */ + .commitment_fee_min_percent = 0, + .commitment_fee_max_percent = 0, + + /* We offer to pay 5 times 2-block fee */ + .commitment_fee_percent = 500, + + /* Use this rate by default if estimatefee doesn't estimate. */ + .default_fee_rate = 40000, + + /* Don't bother me unless I have 6 hours to collect. */ + .min_htlc_expiry = 6 * 6, + /* Don't lock up channel for more than 5 days. */ + .max_htlc_expiry = 5 * 6 * 24, + + /* If we're closing on HTLC expiry, and you're unresponsive, we abort. */ + .deadline_blocks = 4, + + /* How often to bother bitcoind. */ + .poll_time = TIME_FROM_SEC(10), + + /* Send commit 10msec after receiving; almost immediately. */ + .commit_time = TIME_FROM_MSEC(10), + + /* Allow dust payments */ + .fee_base = 1, + /* Take 0.001% */ + .fee_per_satoshi = 10, + + /* Discover new peers using IRC */ + .use_irc = true, +}; + +/* aka. "Dude, where's my coins?" */ +static const struct config mainnet_config = { /* ~one day to catch cheating attempts. */ - config->locktime_blocks = 6 * 24; + .locktime_blocks = 6 * 24, /* They can have up to 3 days. */ - config->locktime_max = 3 * 6 * 24; + .locktime_max = 3 * 6 * 24, /* We're fairly trusting, under normal circumstances. */ - config->anchor_confirms = 3; + .anchor_confirms = 3, /* More than 10 confirms seems overkill. */ - config->anchor_confirms_max = 10; + .anchor_confirms_max = 10, /* At some point, you've got to let it go... */ /* BOLT #onchain: @@ -186,40 +247,40 @@ static void default_config(struct config *config) * known bitcoin fork, and the same value used to wait for * confirmations of miner's rewards[1]. */ - config->forever_confirms = 100; - + .forever_confirms = 100, + /* Insist between 2 and 20 times the 2-block fee. */ - config->commitment_fee_min_percent = 200; - config->commitment_fee_max_percent = 2000; + .commitment_fee_min_percent = 200, + .commitment_fee_max_percent = 2000, /* We offer to pay 5 times 2-block fee */ - config->commitment_fee_percent = 500; + .commitment_fee_percent = 500, /* Use this rate by default if estimatefee doesn't estimate. */ - config->default_fee_rate = 40000; + .default_fee_rate = 40000, /* Don't bother me unless I have 6 hours to collect. */ - config->min_htlc_expiry = 6 * 6; + .min_htlc_expiry = 6 * 6, /* Don't lock up channel for more than 5 days. */ - config->max_htlc_expiry = 5 * 6 * 24; + .max_htlc_expiry = 5 * 6 * 24, /* If we're closing on HTLC expiry, and you're unresponsive, we abort. */ - config->deadline_blocks = 10; + .deadline_blocks = 10, /* How often to bother bitcoind. */ - config->poll_time = time_from_sec(30); + .poll_time = TIME_FROM_SEC(30), /* Send commit 10msec after receiving; almost immediately. */ - config->commit_time = time_from_msec(10); + .commit_time = TIME_FROM_MSEC(10), /* Discourage dust payments */ - config->fee_base = 546000; + .fee_base = 546000, /* Take 0.001% */ - config->fee_per_satoshi = 10; + .fee_per_satoshi = 10, /* Discover new peers using IRC */ - config->use_irc = true; -} + .use_irc = true, +}; static void check_config(struct lightningd_state *dstate) { @@ -231,7 +292,7 @@ static void check_config(struct lightningd_state *dstate) dstate->config.commitment_fee_min_percent, dstate->config.commitment_fee_max_percent); - if (dstate->config.forever_confirms < 100) + if (dstate->config.forever_confirms < 100 && !dstate->testnet) log_unusual(dstate->base_log, "Warning: forever-confirms of %u is less than 100!", dstate->config.forever_confirms); @@ -267,7 +328,6 @@ static struct lightningd_state *lightningd_state(void) txowatch_hash_init(&dstate->txowatches); dstate->secpctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - default_config(&dstate->config); list_head_init(&dstate->bitcoin_req); list_head_init(&dstate->wallet); list_head_init(&dstate->unpaid); @@ -301,6 +361,80 @@ static void tal_freefn(void *ptr) tal_free(ptr); } +static void setup_default_config(struct lightningd_state *dstate) +{ + if (dstate->testnet) + dstate->config = testnet_config; + else + dstate->config = mainnet_config; +} + + +/* FIXME: make this nicer! */ +static void config_log_stderr_exit(const char *fmt, ...) +{ + char *msg; + va_list ap; + + va_start(ap, fmt); + + /* This is the format we expect: mangle it to remove '--'. */ + if (streq(fmt, "%s: %.*s: %s")) { + const char *argv0 = va_arg(ap, const char *); + unsigned int len = va_arg(ap, unsigned int); + const char *arg = va_arg(ap, const char *); + const char *problem = va_arg(ap, const char *); + + msg = tal_fmt(NULL, "%s line %s: %.*s: %s", + argv0, arg+strlen(arg)+1, len-2, arg+2, problem); + } else { + msg = tal_vfmt(NULL, fmt, ap); + } + va_end(ap); + + fatal("%s", msg); +} + +/* We turn the config file into cmdline arguments. */ +static void opt_parse_from_config(struct lightningd_state *dstate) +{ + char *contents, **lines; + char **argv; + int i, argc; + + contents = grab_file(dstate, "config"); + /* Doesn't have to exist. */ + if (!contents) { + if (errno != ENOENT) + fatal("Opening and reading config: %s", + strerror(errno)); + return; + } + + lines = tal_strsplit(contents, contents, "\r\n", STR_NO_EMPTY); + + /* We have to keep argv around, since opt will point into it */ + argv = tal_arr(dstate, char *, argc = 1); + argv[0] = "lightning config file"; + + for (i = 0; i < tal_count(lines) - 1; i++) { + if (strstarts(lines[i], "#")) + continue; + /* Only valid forms are "foo" and "foo=bar" */ + tal_resize(&argv, argc+1); + /* Stash line number after nul. */ + argv[argc++] = tal_fmt(argv, "--%s%c%u", lines[i], 0, i+1); + } + tal_resize(&argv, argc+1); + argv[argc] = NULL; + + opt_early_parse(argc, argv, config_log_stderr_exit); + setup_default_config(dstate); + + opt_parse(&argc, argv, config_log_stderr_exit); + tal_free(contents); +} + int main(int argc, char *argv[]) { struct lightningd_state *dstate = lightningd_state(); @@ -330,7 +464,7 @@ int main(int argc, char *argv[]) config_register_opts(dstate); dev_register_opts(dstate); - /* Get any configdir options first. */ + /* Get any configdir/testnet options first. */ opt_early_parse(argc, argv, opt_log_stderr_exit); /* Move to config dir, to save ourselves the hassle of path manip. */ diff --git a/daemon/test/test.sh b/daemon/test/test.sh index fbc8f666d403..3da669d1dc92 100755 --- a/daemon/test/test.sh +++ b/daemon/test/test.sh @@ -472,7 +472,6 @@ DO_RECONNECT=$RECONNECT if [ -n "$TIMEOUT_ANCHOR" ]; then # Check anchor emitted, not mined deep enough. check_tx_spend lcli1 - $CLI generate 2 # Timeout before anchor committed. MOCKTIME=$((`date +%s` + 7200 + 3 * 1200 + 1)) @@ -517,9 +516,9 @@ if [ -n "$TIMEOUT_ANCHOR" ]; then all_ok fi -# Now make it pass anchor (should be in mempool: three blocks bury it) +# Now make it pass anchor (should be in mempool: one block to bury it) check_tx_spend lcli1 -$CLI generate 3 +$CLI generate 1 check_peerstate lcli1 STATE_NORMAL check_peerstate lcli2 STATE_NORMAL @@ -584,8 +583,8 @@ if [ -n "$DIFFERENT_FEES" ]; then $CLI generate 1 check_peerstate lcli1 STATE_CLOSE_ONCHAIN_MUTUAL check_peerstate lcli2 STATE_CLOSE_ONCHAIN_MUTUAL - # Give it 100 blocks. - $CLI generate 100 + # Give it 10 blocks ie "forever" + $CLI generate 10 check_no_peers lcli1 check_no_peers lcli2 @@ -686,8 +685,8 @@ if [ -n "$DUMP_ONCHAIN" ]; then # lcli1 should have gotten HTLC back. check_tx_spend lcli1 - # Now, after 100 blocks, should all be concluded. - $CLI generate 100 + # Now, after 10 blocks, should all be concluded. + $CLI generate 10 # Both consider it all done now. check_no_peers lcli1 @@ -786,8 +785,8 @@ if [ -n "$STEAL" ]; then check_peerstate lcli2 STATE_CLOSE_ONCHAIN_CHEATED check_tx_spend lcli2 - # Give it 100 blocks. - $CLI generate 100 + # Give it "forever" blocks. + $CLI generate 10 check_no_peers lcli2 @@ -880,8 +879,8 @@ if [ -n "$CLOSE_WITH_HTLCS" ]; then check_peerstate lcli1 STATE_CLOSE_ONCHAIN_MUTUAL check_peerstate lcli2 STATE_CLOSE_ONCHAIN_MUTUAL - # Give it 100 blocks. - $CLI generate 99 + # Give it "forever" blocks. + $CLI generate 9 check_no_peers lcli1 check_no_peers lcli2 @@ -1035,7 +1034,7 @@ if [ ! -n "$MANUALCOMMIT" ]; then lcli2 connect localhost $PORT3 $TX2 check_tx_spend lcli2 - $CLI generate 3 + $CLI generate 1 # Make sure it's STATE_NORMAL. check_peerstate lcli3 STATE_NORMAL @@ -1143,8 +1142,8 @@ $CLI generate 1 check_peerstate lcli1 STATE_CLOSE_ONCHAIN_MUTUAL check_peerstate lcli2 STATE_CLOSE_ONCHAIN_MUTUAL -# Give it 99 blocks. -$CLI generate 98 +# Give it forever-1 blocks. +$CLI generate 8 # Make sure they saw it! check_peerstate lcli1 STATE_CLOSE_ONCHAIN_MUTUAL