2018-07-02 03:01:29 +02:00
|
|
|
#include "config.h"
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
#include <signal.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <lightningd/jsonrpc.h>
|
|
|
|
|
|
|
|
#include <lightningd/params.c>
|
|
|
|
#include <common/json.c>
|
|
|
|
#include <common/json_escaped.c>
|
|
|
|
#include <ccan/array_size/array_size.h>
|
2018-07-04 03:46:52 +02:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <unistd.h>
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
bool failed;
|
|
|
|
char *fail_msg;
|
|
|
|
|
|
|
|
struct command *cmd;
|
|
|
|
|
|
|
|
void command_fail(struct command *cmd, int code, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fail_msg = tal_vfmt(cmd, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void command_fail_detailed(struct command *cmd, int code,
|
|
|
|
const struct json_result *data, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
failed = true;
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
fail_msg = tal_vfmt(cmd, fmt, ap);
|
|
|
|
fail_msg =
|
|
|
|
tal_fmt(cmd, "%s data: %s", fail_msg, json_result_string(data));
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS START */
|
|
|
|
/* Generated stub for json_tok_wtx */
|
|
|
|
bool json_tok_wtx(struct wallet_tx *tx UNNEEDED, const char *buffer UNNEEDED,
|
|
|
|
const jsmntok_t * sattok UNNEEDED)
|
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* AUTOGENERATED MOCKS END */
|
|
|
|
|
|
|
|
struct json {
|
|
|
|
jsmntok_t *toks;
|
|
|
|
char *buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void convert_quotes(char *first)
|
|
|
|
{
|
|
|
|
while (*first != '\0') {
|
|
|
|
if (*first == '\'')
|
|
|
|
*first = '"';
|
|
|
|
first++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct json *json_parse(const tal_t * ctx, const char *str)
|
|
|
|
{
|
|
|
|
struct json *j = tal(ctx, struct json);
|
|
|
|
j->buffer = tal_strdup(j, str);
|
|
|
|
convert_quotes(j->buffer);
|
|
|
|
|
|
|
|
j->toks = tal_arr(j, jsmntok_t, 50);
|
|
|
|
assert(j->toks);
|
|
|
|
jsmn_parser parser;
|
|
|
|
|
|
|
|
again:
|
|
|
|
jsmn_init(&parser);
|
|
|
|
int ret = jsmn_parse(&parser, j->buffer, strlen(j->buffer), j->toks,
|
|
|
|
tal_count(j->toks));
|
|
|
|
if (ret == JSMN_ERROR_NOMEM) {
|
|
|
|
tal_resize(&j->toks, tal_count(j->toks) * 2);
|
|
|
|
goto again;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
failed = false;
|
|
|
|
return j;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void zero_params(void)
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "{}");
|
|
|
|
assert(param_parse(cmd, j->buffer, j->toks, NULL));
|
|
|
|
|
|
|
|
j = json_parse(cmd, "[]");
|
|
|
|
assert(param_parse(cmd, j->buffer, j->toks, NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct sanity {
|
|
|
|
char *str;
|
|
|
|
bool failed;
|
|
|
|
int ival;
|
|
|
|
double dval;
|
|
|
|
char *fail_str;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sanity buffers[] = {
|
|
|
|
// pass
|
|
|
|
{"['42', '3.15']", false, 42, 3.15, NULL},
|
|
|
|
{"{ 'u64' : '42', 'double' : '3.15' }", false, 42, 3.15, NULL},
|
|
|
|
|
|
|
|
// fail
|
|
|
|
{"{'u64':'42', 'double':'3.15', 'extra':'stuff'}", true, 0, 0,
|
|
|
|
"unknown parameter"},
|
|
|
|
{"['42', '3.15', 'stuff']", true, 0, 0, "too many"},
|
|
|
|
{"['42', '3.15', 'null']", true, 0, 0, "too many"},
|
|
|
|
|
|
|
|
// not enough
|
|
|
|
{"{'u64':'42'}", true, 0, 0, "missing required"},
|
|
|
|
{"['42']", true, 0, 0, "missing required"},
|
|
|
|
|
|
|
|
// fail wrong type
|
|
|
|
{"{'u64':'hello', 'double':'3.15'}", true, 0, 0, "\"u64\": \"hello\""},
|
|
|
|
{"['3.15', '3.15', 'stuff']", true, 0, 0, "integer"},
|
|
|
|
};
|
|
|
|
|
|
|
|
static void stest(const struct json *j, struct sanity *b)
|
|
|
|
{
|
|
|
|
u64 ival;
|
|
|
|
double dval;
|
|
|
|
if (!param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("u64", json_tok_u64, &ival),
|
|
|
|
param_req("double", json_tok_double, &dval), NULL)) {
|
|
|
|
assert(failed == true);
|
|
|
|
assert(b->failed == true);
|
|
|
|
assert(strstr(fail_msg, b->fail_str));
|
|
|
|
} else {
|
|
|
|
assert(b->failed == false);
|
|
|
|
assert(ival == 42);
|
|
|
|
assert(dval > 3.1499 && b->dval < 3.1501);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sanity(void)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < ARRAY_SIZE(buffers); ++i) {
|
|
|
|
struct json *j = json_parse(cmd, buffers[i].str);
|
|
|
|
assert(j->toks->type == JSMN_OBJECT
|
|
|
|
|| j->toks->type == JSMN_ARRAY);
|
|
|
|
stest(j, &buffers[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure toks are passed through correctly, and also make sure
|
|
|
|
* optional missing toks are set to NULL.
|
|
|
|
*/
|
|
|
|
static void tok_tok(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
unsigned int n;
|
|
|
|
const jsmntok_t *tok = NULL;
|
|
|
|
struct json *j = json_parse(cmd, "{ 'satoshi', '546' }");
|
|
|
|
|
|
|
|
assert(param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("satoshi", json_tok_tok,
|
|
|
|
&tok), NULL));
|
|
|
|
assert(tok);
|
|
|
|
assert(json_tok_number(j->buffer, tok, &n));
|
|
|
|
assert(n == 546);
|
|
|
|
}
|
|
|
|
// again with missing optional parameter
|
|
|
|
{
|
|
|
|
/* make sure it is *not* NULL */
|
|
|
|
const jsmntok_t *tok = (const jsmntok_t *) 65535;
|
|
|
|
|
|
|
|
struct json *j = json_parse(cmd, "{}");
|
|
|
|
assert(param_parse(cmd, j->buffer, j->toks,
|
2018-07-07 07:52:14 +02:00
|
|
|
param_opt_tok("satoshi", &tok), NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
/* make sure it *is* NULL */
|
|
|
|
assert(tok == NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for valid but duplicate json name-value pairs */
|
2018-07-04 03:46:52 +02:00
|
|
|
static void dup_names(void)
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
{
|
|
|
|
struct json *j =
|
|
|
|
json_parse(cmd,
|
|
|
|
"{ 'u64' : '42', 'u64' : '43', 'double' : '3.15' }");
|
|
|
|
|
|
|
|
u64 i;
|
|
|
|
double d;
|
|
|
|
assert(!param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("u64", json_tok_u64, &i),
|
|
|
|
param_req("double", json_tok_double, &d), NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void null_params(void)
|
|
|
|
{
|
2018-07-02 02:53:11 +02:00
|
|
|
uint64_t *ints = tal_arr(cmd, uint64_t, 4);
|
|
|
|
uint64_t **intptrs = tal_arr(cmd, uint64_t *, 3);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
/* no null params */
|
|
|
|
struct json *j =
|
|
|
|
json_parse(cmd, "[ '10', '11', '12', '13', '14', '15', '16']");
|
|
|
|
for (int i = 0; i < tal_count(ints); ++i)
|
|
|
|
ints[i] = i;
|
|
|
|
|
|
|
|
assert(param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("0", json_tok_u64, &ints[0]),
|
|
|
|
param_req("1", json_tok_u64, &ints[1]),
|
|
|
|
param_req("2", json_tok_u64, &ints[2]),
|
|
|
|
param_req("3", json_tok_u64, &ints[3]),
|
2018-07-04 18:03:12 +02:00
|
|
|
param_opt("4", json_tok_u64, &intptrs[0]),
|
|
|
|
param_opt("5", json_tok_u64, &intptrs[1]),
|
|
|
|
param_opt("6", json_tok_u64, &intptrs[2]),
|
2018-07-02 02:53:11 +02:00
|
|
|
NULL));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
for (int i = 0; i < tal_count(ints); ++i)
|
|
|
|
assert(ints[i] == i + 10);
|
2018-07-02 02:53:11 +02:00
|
|
|
for (int i = 0; i < tal_count(intptrs); ++i)
|
|
|
|
assert(*intptrs[i] == i + 10 + tal_count(ints));
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
/* missing at end */
|
|
|
|
for (int i = 0; i < tal_count(ints); ++i)
|
|
|
|
ints[i] = 42;
|
2018-07-02 02:53:11 +02:00
|
|
|
for (int i = 0; i < tal_count(intptrs); ++i)
|
|
|
|
intptrs[i] = (void *)42;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
j = json_parse(cmd, "[ '10', '11', '12', '13', '14']");
|
|
|
|
assert(param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("0", json_tok_u64, &ints[0]),
|
|
|
|
param_req("1", json_tok_u64, &ints[1]),
|
|
|
|
param_req("2", json_tok_u64, &ints[2]),
|
|
|
|
param_req("3", json_tok_u64, &ints[3]),
|
2018-07-04 18:03:12 +02:00
|
|
|
param_opt("4", json_tok_u64, &intptrs[0]),
|
|
|
|
param_opt("5", json_tok_u64, &intptrs[1]),
|
|
|
|
param_opt("6", json_tok_u64, &intptrs[2]),
|
2018-07-02 02:53:11 +02:00
|
|
|
NULL));
|
|
|
|
assert(*intptrs[0] == 14);
|
|
|
|
assert(intptrs[1] == NULL);
|
|
|
|
assert(intptrs[2] == NULL);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#if DEVELOPER
|
|
|
|
jmp_buf jump;
|
|
|
|
static void handle_abort(int sig)
|
|
|
|
{
|
|
|
|
longjmp(jump, 1);
|
|
|
|
}
|
|
|
|
|
2018-07-04 03:46:52 +02:00
|
|
|
static int set_assert(void)
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
{
|
|
|
|
struct sigaction act;
|
2018-07-04 03:46:52 +02:00
|
|
|
int old_stderr;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
memset(&act, '\0', sizeof(act));
|
|
|
|
act.sa_handler = &handle_abort;
|
2018-07-04 03:46:52 +02:00
|
|
|
if (sigaction(SIGABRT, &act, NULL) < 0)
|
|
|
|
err(1, "set_assert");
|
|
|
|
|
|
|
|
/* Don't spam with assert messages. */
|
|
|
|
old_stderr = dup(STDERR_FILENO);
|
|
|
|
close(STDERR_FILENO);
|
|
|
|
return old_stderr;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 03:46:52 +02:00
|
|
|
static void restore_assert(int old_stderr)
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
{
|
|
|
|
struct sigaction act;
|
2018-07-04 03:46:52 +02:00
|
|
|
|
|
|
|
dup2(old_stderr, STDERR_FILENO);
|
|
|
|
close(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
memset(&act, '\0', sizeof(act));
|
|
|
|
act.sa_handler = SIG_DFL;
|
2018-07-04 03:46:52 +02:00
|
|
|
if (sigaction(SIGABRT, &act, NULL) < 0)
|
|
|
|
err(1, "restore_assert");
|
|
|
|
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to make sure there are no programming mistakes.
|
|
|
|
*/
|
|
|
|
static void bad_programmer(void)
|
|
|
|
{
|
|
|
|
u64 ival;
|
|
|
|
u64 ival2;
|
|
|
|
double dval;
|
|
|
|
struct json *j = json_parse(cmd, "[ '25', '546', '26' ]");
|
2018-07-04 03:46:52 +02:00
|
|
|
int old_stderr = set_assert();
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
/* check for repeated names */
|
|
|
|
if (setjmp(jump) == 0) {
|
|
|
|
param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("repeat", json_tok_u64, &ival),
|
|
|
|
param_req("double", json_tok_double, &dval),
|
|
|
|
param_req("repeat", json_tok_u64, &ival2), NULL);
|
|
|
|
/* shouldn't get here */
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setjmp(jump) == 0) {
|
|
|
|
param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("repeat", json_tok_u64, &ival),
|
|
|
|
param_req("double", json_tok_double, &dval),
|
|
|
|
param_req("repeat", json_tok_u64, &ival), NULL);
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setjmp(jump) == 0) {
|
|
|
|
param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("u64", json_tok_u64, &ival),
|
|
|
|
param_req("repeat", json_tok_double, &dval),
|
|
|
|
param_req("repeat", json_tok_double, &dval), NULL);
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check for repeated arguments */
|
|
|
|
if (setjmp(jump) == 0) {
|
|
|
|
param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("u64", json_tok_u64, &ival),
|
|
|
|
param_req("repeated-arg", json_tok_u64, &ival),
|
|
|
|
NULL);
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setjmp(jump) == 0) {
|
|
|
|
param_parse(cmd, j->buffer, j->toks,
|
2018-07-07 07:52:14 +02:00
|
|
|
param_req("u64", (param_cb)NULL, &ival), NULL);
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setjmp(jump) == 0) {
|
|
|
|
/* Add required param after optional */
|
|
|
|
struct json *j =
|
|
|
|
json_parse(cmd, "[ '25', '546', '26', '1.1' ]");
|
2018-07-02 02:53:11 +02:00
|
|
|
unsigned int *msatoshi;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
double riskfactor;
|
|
|
|
param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("u64", json_tok_u64, &ival),
|
|
|
|
param_req("double", json_tok_double, &dval),
|
2018-07-04 18:03:12 +02:00
|
|
|
param_opt("msatoshi",
|
2018-07-02 02:53:11 +02:00
|
|
|
json_tok_number, &msatoshi),
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
param_req("riskfactor", json_tok_double,
|
|
|
|
&riskfactor), NULL);
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
assert(false);
|
|
|
|
}
|
2018-07-04 03:46:52 +02:00
|
|
|
restore_assert(old_stderr);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void add_members(struct param **params,
|
|
|
|
struct json_result *obj,
|
|
|
|
struct json_result *arr, unsigned int *ints)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < tal_count(ints); ++i) {
|
2018-07-02 03:11:08 +02:00
|
|
|
char *name = tal_fmt(tmpctx, "%i", i);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
json_add_num(obj, name, i);
|
|
|
|
json_add_num(arr, NULL, i);
|
2018-07-04 03:46:07 +02:00
|
|
|
param_add(params, name,
|
|
|
|
typesafe_cb_preargs(bool, void *,
|
|
|
|
json_tok_number,
|
|
|
|
&ints[i],
|
|
|
|
const char *,
|
|
|
|
const jsmntok_t *),
|
2018-07-04 18:03:12 +02:00
|
|
|
&ints[i], 0);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A roundabout way of initializing an array of ints to:
|
|
|
|
* ints[0] = 0, ints[1] = 1, ... ints[499] = 499
|
|
|
|
*/
|
|
|
|
static void five_hundred_params(void)
|
|
|
|
{
|
2018-07-04 03:46:07 +02:00
|
|
|
struct param *params = tal_arr(NULL, struct param, 0);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
|
|
|
|
unsigned int *ints = tal_arr(params, unsigned int, 500);
|
|
|
|
struct json_result *obj = new_json_result(params);
|
|
|
|
struct json_result *arr = new_json_result(params);
|
|
|
|
json_object_start(obj, NULL);
|
|
|
|
json_array_start(arr, NULL);
|
2018-07-04 03:46:07 +02:00
|
|
|
add_members(¶ms, obj, arr, ints);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
json_object_end(obj);
|
|
|
|
json_array_end(arr);
|
|
|
|
|
|
|
|
/* first test object version */
|
|
|
|
struct json *j = json_parse(params, obj->s);
|
|
|
|
assert(param_parse_arr(cmd, j->buffer, j->toks, params));
|
|
|
|
for (int i = 0; i < tal_count(ints); ++i) {
|
|
|
|
assert(ints[i] == i);
|
|
|
|
ints[i] = 65535;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now test array */
|
|
|
|
j = json_parse(params, arr->s);
|
|
|
|
assert(param_parse_arr(cmd, j->buffer, j->toks, params));
|
|
|
|
for (int i = 0; i < tal_count(ints); ++i) {
|
|
|
|
assert(ints[i] == i);
|
|
|
|
}
|
|
|
|
|
|
|
|
tal_free(params);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sendpay(void)
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 'A', '123', 'hello there' '547']");
|
|
|
|
|
|
|
|
const jsmntok_t *routetok, *note;
|
2018-07-02 02:53:11 +02:00
|
|
|
u64 *msatoshi;
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
unsigned cltv;
|
|
|
|
|
|
|
|
if (!param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("route", json_tok_tok, &routetok),
|
|
|
|
param_req("cltv", json_tok_number, &cltv),
|
2018-07-07 07:52:14 +02:00
|
|
|
param_opt_tok("note", ¬e),
|
2018-07-04 18:03:12 +02:00
|
|
|
param_opt("msatoshi", json_tok_u64, &msatoshi),
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
NULL))
|
|
|
|
assert(false);
|
|
|
|
|
2018-07-02 02:53:11 +02:00
|
|
|
assert(note);
|
2018-07-04 19:52:02 +02:00
|
|
|
assert(!strncmp("hello there", j->buffer + note->start, note->end - note->start));
|
2018-07-02 02:53:11 +02:00
|
|
|
assert(msatoshi);
|
|
|
|
assert(*msatoshi == 547);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
}
|
|
|
|
|
2018-07-04 18:50:20 +02:00
|
|
|
static void sendpay_nulltok(void)
|
|
|
|
{
|
|
|
|
struct json *j = json_parse(cmd, "[ 'A', '123']");
|
|
|
|
|
|
|
|
const jsmntok_t *routetok, *note = (void *) 65535;
|
|
|
|
u64 *msatoshi;
|
|
|
|
unsigned cltv;
|
|
|
|
|
|
|
|
if (!param_parse(cmd, j->buffer, j->toks,
|
|
|
|
param_req("route", json_tok_tok, &routetok),
|
|
|
|
param_req("cltv", json_tok_number, &cltv),
|
2018-07-07 07:52:14 +02:00
|
|
|
param_opt_tok("note", ¬e),
|
2018-07-04 18:50:20 +02:00
|
|
|
param_opt("msatoshi", json_tok_u64, &msatoshi),
|
|
|
|
NULL))
|
|
|
|
assert(false);
|
|
|
|
|
|
|
|
assert(note == NULL);
|
|
|
|
assert(msatoshi == NULL);
|
|
|
|
}
|
|
|
|
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
setup_locale();
|
2018-07-02 02:53:11 +02:00
|
|
|
setup_tmpctx();
|
|
|
|
cmd = tal(tmpctx, struct command);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
fail_msg = tal_arr(cmd, char, 10000);
|
|
|
|
|
|
|
|
zero_params();
|
|
|
|
sanity();
|
|
|
|
tok_tok();
|
|
|
|
null_params();
|
|
|
|
#if DEVELOPER
|
|
|
|
bad_programmer();
|
|
|
|
#endif
|
2018-07-04 03:46:52 +02:00
|
|
|
dup_names();
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
five_hundred_params();
|
|
|
|
sendpay();
|
2018-07-04 18:50:20 +02:00
|
|
|
sendpay_nulltok();
|
2018-07-02 02:53:11 +02:00
|
|
|
tal_free(tmpctx);
|
Typesafe callback system for parsing json
This is part of #1464 and incorporates Rusty's suggested updates from #1569.
See comment in param.h for description, here's the basics:
unsigned cltv;
const jsmntok_t *note;
u64 msatoshi;
struct param * mp;
if (!param_parse(cmd, buffer, tokens,
param_req("cltv", json_tok_number, &cltv),
param_opt("note", json_tok_tok, ¬e),
mp = param_opt("msatoshi", json_tok_u64, &msatoshi),
NULL))
return;
if (param_is_set(mp))
do_something()
There is a lot of developer mode code to make sure we don't make mistakes,
like trying to unmarshal into the same variable twice or adding a required param
after optional.
During testing, I found a bug (of sorts) in the current system. It allows you
to provide two named parameters with the same name without error; e.g.:
# cli/lightning-cli -k newaddr addresstype=p2sh-segwit addresstype=bech32
{
"address": "2N3r6fT65PhfhE1mcMS6TtcdaEurud6M7pA"
}
It just takes the first and ignores the second. The new system reports this as an
error for now. We can always change this later.
2018-06-16 17:29:32 +02:00
|
|
|
printf("run-params ok\n");
|
|
|
|
}
|