mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-09 09:04:25 +01:00
param: upgraded json_tok_tok to advanced callback
This was a very simple change and allowed us to remove the special `json_opt_tok` macro. Moved the callback out of `common/json.c` to `lightningd/json.c` because the new callbacks are dependent on `struct command` etc. (I already started on `json_tok_number`) My plan is to: 1. upgrade json_tok_X one a time, maybe a PR for each one. 2. When done, rename macros (i.e, remove "_tal"). 3. Remove all vestiges of the old callbacks 4. Add new callbacks so that we no longer need json_tok_tok! (e.g., json_tok_label, json_tok_str, json_tok_msat) Signed-off-by: Mark Beckwith <wythe@intrig.com>
This commit is contained in:
committed by
Rusty Russell
parent
a97955845f
commit
8f17191099
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
#include <inttypes.h>
|
||||
#include <lightningd/bitcoind.h>
|
||||
#include <lightningd/chaintopology.h>
|
||||
#include <lightningd/json.h>
|
||||
#include <lightningd/jsonrpc.h>
|
||||
#include <lightningd/jsonrpc_errors.h>
|
||||
#include <lightningd/lightningd.h>
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <lightningd/jsonrpc.h>
|
||||
|
||||
#include <lightningd/param.c>
|
||||
#include <lightningd/json.c>
|
||||
#include <common/json.c>
|
||||
#include <common/json_escaped.c>
|
||||
#include <ccan/array_size/array_size.h>
|
||||
@@ -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));
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user