Browse Source

param: upgraded json_tok_double

Also renamed old version to json_to_double for use as a utility funciton.

Signed-off-by: Mark Beckwith <wythe@intrig.com>
ppa-0.6.1
Mark Beckwith 7 years ago
committed by Rusty Russell
parent
commit
e5918f4e5a
  1. 4
      common/json.c
  2. 2
      common/json.h
  3. 2
      lightningd/bitcoind.c
  4. 18
      lightningd/gossip_control.c
  5. 14
      lightningd/json.c
  6. 5
      lightningd/json.h
  7. 1
      lightningd/param.c
  8. 8
      lightningd/payalgo.c
  9. 26
      lightningd/test/run-param.c

4
common/json.c

@ -66,7 +66,7 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok,
return true; 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; 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) 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; return false;
/* Ensure it is in the range [0.0, 100.0] */ /* Ensure it is in the range [0.0, 100.0] */

2
common/json.h

@ -33,7 +33,7 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok,
uint64_t *num); uint64_t *num);
/* Extract double from this (must be a number literal) */ /* 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) */ /* 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, bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok,

2
lightningd/bitcoind.c

@ -303,7 +303,7 @@ static bool extract_feerate(struct bitcoin_cli *bcli,
if (!feeratetok) if (!feeratetok)
return false; return false;
return json_tok_double(output, feeratetok, feerate); return json_to_double(output, feeratetok, feerate);
} }
struct estimatefee { struct estimatefee {

18
lightningd/gossip_control.c

@ -277,34 +277,34 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok
const jsmntok_t *seedtok; const jsmntok_t *seedtok;
u64 *msatoshi; u64 *msatoshi;
unsigned *cltv; unsigned *cltv;
double riskfactor; double *riskfactor;
/* Higher fuzz means that some high-fee paths can be discounted /* Higher fuzz means that some high-fee paths can be discounted
* for an even larger value, increasing the scope for route * for an even larger value, increasing the scope for route
* randomization (the higher-fee paths become more likely to * randomization (the higher-fee paths become more likely to
* be selected) at the cost of increasing the probability of * be selected) at the cost of increasing the probability of
* selecting the higher-fee paths. */ * selecting the higher-fee paths. */
double fuzz; double *fuzz;
struct siphash_seed seed; struct siphash_seed seed;
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req("id", json_tok_pubkey, &destination), p_req("id", json_tok_pubkey, &destination),
p_req_tal("msatoshi", json_tok_u64, &msatoshi), 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_tal("cltv", json_tok_number, &cltv, 9),
p_opt_def("fromid", json_tok_pubkey, &source, ld->id), 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_tal("seed", json_tok_tok, &seedtok),
p_opt_def_tal("fuzzpercent", json_tok_double, &fuzz, 75.0),
NULL)) NULL))
return; return;
if (!(0.0 <= fuzz && fuzz <= 100.0)) { if (!(0.0 <= *fuzz && *fuzz <= 100.0)) {
command_fail(cmd, JSONRPC2_INVALID_PARAMS, command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"fuzz must be in range 0.0 <= %f <= 100.0", "fuzz must be in range 0.0 <= %f <= 100.0",
fuzz); *fuzz);
return; return;
} }
/* Convert from percentage */ /* Convert from percentage */
fuzz = fuzz / 100.0; *fuzz = *fuzz / 100.0;
if (seedtok) { if (seedtok) {
if (seedtok->end - seedtok->start > sizeof(seed)) if (seedtok->end - seedtok->start > sizeof(seed))
@ -317,7 +317,9 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok
} else } else
randombytes_buf(&seed, sizeof(seed)); 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); subd_req(ld->gossip, ld->gossip, req, -1, 0, json_getroute_reply, cmd);
command_still_pending(cmd); command_still_pending(cmd);
} }

14
lightningd/json.c

@ -91,6 +91,20 @@ void json_add_txid(struct json_result *result, const char *fieldname,
json_add_string(result, fieldname, hex); 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, bool json_tok_number(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok, const char *buffer, const jsmntok_t *tok,
unsigned int **num) unsigned int **num)

5
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, void json_add_txid(struct json_result *result, const char *fieldname,
const struct bitcoin_txid *txid); 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) */ /* Extract number from this (may be a string, or a number literal) */
bool json_tok_number(struct command *cmd, const char *name, bool json_tok_number(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok, const char *buffer, const jsmntok_t *tok,

1
lightningd/param.c

@ -51,7 +51,6 @@ struct fail_format {
static struct fail_format fail_formats[] = { static struct fail_format fail_formats[] = {
{json_tok_bool, "'%s' should be 'true' or 'false', not '%.*s'"}, {json_tok_bool, "'%s' should be 'true' or 'false', not '%.*s'"},
{json_tok_double, "'%s' should be a double, not '%.*s'"},
{json_tok_percent, {json_tok_percent,
"'%s' should be a double in range [0.0, 100.0], not '%.*s'"}, "'%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_newaddr, "'%s' should be 'bech32' or 'p2sh-segwit', not '%.*s'"},

8
lightningd/payalgo.c

@ -600,7 +600,7 @@ static void json_pay(struct command *cmd,
const char *buffer, const jsmntok_t *params) const char *buffer, const jsmntok_t *params)
{ {
const jsmntok_t *bolt11tok, *desctok; const jsmntok_t *bolt11tok, *desctok;
double riskfactor; double *riskfactor;
double maxfeepercent; double maxfeepercent;
u64 *msatoshi; u64 *msatoshi;
struct pay *pay = tal(cmd, struct pay); struct pay *pay = tal(cmd, struct pay);
@ -612,9 +612,9 @@ static void json_pay(struct command *cmd,
if (!param(cmd, buffer, params, if (!param(cmd, buffer, params,
p_req_tal("bolt11", json_tok_tok, &bolt11tok), 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_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("maxfeepercent", json_tok_percent, &maxfeepercent, 0.5),
p_opt_def_tal("retry_for", json_tok_number, &retryfor, 60), p_opt_def_tal("retry_for", json_tok_number, &retryfor, 60),
p_opt_def_tal("maxdelay", json_tok_number, &maxdelay, p_opt_def_tal("maxdelay", json_tok_number, &maxdelay,
@ -661,7 +661,7 @@ static void json_pay(struct command *cmd,
} }
} }
pay->msatoshi = *msatoshi; pay->msatoshi = *msatoshi;
pay->riskfactor = riskfactor * 1000; pay->riskfactor = *riskfactor * 1000;
pay->maxfeepercent = maxfeepercent; pay->maxfeepercent = maxfeepercent;
if (*maxdelay < pay->min_final_cltv_expiry) { if (*maxdelay < pay->min_final_cltv_expiry) {

26
lightningd/test/run-param.c

@ -137,10 +137,10 @@ struct sanity buffers[] = {
static void stest(const struct json *j, struct sanity *b) static void stest(const struct json *j, struct sanity *b)
{ {
u64 *ival; u64 *ival;
double dval; double *dval;
if (!param(cmd, j->buffer, j->toks, if (!param(cmd, j->buffer, j->toks,
p_req_tal("u64", json_tok_u64, &ival), 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(check_fail());
assert(b->failed == true); assert(b->failed == true);
if (!strstr(fail_msg, b->fail_str)) { 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(!check_fail());
assert(b->failed == false); assert(b->failed == false);
assert(*ival == 42); 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' : '42', 'u64' : '43', 'double' : '3.15' }");
u64 *i; u64 *i;
double d; double *d;
assert(!param(cmd, j->buffer, j->toks, assert(!param(cmd, j->buffer, j->toks,
p_req_tal("u64", json_tok_u64, &i), 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) static void null_params(void)
@ -259,28 +259,28 @@ static void bad_programmer(void)
{ {
u64 *ival; u64 *ival;
u64 *ival2; u64 *ival2;
double dval; double *dval;
struct json *j = json_parse(cmd, "[ '25', '546', '26' ]"); struct json *j = json_parse(cmd, "[ '25', '546', '26' ]");
/* check for repeated names */ /* check for repeated names */
assert(!param(cmd, j->buffer, j->toks, assert(!param(cmd, j->buffer, j->toks,
p_req_tal("repeat", json_tok_u64, &ival), 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)); p_req_tal("repeat", json_tok_u64, &ival2), NULL));
assert(check_fail()); assert(check_fail());
assert(strstr(fail_msg, "developer error")); assert(strstr(fail_msg, "developer error"));
assert(!param(cmd, j->buffer, j->toks, assert(!param(cmd, j->buffer, j->toks,
p_req_tal("repeat", json_tok_u64, &ival), 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)); p_req_tal("repeat", json_tok_u64, &ival), NULL));
assert(check_fail()); assert(check_fail());
assert(strstr(fail_msg, "developer error")); assert(strstr(fail_msg, "developer error"));
assert(!param(cmd, j->buffer, j->toks, assert(!param(cmd, j->buffer, j->toks,
p_req_tal("u64", json_tok_u64, &ival), p_req_tal("u64", json_tok_u64, &ival),
p_req("repeat", json_tok_double, &dval), p_req_tal("repeat", json_tok_double, &dval),
p_req("repeat", json_tok_double, &dval), NULL)); p_req_tal("repeat", json_tok_double, &dval), NULL));
assert(check_fail()); assert(check_fail());
assert(strstr(fail_msg, "developer error")); assert(strstr(fail_msg, "developer error"));
@ -299,12 +299,12 @@ static void bad_programmer(void)
/* Add required param after optional */ /* Add required param after optional */
j = json_parse(cmd, "[ '25', '546', '26', '1.1' ]"); j = json_parse(cmd, "[ '25', '546', '26', '1.1' ]");
unsigned int *msatoshi; unsigned int *msatoshi;
double riskfactor; double *riskfactor;
assert(!param(cmd, j->buffer, j->toks, assert(!param(cmd, j->buffer, j->toks,
p_req_tal("u64", json_tok_u64, &ival), 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_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);
assert(*msatoshi == 100); assert(*msatoshi == 100);
assert(check_fail()); assert(check_fail());

Loading…
Cancel
Save