common: add command_deprecated_param_ok() and command_deprecated_out_ok()

Generic helpers for libplugin and lightningd.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2024-01-25 10:58:54 +10:30
parent 4764ec794c
commit 7431b8b436
9 changed files with 127 additions and 23 deletions

View File

@ -36,8 +36,17 @@ command_fail_badparam(struct command *cmd,
/* Also caller supplied: is this invoked simply to get usage? */
bool command_usage_only(const struct command *cmd);
/* Do we allow deprecated apis? */
bool command_deprecated_apis(const struct command *cmd);
/* Caller supplies this too: they tried to use a deprecated parameter (or cmd). */
bool command_deprecated_in_ok(struct command *cmd,
const char *param,
const char *depr_start,
const char *depr_end);
/* Caller supplies this: should we output this deprecated thing */
bool command_deprecated_out_ok(struct command *cmd,
const char *fieldname,
const char *depr_start,
const char *depr_end);
/* Do we allow dev commands? */
bool command_dev_apis(const struct command *cmd);

View File

@ -15,9 +15,6 @@ struct command;
/* Generated stub for command_check_only */
bool command_check_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_check_only called!\n"); abort(); }
/* 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(); }

View File

@ -44,9 +44,6 @@ struct amount_sat amount_tx_fee(u32 fee_per_kw UNNEEDED, size_t weight UNNEEDED)
/* Generated stub for command_check_only */
bool command_check_only(const struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_check_only called!\n"); abort(); }
/* 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(); }

View File

@ -99,11 +99,6 @@ bool command_check_only(const struct command *cmd)
return cmd->mode == CMD_CHECK;
}
bool command_deprecated_apis(const struct command *cmd)
{
return cmd->deprecated_apis;
}
struct json {
jsmntok_t *toks;
char *buffer;

View File

@ -652,6 +652,36 @@ struct json_filter **command_filter_ptr(struct command *cmd)
return &cmd->filter;
}
static bool command_deprecated_ok(const struct command *cmd)
{
return cmd->ld->deprecated_apis;
}
bool command_deprecated_in_ok(struct command *cmd,
const char *param,
const char *depr_start,
const char *depr_end)
{
return lightningd_deprecated_in_ok(cmd->ld,
command_log(cmd),
command_deprecated_ok(cmd),
cmd->json_cmd->name, param,
depr_start, depr_end,
cmd->id);
}
bool command_deprecated_out_ok(struct command *cmd,
const char *fieldname,
const char *depr_start,
const char *depr_end)
{
return lightningd_deprecated_out_ok(cmd->ld,
command_deprecated_ok(cmd),
cmd->json_cmd->name,
fieldname,
depr_start, depr_end);
}
struct command_result *command_still_pending(struct command *cmd)
{
notleak_with_children(cmd);
@ -1425,11 +1455,6 @@ bool command_usage_only(const struct command *cmd)
return cmd->mode == CMD_USAGE;
}
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;

View File

@ -63,6 +63,24 @@ bool json_to_jsonrpc_errcode(const char *buffer UNNEEDED, const jsmntok_t *tok U
bool json_to_number(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
unsigned int *num UNNEEDED)
{ fprintf(stderr, "json_to_number called!\n"); abort(); }
/* Generated stub for lightningd_deprecated_in_ok */
bool lightningd_deprecated_in_ok(struct lightningd *ld UNNEEDED,
struct logger *log UNNEEDED,
bool deprecated_apis UNNEEDED,
const char *subsys UNNEEDED,
const char *api UNNEEDED,
const char *start UNNEEDED,
const char *end UNNEEDED,
const char *details UNNEEDED)
{ fprintf(stderr, "lightningd_deprecated_in_ok called!\n"); abort(); }
/* Generated stub for lightningd_deprecated_out_ok */
bool lightningd_deprecated_out_ok(struct lightningd *ld UNNEEDED,
bool deprecated_apis UNNEEDED,
const char *subsys UNNEEDED,
const char *api UNNEEDED,
const char *start UNNEEDED,
const char *end UNNEEDED)
{ fprintf(stderr, "lightningd_deprecated_out_ok called!\n"); abort(); }
/* Generated stub for log_ */
void log_(struct logger *logger UNNEEDED, enum log_level level UNNEEDED,
const struct node_id *node_id UNNEEDED,

View File

@ -32,6 +32,15 @@ void daemon_setup(const char *argv0 UNNEEDED,
void (*backtrace_print)(const char *fmt UNNEEDED, ...) UNNEEDED,
void (*backtrace_exit)(void))
{ fprintf(stderr, "daemon_setup called!\n"); abort(); }
/* Generated stub for deprecated_ok_ */
bool deprecated_ok_(bool deprecated_apis UNNEEDED,
const char *feature UNNEEDED,
const char *start UNNEEDED,
const char *end UNNEEDED,
const char **begs UNNEEDED,
void (*complain)(const char *feat UNNEEDED, bool allowing UNNEEDED, void *) UNNEEDED,
void *cbarg UNNEEDED)
{ fprintf(stderr, "deprecated_ok_ called!\n"); abort(); }
/* Generated stub for first_fee_state */
enum htlc_state first_fee_state(enum side opener UNNEEDED)
{ fprintf(stderr, "first_fee_state called!\n"); abort(); }

View File

@ -38,6 +38,15 @@ void daemon_setup(const char *argv0 UNNEEDED,
void (*backtrace_print)(const char *fmt UNNEEDED, ...) UNNEEDED,
void (*backtrace_exit)(void))
{ fprintf(stderr, "daemon_setup called!\n"); abort(); }
/* Generated stub for deprecated_ok_ */
bool deprecated_ok_(bool deprecated_apis UNNEEDED,
const char *feature UNNEEDED,
const char *start UNNEEDED,
const char *end UNNEEDED,
const char **begs UNNEEDED,
void (*complain)(const char *feat UNNEEDED, bool allowing UNNEEDED, void *) UNNEEDED,
void *cbarg UNNEEDED)
{ fprintf(stderr, "deprecated_ok_ called!\n"); abort(); }
/* Generated stub for first_fee_state */
enum htlc_state first_fee_state(enum side opener UNNEEDED)
{ fprintf(stderr, "first_fee_state called!\n"); abort(); }

View File

@ -7,6 +7,7 @@
#include <ccan/tal/path/path.h>
#include <ccan/tal/str/str.h>
#include <common/daemon.h>
#include <common/deprecation.h>
#include <common/json_filter.h>
#include <common/json_parse_simple.h>
#include <common/json_stream.h>
@ -138,6 +139,55 @@ struct json_filter **command_filter_ptr(struct command *cmd)
return &cmd->filter;
}
static bool command_deprecated_ok(const struct command *cmd)
{
return deprecated_apis;
}
static void complain_deprecated(const char *feature,
bool allowing,
struct command *cmd)
{
if (!allowing) {
/* Mild log message for disallowing */
plugin_log(cmd->plugin, LOG_DBG,
"Note: disallowing deprecated %s for %s",
feature, cmd->id);
} else {
plugin_log(cmd->plugin, LOG_BROKEN,
"DEPRECATED API USED: %s by %s",
feature, cmd->id);
}
}
bool command_deprecated_in_ok(struct command *cmd,
const char *param,
const char *depr_start,
const char *depr_end)
{
return deprecated_ok(command_deprecated_ok(cmd),
param
? tal_fmt(tmpctx, "%s.%s", cmd->methodname, param)
: cmd->methodname,
depr_start, depr_end,
/* FIXME: Get api begs from lightningd! */
NULL,
complain_deprecated, cmd);
}
bool command_deprecated_out_ok(struct command *cmd,
const char *fieldname,
const char *depr_start,
const char *depr_end)
{
return deprecated_ok(command_deprecated_ok(cmd),
tal_fmt(tmpctx, "%s.%s", cmd->methodname, fieldname),
depr_start, depr_end,
/* FIXME: Get api begs from lightningd! */
NULL,
NULL, NULL);
}
static void ld_send(struct plugin *plugin, struct json_stream *stream)
{
struct jstream *jstr = tal(plugin, struct jstream);
@ -500,11 +550,6 @@ bool command_usage_only(const struct command *cmd)
return cmd->usage_only;
}
bool command_deprecated_apis(const struct command *cmd)
{
return deprecated_apis;
}
bool command_dev_apis(const struct command *cmd)
{
return cmd->plugin->developer;