Skip to content

Commit

Permalink
jsonprc: make json_get_params() fail the command, for better error re…
Browse files Browse the repository at this point in the history
…porting.

We move it into jsonrpc where it belongs, and make it fail the command.
This means it can tell us exactly what was wrong.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Feb 1, 2018
1 parent 47577e5 commit 91a22dc
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 154 deletions.
84 changes: 0 additions & 84 deletions common/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -225,90 +225,6 @@ const jsmntok_t *json_delve(const char *buffer,
return tok;
}

bool json_get_params(const char *buffer, const jsmntok_t param[], ...)
{
va_list ap;
const char **names;
size_t num_names;
/* Uninitialized warnings on p and end */
const jsmntok_t **tokptr, *p = NULL, *end = NULL;

if (param->type == JSMN_ARRAY) {
if (param->size == 0)
p = NULL;
else
p = param + 1;
end = json_next(param);
} else if (param->type != JSMN_OBJECT)
return false;

num_names = 0;
names = tal_arr(NULL, const char *, num_names + 1);
va_start(ap, param);
while ((names[num_names] = va_arg(ap, const char *)) != NULL) {
tokptr = va_arg(ap, const jsmntok_t **);
bool compulsory = true;
if (names[num_names][0] == '?') {
names[num_names]++;
compulsory = false;
}
if (param->type == JSMN_ARRAY) {
*tokptr = p;
if (p) {
p = json_next(p);
if (p == end)
p = NULL;
}
} else {
*tokptr = json_get_member(buffer, param,
names[num_names]);
}
/* Convert 'null' to NULL */
if (*tokptr
&& (*tokptr)->type == JSMN_PRIMITIVE
&& buffer[(*tokptr)->start] == 'n') {
*tokptr = NULL;
}
if (compulsory && !*tokptr) {
va_end(ap);
tal_free(names);
return false;
}
num_names++;
tal_resize(&names, num_names + 1);
}

va_end(ap);

/* Now make sure there aren't any params which aren't valid */
if (param->type == JSMN_ARRAY) {
if (param->size > num_names) {
tal_free(names);
return false;
}
} else {
const jsmntok_t *t;

end = json_next(param);

/* Find each parameter among the valid names */
for (t = param + 1; t < end; t = json_next(t+1)) {
bool found = false;
for (size_t i = 0; i < num_names; i++) {
if (json_tok_streq(buffer, t, names[i]))
found = true;
}
if (!found) {
tal_free(names);
return false;
}
}
}

tal_free(names);
return true;
}

static bool strange_chars(const char *str, size_t len)
{
for (size_t i = 0; i < len; i++) {
Expand Down
9 changes: 0 additions & 9 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok);
/* Returns next token with same parent. */
const jsmntok_t *json_next(const jsmntok_t *tok);

/* Get the parameters (by position or name). Followed by triples of
* of const char *name, const jsmntok_t **ret_ptr, then NULL.
*
* If name starts with '?' it is optional (and will be set to NULL
* if it's a literal 'null' or not present).
* Otherwise false is returned.
*/
bool json_get_params(const char *buffer, const jsmntok_t param[], ...);

/* Get top-level member. */
const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[],
const char *label);
Expand Down
6 changes: 2 additions & 4 deletions lightningd/chaintopology.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,10 +556,9 @@ void json_dev_broadcast(struct command *cmd,
jsmntok_t *enabletok;
bool enable;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"enable", &enabletok,
NULL)) {
command_fail(cmd, "Need enable");
return;
}

Expand Down Expand Up @@ -606,12 +605,11 @@ static void json_dev_setfees(struct command *cmd,
struct chain_topology *topo = cmd->ld->topology;
struct json_result *response;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"?immediate", &ratetok[FEERATE_IMMEDIATE],
"?normal", &ratetok[FEERATE_NORMAL],
"?slow", &ratetok[FEERATE_SLOW],
NULL)) {
command_fail(cmd, "Bad parameters");
return;
}

Expand Down
3 changes: 1 addition & 2 deletions lightningd/dev_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ static void json_dev_ping(struct command *cmd,
struct pubkey id;
struct subd *owner;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"id", &idtok,
"len", &lentok,
"pongbytes", &pongbytestok,
NULL)) {
command_fail(cmd, "Need id, len and pongbytes");
return;
}

Expand Down
9 changes: 3 additions & 6 deletions lightningd/gossip_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,9 @@ static void json_listnodes(struct command *cmd, const char *buffer,
jsmntok_t *idtok = NULL;
struct pubkey *id = NULL;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"?id", &idtok,
NULL)) {
command_fail(cmd, "Invalid arguments");
return;
}

Expand Down Expand Up @@ -302,13 +301,12 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok
double riskfactor;
struct lightningd *ld = cmd->ld;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"id", &idtok,
"msatoshi", &msatoshitok,
"riskfactor", &riskfactortok,
"?cltv", &cltvtok,
NULL)) {
command_fail(cmd, "Need id, msatoshi and riskfactor");
return;
}

Expand Down Expand Up @@ -396,10 +394,9 @@ static void json_listchannels(struct command *cmd, const char *buffer,
jsmntok_t *idtok;
struct short_channel_id *id = NULL;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"?short_channel_id", &idtok,
NULL)) {
command_fail(cmd, "Invalid arguments");
return;
}

Expand Down
18 changes: 6 additions & 12 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,12 @@ static void json_invoice(struct command *cmd,
char *b11enc;
u64 expiry = 3600;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"msatoshi", &msatoshi,
"label", &label,
"description", &desc,
"?expiry", &exp,
NULL)) {
command_fail(cmd, "Need {msatoshi}, {label} and {description}");
return;
}

Expand Down Expand Up @@ -228,10 +227,9 @@ static void json_listinvoice_internal(struct command *cmd,
struct json_result *response = new_json_result(cmd);
struct wallet *wallet = cmd->ld->wallet;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"?label", &label,
NULL)) {
command_fail(cmd, "Invalid arguments");
return;
}

Expand Down Expand Up @@ -284,11 +282,10 @@ static void json_delinvoice(struct command *cmd,
const char *label, *status, *actual_status;
struct wallet *wallet = cmd->ld->wallet;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"label", &labeltok,
"status", &statustok,
NULL)) {
command_fail(cmd, "Invalid arguments");
return;
}

Expand Down Expand Up @@ -340,10 +337,9 @@ static void json_waitanyinvoice(struct command *cmd,
u64 pay_index;
struct wallet *wallet = cmd->ld->wallet;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"?lastpay_index", &pay_indextok,
NULL)) {
command_fail(cmd, "Invalid arguments");
return;
}

Expand Down Expand Up @@ -389,8 +385,7 @@ static void json_waitinvoice(struct command *cmd,
jsmntok_t *labeltok;
const char *label = NULL;

if (!json_get_params(buffer, params, "label", &labeltok, NULL)) {
command_fail(cmd, "Missing {label}");
if (!json_get_params(cmd, buffer, params, "label", &labeltok, NULL)) {
return;
}

Expand Down Expand Up @@ -427,11 +422,10 @@ static void json_decodepay(struct command *cmd,
struct json_result *response;
char *str, *desc, *fail;

if (!json_get_params(buffer, params,
if (!json_get_params(cmd, buffer, params,
"bolt11", &bolt11tok,
"?description", &desctok,
NULL)) {
command_fail(cmd, "Need bolt11 string");
return;
}

Expand Down
Loading

0 comments on commit 91a22dc

Please sign in to comment.