diff --git a/common/json.c b/common/json.c index 607a8f7bd..e4a4e00eb 100644 --- a/common/json.c +++ b/common/json.c @@ -166,13 +166,6 @@ bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, hash, sizeof(*hash)); } -bool json_tok_tok(const char *buffer, const jsmntok_t * tok, - const jsmntok_t **out) -{ - *out = tok; - return true; -} - const jsmntok_t *json_next(const jsmntok_t *tok) { const jsmntok_t *t; diff --git a/common/json.h b/common/json.h index bd5bb2696..b2cfdb6e3 100644 --- a/common/json.h +++ b/common/json.h @@ -49,13 +49,6 @@ bool json_tok_bool(const char *buffer, const jsmntok_t *tok, bool *b); bool json_tok_sha256(const char *buffer, const jsmntok_t * tok, struct sha256 *hash); -/* - * Set the address of @out to @tok. Used as a param_table callback by handlers that - * want to unmarshal @tok themselves. - */ -bool json_tok_tok(const char *buffer, const jsmntok_t * tok, - const jsmntok_t **out); - /* Is this the null primitive? */ bool json_tok_is_null(const char *buffer, const jsmntok_t *tok); diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index cb85f2acf..3eff7c6ac 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -87,9 +87,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_tok("host", &hosttok), - p_opt_tok("port", &porttok), + p_req_tal("id", json_tok_tok, (const jsmntok_t **) &idtok), + p_opt_tal("host", json_tok_tok, &hosttok), + p_opt_tal("port", json_tok_tok, &porttok), NULL)) return; diff --git a/lightningd/gossip_control.c b/lightningd/gossip_control.c index 0f2d1a494..27ce11563 100644 --- a/lightningd/gossip_control.c +++ b/lightningd/gossip_control.c @@ -274,7 +274,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok struct lightningd *ld = cmd->ld; struct pubkey destination; struct pubkey source; - jsmntok_t *seedtok; + const jsmntok_t *seedtok; u64 msatoshi; unsigned cltv; double riskfactor; @@ -293,7 +293,7 @@ static void json_getroute(struct command *cmd, const char *buffer, const jsmntok p_opt_def("cltv", json_tok_number, &cltv, 9), p_opt_def("fromid", json_tok_pubkey, &source, ld->id), p_opt_def("fuzzpercent", json_tok_double, &fuzz, 75.0), - p_opt_tok("seed", &seedtok), + p_opt_tal("seed", json_tok_tok, &seedtok), NULL)) return; @@ -435,7 +435,7 @@ static void json_dev_query_scids(struct command *cmd, if (!param(cmd, buffer, params, p_req("id", json_tok_pubkey, &id), - p_req("scids", json_tok_tok, &scidstok), + p_req_tal("scids", json_tok_tok, &scidstok), NULL)) return; diff --git a/lightningd/invoice.c b/lightningd/invoice.c index a20e27b60..375acc241 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -168,12 +168,12 @@ static void json_invoice(struct command *cmd, bool result; if (!param(cmd, buffer, params, - p_req("msatoshi", json_tok_tok, &msatoshi), - p_req("label", json_tok_tok, &label), - p_req("description", json_tok_tok, &desctok), + p_req_tal("msatoshi", json_tok_tok, &msatoshi), + p_req_tal("label", json_tok_tok, &label), + p_req_tal("description", json_tok_tok, &desctok), p_opt_def("expiry", json_tok_u64, &expiry, 3600), - p_opt_tok("fallbacks", &fallbacks), - p_opt_tok("preimage", &preimagetok), + p_opt_tal("fallbacks", json_tok_tok, &fallbacks), + p_opt_tal("preimage", json_tok_tok, &preimagetok), NULL)) return; @@ -366,13 +366,13 @@ static void json_add_invoices(struct json_result *response, static void json_listinvoices(struct command *cmd, const char *buffer, const jsmntok_t *params) { - jsmntok_t *labeltok; + const jsmntok_t *labeltok; struct json_escaped *label; struct json_result *response = new_json_result(cmd); struct wallet *wallet = cmd->ld->wallet; if (!param(cmd, buffer, params, - p_opt_tok("label", &labeltok), + p_opt_tal("label", json_tok_tok, &labeltok), NULL)) return; @@ -415,8 +415,8 @@ static void json_delinvoice(struct command *cmd, struct wallet *wallet = cmd->ld->wallet; if (!param(cmd, buffer, params, - p_req("label", json_tok_tok, &labeltok), - p_req("status", json_tok_tok, &statustok), + p_req_tal("label", json_tok_tok, &labeltok), + p_req_tal("status", json_tok_tok, &statustok), NULL)) return; @@ -568,7 +568,7 @@ static void json_waitinvoice(struct command *cmd, struct json_escaped *label; if (!param(cmd, buffer, params, - p_req("label", json_tok_tok, &labeltok), + p_req_tal("label", json_tok_tok, &labeltok), NULL)) return; @@ -651,8 +651,8 @@ static void json_decodepay(struct command *cmd, char *str, *desc, *fail; if (!param(cmd, buffer, params, - p_req("bolt11", json_tok_tok, &bolt11tok), - p_opt_tok("description", &desctok), + p_req_tal("bolt11", json_tok_tok, &bolt11tok), + p_opt_tal("description", json_tok_tok, &desctok), NULL)) return; diff --git a/lightningd/json.c b/lightningd/json.c index 4b848fd29..ec5fb7509 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -187,3 +187,10 @@ void json_add_address_internal(struct json_result *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); +} diff --git a/lightningd/json.h b/lightningd/json.h index 29b629884..2e85a3915 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -13,6 +13,7 @@ struct bitcoin_txid; struct channel_id; +struct command; struct json_result; struct pubkey; struct route_hop; @@ -63,4 +64,13 @@ void json_add_address(struct json_result *response, const char *fieldname, void json_add_address_internal(struct json_result *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); + #endif /* LIGHTNING_LIGHTNINGD_JSON_H */ diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index c82862523..05e2c509e 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -189,7 +189,7 @@ static void json_help(struct command *cmd, const jsmntok_t *cmdtok; if (!param(cmd, buffer, params, - p_opt_tok("command", &cmdtok), + p_opt_tal("command", json_tok_tok, &cmdtok), NULL)) return; diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index e66ac8b10..5f4e5becd 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -770,7 +770,7 @@ static void json_fund_channel(struct command *cmd, 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_req_tal("satoshi", json_tok_tok, &sattok), NULL)) return; diff --git a/lightningd/options.c b/lightningd/options.c index 4e8b93310..e8419ed12 100644 --- a/lightningd/options.c +++ b/lightningd/options.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -1005,7 +1006,7 @@ static void json_listconfigs(struct command *cmd, bool found = false; if (!param(cmd, buffer, params, - p_opt_tok("config", &configtok), + p_opt_tal("config", json_tok_tok, &configtok), NULL)) return; diff --git a/lightningd/param.c b/lightningd/param.c index e1e02d6b5..2924edddb 100644 --- a/lightningd/param.c +++ b/lightningd/param.c @@ -96,7 +96,7 @@ static bool make_callback(struct command *cmd, def->is_set = true; if (def->cb) { - if (def->argsize && def->cb != (param_cb)json_tok_tok) { + if (def->argsize) { *(void **)def->arg = arg = tal_arr_label(cmd, char, def->argsize, "param"); diff --git a/lightningd/param.h b/lightningd/param.h index 383dfe2d7..89ed4c12f 100644 --- a/lightningd/param.h +++ b/lightningd/param.h @@ -146,17 +146,4 @@ typedef bool(*param_cbx)(struct command *cmd, (arg)) == true), \ ({ (*arg) = tal((cmd), typeof(**arg)); (**arg) = (def); (size_t)0;}) -/* - * For when you want an optional raw token. - * - * Note: weird sizeof() does type check that arg really is a (const) jsmntok_t **. - */ -#define p_opt_tok(name, arg) \ - name"", \ - false, \ - false, \ - json_tok_tok, \ - (arg) + 0*sizeof(*(arg) == (jsmntok_t *)NULL), \ - sizeof(const jsmntok_t *) - #endif /* LIGHTNING_LIGHTNINGD_PARAM_H */ diff --git a/lightningd/pay.c b/lightningd/pay.c index cdf6c933f..c493b560f 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_tok, &routetok), + p_req_tal("route", json_tok_tok, &routetok), p_req("payment_hash", json_tok_sha256, &rhash), p_opt("msatoshi", json_tok_u64, &msatoshi), - p_opt_tok("description", &desctok), + p_opt_tal("description", json_tok_tok, &desctok), NULL)) return; @@ -1120,12 +1120,12 @@ static void json_listpayments(struct command *cmd, const char *buffer, { const struct wallet_payment **payments; struct json_result *response = new_json_result(cmd); - jsmntok_t *bolt11tok, *rhashtok; + const jsmntok_t *bolt11tok, *rhashtok; struct sha256 *rhash = NULL; if (!param(cmd, buffer, params, - p_opt_tok("bolt11", &bolt11tok), - p_opt_tok("payment_hash", &rhashtok), + p_opt_tal("bolt11", json_tok_tok, &bolt11tok), + p_opt_tal("payment_hash", json_tok_tok, &rhashtok), NULL)) return; diff --git a/lightningd/payalgo.c b/lightningd/payalgo.c index 1dc425668..bc4b2755f 100644 --- a/lightningd/payalgo.c +++ b/lightningd/payalgo.c @@ -611,9 +611,9 @@ static void json_pay(struct command *cmd, unsigned int exemptfee; if (!param(cmd, buffer, params, - p_req("bolt11", json_tok_tok, &bolt11tok), + p_req_tal("bolt11", json_tok_tok, &bolt11tok), p_opt("msatoshi", json_tok_u64, &msatoshi), - p_opt_tok("description", &desctok), + p_opt_tal("description", json_tok_tok, &desctok), 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), diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index eb2d9ff2c..afe15a65a 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -836,7 +836,7 @@ static void json_close(struct command *cmd, bool force; if (!param(cmd, buffer, params, - p_req("id", json_tok_tok, &idtok), + p_req_tal("id", json_tok_tok, &idtok), p_opt_def("force", json_tok_bool, &force, false), p_opt_def("timeout", json_tok_number, &timeout, 30), NULL)) diff --git a/lightningd/test/run-param.c b/lightningd/test/run-param.c index 3be30b128..8028e1011 100644 --- a/lightningd/test/run-param.c +++ b/lightningd/test/run-param.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -44,6 +45,9 @@ void command_fail_detailed(struct command *cmd, int code, } /* AUTOGENERATED MOCKS START */ +/* 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_tok_newaddr */ bool json_tok_newaddr(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, bool *is_p2wpkh UNNEEDED) { fprintf(stderr, "json_tok_newaddr called!\n"); abort(); } @@ -169,7 +173,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_tal("satoshi", json_tok_tok, &tok), NULL)); assert(tok); assert(json_tok_number(j->buffer, tok, &n)); assert(n == 546); @@ -181,7 +185,7 @@ static void tok_tok(void) struct json *j = json_parse(cmd, "{}"); assert(param(cmd, j->buffer, j->toks, - p_opt_tok("satoshi", &tok), NULL)); + p_opt_tal("satoshi", json_tok_tok, &tok), NULL)); /* make sure it *is* NULL */ assert(tok == NULL); @@ -369,9 +373,9 @@ static void sendpay(void) unsigned cltv; if (!param(cmd, j->buffer, j->toks, - p_req("route", json_tok_tok, &routetok), + p_req_tal("route", json_tok_tok, &routetok), p_req("cltv", json_tok_number, &cltv), - p_opt_tok("note", ¬e), + p_opt_tal("note", json_tok_tok, ¬e), p_opt("msatoshi", json_tok_u64, &msatoshi), NULL)) assert(false); @@ -392,9 +396,9 @@ static void sendpay_nulltok(void) unsigned cltv; if (!param(cmd, j->buffer, j->toks, - p_req("route", json_tok_tok, &routetok), + p_req_tal("route", json_tok_tok, &routetok), p_req("cltv", json_tok_number, &cltv), - p_opt_tok("note", ¬e), + p_opt_tal("note", json_tok_tok, ¬e), p_opt("msatoshi", json_tok_u64, &msatoshi), NULL)) assert(false); @@ -426,17 +430,6 @@ static bool json_tok_msat(struct command *cmd, return false; } -/* This can eventually replace json_tok_tok and we can remove the special p_opt_tok() - * macro. */ -static bool json_tok_tok_x(struct command *cmd, - const char *name, - const char *buffer, - const jsmntok_t *tok, - const jsmntok_t **arg) -{ - return (*arg = tok); -} - /* * New version of json_tok_label conforming to advanced style. This can eventually * replace the existing json_tok_label. @@ -482,7 +475,7 @@ static void advanced(void) assert(param(cmd, j->buffer, j->toks, p_req_tal("description", json_tok_label_x, &label), p_req_tal("msat", json_tok_msat, &msat), - p_req_tal("tok", json_tok_tok_x, &tok), + p_req_tal("tok", json_tok_tok, &tok), p_opt_tal("msat_opt1", json_tok_msat, &msat_opt1), p_opt_tal("msat_opt2", json_tok_msat, &msat_opt2), NULL)); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 9d5ae4706..7380d8e5b 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -241,7 +241,8 @@ bool json_tok_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok 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(const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, +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 kill_uncommitted_channel */ diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 2b1dadb1d..ced6ecada 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -99,8 +99,8 @@ static void json_withdraw(struct command *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_req_tal("destination", json_tok_tok, &desttok), + p_req_tal("satoshi", json_tok_tok, &sattok), NULL)) return;