diff --git a/common/Makefile b/common/Makefile index 378f1ed9c..7bd094b30 100644 --- a/common/Makefile +++ b/common/Makefile @@ -25,10 +25,13 @@ COMMON_SRC_NOGEN := \ common/initial_commit_tx.c \ common/io_lock.c \ common/json.c \ + common/json_escaped.c \ + common/json_tok.c \ common/key_derive.c \ common/keyset.c \ common/memleak.c \ common/msg_queue.c \ + common/param.c \ common/peer_billboard.c \ common/peer_failed.c \ common/permute_tx.c \ @@ -52,7 +55,7 @@ COMMON_SRC_NOGEN := \ COMMON_SRC_GEN := common/gen_status_wire.c common/gen_peer_status_wire.c -COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h +COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h common/jsonrpc_errors.h COMMON_HEADERS_GEN := common/gen_htlc_state_names.h common/gen_status_wire.h common/gen_peer_status_wire.h COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN) diff --git a/lightningd/json_escaped.c b/common/json_escaped.c similarity index 98% rename from lightningd/json_escaped.c rename to common/json_escaped.c index 5e62548e5..d60654b92 100644 --- a/lightningd/json_escaped.c +++ b/common/json_escaped.c @@ -1,4 +1,4 @@ -#include +#include #include struct json_escaped *json_escaped_string_(const tal_t *ctx, diff --git a/lightningd/json_escaped.h b/common/json_escaped.h similarity index 88% rename from lightningd/json_escaped.h rename to common/json_escaped.h index 8ef77123d..0f742a86f 100644 --- a/lightningd/json_escaped.h +++ b/common/json_escaped.h @@ -1,5 +1,5 @@ -#ifndef LIGHTNING_LIGHTNINGD_JSON_ESCAPED_H -#define LIGHTNING_LIGHTNINGD_JSON_ESCAPED_H +#ifndef LIGHTNING_COMMON_JSON_ESCAPED_H +#define LIGHTNING_COMMON_JSON_ESCAPED_H #include "config.h" #include @@ -32,4 +32,4 @@ struct json_escaped *json_escaped_string_(const tal_t *ctx, /* Be very careful here! Can fail! Doesn't handle \u: use UTF-8 please. */ const char *json_escaped_unescape(const tal_t *ctx, const struct json_escaped *esc); -#endif /* LIGHTNING_LIGHTNINGD_JSON_ESCAPED_H */ +#endif /* LIGHTNING_COMMON_JSON_ESCAPED_H */ diff --git a/common/json_tok.c b/common/json_tok.c new file mode 100644 index 000000000..6e2732216 --- /dev/null +++ b/common/json_tok.c @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +bool json_tok_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); + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an array, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + +bool json_tok_bool(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + bool **b) +{ + *b = tal(cmd, bool); + if (tok->type == JSMN_PRIMITIVE) { + if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) { + **b = true; + return true; + } + if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) { + **b = false; + 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; +} + +bool json_tok_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; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a double, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + +bool json_tok_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; + } + 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; +} + +bool json_tok_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; +} + +bool json_tok_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; + + 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; +} + +bool json_tok_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; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be an integer, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + +bool json_tok_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; + + 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; +} + +bool json_tok_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; + } + *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; +} + +bool json_tok_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; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a positive double, not '%.*s'", + name, tok->end - tok->start, buffer + tok->start); + return false; +} + +bool json_tok_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; + + 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; +} + +bool json_tok_tok(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t * tok, + const jsmntok_t **out) +{ + return (*out = tok); +} diff --git a/common/json_tok.h b/common/json_tok.h new file mode 100644 index 000000000..b0d6d1898 --- /dev/null +++ b/common/json_tok.h @@ -0,0 +1,72 @@ +/* Helpers for use with param parsing. */ +#ifndef LIGHTNING_COMMON_JSON_TOK_H +#define LIGHTNING_COMMON_JSON_TOK_H +#include "config.h" +#include + +struct command; + +/* 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); + +/* 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); + +/* 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); + +/* 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); + +/* Extract a string */ +bool json_tok_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); + +/* 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); + +/* Extract sha256 hash */ +bool json_tok_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); + +/* 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); + +/* 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); + +/* + * Set the address of @out to @tok. Used as a callback by handlers that + * want to unmarshal @tok themselves. + */ +bool json_tok_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/lightningd/jsonrpc_errors.h b/common/jsonrpc_errors.h similarity index 88% rename from lightningd/jsonrpc_errors.h rename to common/jsonrpc_errors.h index 65757e734..5d91eaf66 100644 --- a/lightningd/jsonrpc_errors.h +++ b/common/jsonrpc_errors.h @@ -1,8 +1,8 @@ -/* lightningd/jsonrpc_errors.h +/* common/jsonrpc_errors.h * Lists error codes for JSON-RPC. */ -#ifndef LIGHTNING_LIGHTNINGD_JSONRPC_ERRORS_H -#define LIGHTNING_LIGHTNINGD_JSONRPC_ERRORS_H +#ifndef LIGHTNING_COMMON_JSONRPC_ERRORS_H +#define LIGHTNING_COMMON_JSONRPC_ERRORS_H #include "config.h" /* Standard errors defined by JSON-RPC 2.0 standard */ @@ -44,4 +44,4 @@ #define INVOICE_LABEL_ALREADY_EXISTS 900 #define INVOICE_PREIMAGE_ALREADY_EXISTS 901 -#endif /* LIGHTNING_LIGHTNINGD_JSONRPC_ERRORS_H */ +#endif /* LIGHTNING_COMMON_JSONRPC_ERRORS_H */ diff --git a/lightningd/param.c b/common/param.c similarity index 97% rename from lightningd/param.c rename to common/param.c index 34f0999fa..bcdd0773a 100644 --- a/lightningd/param.c +++ b/common/param.c @@ -1,12 +1,9 @@ #include #include #include +#include +#include #include -#include -#include -#include -#include -#include struct param { const char *name; diff --git a/lightningd/param.h b/common/param.h similarity index 92% rename from lightningd/param.h rename to common/param.h index 0701444c6..2306114e2 100644 --- a/lightningd/param.h +++ b/common/param.h @@ -1,6 +1,9 @@ -#ifndef LIGHTNING_LIGHTNINGD_PARAM_H -#define LIGHTNING_LIGHTNINGD_PARAM_H +#ifndef LIGHTNING_COMMON_PARAM_H +#define LIGHTNING_COMMON_PARAM_H #include "config.h" +#include +#include +#include /*~ Greetings adventurer! * @@ -28,8 +31,9 @@ * * All the command handlers throughout the code use this system. * json_invoice() is a great example. The common callbacks can be found in - * lightningd/json.c. Use them directly or feel free to write your own. + * common/json_tok.c. Use them directly or feel free to write your own. */ +struct command; /* * Parse the json tokens. @params can be an array of values or an object @@ -92,4 +96,4 @@ typedef bool(*param_cbx)(struct command *cmd, /* Special flag for 'check' which allows any parameters. */ #define p_opt_any() "", false, NULL, NULL -#endif /* LIGHTNING_LIGHTNINGD_PARAM_H */ +#endif /* LIGHTNING_COMMON_PARAM_H */ diff --git a/lightningd/test/run-json_escaped.c b/common/test/run-json_escaped.c similarity index 100% rename from lightningd/test/run-json_escaped.c rename to common/test/run-json_escaped.c diff --git a/lightningd/test/run-param.c b/common/test/run-param.c similarity index 93% rename from lightningd/test/run-param.c rename to common/test/run-param.c index 765e4b9ec..cf86444ec 100644 --- a/lightningd/test/run-param.c +++ b/common/test/run-param.c @@ -1,12 +1,11 @@ #include "config.h" #include "../json.c" #include "../json_escaped.c" -#include "../json_stream.c" +#include "../json_tok.c" #include "../param.c" #include #include #include -#include #include #include #include @@ -33,21 +32,20 @@ void command_fail(struct command *cmd, int code, const char *fmt, ...) } /* AUTOGENERATED MOCKS START */ -/* Generated stub for feerate_from_style */ -u32 feerate_from_style(u32 feerate UNNEEDED, enum feerate_style style UNNEEDED) -{ fprintf(stderr, "feerate_from_style 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 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(); } /* AUTOGENERATED MOCKS END */ +/* We do this lightningd-style: */ +enum command_mode { + CMD_NORMAL, + CMD_USAGE, + CMD_CHECK +}; + +struct command { + enum command_mode mode; + const char *usage; +}; + void command_set_usage(struct command *cmd, const char *usage) { cmd->usage = usage; @@ -551,20 +549,9 @@ static void test_invoice(struct command *cmd, static void usage(void) { - /* Do this to simulate a call to our pretend handler (test_invoice) */ - struct json_command invoice_command = { - "invoice", - test_invoice, - "", - false, - "" - }; - cmd->mode = CMD_USAGE; - cmd->json_cmd = &invoice_command; - - cmd->json_cmd->dispatch(cmd, NULL, NULL, NULL); + test_invoice(cmd, NULL, NULL, NULL); assert(streq(cmd->usage, "msatoshi label description " "[expiry] [fallbacks] [preimage]")); diff --git a/common/wallet_tx.c b/common/wallet_tx.c index 88a36460b..e627f0c7b 100644 --- a/common/wallet_tx.c +++ b/common/wallet_tx.c @@ -1,6 +1,7 @@ +#include +#include #include #include -#include #include void wtx_init(struct command *cmd, struct wallet_tx * wtx) diff --git a/lightningd/Makefile b/lightningd/Makefile index 62090aa4f..d516b5e1d 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -34,8 +34,11 @@ LIGHTNINGD_COMMON_OBJS := \ common/key_derive.o \ common/io_lock.o \ common/json.o \ + common/json_escaped.o \ + common/json_tok.o \ common/memleak.o \ common/msg_queue.o \ + common/param.o \ common/permute_tx.o \ common/pseudorand.o \ common/sphinx.o \ @@ -64,7 +67,6 @@ LIGHTNINGD_SRC := \ lightningd/htlc_end.c \ lightningd/invoice.c \ lightningd/json.c \ - lightningd/json_escaped.c \ lightningd/json_stream.c \ lightningd/jsonrpc.c \ lightningd/lightningd.c \ @@ -74,7 +76,6 @@ LIGHTNINGD_SRC := \ lightningd/onchain_control.c \ lightningd/opening_control.c \ lightningd/options.c \ - lightningd/param.c \ lightningd/pay.c \ lightningd/payalgo.c \ lightningd/peer_control.c \ @@ -92,8 +93,7 @@ ALL_OBJS += $(LIGHTNINGD_OBJS) # We accumulate all lightningd/ headers in these three: LIGHTNINGD_HEADERS_NOGEN = \ $(LIGHTNINGD_SRC:.c=.h) \ - lightningd/channel_state.h \ - lightningd/jsonrpc_errors.h + lightningd/channel_state.h # Generated headers LIGHTNINGD_HEADERS_GEN = \ diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index 9fe3db227..c070169dc 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -13,14 +13,15 @@ #include #include #include +#include +#include #include +#include #include #include #include #include #include -#include -#include /* Mutual recursion via timer. */ static void try_extend_tip(struct chain_topology *topo); diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index 68cc51d90..b488f7225 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -4,7 +4,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -18,11 +21,9 @@ #include #include #include -#include #include #include #include -#include #include #include #include diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index a0230437f..8c3f2538a 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -11,6 +11,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include @@ -22,12 +26,9 @@ #include #include #include -#include #include -#include #include #include -#include #include #include #include diff --git a/lightningd/invoice.c b/lightningd/invoice.c index bade4f930..2c527971f 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -1,7 +1,6 @@ #include "invoice.h" #include "json.h" #include "jsonrpc.h" -#include "jsonrpc_errors.h" #include "lightningd.h" #include #include @@ -10,6 +9,10 @@ #include #include #include +#include +#include +#include +#include #include #include #include @@ -18,11 +21,8 @@ #include #include #include -#include -#include #include #include -#include #include #include #include diff --git a/lightningd/json.c b/lightningd/json.c index 16c0068e9..e94be2c34 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -3,15 +3,17 @@ #include #include #include +#include +#include +#include #include +#include #include #include #include #include -#include #include #include -#include #include #include #include @@ -95,175 +97,6 @@ void json_add_txid(struct json_stream *result, const char *fieldname, json_add_string(result, fieldname, hex); } -bool json_tok_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); - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be an array, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; -} - -bool json_tok_bool(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t *tok, - bool **b) -{ - *b = tal(cmd, bool); - if (tok->type == JSMN_PRIMITIVE) { - if (memeqstr(buffer + tok->start, tok->end - tok->start, "true")) { - **b = true; - return true; - } - if (memeqstr(buffer + tok->start, tok->end - tok->start, "false")) { - **b = false; - 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; -} - -bool json_tok_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; - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a double, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; -} - -bool json_tok_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; - } - 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; -} - -bool json_tok_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; -} - -bool json_tok_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; - - 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; -} - -bool json_tok_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; - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be an integer, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; -} - -bool json_tok_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; - - 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; -} - -bool json_tok_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; - } - *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; -} - -bool json_tok_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; - - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "'%s' should be a positive double, not '%.*s'", - name, tok->end - tok->start, buffer + tok->start); - return false; -} - -bool json_tok_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; - - 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; -} - bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, struct pubkey *pubkey) { @@ -470,13 +303,6 @@ void json_add_address_internal(struct json_stream *response, abort(); } -bool json_tok_tok(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t * tok, - const jsmntok_t **out) -{ - return (*out = tok); -} - void json_add_num(struct json_stream *result, const char *fieldname, unsigned int value) { json_add_member(result, fieldname, "%u", value); diff --git a/lightningd/json.h b/lightningd/json.h index e960eb7a7..00f723bd0 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -47,56 +47,6 @@ void json_add_pubkey(struct json_stream *response, void json_add_txid(struct json_stream *result, const char *fieldname, const struct bitcoin_txid *txid); -/* 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); - -/* 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); - -/* 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); - -/* 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); - -/* Extract a string */ -bool json_tok_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); - -/* 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); - -/* Extract sha256 hash */ -bool json_tok_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); - -/* 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); - /* Extract a pubkey from this */ bool json_to_pubkey(const char *buffer, const jsmntok_t *tok, struct pubkey *pubkey); @@ -113,11 +63,6 @@ bool json_tok_short_channel_id(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, struct short_channel_id **scid); -/* 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); - enum feerate_style { FEERATE_PER_KSIPA, FEERATE_PER_KBYTE @@ -152,14 +97,6 @@ void json_add_address_internal(struct json_stream *response, const char *fieldname, const struct wireaddr_internal *addr); -/* - * Set the address of @out to @tok. Used as a callback by handlers that - * want to unmarshal @tok themselves. - */ -bool json_tok_tok(struct command *cmd, const char *name, - const char *buffer, const jsmntok_t * tok, - const jsmntok_t **out); - /* '"fieldname" : "value"' or '"value"' if fieldname is NULL. Turns * any non-printable chars into JSON escapes, but leaves existing escapes alone. diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index f7dff296c..1ccd0ed29 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -24,7 +24,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -33,12 +36,9 @@ #include #include #include -#include #include -#include #include #include -#include #include #include #include diff --git a/lightningd/jsonrpc.h b/lightningd/jsonrpc.h index ce5ae71ba..ffc98a63b 100644 --- a/lightningd/jsonrpc.h +++ b/lightningd/jsonrpc.h @@ -64,7 +64,7 @@ struct json_stream *json_stream_success(struct command *cmd); /** * json_stream_fail - start streaming a failed json result. * @cmd: the command we're running. - * @code: the error code from lightningd/jsonrpc_errors.h + * @code: the error code from common/jsonrpc_errors.h * @errmsg: the error string. * * The returned value should go to command_failed() when done; @@ -77,7 +77,7 @@ struct json_stream *json_stream_fail(struct command *cmd, /** * json_stream_fail_nodata - start streaming a failed json result. * @cmd: the command we're running. - * @code: the error code from lightningd/jsonrpc_errors.h + * @code: the error code from common/jsonrpc_errors.h * @errmsg: the error string. * * This is used by command_fail(), which doesn't add any JSON data. @@ -89,8 +89,6 @@ struct json_stream *json_stream_fail_nodata(struct command *cmd, struct json_stream *null_response(struct command *cmd); void command_success(struct command *cmd, struct json_stream *response); void command_failed(struct command *cmd, struct json_stream *result); -void PRINTF_FMT(3, 4) command_fail(struct command *cmd, int code, - const char *fmt, ...); /* Mainly for documentation, that we plan to close this later. */ void command_still_pending(struct command *cmd); diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 3708fa223..d300c1a74 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -57,6 +57,7 @@ /*~ This is common code: routines shared by one or more executables * (separate daemons, or the lightning-cli program). */ #include +#include #include #include #include @@ -68,7 +69,6 @@ #include #include #include -#include #include #include #include diff --git a/lightningd/log.c b/lightningd/log.c index 3dfd18f2c..c8bb4c956 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -10,17 +10,18 @@ #include #include #include +#include +#include #include +#include #include #include #include #include #include #include -#include #include #include -#include #include #include #include diff --git a/lightningd/memdump.c b/lightningd/memdump.c index 25797b7d3..5b3d683c7 100644 --- a/lightningd/memdump.c +++ b/lightningd/memdump.c @@ -4,7 +4,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -12,11 +15,9 @@ #include #include #include -#include #include #include #include -#include #include #include #include diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index c08c7f16f..99c95cbb0 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -4,7 +4,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -16,11 +19,9 @@ #include #include #include -#include #include #include #include -#include #include #include #include diff --git a/lightningd/options.c b/lightningd/options.c index 9d3881cb9..8fd78f8ca 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -10,7 +10,11 @@ #include #include #include +#include +#include +#include #include +#include #include #include #include @@ -19,13 +23,10 @@ #include #include #include -#include #include -#include #include #include #include -#include #include #include #include diff --git a/lightningd/pay.c b/lightningd/pay.c index 61b4a7ce4..52ebd99d5 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -2,16 +2,17 @@ #include #include #include +#include +#include +#include #include #include #include #include #include -#include #include #include #include -#include #include #include #include diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index ef525db95..f657bf0b1 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -5,6 +5,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -12,10 +15,8 @@ #include #include #include -#include #include #include -#include #include #include #include diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 51e026e19..2984e4a52 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -15,7 +15,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -33,13 +36,11 @@ #include #include #include -#include #include #include #include #include #include -#include #include #include #include diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index d7f620260..bcac32520 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -5,19 +5,20 @@ #include #include #include +#include +#include +#include #include +#include #include #include #include #include #include #include -#include #include -#include #include #include -#include #include #include #include diff --git a/lightningd/ping.c b/lightningd/ping.c index 115853f69..b379b4244 100644 --- a/lightningd/ping.c +++ b/lightningd/ping.c @@ -1,14 +1,15 @@ #include +#include +#include +#include #include #include #include #include #include #include -#include #include #include -#include #include #include #include diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 0d7150bba..91962aa3d 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -9,12 +9,14 @@ #include #include #include +#include +#include #include +#include #include #include #include #include -#include #include #include #include diff --git a/lightningd/test/Makefile b/lightningd/test/Makefile index 294106508..f0b7c3504 100644 --- a/lightningd/test/Makefile +++ b/lightningd/test/Makefile @@ -13,6 +13,7 @@ LIGHTNINGD_TEST_COMMON_OBJS := \ common/htlc_state.o \ common/io_lock.o \ common/json.o \ + common/json_escaped.o \ common/key_derive.o \ common/pseudorand.o \ common/memleak.o \ diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index 9044ea432..47c5e7ed4 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -91,9 +91,6 @@ void hsm_init(struct lightningd *ld UNNEEDED) /* Generated stub for htlcs_notify_new_block */ void htlcs_notify_new_block(struct lightningd *ld UNNEEDED, u32 height UNNEEDED) { fprintf(stderr, "htlcs_notify_new_block called!\n"); abort(); } -/* Generated stub for json_escape */ -struct json_escaped *json_escape(const tal_t *ctx UNNEEDED, const char *str TAKES UNNEEDED) -{ fprintf(stderr, "json_escape called!\n"); abort(); } /* Generated stub for jsonrpc_listen */ void jsonrpc_listen(struct jsonrpc *rpc UNNEEDED, struct lightningd *ld UNNEEDED) { fprintf(stderr, "jsonrpc_listen called!\n"); abort(); } diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index f4953520d..58205b817 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -164,9 +164,6 @@ void json_array_end(struct json_stream *js UNNEEDED) /* Generated stub for json_array_start */ void json_array_start(struct json_stream *js UNNEEDED, const char *fieldname UNNEEDED) { fprintf(stderr, "json_array_start called!\n"); abort(); } -/* Generated stub for json_escape */ -struct json_escaped *json_escape(const tal_t *ctx UNNEEDED, const char *str TAKES UNNEEDED) -{ fprintf(stderr, "json_escape called!\n"); abort(); } /* Generated stub for json_object_end */ void json_object_end(struct json_stream *js UNNEEDED) { fprintf(stderr, "json_object_end called!\n"); abort(); } diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 24f5dbee7..7b1e787f4 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -1,4 +1,3 @@ -#include "../json_escaped.c" #include "../json_stream.c" #include "../jsonrpc.c" #include "../json.c" @@ -26,6 +25,21 @@ char *fmt_wireaddr_without_port(const tal_t *ctx UNNEEDED, const struct wireaddr 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, ...) diff --git a/wallet/db.c b/wallet/db.c index 6b4574e29..a7523161f 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -1,9 +1,9 @@ #include "db.h" #include +#include #include #include -#include #include #include diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index a3771d43e..e2410c7e0 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -3,7 +3,10 @@ #include #include #include +#include +#include #include +#include #include #include #include @@ -16,10 +19,8 @@ #include #include #include -#include #include #include -#include #include #include #include