process_estimatefee: fix bad logic.

Refactor the fallback to make it more robust.

Fixes: #28
Reported-by: Jacob <jacobdnd@gmail.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2016-07-19 12:52:18 +09:30
parent 6199b886fa
commit 6015cedee8

View File

@ -176,7 +176,34 @@ start_bitcoin_cli(struct lightningd_state *dstate,
next_bcli(dstate); next_bcli(dstate);
} }
static void process_estimatefee(struct bitcoin_cli *bcli) static void process_estimatefee_6(struct bitcoin_cli *bcli)
{
double fee;
char *p, *end;
u64 fee_rate;
void (*cb)(struct lightningd_state *, u64, void *) = bcli->cb;
p = tal_strndup(bcli, bcli->output, bcli->output_bytes);
fee = strtod(p, &end);
if (end == p || *end != '\n')
fatal("%s: gave non-numeric fee %s",
bcli_args(bcli), p);
if (fee < 0) {
log_unusual(bcli->dstate->base_log,
"Unable to estimate fee, using %"PRIu64,
bcli->dstate->config.closing_fee_rate);
fee_rate = bcli->dstate->config.closing_fee_rate;
} else {
/* Since we used 6 as an estimate, double it. */
fee *= 2;
fee_rate = fee * 100000000;
}
cb(bcli->dstate, fee_rate, bcli->cb_arg);
}
static void process_estimatefee_2(struct bitcoin_cli *bcli)
{ {
double fee; double fee;
char *p, *end; char *p, *end;
@ -191,23 +218,12 @@ static void process_estimatefee(struct bitcoin_cli *bcli)
/* Don't know at 2? Try 6... */ /* Don't know at 2? Try 6... */
if (fee < 0) { if (fee < 0) {
if (streq(bcli->args[3], "2")) { start_bitcoin_cli(bcli->dstate, process_estimatefee_6,
start_bitcoin_cli(bcli->dstate, process_estimatefee,
false, bcli->cb, bcli->cb_arg, false, bcli->cb, bcli->cb_arg,
"estimatefee", "6", NULL); "estimatefee", "6", NULL);
return; return;
} }
log_unusual(bcli->dstate->base_log,
"Unable to estimate fee, using %"PRIu64,
bcli->dstate->config.closing_fee_rate);
fee_rate = bcli->dstate->config.closing_fee_rate;
} else {
/* If we used 6 as an estimate, double it. */
if (streq(bcli->args[3], "6"))
fee *= 2;
fee_rate = fee * 100000000; fee_rate = fee * 100000000;
}
cb(bcli->dstate, fee_rate, bcli->cb_arg); cb(bcli->dstate, fee_rate, bcli->cb_arg);
} }
@ -216,7 +232,7 @@ void bitcoind_estimate_fee_(struct lightningd_state *dstate,
u64, void *), u64, void *),
void *arg) void *arg)
{ {
start_bitcoin_cli(dstate, process_estimatefee, false, cb, arg, start_bitcoin_cli(dstate, process_estimatefee_2, false, cb, arg,
"estimatefee", "2", NULL); "estimatefee", "2", NULL);
} }