lightningd/json.h: remove.

There are hardly any lightningd-specific JSON functions: all that's left
are the feerate ones, and there's already a comment that we should have
a lightningd/feerate.h.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-07-04 13:22:34 +09:30 committed by neil saitug
parent 401f1debc5
commit 36a29fbfbc
21 changed files with 200 additions and 245 deletions

View File

@ -685,40 +685,6 @@ struct command_result *param_secrets_array(struct command *cmd,
return NULL;
}
struct command_result *param_feerate_val(struct command *cmd,
const char *name, const char *buffer,
const jsmntok_t *tok,
u32 **feerate_per_kw)
{
jsmntok_t base = *tok;
enum feerate_style style;
unsigned int num;
if (json_tok_endswith(buffer, tok,
feerate_style_name(FEERATE_PER_KBYTE))) {
style = FEERATE_PER_KBYTE;
base.end -= strlen(feerate_style_name(FEERATE_PER_KBYTE));
} else if (json_tok_endswith(buffer, tok,
feerate_style_name(FEERATE_PER_KSIPA))) {
style = FEERATE_PER_KSIPA;
base.end -= strlen(feerate_style_name(FEERATE_PER_KSIPA));
} else
style = FEERATE_PER_KBYTE;
if (!json_to_number(buffer, &base, &num)) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be an integer with optional perkw/perkb, not '%.*s'",
name, base.end - base.start,
buffer + base.start);
}
*feerate_per_kw = tal(cmd, u32);
**feerate_per_kw = feerate_from_style(num, style);
if (**feerate_per_kw < FEERATE_FLOOR)
**feerate_per_kw = FEERATE_FLOOR;
return NULL;
}
/**
* segwit_addr_net_decode - Try to decode a Bech32 address and detect
* testnet/mainnet/regtest/signet
@ -1093,3 +1059,16 @@ struct command_result *param_lease_hex(struct command *cmd,
json_tok_full(buffer, tok));
return NULL;
}
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 NULL;
return command_fail_badparam(cmd, name, buffer, tok,
"should be a compressed pubkey");
}

View File

@ -268,11 +268,6 @@ struct command_result *param_secrets_array(struct command *cmd,
const jsmntok_t *tok,
struct secret **secrets);
struct command_result *param_feerate_val(struct command *cmd,
const char *name, const char *buffer,
const jsmntok_t *tok,
u32 **feerate_per_kw);
struct command_result *param_txid(struct command *cmd,
const char *name,
const char *buffer,
@ -343,4 +338,9 @@ struct command_result *param_lease_hex(struct command *cmd,
const char *buffer,
const jsmntok_t *tok,
struct lease_rates **rates);
struct command_result *param_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey);
#endif /* LIGHTNING_COMMON_JSON_PARAM_H */

View File

@ -717,3 +717,12 @@ json_to_reply_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
return rpath;
}
bool
json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
struct channel_id *cid)
{
return hex_decode(buffer + tok->start, tok->end - tok->start,
cid, sizeof(*cid));
}

View File

@ -120,6 +120,8 @@ json_to_reply_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok);
struct tlv_obs2_onionmsg_payload_reply_path *
json_to_obs2_reply_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok);
bool json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
struct channel_id *cid);
/* Guide is % for a token: each must be followed by JSON_SCAN().
* Returns NULL on error (asserts() on bad guide). */

View File

@ -10,13 +10,13 @@ LIGHTNINGD_SRC := \
lightningd/dual_open_control.c \
lightningd/connect_control.c \
lightningd/onion_message.c \
lightningd/feerate.c \
lightningd/gossip_control.c \
lightningd/hsm_control.c \
lightningd/htlc_end.c \
lightningd/htlc_set.c \
lightningd/invoice.c \
lightningd/io_loop_with_timers.c \
lightningd/json.c \
lightningd/jsonrpc.c \
lightningd/lightningd.c \
lightningd/log.c \

View File

@ -18,7 +18,6 @@
#include <lightningd/coin_mvts.h>
#include <lightningd/gossip_control.h>
#include <lightningd/io_loop_with_timers.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>
@ -311,33 +310,6 @@ static void watch_for_utxo_reconfirmation(struct chain_topology *topo,
}
}
const char *feerate_name(enum feerate feerate)
{
switch (feerate) {
case FEERATE_OPENING: return "opening";
case FEERATE_MUTUAL_CLOSE: return "mutual_close";
case FEERATE_UNILATERAL_CLOSE: return "unilateral_close";
case FEERATE_DELAYED_TO_US: return "delayed_to_us";
case FEERATE_HTLC_RESOLUTION: return "htlc_resolution";
case FEERATE_PENALTY: return "penalty";
case FEERATE_MIN: return "min_acceptable";
case FEERATE_MAX: return "max_acceptable";
}
abort();
}
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)
return command_fail(cmd, LIGHTNINGD, "Cannot estimate fees");
return NULL;
}
/* Mutual recursion via timer. */
static void next_updatefee_timer(struct chain_topology *topo);

View File

@ -3,6 +3,7 @@
#include "config.h"
#include <bitcoin/block.h>
#include <ccan/list/list.h>
#include <lightningd/feerate.h>
#include <lightningd/watch.h>
struct bitcoin_tx;
@ -12,20 +13,6 @@ struct lightningd;
struct peer;
struct txwatch;
/* FIXME: move all feerate stuff out to new lightningd/feerate.[ch] files */
enum feerate {
/* DO NOT REORDER: force-feerates uses this order! */
FEERATE_OPENING,
FEERATE_MUTUAL_CLOSE,
FEERATE_UNILATERAL_CLOSE,
FEERATE_DELAYED_TO_US,
FEERATE_HTLC_RESOLUTION,
FEERATE_PENALTY,
FEERATE_MIN,
FEERATE_MAX,
};
#define NUM_FEERATES (FEERATE_MAX+1)
/* We keep the last three in case there are outliers (for min/max) */
#define FEE_HISTORY_NUM 3
@ -164,13 +151,6 @@ u32 delayed_to_us_feerate(struct chain_topology *topo);
u32 htlc_resolution_feerate(struct chain_topology *topo);
u32 penalty_feerate(struct chain_topology *topo);
const char *feerate_name(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. */
void broadcast_tx(struct chain_topology *topo,

View File

@ -27,7 +27,6 @@
#include <lightningd/closing_control.h>
#include <lightningd/dual_open_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>

View File

@ -24,7 +24,6 @@
#include <lightningd/dual_open_control.h>
#include <lightningd/gossip_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/notification.h>
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>

114
lightningd/feerate.c Normal file
View File

@ -0,0 +1,114 @@
#include "config.h"
#include <common/json_command.h>
#include <lightningd/chaintopology.h>
#include <lightningd/feerate.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
const char *feerate_name(enum feerate feerate)
{
switch (feerate) {
case FEERATE_OPENING: return "opening";
case FEERATE_MUTUAL_CLOSE: return "mutual_close";
case FEERATE_UNILATERAL_CLOSE: return "unilateral_close";
case FEERATE_DELAYED_TO_US: return "delayed_to_us";
case FEERATE_HTLC_RESOLUTION: return "htlc_resolution";
case FEERATE_PENALTY: return "penalty";
case FEERATE_MIN: return "min_acceptable";
case FEERATE_MAX: return "max_acceptable";
}
abort();
}
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,
feerate_style_name(FEERATE_PER_KSIPA))) {
**style = FEERATE_PER_KSIPA;
return NULL;
} else if (json_tok_streq(buffer, tok,
feerate_style_name(FEERATE_PER_KBYTE))) {
**style = FEERATE_PER_KBYTE;
return NULL;
}
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be '%s' or '%s', not '%.*s'",
name,
feerate_style_name(FEERATE_PER_KSIPA),
feerate_style_name(FEERATE_PER_KBYTE),
json_tok_full_len(tok), json_tok_full(buffer, tok));
}
struct command_result *param_feerate(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
u32 **feerate)
{
for (size_t i = 0; i < NUM_FEERATES; i++) {
if (json_tok_streq(buffer, tok, feerate_name(i)))
return param_feerate_estimate(cmd, feerate, i);
}
/* We used SLOW, NORMAL, and URGENT as feerate targets previously,
* and many commands rely on this syntax now.
* It's also really more natural for an user interface. */
if (json_tok_streq(buffer, tok, "slow"))
return param_feerate_estimate(cmd, feerate, FEERATE_MIN);
else if (json_tok_streq(buffer, tok, "normal"))
return param_feerate_estimate(cmd, feerate, FEERATE_OPENING);
else if (json_tok_streq(buffer, tok, "urgent"))
return param_feerate_estimate(cmd, feerate, FEERATE_UNILATERAL_CLOSE);
/* It's a number... */
return param_feerate_val(cmd, name, buffer, tok, 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)
return command_fail(cmd, LIGHTNINGD, "Cannot estimate fees");
return NULL;
}
struct command_result *param_feerate_val(struct command *cmd,
const char *name, const char *buffer,
const jsmntok_t *tok,
u32 **feerate_per_kw)
{
jsmntok_t base = *tok;
enum feerate_style style;
unsigned int num;
if (json_tok_endswith(buffer, tok,
feerate_style_name(FEERATE_PER_KBYTE))) {
style = FEERATE_PER_KBYTE;
base.end -= strlen(feerate_style_name(FEERATE_PER_KBYTE));
} else if (json_tok_endswith(buffer, tok,
feerate_style_name(FEERATE_PER_KSIPA))) {
style = FEERATE_PER_KSIPA;
base.end -= strlen(feerate_style_name(FEERATE_PER_KSIPA));
} else
style = FEERATE_PER_KBYTE;
if (!json_to_number(buffer, &base, &num)) {
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be an integer with optional perkw/perkb, not '%.*s'",
name, base.end - base.start,
buffer + base.start);
}
*feerate_per_kw = tal(cmd, u32);
**feerate_per_kw = feerate_from_style(num, style);
if (**feerate_per_kw < FEERATE_FLOOR)
**feerate_per_kw = FEERATE_FLOOR;
return NULL;
}

47
lightningd/feerate.h Normal file
View File

@ -0,0 +1,47 @@
#ifndef LIGHTNING_LIGHTNINGD_FEERATE_H
#define LIGHTNING_LIGHTNINGD_FEERATE_H
#include "config.h"
#include <bitcoin/feerate.h>
#include <common/json_parse_simple.h>
struct command;
enum feerate {
/* DO NOT REORDER: force-feerates uses this order! */
FEERATE_OPENING,
FEERATE_MUTUAL_CLOSE,
FEERATE_UNILATERAL_CLOSE,
FEERATE_DELAYED_TO_US,
FEERATE_HTLC_RESOLUTION,
FEERATE_PENALTY,
FEERATE_MIN,
FEERATE_MAX,
};
#define NUM_FEERATES (FEERATE_MAX+1)
const char *feerate_name(enum feerate feerate);
/* Extract a feerate style. */
struct command_result *param_feerate_style(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
enum feerate_style **style);
/* 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);
/* Extract a feerate with optional style suffix. */
struct command_result *param_feerate_val(struct command *cmd,
const char *name, const char *buffer,
const jsmntok_t *tok,
u32 **feerate_per_kw);
/* This also accepts names like "slow" etc */
struct command_result *param_feerate(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
u32 **feerate);
#endif /* LIGHTNING_LIGHTNINGD_FEERATE_H */

View File

@ -11,7 +11,6 @@
#include <errno.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/subd.h>

View File

@ -1,74 +0,0 @@
#include "config.h"
#include <bitcoin/pubkey.h>
#include <ccan/str/hex/hex.h>
#include <common/json_command.h>
#include <common/json_param.h>
#include <lightningd/chaintopology.h>
#include <lightningd/json.h>
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 NULL;
return command_fail_badparam(cmd, name, buffer, tok,
"should be a compressed pubkey");
}
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,
feerate_style_name(FEERATE_PER_KSIPA))) {
**style = FEERATE_PER_KSIPA;
return NULL;
} else if (json_tok_streq(buffer, tok,
feerate_style_name(FEERATE_PER_KBYTE))) {
**style = FEERATE_PER_KBYTE;
return NULL;
}
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be '%s' or '%s', not '%.*s'",
name,
feerate_style_name(FEERATE_PER_KSIPA),
feerate_style_name(FEERATE_PER_KBYTE),
json_tok_full_len(tok), json_tok_full(buffer, tok));
}
struct command_result *param_feerate(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
u32 **feerate)
{
for (size_t i = 0; i < NUM_FEERATES; i++) {
if (json_tok_streq(buffer, tok, feerate_name(i)))
return param_feerate_estimate(cmd, feerate, i);
}
/* We used SLOW, NORMAL, and URGENT as feerate targets previously,
* and many commands rely on this syntax now.
* It's also really more natural for an user interface. */
if (json_tok_streq(buffer, tok, "slow"))
return param_feerate_estimate(cmd, feerate, FEERATE_MIN);
else if (json_tok_streq(buffer, tok, "normal"))
return param_feerate_estimate(cmd, feerate, FEERATE_OPENING);
else if (json_tok_streq(buffer, tok, "urgent"))
return param_feerate_estimate(cmd, feerate, FEERATE_UNILATERAL_CLOSE);
/* It's a number... */
return param_feerate_val(cmd, name, buffer, tok, feerate);
}
bool
json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
struct channel_id *cid)
{
return hex_decode(buffer + tok->start, tok->end - tok->start,
cid, sizeof(*cid));
}

View File

@ -1,54 +0,0 @@
/* lightningd/json.h
* Helpers for outputting JSON results that are specific only for
* lightningd.
*/
#ifndef LIGHTNING_LIGHTNINGD_JSON_H
#define LIGHTNING_LIGHTNINGD_JSON_H
#include "config.h"
#include <bitcoin/feerate.h>
#include <bitcoin/privkey.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#define JSMN_STRICT 1
# include <external/jsmn/jsmn.h>
struct bitcoin_txid;
struct chainparams;
struct channel_id;
struct command;
struct json_escape;
struct pubkey;
struct node_id;
struct short_channel_id;
struct command_result *param_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey);
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);
/* Extract a feerate 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. */
struct command_result *param_feerate(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
u32 **feerate);
bool json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
struct channel_id *cid);
#endif /* LIGHTNING_LIGHTNINGD_JSON_H */

View File

@ -6,7 +6,6 @@
#include <common/type_to_string.h>
#include <connectd/connectd_wiregen.h>
#include <lightningd/channel.h>
#include <lightningd/json.h>
#include <lightningd/onion_message.h>
#include <lightningd/peer_control.h>
#include <lightningd/plugin_hook.h>

View File

@ -20,7 +20,6 @@
#include <lightningd/channel.h>
#include <lightningd/channel_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/notification.h>
#include <lightningd/opening_common.h>
#include <lightningd/opening_control.h>

View File

@ -11,7 +11,6 @@
#include <common/type_to_string.h>
#include <lightningd/chaintopology.h>
#include <lightningd/channel.h>
#include <lightningd/json.h>
#include <lightningd/notification.h>
#include <lightningd/pay.h>
#include <lightningd/peer_control.h>

View File

@ -45,7 +45,6 @@
#include <lightningd/connect_control.h>
#include <lightningd/dual_open_control.h>
#include <lightningd/hsm_control.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <lightningd/log.h>

View File

@ -4,7 +4,6 @@
#include <common/json_param.h>
#include <errno.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/json.h>
#include <lightningd/plugin.h>
#include <wire/wire_sync.h>

View File

@ -1,7 +1,7 @@
#include "config.h"
#include "../../common/json_stream.c"
#include "../jsonrpc.c"
#include "../json.c"
#include "../feerate.c"
#include <common/setup.h>
#include <stdio.h>
@ -17,9 +17,6 @@ bool deprecated_apis;
/* Generated stub for fatal */
void fatal(const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "fatal called!\n"); abort(); }
/* Generated stub for feerate_name */
const char *feerate_name(enum feerate feerate UNNEEDED)
{ fprintf(stderr, "feerate_name called!\n"); abort(); }
/* Generated stub for fromwire_bigsize */
bigsize_t fromwire_bigsize(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_bigsize called!\n"); abort(); }
@ -33,10 +30,10 @@ void fromwire_node_id(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct n
/* Generated stub for json_to_errcode */
bool json_to_errcode(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, errcode_t *errcode UNNEEDED)
{ fprintf(stderr, "json_to_errcode 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)
{ fprintf(stderr, "json_to_pubkey called!\n"); abort(); }
/* Generated stub for json_to_number */
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 log_ */
void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED,
const struct node_id *node_id UNNEEDED,
@ -73,17 +70,6 @@ struct command_result *param_bool(struct command *cmd UNNEEDED, const char *name
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
bool **b UNNEEDED)
{ fprintf(stderr, "param_bool 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_feerate_val */
struct command_result *param_feerate_val(struct command *cmd UNNEEDED,
const char *name UNNEEDED, const char *buffer UNNEEDED,
const jsmntok_t *tok UNNEEDED,
u32 **feerate_per_kw UNNEEDED)
{ fprintf(stderr, "param_feerate_val called!\n"); abort(); }
/* Generated stub for param_ignore */
struct command_result *param_ignore(struct command *cmd UNNEEDED, const char *name UNNEEDED,
const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
@ -122,6 +108,9 @@ void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id U
/* Generated stub for towire_node_id */
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
/* Generated stub for try_get_feerate */
u32 try_get_feerate(const struct chain_topology *topo UNNEEDED, enum feerate feerate UNNEEDED)
{ fprintf(stderr, "try_get_feerate called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */
static int test_json_filter(void)

View File

@ -10,7 +10,6 @@
#include <common/key_derive.h>
#include <common/type_to_string.h>
#include <lightningd/chaintopology.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <wallet/txfilter.h>