Skip to content

Commit

Permalink
param: upgraded json_tok_percent
Browse files Browse the repository at this point in the history
Made it a local static since its only used once.

Signed-off-by: Mark Beckwith <[email protected]>
  • Loading branch information
wythe authored and rustyrussell committed Aug 20, 2018
1 parent 47555ef commit 947752b
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 23 deletions.
15 changes: 0 additions & 15 deletions common/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
3 changes: 0 additions & 3 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 0 additions & 2 deletions lightningd/param.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down
22 changes: 19 additions & 3 deletions lightningd/payalgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 947752b

Please sign in to comment.