common: add option for dev-only parameters.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-09-21 15:06:27 +09:30
parent de81a59b1e
commit ef87999f9a
9 changed files with 53 additions and 2 deletions

View file

@ -39,6 +39,9 @@ bool command_usage_only(const struct command *cmd);
/* Do we allow deprecated apis? */
bool command_deprecated_apis(const struct command *cmd);
/* Do we allow dev commands? */
bool command_dev_apis(const struct command *cmd);
/* If so, this is called. */
void command_set_usage(struct command *cmd, const char *usage);

View file

@ -65,7 +65,9 @@ static struct command_result *make_callback(struct command *cmd,
const jsmntok_t *tok)
{
/* If it had a default, free that now to avoid leak */
if (def->style == PARAM_OPTIONAL_WITH_DEFAULT && !def->is_set)
if ((def->style == PARAM_OPTIONAL_WITH_DEFAULT
|| def->style == PARAM_OPTIONAL_DEV_WITH_DEFAULT)
&& !def->is_set)
tal_free(*(void **)def->arg);
def->is_set = true;
@ -115,6 +117,11 @@ static struct command_result *parse_by_position(struct command *cmd,
}
if (!json_tok_is_null(buffer, tok)) {
if (params[i].style == PARAM_OPTIONAL_DEV_WITH_DEFAULT
&& !command_dev_apis(cmd)) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Parameter %zu is developer-only", i);
}
res = make_callback(cmd, params+i, buffer, tok);
if (res)
return res;
@ -176,6 +183,12 @@ static struct command_result *parse_by_name(struct command *cmd,
p->name);
}
if (p->style == PARAM_OPTIONAL_DEV_WITH_DEFAULT
&& !command_dev_apis(cmd)) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Parameter '%s' is developer-only",
p->name);
}
res = make_callback(cmd, p, buffer, t + 1);
if (res)
return res;

View file

@ -78,6 +78,7 @@ enum param_style {
PARAM_REQUIRED_ALLOW_DUPS,
PARAM_OPTIONAL,
PARAM_OPTIONAL_WITH_DEFAULT,
PARAM_OPTIONAL_DEV_WITH_DEFAULT,
};
/*
@ -136,6 +137,21 @@ enum param_style {
(const jsmntok_t *)NULL, \
(arg)) == (struct command_result *)NULL); })
/*
* Add a dev-only parameter. *arg is set to @def if it isn't found.
*/
#define p_opt_dev(name, cbx, arg, def) \
name"", \
PARAM_OPTIONAL_DEV_WITH_DEFAULT, \
(param_cbx)(cbx), \
({ (*arg) = tal((cmd), typeof(**arg)); \
(**arg) = (def); \
(arg) + 0*sizeof((cbx)((struct command *)NULL, \
(const char *)NULL, \
(const char *)NULL, \
(const jsmntok_t *)NULL, \
(arg)) == (struct command_result *)NULL); })
/* Special flag for 'check' which allows any parameters. */
#define p_opt_any() "", PARAM_OPTIONAL, NULL, NULL

View file

@ -18,6 +18,9 @@ bool command_check_only(const struct command *cmd UNNEEDED)
/* Generated stub for command_deprecated_apis */
bool command_deprecated_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_deprecated_apis called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

View file

@ -50,6 +50,9 @@ bool command_check_only(const struct command *cmd UNNEEDED)
/* Generated stub for command_deprecated_apis */
bool command_deprecated_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_deprecated_apis called!\n"); abort(); }
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_fail */
struct command_result *command_fail(struct command *cmd UNNEEDED, enum jsonrpc_errcode code UNNEEDED,
const char *fmt UNNEEDED, ...)

View file

@ -49,6 +49,9 @@ struct command_result *command_fail(struct command *cmd,
}
/* AUTOGENERATED MOCKS START */
/* Generated stub for command_dev_apis */
bool command_dev_apis(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_dev_apis called!\n"); abort(); }
/* Generated stub for command_filter_ptr */
struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_filter_ptr called!\n"); abort(); }

View file

@ -1295,6 +1295,11 @@ bool command_deprecated_apis(const struct command *cmd)
return cmd->ld->deprecated_apis;
}
bool command_dev_apis(const struct command *cmd)
{
return cmd->ld->developer;
}
void command_set_usage(struct command *cmd, const char *usage TAKES)
{
usage = tal_strdup(cmd->ld, usage);

View file

@ -505,6 +505,11 @@ bool command_deprecated_apis(const struct command *cmd)
return deprecated_apis;
}
bool command_dev_apis(const struct command *cmd)
{
return cmd->plugin->developer;
}
/* FIXME: would be good to support this! */
bool command_check_only(const struct command *cmd)
{

View file

@ -20,7 +20,7 @@ struct payment *payment_new(const tal_t *ctx,
unsigned int maxdelay,
u64 retryfor,
u16 final_cltv,
/* Tweakable in DEVELOPER mode */
/* Tweakable in --developer mode */
u64 base_fee_penalty,
u64 prob_cost_factor,
u64 riskfactor_millionths,