mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
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>
This commit is contained in:
parent
bab8ff991a
commit
e5918f4e5a
@ -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] */
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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,
|
||||
|
@ -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'"},
|
||||
|
@ -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) {
|
||||
|
@ -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());
|
||||
|
Loading…
Reference in New Issue
Block a user