diff --git a/common/json_tok.c b/common/json_tok.c index b03be4d41..6174aa60f 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -6,170 +6,165 @@ #include #include -bool json_tok_array(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - const jsmntok_t **arr) +struct command_result *param_array(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + const jsmntok_t **arr) { - if (tok->type == JSMN_ARRAY) - return (*arr = tok); + if (tok->type == JSMN_ARRAY) { + *arr = tok; + return NULL; + } - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be an array, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an array, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_bool(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - bool **b) +struct command_result *param_bool(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + bool **b) { *b = tal(cmd, bool); if (json_to_bool(buffer, tok, *b)) - return true; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be 'true' or 'false', not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return NULL; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be 'true' or 'false', not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_double(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - double **num) +struct command_result *param_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)) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a double, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a double, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_escaped_string(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - const char **str) +struct command_result *param_escaped_string(struct command *cmd, + const char *name, + const char * buffer, + const jsmntok_t *tok, + const char **str) { struct json_escaped *esc = json_to_escaped_string(cmd, buffer, tok); if (esc) { *str = json_escaped_unescape(cmd, esc); if (*str) - return true; + return NULL; } - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a string, not '%.*s'" - " (note, we don't allow \\u)", - name, - tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a string, not '%.*s'" + " (note, we don't allow \\u)", + name, + tok->end - tok->start, buffer + tok->start); } -bool json_tok_string(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - const char **str) +struct command_result *param_string(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + const char **str) { *str = tal_strndup(cmd, buffer + tok->start, tok->end - tok->start); - return true; + return NULL; } -bool json_tok_label(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - struct json_escaped **label) +struct command_result *param_label(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + struct json_escaped **label) { /* We accept both strings and number literals here. */ *label = json_escaped_string_(cmd, buffer + tok->start, tok->end - tok->start); if (*label && (tok->type == JSMN_STRING || json_tok_is_num(buffer, tok))) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a string or number, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a string or number, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_number(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - unsigned int **num) +struct command_result *param_number(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + unsigned int **num) { *num = tal(cmd, unsigned int); if (json_to_number(buffer, tok, *num)) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be an integer, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an integer, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_sha256(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - struct sha256 **hash) +struct command_result *param_sha256(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct sha256 **hash) { *hash = tal(cmd, struct sha256); if (hex_decode(buffer + tok->start, tok->end - tok->start, *hash, sizeof(**hash))) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a 32 byte hex value, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a 32 byte hex value, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_msat(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t * tok, - u64 **msatoshi_val) +struct command_result *param_msat(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + u64 **msatoshi_val) { if (json_tok_streq(buffer, tok, "any")) { *msatoshi_val = NULL; - return true; + return NULL; } *msatoshi_val = tal(cmd, u64); if (json_to_u64(buffer, tok, *msatoshi_val) && *msatoshi_val != 0) - return true; - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a positive number or 'any', not '%.*s'", - name, - tok->end - tok->start, - buffer + tok->start); - return false; + return NULL; + + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a positive number or 'any', not '%.*s'", + name, + tok->end - tok->start, + buffer + tok->start); } -bool json_tok_percent(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - double **num) +struct command_result *param_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) && **num >= 0.0) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a positive double, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a positive double, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_u64(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - uint64_t **num) +struct command_result *param_u64(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + uint64_t **num) { *num = tal(cmd, uint64_t); if (json_to_u64(buffer, tok, *num)) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be an unsigned 64 bit integer, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an unsigned 64 bit integer, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } -bool json_tok_tok(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t * tok, - const jsmntok_t **out) +struct command_result *param_tok(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + const jsmntok_t **out) { - return (*out = tok); + *out = tok; + return NULL; } diff --git a/common/json_tok.h b/common/json_tok.h index d2b82b97c..ff6434f38 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -5,61 +5,64 @@ #include struct command; +struct command_result; /* Extract json array token */ -bool json_tok_array(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - const jsmntok_t **arr); +struct command_result *param_array(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + const jsmntok_t **arr); /* Extract boolean this (must be a true or false) */ -bool json_tok_bool(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - bool **b); +struct command_result *param_bool(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + bool **b); /* 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); +struct command_result *param_double(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + double **num); /* Extract an escaped string (and unescape it) */ -bool json_tok_escaped_string(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - const char **str); +struct command_result *param_escaped_string(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + const char **str); /* Extract a string */ -bool json_tok_string(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - const char **str); +struct command_result *param_string(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + const char **str); /* Extract a label. It is either an escaped string or a number. */ -bool json_tok_label(struct command *cmd, const char *name, - const char * buffer, const jsmntok_t *tok, - struct json_escaped **label); +struct command_result *param_label(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + struct json_escaped **label); /* 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, - unsigned int **num); +struct command_result *param_number(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + unsigned int **num); /* Extract sha256 hash */ -bool json_tok_sha256(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - struct sha256 **hash); +struct command_result *param_sha256(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct sha256 **hash); /* Extract positive integer, or NULL if tok is 'any'. */ -bool json_tok_msat(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t * tok, - u64 **msatoshi_val); +struct command_result *param_msat(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + u64 **msatoshi_val); /* Extract double in range [0.0, 100.0] */ -bool json_tok_percent(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - double **num); +struct command_result *param_percent(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_u64(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - uint64_t **num); +struct command_result *param_u64(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + uint64_t **num); /* * Set the address of @out to @tok. Used as a callback by handlers that @@ -68,8 +71,8 @@ bool json_tok_u64(struct command *cmd, const char *name, * Usage of this is discouraged. Writing a local static bespoke handler is * preferred. */ -bool json_tok_tok(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t * tok, - const jsmntok_t **out); +struct command_result *param_tok(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + const jsmntok_t **out); #endif /* LIGHTNING_COMMON_JSON_TOK_H */ diff --git a/common/param.c b/common/param.c index bcdd0773a..7b67d0c98 100644 --- a/common/param.c +++ b/common/param.c @@ -31,16 +31,18 @@ static bool param_add(struct param **params, return true; } -static bool make_callback(struct command *cmd, - struct param *def, - const char *buffer, const jsmntok_t *tok) +static struct command_result *make_callback(struct command *cmd, + struct param *def, + const char *buffer, + const jsmntok_t *tok) { def->is_set = true; return def->cbx(cmd, def->name, buffer, tok, def->arg); } -static bool post_check(struct command *cmd, struct param *params) +static struct command_result *post_check(struct command *cmd, + struct param *params) { struct param *first = params; struct param *last = first + tal_count(params); @@ -48,42 +50,43 @@ static bool post_check(struct command *cmd, struct param *params) /* Make sure required params were provided. */ while (first != last && first->required) { if (!first->is_set) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "missing required parameter: '%s'", - first->name); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "missing required parameter: '%s'", + first->name); } first++; } - return true; + return NULL; } -static bool parse_by_position(struct command *cmd, - struct param *params, - const char *buffer, - const jsmntok_t tokens[], - bool allow_extra) +static struct command_result *parse_by_position(struct command *cmd, + struct param *params, + const char *buffer, + const jsmntok_t tokens[], + bool allow_extra) { + struct command_result *res; const jsmntok_t *tok = tokens + 1; const jsmntok_t *end = json_next(tokens); struct param *first = params; struct param *last = first + tal_count(params); while (first != last && tok != end) { - if (!json_tok_is_null(buffer, tok)) - if (!make_callback(cmd, first, buffer, tok)) - return NULL; + if (!json_tok_is_null(buffer, tok)) { + res = make_callback(cmd, first, buffer, tok); + if (res) + return res; + } tok = json_next(tok); first++; } /* check for unexpected trailing params */ if (!allow_extra && tok != end) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "too many parameters:" - " got %u, expected %zu", - tokens->size, tal_count(params)); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "too many parameters:" + " got %u, expected %zu", + tokens->size, tal_count(params)); } return post_check(cmd, params); @@ -104,11 +107,11 @@ static struct param *find_param(struct param *params, const char *start, return NULL; } -static bool parse_by_name(struct command *cmd, - struct param *params, - const char *buffer, - const jsmntok_t tokens[], - bool allow_extra) +static struct command_result *parse_by_name(struct command *cmd, + struct param *params, + const char *buffer, + const jsmntok_t tokens[], + bool allow_extra) { const jsmntok_t *first = tokens + 1; const jsmntok_t *last = json_next(tokens); @@ -118,21 +121,23 @@ static bool parse_by_name(struct command *cmd, first->end - first->start); if (!p) { if (!allow_extra) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "unknown parameter: '%.*s'", - first->end - first->start, - buffer + first->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "unknown parameter: '%.*s'", + first->end - first->start, + buffer + first->start); } } else { + struct command_result *res; + if (p->is_set) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "duplicate json names: '%s'", p->name); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "duplicate json names: '%s'", + p->name); } - if (!make_callback(cmd, p, buffer, first + 1)) - return false; + res = make_callback(cmd, p, buffer, first + 1); + if (res) + return res; } first = json_next(first + 1); } @@ -237,15 +242,15 @@ static char *param_usage(const tal_t *ctx, return usage; } -static bool param_arr(struct command *cmd, const char *buffer, - const jsmntok_t tokens[], - struct param *params, - bool allow_extra) +static struct command_result *param_arr(struct command *cmd, const char *buffer, + const jsmntok_t tokens[], + struct param *params, + bool allow_extra) { #if DEVELOPER if (!check_params(params)) { - command_fail(cmd, PARAM_DEV_ERROR, "developer error: check_params"); - return false; + return command_fail(cmd, PARAM_DEV_ERROR, + "developer error: check_params"); } #endif if (tokens->type == JSMN_ARRAY) @@ -253,9 +258,8 @@ static bool param_arr(struct command *cmd, const char *buffer, else if (tokens->type == JSMN_OBJECT) return parse_by_name(cmd, params, buffer, tokens, allow_extra); - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "Expected array or object for params"); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "Expected array or object for params"); } bool param(struct command *cmd, const char *buffer, @@ -291,6 +295,6 @@ bool param(struct command *cmd, const char *buffer, /* Always return false if we're simply checking command parameters; * normally this returns true if all parameters are valid. */ - return param_arr(cmd, buffer, tokens, params, allow_extra) + return param_arr(cmd, buffer, tokens, params, allow_extra) == NULL && !command_check_only(cmd); } diff --git a/common/param.h b/common/param.h index b8214e077..d0f160095 100644 --- a/common/param.h +++ b/common/param.h @@ -35,6 +35,10 @@ */ struct command; +/* A dummy type returned by command_ functions, to ensure you return them + * immediately */ +struct command_result; + /* * Parse the json tokens. @params can be an array of values or an object * of named values. @@ -43,14 +47,16 @@ bool param(struct command *cmd, const char *buffer, const jsmntok_t params[], ...) LAST_ARG_NULL; /* - * The callback signature. Callbacks must return true on success. On failure they - * must call comand_fail and return false. + * The callback signature. + * + * Callbacks must return NULL on success. On failure they + * must return command_fail(...). */ -typedef bool(*param_cbx)(struct command *cmd, - const char *name, - const char *buffer, - const jsmntok_t *tok, - void **arg); +typedef struct command_result *(*param_cbx)(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + void **arg); /* * Add a required parameter. @@ -63,7 +69,7 @@ typedef bool(*param_cbx)(struct command *cmd, (const char *)NULL, \ (const char *)NULL, \ (const jsmntok_t *)NULL, \ - (arg)) == true) + (arg)) == (struct command_result *)NULL) /* * Add an optional parameter. *arg is set to NULL if it isn't found. @@ -77,7 +83,7 @@ typedef bool(*param_cbx)(struct command *cmd, (const char *)NULL, \ (const char *)NULL, \ (const jsmntok_t *)NULL, \ - (arg)) == true); }) + (arg)) == (struct command_result *)NULL); }) /* * Add an optional parameter. *arg is set to @def if it isn't found. @@ -92,7 +98,7 @@ typedef bool(*param_cbx)(struct command *cmd, (const char *)NULL, \ (const char *)NULL, \ (const jsmntok_t *)NULL, \ - (arg)) == true); }) + (arg)) == (struct command_result *)NULL); }) /* Special flag for 'check' which allows any parameters. */ #define p_opt_any() "", false, NULL, NULL diff --git a/common/test/run-param.c b/common/test/run-param.c index 80ea498a3..2909329c1 100644 --- a/common/test/run-param.c +++ b/common/test/run-param.c @@ -22,6 +22,10 @@ static bool check_fail(void) { struct command *cmd; +struct command_result { +}; +static struct command_result cmd_failed; + struct command_result *command_fail(struct command *cmd, int code, const char *fmt, ...) { @@ -30,7 +34,7 @@ struct command_result *command_fail(struct command *cmd, va_start(ap, fmt); fail_msg = tal_vfmt(cmd, fmt, ap); va_end(ap); - return NULL; + return &cmd_failed; } /* AUTOGENERATED MOCKS START */ @@ -144,8 +148,8 @@ static void stest(const struct json *j, struct sanity *b) u64 *ival; double *dval; if (!param(cmd, j->buffer, j->toks, - p_req("u64", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), NULL)) { + p_req("u64", param_u64, &ival), + p_req("double", param_double, &dval), NULL)) { assert(check_fail()); assert(b->failed == true); if (!strstr(fail_msg, b->fail_str)) { @@ -182,7 +186,7 @@ static void tok_tok(void) struct json *j = json_parse(cmd, "{ 'satoshi', '546' }"); assert(param(cmd, j->buffer, j->toks, - p_req("satoshi", json_tok_tok, &tok), NULL)); + p_req("satoshi", param_tok, &tok), NULL)); assert(tok); assert(json_to_number(j->buffer, tok, &n)); assert(n == 546); @@ -194,7 +198,7 @@ static void tok_tok(void) struct json *j = json_parse(cmd, "{}"); assert(param(cmd, j->buffer, j->toks, - p_opt("satoshi", json_tok_tok, &tok), NULL)); + p_opt("satoshi", param_tok, &tok), NULL)); /* make sure it *is* NULL */ assert(tok == NULL); @@ -211,8 +215,8 @@ static void dup_names(void) u64 *i; double *d; assert(!param(cmd, j->buffer, j->toks, - p_req("u64", json_tok_u64, &i), - p_req("double", json_tok_double, &d), NULL)); + p_req("u64", param_u64, &i), + p_req("double", param_double, &d), NULL)); } static void null_params(void) @@ -223,13 +227,13 @@ static void null_params(void) json_parse(cmd, "[ '10', '11', '12', '13', '14', '15', '16']"); assert(param(cmd, j->buffer, j->toks, - p_req("0", json_tok_u64, &intptrs[0]), - p_req("1", json_tok_u64, &intptrs[1]), - p_req("2", json_tok_u64, &intptrs[2]), - p_req("3", json_tok_u64, &intptrs[3]), - p_opt_def("4", json_tok_u64, &intptrs[4], 999), - p_opt("5", json_tok_u64, &intptrs[5]), - p_opt("6", json_tok_u64, &intptrs[6]), + p_req("0", param_u64, &intptrs[0]), + p_req("1", param_u64, &intptrs[1]), + p_req("2", param_u64, &intptrs[2]), + p_req("3", param_u64, &intptrs[3]), + p_opt_def("4", param_u64, &intptrs[4], 999), + p_opt("5", param_u64, &intptrs[5]), + p_opt("6", param_u64, &intptrs[6]), NULL)); for (int i = 0; i < tal_count(intptrs); ++i) { assert(intptrs[i]); @@ -239,13 +243,13 @@ static void null_params(void) /* missing at end */ j = json_parse(cmd, "[ '10', '11', '12', '13', '14']"); assert(param(cmd, j->buffer, j->toks, - p_req("0", json_tok_u64, &intptrs[0]), - p_req("1", json_tok_u64, &intptrs[1]), - p_req("2", json_tok_u64, &intptrs[2]), - p_req("3", json_tok_u64, &intptrs[3]), - p_opt("4", json_tok_u64, &intptrs[4]), - p_opt("5", json_tok_u64, &intptrs[5]), - p_opt_def("6", json_tok_u64, &intptrs[6], 888), + p_req("0", param_u64, &intptrs[0]), + p_req("1", param_u64, &intptrs[1]), + p_req("2", param_u64, &intptrs[2]), + p_req("3", param_u64, &intptrs[3]), + p_opt("4", param_u64, &intptrs[4]), + p_opt("5", param_u64, &intptrs[5]), + p_opt_def("6", param_u64, &intptrs[6], 888), NULL)); assert(*intptrs[0] == 10); assert(*intptrs[1] == 11); @@ -279,30 +283,30 @@ static void bad_programmer(void) /* check for repeated names */ assert(!param(cmd, j->buffer, j->toks, - p_req("repeat", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), - p_req("repeat", json_tok_u64, &ival2), NULL)); + p_req("repeat", param_u64, &ival), + p_req("double", param_double, &dval), + p_req("repeat", param_u64, &ival2), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); assert(!param(cmd, j->buffer, j->toks, - p_req("repeat", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), - p_req("repeat", json_tok_u64, &ival), NULL)); + p_req("repeat", param_u64, &ival), + p_req("double", param_double, &dval), + p_req("repeat", param_u64, &ival), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); assert(!param(cmd, j->buffer, j->toks, - p_req("u64", json_tok_u64, &ival), - p_req("repeat", json_tok_double, &dval), - p_req("repeat", json_tok_double, &dval), NULL)); + p_req("u64", param_u64, &ival), + p_req("repeat", param_double, &dval), + p_req("repeat", param_double, &dval), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); /* check for repeated arguments */ assert(!param(cmd, j->buffer, j->toks, - p_req("u64", json_tok_u64, &ival), - p_req("repeated-arg", json_tok_u64, &ival), NULL)); + p_req("u64", param_u64, &ival), + p_req("repeated-arg", param_u64, &ival), NULL)); assert(check_fail()); assert(strstr(fail_msg, "developer error")); @@ -316,10 +320,10 @@ static void bad_programmer(void) unsigned int *msatoshi; double *riskfactor; assert(!param(cmd, j->buffer, j->toks, - p_req("u64", json_tok_u64, &ival), - p_req("double", json_tok_double, &dval), - p_opt_def("msatoshi", json_tok_number, &msatoshi, 100), - p_req("riskfactor", json_tok_double, &riskfactor), NULL)); + p_req("u64", param_u64, &ival), + p_req("double", param_double, &dval), + p_opt_def("msatoshi", param_number, &msatoshi, 100), + p_req("riskfactor", param_double, &riskfactor), NULL)); assert(*msatoshi); assert(*msatoshi == 100); assert(check_fail()); @@ -340,8 +344,8 @@ static void add_members(struct param **params, tal_append_fmt(obj, "\"%i\" : %i", i, i); tal_append_fmt(arr, "%i", i); param_add(params, name, true, - typesafe_cb_preargs(bool, void **, - json_tok_number, + typesafe_cb_preargs(struct command_result *, void **, + param_number, &ints[i], struct command *, const char *, @@ -368,7 +372,7 @@ static void five_hundred_params(void) /* first test object version */ struct json *j = json_parse(params, obj); - assert(param_arr(cmd, j->buffer, j->toks, params, false)); + assert(param_arr(cmd, j->buffer, j->toks, params, false) == NULL); for (int i = 0; i < tal_count(ints); ++i) { assert(ints[i]); assert(*ints[i] == i); @@ -377,7 +381,7 @@ static void five_hundred_params(void) /* now test array */ j = json_parse(params, arr); - assert(param_arr(cmd, j->buffer, j->toks, params, false)); + assert(param_arr(cmd, j->buffer, j->toks, params, false) == NULL); for (int i = 0; i < tal_count(ints); ++i) { assert(*ints[i] == i); } @@ -394,10 +398,10 @@ static void sendpay(void) unsigned *cltv; if (!param(cmd, j->buffer, j->toks, - p_req("route", json_tok_tok, &routetok), - p_req("cltv", json_tok_number, &cltv), - p_opt("note", json_tok_tok, ¬e), - p_opt("msatoshi", json_tok_u64, &msatoshi), + p_req("route", param_tok, &routetok), + p_req("cltv", param_number, &cltv), + p_opt("note", param_tok, ¬e), + p_opt("msatoshi", param_u64, &msatoshi), NULL)) assert(false); @@ -417,10 +421,10 @@ static void sendpay_nulltok(void) unsigned *cltv; if (!param(cmd, j->buffer, j->toks, - p_req("route", json_tok_tok, &routetok), - p_req("cltv", json_tok_number, &cltv), - p_opt("note", json_tok_tok, ¬e), - p_opt("msatoshi", json_tok_u64, &msatoshi), + p_req("route", param_tok, &routetok), + p_req("cltv", param_number, &cltv), + p_opt("note", param_tok, ¬e), + p_opt("msatoshi", param_u64, &msatoshi), NULL)) assert(false); @@ -439,11 +443,11 @@ static void advanced(void) const jsmntok_t *tok; assert(param(cmd, j->buffer, j->toks, - p_req("description", json_tok_label, &label), - p_req("msat", json_tok_msat, &msat), - p_req("tok", json_tok_tok, &tok), - p_opt("msat_opt1", json_tok_msat, &msat_opt1), - p_opt("msat_opt2", json_tok_msat, &msat_opt2), + p_req("description", param_label, &label), + p_req("msat", param_msat, &msat), + p_req("tok", param_tok, &tok), + p_opt("msat_opt1", param_msat, &msat_opt1), + p_opt("msat_opt2", param_msat, &msat_opt2), NULL)); assert(label != NULL); assert(streq(label->s, "lightning")); @@ -457,8 +461,8 @@ static void advanced(void) struct json *j = json_parse(cmd, "[ 3 'foo' ]"); struct json_escaped *label, *foo; assert(param(cmd, j->buffer, j->toks, - p_req("label", json_tok_label, &label), - p_opt("foo", json_tok_label, &foo), + p_req("label", param_label, &label), + p_opt("foo", param_label, &foo), NULL)); assert(streq(label->s, "3")); assert(streq(foo->s, "foo")); @@ -468,8 +472,8 @@ static void advanced(void) u64 *msat2; struct json *j = json_parse(cmd, "[ 3 ]"); assert(param(cmd, j->buffer, j->toks, - p_opt_def("msat", json_tok_msat, &msat, 23), - p_opt_def("msat2", json_tok_msat, &msat2, 53), + p_opt_def("msat", param_msat, &msat, 23), + p_opt_def("msat2", param_msat, &msat2, 53), NULL)); assert(*msat == 3); assert(msat2); @@ -483,7 +487,7 @@ static void advanced_fail(void) struct json *j = json_parse(cmd, "[ 'anyx' ]"); u64 *msat; assert(!param(cmd, j->buffer, j->toks, - p_req("msat", json_tok_msat, &msat), + p_req("msat", param_msat, &msat), NULL)); assert(check_fail()); assert(strstr(fail_msg, "'msat' should be a positive" @@ -495,31 +499,31 @@ static void advanced_fail(void) { \ struct json *j = json_parse(cmd, json_); \ T *v; \ - bool ret = cb(cmd, "name", j->buffer, j->toks + 1, &v); \ - assert(ret == pass); \ - if (ret) { \ + struct command_result *ret = cb(cmd, "name", j->buffer, j->toks + 1, &v); \ + assert((ret == NULL) == pass); \ + if (ret == NULL) { \ assert(v); \ assert(*v == value); \ } \ } -static void json_tok_tests(void) +static void param_tests(void) { - test_cb(json_tok_bool, bool, "[ true ]", true, true); - test_cb(json_tok_bool, bool, "[ false ]", false, true); - test_cb(json_tok_bool, bool, "[ tru ]", false, false); - test_cb(json_tok_bool, bool, "[ 1 ]", false, false); - - test_cb(json_tok_percent, double, "[ -0.01 ]", 0, false); - test_cb(json_tok_percent, double, "[ 0.00 ]", 0, true); - test_cb(json_tok_percent, double, "[ 1 ]", 1, true); - test_cb(json_tok_percent, double, "[ 1.1 ]", 1.1, true); - test_cb(json_tok_percent, double, "[ 1.01 ]", 1.01, true); - test_cb(json_tok_percent, double, "[ 99.99 ]", 99.99, true); - test_cb(json_tok_percent, double, "[ 100.0 ]", 100, true); - test_cb(json_tok_percent, double, "[ 100.001 ]", 100.001, true); - test_cb(json_tok_percent, double, "[ 1000 ]", 1000, true); - test_cb(json_tok_percent, double, "[ 'wow' ]", 0, false); + test_cb(param_bool, bool, "[ true ]", true, true); + test_cb(param_bool, bool, "[ false ]", false, true); + test_cb(param_bool, bool, "[ tru ]", false, false); + test_cb(param_bool, bool, "[ 1 ]", false, false); + + test_cb(param_percent, double, "[ -0.01 ]", 0, false); + test_cb(param_percent, double, "[ 0.00 ]", 0, true); + test_cb(param_percent, double, "[ 1 ]", 1, true); + test_cb(param_percent, double, "[ 1.1 ]", 1.1, true); + test_cb(param_percent, double, "[ 1.01 ]", 1.01, true); + test_cb(param_percent, double, "[ 99.99 ]", 99.99, true); + test_cb(param_percent, double, "[ 100.0 ]", 100, true); + test_cb(param_percent, double, "[ 100.001 ]", 100.001, true); + test_cb(param_percent, double, "[ 1000 ]", 1000, true); + test_cb(param_percent, double, "[ 'wow' ]", 0, false); } static void test_invoice(struct command *cmd, @@ -536,12 +540,12 @@ static void test_invoice(struct command *cmd, assert(cmd->mode == CMD_USAGE); if(!param(cmd, buffer, params, - p_req("msatoshi", json_tok_msat, &msatoshi_val), - p_req("label", json_tok_label, &label_val), - p_req("description", json_tok_escaped_string, &desc_val), - p_opt("expiry", json_tok_u64, &expiry), - p_opt("fallbacks", json_tok_array, &fallbacks), - p_opt("preimage", json_tok_tok, &preimagetok), NULL)) + p_req("msatoshi", param_msat, &msatoshi_val), + p_req("label", param_label, &label_val), + p_req("description", param_escaped_string, &desc_val), + p_opt("expiry", param_u64, &expiry), + p_opt("fallbacks", param_array, &fallbacks), + p_opt("preimage", param_tok, &preimagetok), NULL)) return; /* should not be here since we are in the mode of CMD_USAGE @@ -583,7 +587,7 @@ int main(void) sendpay_nulltok(); advanced(); advanced_fail(); - json_tok_tests(); + param_tests(); usage(); tal_free(tmpctx); diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index c070169dc..c24592f2a 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -286,16 +286,16 @@ const char *feerate_name(enum feerate feerate) abort(); } -bool json_feerate_estimate(struct command *cmd, - u32 **feerate_per_kw, enum feerate feerate) +struct command_result *param_feerate_estimate(struct command *cmd, + u32 **feerate_per_kw, + enum feerate feerate) { *feerate_per_kw = tal(cmd, u32); **feerate_per_kw = try_get_feerate(cmd->ld->topology, feerate); - if (!**feerate_per_kw) { - command_fail(cmd, LIGHTNINGD, "Cannot estimate fees"); - return false; - } - return true; + if (!**feerate_per_kw) + return command_fail(cmd, LIGHTNINGD, "Cannot estimate fees"); + + return NULL; } /* Mutual recursion via timer. */ @@ -475,7 +475,7 @@ static void json_feerates(struct command *cmd, enum feerate_style *style; if (!param(cmd, buffer, params, - p_req("style", json_tok_feerate_style, &style), + p_req("style", param_feerate_style, &style), NULL)) return; diff --git a/lightningd/chaintopology.h b/lightningd/chaintopology.h index 175dfcdc5..73d0be0fb 100644 --- a/lightningd/chaintopology.h +++ b/lightningd/chaintopology.h @@ -150,9 +150,10 @@ u32 feerate_to_style(u32 feerate_perkw, enum feerate_style style); const char *feerate_name(enum feerate feerate); -/* Set feerate_per_kw to this estimate, or fail cmd */ -bool json_feerate_estimate(struct command *cmd, - u32 **feerate_per_kw, enum feerate feerate); +/* Set feerate_per_kw to this estimate & return NULL, or fail cmd */ +struct command_result *param_feerate_estimate(struct command *cmd, + u32 **feerate_per_kw, + enum feerate feerate); /* Broadcast a single tx, and rebroadcast as reqd (copies tx). * If failed is non-NULL, call that and don't rebroadcast. */ diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index a15c29d80..9425365b4 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -92,9 +92,9 @@ static void json_connect(struct command *cmd, struct peer *peer; if (!param(cmd, buffer, params, - p_req("id", json_tok_tok, (const jsmntok_t **) &idtok), - p_opt("host", json_tok_string, &name), - p_opt("port", json_tok_number, &port), + p_req("id", param_tok, (const jsmntok_t **) &idtok), + p_opt("host", param_string, &name), + p_opt("port", param_number, &port), NULL)) return; diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 6b6c6bc52..7ad8a88ea 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -253,7 +253,7 @@ static void json_listnodes(struct command *cmd, struct pubkey *id; if (!param(cmd, buffer, params, - p_opt("id", json_tok_pubkey, &id), + p_opt("id", param_pubkey, &id), NULL)) return; @@ -310,13 +310,13 @@ static void json_getroute(struct command *cmd, struct siphash_seed seed; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &destination), - p_req("msatoshi", json_tok_u64, &msatoshi), - p_req("riskfactor", json_tok_double, &riskfactor), - p_opt_def("cltv", json_tok_number, &cltv, 9), - p_opt_def("fromid", json_tok_pubkey, &source, ld->id), - p_opt("seed", json_tok_tok, &seedtok), - p_opt_def("fuzzpercent", json_tok_percent, &fuzz, 75.0), + p_req("id", param_pubkey, &destination), + p_req("msatoshi", param_u64, &msatoshi), + p_req("riskfactor", param_double, &riskfactor), + p_opt_def("cltv", param_number, &cltv, 9), + p_opt_def("fromid", param_pubkey, &source, ld->id), + p_opt("seed", param_tok, &seedtok), + p_opt_def("fuzzpercent", param_percent, &fuzz, 75.0), NULL)) return; @@ -407,7 +407,7 @@ static void json_listchannels(struct command *cmd, u8 *req; struct short_channel_id *id; if (!param(cmd, buffer, params, - p_opt("short_channel_id", json_tok_short_channel_id, &id), + p_opt("short_channel_id", param_short_channel_id, &id), NULL)) return; @@ -463,8 +463,8 @@ static void json_dev_query_scids(struct command *cmd, size_t i; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), - p_req("scids", json_tok_array, &scidstok), + p_req("id", param_pubkey, &id), + p_req("scids", param_array, &scidstok), NULL)) return; @@ -504,9 +504,9 @@ static void json_dev_send_timestamp_filter(struct command *cmd, u32 *first, *range; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), - p_req("first", json_tok_number, &first), - p_req("range", json_tok_number, &range), + p_req("id", param_pubkey, &id), + p_req("first", param_number, &first), + p_req("range", param_number, &range), NULL)) return; @@ -574,9 +574,9 @@ static void json_dev_query_channel_range(struct command *cmd, u32 *first, *num; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), - p_req("first", json_tok_number, &first), - p_req("num", json_tok_number, &num), + p_req("id", param_pubkey, &id), + p_req("first", param_number, &first), + p_req("num", param_number, &num), NULL)) return; @@ -603,7 +603,7 @@ static void json_dev_set_max_scids_encode_size(struct command *cmd, u32 *max; if (!param(cmd, buffer, params, - p_req("max", json_tok_number, &max), + p_req("max", param_number, &max), NULL)) return; diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 2c527971f..771421df5 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -309,12 +309,12 @@ static void json_invoice(struct command *cmd, info->cmd = cmd; if (!param(cmd, buffer, params, - p_req("msatoshi", json_tok_msat, &msatoshi_val), - p_req("label", json_tok_label, &info->label), - p_req("description", json_tok_escaped_string, &desc_val), - p_opt_def("expiry", json_tok_u64, &expiry, 3600), - p_opt("fallbacks", json_tok_array, &fallbacks), - p_opt("preimage", json_tok_tok, &preimagetok), + p_req("msatoshi", param_msat, &msatoshi_val), + p_req("label", param_label, &info->label), + p_req("description", param_escaped_string, &desc_val), + p_opt_def("expiry", param_u64, &expiry, 3600), + p_opt("fallbacks", param_array, &fallbacks), + p_opt("preimage", param_tok, &preimagetok), NULL)) return; @@ -425,7 +425,7 @@ static void json_listinvoices(struct command *cmd, struct json_stream *response; struct wallet *wallet = cmd->ld->wallet; if (!param(cmd, buffer, params, - p_opt("label", json_tok_label, &label), + p_opt("label", param_label, &label), NULL)) return; response = json_stream_success(cmd); @@ -457,8 +457,8 @@ static void json_delinvoice(struct command *cmd, struct wallet *wallet = cmd->ld->wallet; if (!param(cmd, buffer, params, - p_req("label", json_tok_label, &label), - p_req("status", json_tok_string, &status), + p_req("label", param_label, &label), + p_req("status", param_string, &status), NULL)) return; @@ -506,7 +506,7 @@ static void json_delexpiredinvoice(struct command *cmd, u64 *maxexpirytime; if (!param(cmd, buffer, params, - p_opt_def("maxexpirytime", json_tok_u64, &maxexpirytime, + p_opt_def("maxexpirytime", param_u64, &maxexpirytime, time_now().ts.tv_sec), NULL)) return; @@ -531,8 +531,8 @@ static void json_autocleaninvoice(struct command *cmd, u64 *exby; if (!param(cmd, buffer, params, - p_opt_def("cycle_seconds", json_tok_u64, &cycle, 3600), - p_opt_def("expired_by", json_tok_u64, &exby, 86400), + p_opt_def("cycle_seconds", param_u64, &cycle, 3600), + p_opt_def("expired_by", param_u64, &exby, 86400), NULL)) return; @@ -558,7 +558,7 @@ static void json_waitanyinvoice(struct command *cmd, struct wallet *wallet = cmd->ld->wallet; if (!param(cmd, buffer, params, - p_opt_def("lastpay_index", json_tok_u64, &pay_index, 0), + p_opt_def("lastpay_index", param_u64, &pay_index, 0), NULL)) return; @@ -596,7 +596,7 @@ static void json_waitinvoice(struct command *cmd, struct json_escaped *label; if (!param(cmd, buffer, params, - p_req("label", json_tok_label, &label), + p_req("label", param_label, &label), NULL)) return; @@ -671,8 +671,8 @@ static void json_decodepay(struct command *cmd, char *fail; if (!param(cmd, buffer, params, - p_req("bolt11", json_tok_string, &str), - p_opt("description", json_tok_string, &desc), + p_req("bolt11", param_string, &str), + p_opt("description", param_string, &desc), NULL)) return; diff --git a/lightningd/json.c b/lightningd/json.c index af97c2d20..a4d79b553 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -104,18 +104,18 @@ bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, tok->end - tok->start, pubkey); } -bool json_tok_pubkey(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - struct pubkey **pubkey) +struct command_result *param_pubkey(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct pubkey **pubkey) { *pubkey = tal(cmd, struct pubkey); if (json_to_pubkey(buffer, tok, *pubkey)) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a pubkey, not '%.*s'", - name, json_tok_full_len(tok), json_tok_full(buffer, tok)); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a pubkey, not '%.*s'", + name, json_tok_full_len(tok), + json_tok_full(buffer, tok)); } void json_add_short_channel_id(struct json_stream *response, @@ -133,18 +133,20 @@ bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, tok->end - tok->start, scid)); } -bool json_tok_short_channel_id(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - struct short_channel_id **scid) +struct command_result *param_short_channel_id(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + struct short_channel_id **scid) { *scid = tal(cmd, struct short_channel_id); if (json_to_short_channel_id(buffer, tok, *scid)) - return true; + return NULL; - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a short channel id, not '%.*s'", - name, json_tok_full_len(tok), json_tok_full(buffer, tok)); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a short channel id, not '%.*s'", + name, json_tok_full_len(tok), + json_tok_full(buffer, tok)); } const char *json_feerate_style_name(enum feerate_style style) @@ -158,33 +160,34 @@ const char *json_feerate_style_name(enum feerate_style style) abort(); } -bool json_tok_feerate_style(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - enum feerate_style **style) +struct command_result *param_feerate_style(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + enum feerate_style **style) { *style = tal(cmd, enum feerate_style); if (json_tok_streq(buffer, tok, json_feerate_style_name(FEERATE_PER_KSIPA))) { **style = FEERATE_PER_KSIPA; - return true; + return NULL; } else if (json_tok_streq(buffer, tok, json_feerate_style_name(FEERATE_PER_KBYTE))) { **style = FEERATE_PER_KBYTE; - return true; + return NULL; } - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be '%s' or '%s', not '%.*s'", - name, - json_feerate_style_name(FEERATE_PER_KSIPA), - json_feerate_style_name(FEERATE_PER_KBYTE), - json_tok_full_len(tok), json_tok_full(buffer, tok)); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be '%s' or '%s', not '%.*s'", + name, + json_feerate_style_name(FEERATE_PER_KSIPA), + json_feerate_style_name(FEERATE_PER_KBYTE), + json_tok_full_len(tok), json_tok_full(buffer, tok)); } -bool json_tok_feerate(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - u32 **feerate) +struct command_result *param_feerate(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + u32 **feerate) { jsmntok_t base = *tok, suffix = *tok; enum feerate_style style; @@ -192,7 +195,7 @@ bool json_tok_feerate(struct command *cmd, const char *name, for (size_t i = 0; i < NUM_FEERATES; i++) { if (json_tok_streq(buffer, tok, feerate_name(i))) - return json_feerate_estimate(cmd, feerate, i); + return param_feerate_estimate(cmd, feerate, i); } /* We have to split the number and suffix. */ @@ -203,10 +206,10 @@ bool json_tok_feerate(struct command *cmd, const char *name, } if (!json_to_number(buffer, &base, &num)) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' prefix should be an integer, not '%.*s'", - name, base.end - base.start, buffer + base.start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' prefix should be an integer, not '%.*s'", + name, base.end - base.start, + buffer + base.start); } if (json_tok_streq(buffer, &suffix, "") @@ -217,18 +220,18 @@ bool json_tok_feerate(struct command *cmd, const char *name, json_feerate_style_name(FEERATE_PER_KSIPA))) { style = FEERATE_PER_KSIPA; } else { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' suffix should be '%s' or '%s', not '%.*s'", - name, - json_feerate_style_name(FEERATE_PER_KSIPA), - json_feerate_style_name(FEERATE_PER_KBYTE), - suffix.end - suffix.start, buffer + suffix.start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' suffix should be '%s' or '%s', not '%.*s'", + name, + json_feerate_style_name(FEERATE_PER_KSIPA), + json_feerate_style_name(FEERATE_PER_KBYTE), + suffix.end - suffix.start, + buffer + suffix.start); } *feerate = tal(cmd, u32); **feerate = feerate_from_style(num, style); - return true; + return NULL; } bool diff --git a/lightningd/json.h b/lightningd/json.h index 00f723bd0..984f0379a 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -51,17 +51,19 @@ void json_add_txid(struct json_stream *result, const char *fieldname, bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, struct pubkey *pubkey); -bool json_tok_pubkey(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - struct pubkey **pubkey); +struct command_result *param_pubkey(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct pubkey **pubkey); /* Extract a short_channel_id from this */ bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, struct short_channel_id *scid); -bool json_tok_short_channel_id(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - struct short_channel_id **scid); +struct command_result *param_short_channel_id(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + struct short_channel_id **scid); enum feerate_style { FEERATE_PER_KSIPA, @@ -69,16 +71,18 @@ enum feerate_style { }; /* Extract a feerate style. */ -bool json_tok_feerate_style(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - enum feerate_style **style); +struct command_result *param_feerate_style(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + enum feerate_style **style); const char *json_feerate_style_name(enum feerate_style style); /* Extract a feerate with optional style suffix. */ -bool json_tok_feerate(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - u32 **feerate); +struct command_result *param_feerate(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + u32 **feerate); /* '"fieldname" : "1234:5:6"' */ void json_add_short_channel_id(struct json_stream *response, @@ -144,12 +148,12 @@ enum address_parse_result { * allocated off ctx if ADDRESS_PARSE_SUCCESS */ enum address_parse_result json_tok_address_scriptpubkey(const tal_t *ctx, - const struct chainparams *chainparams, - const char *buffer, - const jsmntok_t *tok, const u8 **scriptpubkey); + const struct chainparams *chainparams, + const char *buffer, + const jsmntok_t *tok, const u8 **scriptpubkey); /* Parse the satoshi token in wallet_tx. */ -bool json_tok_wtx(struct wallet_tx * tx, const char * buffer, - const jsmntok_t * sattok, u64 max); +struct command_result *param_wtx(struct wallet_tx * tx, const char * buffer, + const jsmntok_t * sattok, u64 max); #endif /* LIGHTNING_LIGHTNINGD_JSON_H */ diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 0962c386b..ba29ed91e 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -200,7 +200,7 @@ static void json_rhash(struct command *cmd, struct sha256 *secret; if (!param(cmd, buffer, params, - p_req("secret", json_tok_sha256, &secret), + p_req("secret", param_sha256, &secret), NULL)) return; @@ -250,7 +250,7 @@ static void json_slowcmd(struct command *cmd, sc->cmd = cmd; if (!param(cmd, buffer, params, - p_opt_def("msec", json_tok_number, &sc->msec, 1000), + p_opt_def("msec", param_number, &sc->msec, 1000), NULL)) return; @@ -335,7 +335,7 @@ static void json_help(struct command *cmd, struct json_command **commands = cmd->ld->jsonrpc->commands; if (!param(cmd, buffer, params, - p_opt("command", json_tok_tok, &cmdtok), + p_opt("command", param_tok, &cmdtok), NULL)) return; @@ -963,36 +963,37 @@ json_tok_address_scriptpubkey(const tal_t *cxt, return ADDRESS_PARSE_UNRECOGNIZED; } -bool json_tok_wtx(struct wallet_tx * tx, const char * buffer, - const jsmntok_t *sattok, u64 max) +struct command_result *param_wtx(struct wallet_tx * tx, const char * buffer, + const jsmntok_t *sattok, u64 max) { if (json_tok_streq(buffer, sattok, "all")) { tx->all_funds = true; tx->amount = max; } else if (!json_to_u64(buffer, sattok, &tx->amount)) { - command_fail(tx->cmd, JSONRPC2_INVALID_PARAMS, - "Invalid satoshis"); - return false; + return command_fail(tx->cmd, JSONRPC2_INVALID_PARAMS, + "Invalid satoshis"); } else if (tx->amount > max) { - command_fail(tx->cmd, FUND_MAX_EXCEEDED, - "Amount exceeded %"PRIu64, max); - return false; + return command_fail(tx->cmd, FUND_MAX_EXCEEDED, + "Amount exceeded %"PRIu64, max); } - return true; + return NULL; } -static bool json_tok_command(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - const jsmntok_t **out) +static struct command_result *param_command(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + const jsmntok_t **out) { cmd->json_cmd = find_cmd(cmd->jcon->ld->jsonrpc, buffer, tok); - if (cmd->json_cmd) - return (*out = tok); + if (cmd->json_cmd) { + *out = tok; + return NULL; + } - command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND, - "Unknown command '%.*s'", - tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_METHOD_NOT_FOUND, + "Unknown command '%.*s'", + tok->end - tok->start, buffer + tok->start); } /* We add this destructor as a canary to detect cmd failing. */ @@ -1018,7 +1019,7 @@ static void json_check(struct command *cmd, } if (!param(cmd, buffer, mod_params, - p_req("command_to_check", json_tok_command, &name_tok), + p_req("command_to_check", param_command, &name_tok), p_opt_any(), NULL)) return; diff --git a/lightningd/log.c b/lightningd/log.c index 57de95bcd..bed8e138e 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -702,9 +702,11 @@ void json_add_log(struct json_stream *response, json_array_end(info.response); } -bool json_tok_loglevel(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - enum log_level **level) +struct command_result *param_loglevel(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + enum log_level **level) { *level = tal(cmd, enum log_level); if (json_tok_streq(buffer, tok, "io")) @@ -716,14 +718,14 @@ bool json_tok_loglevel(struct command *cmd, const char *name, else if (json_tok_streq(buffer, tok, "unusual")) **level = LOG_UNUSUAL; else { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be 'io', 'debug', 'info', or " - "'unusual', not '%.*s'", - name, - json_tok_full_len(tok), json_tok_full(buffer, tok)); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be 'io', 'debug', 'info', or " + "'unusual', not '%.*s'", + name, + json_tok_full_len(tok), + json_tok_full(buffer, tok)); } - return true; + return NULL; } static void json_getlog(struct command *cmd, @@ -736,8 +738,7 @@ static void json_getlog(struct command *cmd, struct log_book *lr = cmd->ld->log_book; if (!param(cmd, buffer, params, - p_opt_def("level", json_tok_loglevel, &minlevel, - LOG_INFORM), + p_opt_def("level", param_loglevel, &minlevel, LOG_INFORM), NULL)) return; diff --git a/lightningd/log.h b/lightningd/log.h index 95e118134..fb38a316c 100644 --- a/lightningd/log.h +++ b/lightningd/log.h @@ -103,8 +103,10 @@ void log_backtrace_exit(void); void json_add_log(struct json_stream *result, const struct log_book *lr, enum log_level minlevel); -bool json_tok_loglevel(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - enum log_level **level); +struct command_result *param_loglevel(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + enum log_level **level); #endif /* LIGHTNING_LIGHTNINGD_LOG_H */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 99c95cbb0..f46c274ab 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -767,6 +767,7 @@ static void json_fund_channel(struct command *cmd, const jsmntok_t *obj UNNEEDED, const jsmntok_t *params) { + struct command_result *res; const jsmntok_t *sattok; struct funding_channel * fc = tal(cmd, struct funding_channel); struct pubkey *id; @@ -781,14 +782,15 @@ static void json_fund_channel(struct command *cmd, fc->uc = NULL; wtx_init(cmd, &fc->wtx); if (!param(fc->cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), - p_req("satoshi", json_tok_tok, &sattok), - p_opt("feerate", json_tok_feerate, &feerate_per_kw), - p_opt_def("announce", json_tok_bool, &announce_channel, true), + p_req("id", param_pubkey, &id), + p_req("satoshi", param_tok, &sattok), + p_opt("feerate", param_feerate, &feerate_per_kw), + p_opt_def("announce", param_bool, &announce_channel, true), NULL)) return; - if (!json_tok_wtx(&fc->wtx, buffer, sattok, max_funding_satoshi)) + res = param_wtx(&fc->wtx, buffer, sattok, max_funding_satoshi); + if (res) return; if (!feerate_per_kw) { diff --git a/lightningd/options.c b/lightningd/options.c index 95d4620a1..6e12b9614 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -1056,7 +1056,7 @@ static void json_listconfigs(struct command *cmd, const jsmntok_t *configtok; if (!param(cmd, buffer, params, - p_opt("config", json_tok_tok, &configtok), + p_opt("config", param_tok, &configtok), NULL)) return; diff --git a/lightningd/pay.c b/lightningd/pay.c index 52ebd99d5..dcf6c4b74 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -955,10 +955,10 @@ static void json_sendpay(struct command *cmd, const char *description; if (!param(cmd, buffer, params, - p_req("route", json_tok_array, &routetok), - p_req("payment_hash", json_tok_sha256, &rhash), - p_opt("description", json_tok_escaped_string, &description), - p_opt("msatoshi", json_tok_u64, &msatoshi), + p_req("route", param_array, &routetok), + p_req("payment_hash", param_sha256, &rhash), + p_opt("description", param_escaped_string, &description), + p_opt("msatoshi", param_u64, &msatoshi), NULL)) return; @@ -973,10 +973,10 @@ static void json_sendpay(struct command *cmd, unsigned *delay; if (!param(cmd, buffer, t, - p_req("msatoshi", json_tok_u64, &amount), - p_req("id", json_tok_pubkey, &id), - p_req("delay", json_tok_number, &delay), - p_req("channel", json_tok_short_channel_id, &channel), + p_req("msatoshi", param_u64, &amount), + p_req("id", param_pubkey, &id), + p_req("delay", param_number, &delay), + p_req("channel", param_short_channel_id, &channel), NULL)) return; @@ -1037,8 +1037,8 @@ static void json_waitsendpay(struct command *cmd, unsigned int *timeout; if (!param(cmd, buffer, params, - p_req("payment_hash", json_tok_sha256, &rhash), - p_opt("timeout", json_tok_number, &timeout), + p_req("payment_hash", param_sha256, &rhash), + p_opt("timeout", param_number, &timeout), NULL)) return; @@ -1070,8 +1070,8 @@ static void json_listpayments(struct command *cmd, const char *b11str; if (!param(cmd, buffer, params, - p_opt("bolt11", json_tok_string, &b11str), - p_opt("payment_hash", json_tok_sha256, &rhash), + p_opt("bolt11", param_string, &b11str), + p_opt("payment_hash", param_sha256, &rhash), NULL)) return; diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index f657bf0b1..4bd67a8cb 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -609,15 +609,15 @@ static void json_pay(struct command *cmd, unsigned int *exemptfee; if (!param(cmd, buffer, params, - p_req("bolt11", json_tok_string, &b11str), - p_opt("msatoshi", json_tok_u64, &msatoshi), - p_opt("description", json_tok_string, &desc), - p_opt_def("riskfactor", json_tok_double, &riskfactor, 1.0), - p_opt_def("maxfeepercent", json_tok_percent, &maxfeepercent, 0.5), - p_opt_def("retry_for", json_tok_number, &retryfor, 60), - p_opt_def("maxdelay", json_tok_number, &maxdelay, + p_req("bolt11", param_string, &b11str), + p_opt("msatoshi", param_u64, &msatoshi), + p_opt("description", param_string, &desc), + p_opt_def("riskfactor", param_double, &riskfactor, 1.0), + p_opt_def("maxfeepercent", param_percent, &maxfeepercent, 0.5), + p_opt_def("retry_for", param_number, &retryfor, 60), + p_opt_def("maxdelay", param_number, &maxdelay, cmd->ld->config.locktime_max), - p_opt_def("exemptfee", json_tok_number, &exemptfee, 5000), + p_opt_def("exemptfee", param_number, &exemptfee, 5000), NULL)) return; diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 6bc8e632e..c87b67e17 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -814,8 +814,8 @@ static void json_listpeers(struct command *cmd, struct json_stream *response; if (!param(cmd, buffer, params, - p_opt("id", json_tok_pubkey, &specific_id), - p_opt("level", json_tok_loglevel, &ll), + p_opt("id", param_pubkey, &specific_id), + p_opt("level", param_loglevel, &ll), NULL)) return; @@ -903,9 +903,9 @@ static void json_close(struct command *cmd, bool *force; if (!param(cmd, buffer, params, - p_req("id", json_tok_tok, &idtok), - p_opt_def("force", json_tok_bool, &force, false), - p_opt_def("timeout", json_tok_number, &timeout, 30), + p_req("id", param_tok, &idtok), + p_opt_def("force", param_bool, &force, false), + p_opt_def("timeout", param_number, &timeout, 30), NULL)) return; @@ -1046,8 +1046,8 @@ static void json_disconnect(struct command *cmd, bool *force; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), - p_opt_def("force", json_tok_bool, &force, false), + p_req("id", param_pubkey, &id), + p_opt_def("force", param_bool, &force, false), NULL)) return; @@ -1171,7 +1171,7 @@ static void json_sign_last_tx(struct command *cmd, struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), + p_req("id", param_pubkey, &peerid), NULL)) return; @@ -1218,7 +1218,7 @@ static void json_dev_fail(struct command *cmd, struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), + p_req("id", param_pubkey, &peerid), NULL)) return; @@ -1266,7 +1266,7 @@ static void json_dev_reenable_commit(struct command *cmd, struct channel *channel; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), + p_req("id", param_pubkey, &peerid), NULL)) return; @@ -1361,9 +1361,9 @@ static void json_dev_forget_channel(struct command *cmd, bool *force; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), - p_opt("short_channel_id", json_tok_short_channel_id, &scid), - p_opt_def("force", json_tok_bool, &force, false), + p_req("id", param_pubkey, &peerid), + p_opt("short_channel_id", param_short_channel_id, &scid), + p_opt_def("force", param_bool, &force, false), NULL)) return; diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index bcac32520..b3b8cdf0b 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -1802,8 +1802,8 @@ static void json_dev_ignore_htlcs(struct command *cmd, bool *ignore; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &peerid), - p_req("ignore", json_tok_bool, &ignore), + p_req("id", param_pubkey, &peerid), + p_req("ignore", param_bool, &ignore), NULL)) return; diff --git a/lightningd/ping.c b/lightningd/ping.c index b379b4244..d63e9f511 100644 --- a/lightningd/ping.c +++ b/lightningd/ping.c @@ -89,9 +89,9 @@ static void json_ping(struct command *cmd, struct pubkey *id; if (!param(cmd, buffer, params, - p_req("id", json_tok_pubkey, &id), - p_opt_def("len", json_tok_number, &len, 128), - p_opt_def("pongbytes", json_tok_number, &pongbytes, 128), + p_req("id", param_pubkey, &id), + p_opt_def("len", param_number, &len, 128), + p_opt_def("pongbytes", param_number, &pongbytes, 128), NULL)) return; diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 10de66cf1..e9e65d24d 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -183,74 +183,14 @@ struct json_stream *json_stream_success(struct command *cmd UNNEEDED) { fprintf(stderr, "json_stream_success called!\n"); abort(); } /* Generated stub for json_tok_address_scriptpubkey */ enum address_parse_result json_tok_address_scriptpubkey(const tal_t *ctx UNNEEDED, - const struct chainparams *chainparams UNNEEDED, - const char *buffer UNNEEDED, - const jsmntok_t *tok UNNEEDED, const u8 **scriptpubkey UNNEEDED) + const struct chainparams *chainparams UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, const u8 **scriptpubkey UNNEEDED) { fprintf(stderr, "json_tok_address_scriptpubkey called!\n"); abort(); } -/* Generated stub for json_tok_array */ -bool json_tok_array(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - const jsmntok_t **arr UNNEEDED) -{ fprintf(stderr, "json_tok_array called!\n"); abort(); } -/* Generated stub for json_tok_bool */ -bool json_tok_bool(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - bool **b UNNEEDED) -{ fprintf(stderr, "json_tok_bool called!\n"); abort(); } /* Generated stub for json_tok_channel_id */ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct channel_id *cid UNNEEDED) { fprintf(stderr, "json_tok_channel_id called!\n"); abort(); } -/* Generated stub for json_tok_escaped_string */ -bool json_tok_escaped_string(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - const char **str UNNEEDED) -{ fprintf(stderr, "json_tok_escaped_string called!\n"); abort(); } -/* Generated stub for json_tok_label */ -bool json_tok_label(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct json_escaped **label UNNEEDED) -{ fprintf(stderr, "json_tok_label called!\n"); abort(); } -/* Generated stub for json_tok_loglevel */ -bool json_tok_loglevel(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - enum log_level **level UNNEEDED) -{ fprintf(stderr, "json_tok_loglevel called!\n"); abort(); } -/* Generated stub for json_tok_msat */ -bool json_tok_msat(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, - u64 **msatoshi_val UNNEEDED) -{ fprintf(stderr, "json_tok_msat called!\n"); abort(); } -/* Generated stub for json_tok_number */ -bool json_tok_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - unsigned int **num UNNEEDED) -{ fprintf(stderr, "json_tok_number called!\n"); abort(); } -/* Generated stub for json_tok_pubkey */ -bool json_tok_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct pubkey **pubkey UNNEEDED) -{ fprintf(stderr, "json_tok_pubkey called!\n"); abort(); } -/* Generated stub for json_tok_short_channel_id */ -bool json_tok_short_channel_id(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct short_channel_id **scid UNNEEDED) -{ fprintf(stderr, "json_tok_short_channel_id called!\n"); abort(); } -/* Generated stub for json_tok_string */ -bool json_tok_string(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - const char **str UNNEEDED) -{ fprintf(stderr, "json_tok_string called!\n"); abort(); } -/* Generated stub for json_tok_tok */ -bool json_tok_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, - const jsmntok_t **out UNNEEDED) -{ fprintf(stderr, "json_tok_tok called!\n"); abort(); } -/* Generated stub for json_tok_u64 */ -bool json_tok_u64(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - uint64_t **num UNNEEDED) -{ fprintf(stderr, "json_tok_u64 called!\n"); abort(); } /* Generated stub for json_to_pubkey */ bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct pubkey *pubkey UNNEEDED) @@ -305,6 +245,72 @@ void opening_peer_no_active_channels(struct peer *peer UNNEEDED) bool param(struct command *cmd UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t params[] UNNEEDED, ...) { fprintf(stderr, "param called!\n"); abort(); } +/* Generated stub for param_array */ +struct command_result *param_array(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + const jsmntok_t **arr UNNEEDED) +{ fprintf(stderr, "param_array called!\n"); abort(); } +/* Generated stub for param_bool */ +struct command_result *param_bool(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + bool **b UNNEEDED) +{ fprintf(stderr, "param_bool called!\n"); abort(); } +/* Generated stub for param_escaped_string */ +struct command_result *param_escaped_string(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const char **str UNNEEDED) +{ fprintf(stderr, "param_escaped_string called!\n"); abort(); } +/* Generated stub for param_label */ +struct command_result *param_label(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct json_escaped **label UNNEEDED) +{ fprintf(stderr, "param_label called!\n"); abort(); } +/* Generated stub for param_loglevel */ +struct command_result *param_loglevel(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + enum log_level **level UNNEEDED) +{ fprintf(stderr, "param_loglevel called!\n"); abort(); } +/* Generated stub for param_msat */ +struct command_result *param_msat(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, + u64 **msatoshi_val UNNEEDED) +{ fprintf(stderr, "param_msat called!\n"); abort(); } +/* Generated stub for param_number */ +struct command_result *param_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + unsigned int **num UNNEEDED) +{ fprintf(stderr, "param_number called!\n"); abort(); } +/* Generated stub for param_pubkey */ +struct command_result *param_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct pubkey **pubkey UNNEEDED) +{ fprintf(stderr, "param_pubkey called!\n"); abort(); } +/* Generated stub for param_short_channel_id */ +struct command_result *param_short_channel_id(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + struct short_channel_id **scid UNNEEDED) +{ fprintf(stderr, "param_short_channel_id called!\n"); abort(); } +/* Generated stub for param_string */ +struct command_result *param_string(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + const char **str UNNEEDED) +{ fprintf(stderr, "param_string called!\n"); abort(); } +/* Generated stub for param_tok */ +struct command_result *param_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, + const jsmntok_t **out UNNEEDED) +{ fprintf(stderr, "param_tok called!\n"); abort(); } +/* Generated stub for param_u64 */ +struct command_result *param_u64(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + uint64_t **num UNNEEDED) +{ fprintf(stderr, "param_u64 called!\n"); abort(); } /* Generated stub for peer_memleak_done */ void peer_memleak_done(struct command *cmd UNNEEDED, struct subd *leaker UNNEEDED) { fprintf(stderr, "peer_memleak_done called!\n"); abort(); } diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 7b1e787f4..09055b6e3 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -21,25 +21,6 @@ const char *feerate_name(enum feerate feerate UNNEEDED) /* Generated stub for fmt_wireaddr_without_port */ char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr *a UNNEEDED) { fprintf(stderr, "fmt_wireaddr_without_port called!\n"); abort(); } -/* Generated stub for json_feerate_estimate */ -bool json_feerate_estimate(struct command *cmd UNNEEDED, - u32 **feerate_per_kw UNNEEDED, enum feerate feerate UNNEEDED) -{ fprintf(stderr, "json_feerate_estimate called!\n"); abort(); } -/* Generated stub for json_tok_number */ -bool json_tok_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - unsigned int **num UNNEEDED) -{ fprintf(stderr, "json_tok_number called!\n"); abort(); } -/* Generated stub for json_tok_sha256 */ -bool json_tok_sha256(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct sha256 **hash UNNEEDED) -{ fprintf(stderr, "json_tok_sha256 called!\n"); abort(); } -/* Generated stub for json_tok_tok */ -bool json_tok_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, - const jsmntok_t **out UNNEEDED) -{ fprintf(stderr, "json_tok_tok called!\n"); abort(); } /* Generated stub for log_ */ void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...) @@ -64,6 +45,26 @@ struct oneshot *new_reltimer_(struct timers *timers UNNEEDED, bool param(struct command *cmd UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t params[] UNNEEDED, ...) { fprintf(stderr, "param called!\n"); abort(); } +/* Generated stub for param_feerate_estimate */ +struct command_result *param_feerate_estimate(struct command *cmd UNNEEDED, + u32 **feerate_per_kw UNNEEDED, + enum feerate feerate UNNEEDED) +{ fprintf(stderr, "param_feerate_estimate called!\n"); abort(); } +/* Generated stub for param_number */ +struct command_result *param_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + unsigned int **num UNNEEDED) +{ fprintf(stderr, "param_number called!\n"); abort(); } +/* Generated stub for param_sha256 */ +struct command_result *param_sha256(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct sha256 **hash UNNEEDED) +{ fprintf(stderr, "param_sha256 called!\n"); abort(); } +/* Generated stub for param_tok */ +struct command_result *param_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, + const jsmntok_t **out UNNEEDED) +{ fprintf(stderr, "param_tok called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ bool deprecated_apis; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 4cc7d5cd3..255933802 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -257,11 +257,6 @@ void json_object_start(struct json_stream *ks UNNEEDED, const char *fieldname UN /* Generated stub for json_stream_success */ struct json_stream *json_stream_success(struct command *cmd UNNEEDED) { fprintf(stderr, "json_stream_success called!\n"); abort(); } -/* Generated stub for json_tok_bool */ -bool json_tok_bool(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - bool **b UNNEEDED) -{ fprintf(stderr, "json_tok_bool called!\n"); abort(); } /* Generated stub for json_tok_channel_id */ bool json_tok_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct channel_id *cid UNNEEDED) @@ -272,31 +267,6 @@ const char *json_tok_full(const char *buffer UNNEEDED, const jsmntok_t *t UNNEED /* Generated stub for json_tok_full_len */ int json_tok_full_len(const jsmntok_t *t UNNEEDED) { fprintf(stderr, "json_tok_full_len called!\n"); abort(); } -/* Generated stub for json_tok_loglevel */ -bool json_tok_loglevel(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - enum log_level **level UNNEEDED) -{ fprintf(stderr, "json_tok_loglevel called!\n"); abort(); } -/* Generated stub for json_tok_number */ -bool json_tok_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - unsigned int **num UNNEEDED) -{ fprintf(stderr, "json_tok_number called!\n"); abort(); } -/* Generated stub for json_tok_pubkey */ -bool json_tok_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct pubkey **pubkey UNNEEDED) -{ fprintf(stderr, "json_tok_pubkey called!\n"); abort(); } -/* Generated stub for json_tok_short_channel_id */ -bool json_tok_short_channel_id(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, - struct short_channel_id **scid UNNEEDED) -{ fprintf(stderr, "json_tok_short_channel_id called!\n"); abort(); } -/* Generated stub for json_tok_tok */ -bool json_tok_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, - const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, - const jsmntok_t **out UNNEEDED) -{ fprintf(stderr, "json_tok_tok called!\n"); abort(); } /* Generated stub for json_to_pubkey */ bool json_to_pubkey(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct pubkey *pubkey UNNEEDED) @@ -349,6 +319,40 @@ void outpointfilter_remove(struct outpointfilter *of UNNEEDED, bool param(struct command *cmd UNNEEDED, const char *buffer UNNEEDED, const jsmntok_t params[] UNNEEDED, ...) { fprintf(stderr, "param called!\n"); abort(); } +/* Generated stub for param_bool */ +struct command_result *param_bool(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + bool **b UNNEEDED) +{ fprintf(stderr, "param_bool called!\n"); abort(); } +/* Generated stub for param_loglevel */ +struct command_result *param_loglevel(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + enum log_level **level UNNEEDED) +{ fprintf(stderr, "param_loglevel called!\n"); abort(); } +/* Generated stub for param_number */ +struct command_result *param_number(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + unsigned int **num UNNEEDED) +{ fprintf(stderr, "param_number called!\n"); abort(); } +/* Generated stub for param_pubkey */ +struct command_result *param_pubkey(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct pubkey **pubkey UNNEEDED) +{ fprintf(stderr, "param_pubkey called!\n"); abort(); } +/* Generated stub for param_short_channel_id */ +struct command_result *param_short_channel_id(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + struct short_channel_id **scid UNNEEDED) +{ fprintf(stderr, "param_short_channel_id called!\n"); abort(); } +/* Generated stub for param_tok */ +struct command_result *param_tok(struct command *cmd UNNEEDED, const char *name UNNEEDED, + const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, + const jsmntok_t **out UNNEEDED) +{ fprintf(stderr, "param_tok called!\n"); abort(); } /* Generated stub for parse_onionpacket */ struct onionpacket *parse_onionpacket( const tal_t *ctx UNNEEDED, diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index e2410c7e0..63f9199b9 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -95,22 +95,26 @@ static void json_withdraw(struct command *cmd, u32 *feerate_per_kw; struct bitcoin_tx *tx; enum address_parse_result addr_parse; + struct command_result *res; withdraw->cmd = cmd; wtx_init(cmd, &withdraw->wtx); if (!param(cmd, buffer, params, - p_req("destination", json_tok_tok, &desttok), - p_req("satoshi", json_tok_tok, &sattok), - p_opt("feerate", json_tok_feerate, &feerate_per_kw), + p_req("destination", param_tok, &desttok), + p_req("satoshi", param_tok, &sattok), + p_opt("feerate", param_feerate, &feerate_per_kw), NULL)) return; - if (!json_tok_wtx(&withdraw->wtx, buffer, sattok, -1ULL)) + res = param_wtx(&withdraw->wtx, buffer, sattok, -1ULL); + if (res) return; if (!feerate_per_kw) { - if (!json_feerate_estimate(cmd, &feerate_per_kw, FEERATE_NORMAL)) + res = param_feerate_estimate(cmd, &feerate_per_kw, + FEERATE_NORMAL); + if (res) return; } @@ -223,23 +227,24 @@ encode_pubkey_to_addr(const tal_t *ctx, } /* Extract a bool indicating "p2sh-segwit" or "bech32" */ -static bool json_tok_newaddr(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - bool **is_p2wpkh) +static struct command_result *param_newaddr(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + bool **is_p2wpkh) { *is_p2wpkh = tal(cmd, bool); if (json_tok_streq(buffer, tok, "p2sh-segwit")) { **is_p2wpkh = false; - return true; + return NULL; } if (json_tok_streq(buffer, tok, "bech32")) { **is_p2wpkh = true; - return true; + return NULL; } - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be 'bech32' or 'p2sh-segwit', not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; + return command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be 'bech32' or 'p2sh-segwit', not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); } static void json_newaddr(struct command *cmd, @@ -255,7 +260,7 @@ static void json_newaddr(struct command *cmd, char *out; if (!param(cmd, buffer, params, - p_opt_def("addresstype", json_tok_newaddr, &is_p2wpkh, true), + p_opt_def("addresstype", param_newaddr, &is_p2wpkh, true), NULL)) return; @@ -314,7 +319,7 @@ static void json_listaddrs(struct command *cmd, u64 *bip32_max_index; if (!param(cmd, buffer, params, - p_opt_def("bip32_max_index", json_tok_u64, &bip32_max_index, + p_opt_def("bip32_max_index", param_u64, &bip32_max_index, db_get_intvar(cmd->ld->wallet->db, "bip32_max_index", 0)), NULL))