From c553bba7a8a517f484ba4ecca301bd1f849c8c83 Mon Sep 17 00:00:00 2001 From: Mark Beckwith Date: Mon, 27 Aug 2018 09:19:09 -0500 Subject: [PATCH] param: getroute fuzz now uses json_tok_percent Signed-off-by: Mark Beckwith --- lightningd/gossip_control.c | 8 +------- lightningd/json.c | 15 +++++++++++++++ lightningd/json.h | 5 +++++ lightningd/payalgo.c | 16 ---------------- 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 2b9495f0b..559b6c534 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -293,16 +293,10 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok p_opt_def("cltv", json_tok_number, &cltv, 9), p_opt_def("fromid", json_tok_pubkey, &source, ld->id), p_opt("seed", json_tok_tok, &seedtok), - p_opt_def("fuzzpercent", json_tok_double, &fuzz, 75.0), + p_opt_def("fuzzpercent", json_tok_percent, &fuzz, 75.0), NULL)) return; - if (!(0.0 <= *fuzz && *fuzz <= 100.0)) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "fuzz must be in range 0.0 <= %f <= 100.0", - *fuzz); - return; - } /* Convert from percentage */ *fuzz = *fuzz / 100.0; diff --git a/lightningd/json.c b/lightningd/json.c index 40f398a03..70ca770bb 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -159,6 +159,21 @@ bool json_tok_sha256(struct command *cmd, const char *name, return false; } +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; +} + bool json_tok_u64(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, uint64_t **num) diff --git a/lightningd/json.h b/lightningd/json.h index 069042ddf..25f85a21e 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -63,6 +63,11 @@ bool json_tok_sha256(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, struct sha256 **hash); +/* Extract double in range [0.0, 100.0] */ +bool json_tok_percent(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + double **num); + /* Extract a pubkey from this */ bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, struct pubkey *pubkey); diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index 04f698262..21eecad73 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -596,22 +596,6 @@ 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) {