diff --git a/common/json.c b/common/json.c index 71dc9b382382..162d7a625a15 100644 --- a/common/json.c +++ b/common/json.c @@ -76,21 +76,6 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num) return true; } -bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num) -{ - if (!json_to_double(buffer, tok, num)) - return false; - - /* Ensure it is in the range [0.0, 100.0] */ - if (!(0.0 <= *num)) - return false; - - if (!(*num <= 100.0)) - return false; - - return true; -} - bool json_to_number(const char *buffer, const jsmntok_t *tok, unsigned int *num) { diff --git a/common/json.h b/common/json.h index 6a6b75abdc99..aa56d188bd24 100644 --- a/common/json.h +++ b/common/json.h @@ -39,9 +39,6 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num); bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok, uint64_t *satoshi); -/* Extract double in range [0.0, 100.0] */ -bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num); - /* Extract sha256 hash */ bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, struct sha256 *hash); diff --git a/lightningd/param.c b/lightningd/param.c index 376c6d7e2afb..d51fe9c8360f 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -50,8 +50,6 @@ struct fail_format { }; static struct fail_format fail_formats[] = { - {json_tok_percent, - "'%s' should be a double in range [0.0, 100.0], not '%.*s'"}, {json_tok_newaddr, "'%s' should be 'bech32' or 'p2sh-segwit', not '%.*s'"}, {json_tok_wtx, "'%s' should be 'all' or a positive integer greater than " diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index cb0557d9c399..bec0db98dbae 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -596,12 +596,28 @@ static void json_pay_stop_retrying(struct pay *pay) json_pay_failure(pay, sr); } +/* Extract double in range [0.0, 100.0] */ +static bool json_tok_percent(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + double **num) +{ + *num = tal(cmd, double); + if (json_to_double(buffer, tok, *num)) + if (**num >= 0.0 && **num >= 100.0) + return true; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a double in range [0.0, 100.0], not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + static void json_pay(struct command *cmd, const char *buffer, const jsmntok_t *params) { const jsmntok_t *bolt11tok, *desctok; double *riskfactor; - double maxfeepercent; + double *maxfeepercent; u64 *msatoshi; struct pay *pay = tal(cmd, struct pay); struct bolt11 *b11; @@ -615,7 +631,7 @@ static void json_pay(struct command *cmd, p_opt_tal("msatoshi", json_tok_u64, &msatoshi), p_opt_tal("description", json_tok_tok, &desctok), p_opt_def_tal("riskfactor", json_tok_double, &riskfactor, 1.0), - p_opt_def("maxfeepercent", json_tok_percent, &maxfeepercent, 0.5), + p_opt_def_tal("maxfeepercent", json_tok_percent, &maxfeepercent, 0.5), p_opt_def_tal("retry_for", json_tok_number, &retryfor, 60), p_opt_def_tal("maxdelay", json_tok_number, &maxdelay, cmd->ld->config.locktime_max), @@ -662,7 +678,7 @@ static void json_pay(struct command *cmd, } pay->msatoshi = *msatoshi; pay->riskfactor = *riskfactor * 1000; - pay->maxfeepercent = maxfeepercent; + pay->maxfeepercent = *maxfeepercent; if (*maxdelay < pay->min_final_cltv_expiry) { command_fail(cmd, JSONRPC2_INVALID_PARAMS,