diff --git a/common/json.c b/common/json.c index 2db012c2d..1383b877d 100644 --- a/common/json.c +++ b/common/json.c @@ -66,7 +66,7 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, return true; } -bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *num) +bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num) { char *end; @@ -78,7 +78,7 @@ bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *num) bool json_tok_percent(const char *buffer, const jsmntok_t *tok, double *num) { - if (!json_tok_double(buffer, tok, num)) + if (!json_to_double(buffer, tok, num)) return false; /* Ensure it is in the range [0.0, 100.0] */ diff --git a/common/json.h b/common/json.h index 1cf45d0ba..fc175dd5d 100644 --- a/common/json.h +++ b/common/json.h @@ -33,7 +33,7 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, uint64_t *num); /* Extract double from this (must be a number literal) */ -bool json_tok_double(const char *buffer, const jsmntok_t *tok, double *num); +bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num); /* Extract satoshis from this (may be a string, or a decimal number literal) */ bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index 6d3781c7a..95a6e3b9d 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -303,7 +303,7 @@ static bool extract_feerate(struct bitcoin_cli *bcli, if (!feeratetok) return false; - return json_tok_double(output, feeratetok, feerate); + return json_to_double(output, feeratetok, feerate); } struct estimatefee { diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index becf3bece..586952d22 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -277,34 +277,34 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok const jsmntok_t *seedtok; u64 *msatoshi; unsigned *cltv; - double riskfactor; + double *riskfactor; /* Higher fuzz means that some high-fee paths can be discounted * for an even larger value, increasing the scope for route * randomization (the higher-fee paths become more likely to * be selected) at the cost of increasing the probability of * selecting the higher-fee paths. */ - double fuzz; + double *fuzz; struct siphash_seed seed; if (!param(cmd, buffer, params, p_req("id", json_tok_pubkey, &destination), p_req_tal("msatoshi", json_tok_u64, &msatoshi), - p_req("riskfactor", json_tok_double, &riskfactor), + p_req_tal("riskfactor", json_tok_double, &riskfactor), p_opt_def_tal("cltv", json_tok_number, &cltv, 9), p_opt_def("fromid", json_tok_pubkey, &source, ld->id), - p_opt_def("fuzzpercent", json_tok_double, &fuzz, 75.0), p_opt_tal("seed", json_tok_tok, &seedtok), + p_opt_def_tal("fuzzpercent", json_tok_double, &fuzz, 75.0), NULL)) return; - if (!(0.0 <= fuzz && fuzz <= 100.0)) { + if (!(0.0 <= *fuzz && *fuzz <= 100.0)) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "fuzz must be in range 0.0 <= %f <= 100.0", - fuzz); + *fuzz); return; } /* Convert from percentage */ - fuzz = fuzz / 100.0; + *fuzz = *fuzz / 100.0; if (seedtok) { if (seedtok->end - seedtok->start > sizeof(seed)) @@ -317,7 +317,9 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok } else randombytes_buf(&seed, sizeof(seed)); - u8 *req = towire_gossip_getroute_request(cmd, &source, &destination, *msatoshi, riskfactor*1000, *cltv, &fuzz, &seed); + u8 *req = towire_gossip_getroute_request(cmd, &source, &destination, + *msatoshi, *riskfactor * 1000, + *cltv, fuzz, &seed); subd_req(ld->gossip, ld->gossip, req, -1, 0, json_getroute_reply, cmd); command_still_pending(cmd); } diff --git a/lightningd/json.c b/lightningd/json.c index b3792a431..2488c4a95 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -91,6 +91,20 @@ void json_add_txid(struct json_result *result, const char *fieldname, json_add_string(result, fieldname, hex); } +bool json_tok_double(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)) { + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a double, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; + } + return true; +} + bool json_tok_number(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, unsigned int **num) diff --git a/lightningd/json.h b/lightningd/json.h index 7dc26f199..fc4acee44 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -41,6 +41,11 @@ void json_add_pubkey(struct json_result *response, void json_add_txid(struct json_result *result, const char *fieldname, const struct bitcoin_txid *txid); +/* Extract double from this (must be a number literal) */ +bool json_tok_double(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + double **num); + /* Extract number from this (may be a string, or a number literal) */ bool json_tok_number(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, diff --git a/lightningd/param.c b/lightningd/param.c index 7f7aa1e7e..4e9c12f37 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -51,7 +51,6 @@ struct fail_format { static struct fail_format fail_formats[] = { {json_tok_bool, "'%s' should be 'true' or 'false', not '%.*s'"}, - {json_tok_double, "'%s' should be a double, not '%.*s'"}, {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'"}, diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index f9809cd74..cb0557d9c 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -600,7 +600,7 @@ static void json_pay(struct command *cmd, const char *buffer, const jsmntok_t *params) { const jsmntok_t *bolt11tok, *desctok; - double riskfactor; + double *riskfactor; double maxfeepercent; u64 *msatoshi; struct pay *pay = tal(cmd, struct pay); @@ -612,9 +612,9 @@ static void json_pay(struct command *cmd, if (!param(cmd, buffer, params, p_req_tal("bolt11", json_tok_tok, &bolt11tok), - p_opt_tal("description", json_tok_tok, &desctok), p_opt_tal("msatoshi", json_tok_u64, &msatoshi), - p_opt_def("riskfactor", json_tok_double, &riskfactor, 1.0), + 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("retry_for", json_tok_number, &retryfor, 60), p_opt_def_tal("maxdelay", json_tok_number, &maxdelay, @@ -661,7 +661,7 @@ static void json_pay(struct command *cmd, } } pay->msatoshi = *msatoshi; - pay->riskfactor = riskfactor * 1000; + pay->riskfactor = *riskfactor * 1000; pay->maxfeepercent = maxfeepercent; if (*maxdelay < pay->min_final_cltv_expiry) { diff --git a/lightningd/test/run-param.c b/lightningd/test/run-param.c index c7b1c8ca5..7b9f63e06 100644 --- a/lightningd/test/run-param.c +++ b/lightningd/test/run-param.c @@ -137,10 +137,10 @@ struct sanity buffers[] = { static void stest(const struct json *j, struct sanity *b) { u64 *ival; - double dval; + double *dval; if (!param(cmd, j->buffer, j->toks, p_req_tal("u64", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), NULL)) { + p_req_tal("double", json_tok_double, &dval), NULL)) { assert(check_fail()); assert(b->failed == true); if (!strstr(fail_msg, b->fail_str)) { @@ -151,7 +151,7 @@ static void stest(const struct json *j, struct sanity *b) assert(!check_fail()); assert(b->failed == false); assert(*ival == 42); - assert(dval > 3.1499 && b->dval < 3.1501); + assert(*dval > 3.1499 && b->dval < 3.1501); } } @@ -204,10 +204,10 @@ static void dup_names(void) "{ 'u64' : '42', 'u64' : '43', 'double' : '3.15' }"); u64 *i; - double d; + double *d; assert(!param(cmd, j->buffer, j->toks, p_req_tal("u64", json_tok_u64, &i), - p_req("double", json_tok_double, &d), NULL)); + p_req_tal("double", json_tok_double, &d), NULL)); } static void null_params(void) @@ -259,28 +259,28 @@ static void bad_programmer(void) { u64 *ival; u64 *ival2; - double dval; + double *dval; struct json *j = json_parse(cmd, "[ '25', '546', '26' ]"); /* check for repeated names */ assert(!param(cmd, j->buffer, j->toks, p_req_tal("repeat", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), + p_req_tal("double", json_tok_double, &dval), p_req_tal("repeat", json_tok_u64, &ival2), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); assert(!param(cmd, j->buffer, j->toks, p_req_tal("repeat", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), + p_req_tal("double", json_tok_double, &dval), p_req_tal("repeat", json_tok_u64, &ival), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); assert(!param(cmd, j->buffer, j->toks, p_req_tal("u64", json_tok_u64, &ival), - p_req("repeat", json_tok_double, &dval), - p_req("repeat", json_tok_double, &dval), NULL)); + p_req_tal("repeat", json_tok_double, &dval), + p_req_tal("repeat", json_tok_double, &dval), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); @@ -299,12 +299,12 @@ static void bad_programmer(void) /* Add required param after optional */ j = json_parse(cmd, "[ '25', '546', '26', '1.1' ]"); unsigned int *msatoshi; - double riskfactor; + double *riskfactor; assert(!param(cmd, j->buffer, j->toks, p_req_tal("u64", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), + p_req_tal("double", json_tok_double, &dval), p_opt_def_tal("msatoshi", json_tok_number, &msatoshi, 100), - p_req("riskfactor", json_tok_double, &riskfactor), NULL)); + p_req_tal("riskfactor", json_tok_double, &riskfactor), NULL)); assert(*msatoshi); assert(*msatoshi == 100); assert(check_fail());