Browse Source

libplugin: replace rpc_delve with rpc_scan.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa
Rusty Russell 4 years ago
committed by Christian Decker
parent
commit
09b18bf64f
  1. 9
      plugins/fetchinvoice.c
  2. 15
      plugins/keysend.c
  3. 28
      plugins/libplugin.c
  4. 8
      plugins/libplugin.h
  5. 14
      plugins/offers.c
  6. 19
      plugins/pay.c

9
plugins/fetchinvoice.c

@ -809,12 +809,9 @@ static const struct plugin_command commands[] = { {
static void init(struct plugin *p, const char *buf UNUSED, static void init(struct plugin *p, const char *buf UNUSED,
const jsmntok_t *config UNUSED) const jsmntok_t *config UNUSED)
{ {
const char *field; rpc_scan(p, "getinfo",
take(json_out_obj(NULL, NULL, NULL)),
field = rpc_delve(tmpctx, p, "getinfo", "{id:%}", JSON_SCAN(json_to_node_id, &local_id));
take(json_out_obj(NULL, NULL, NULL)), ".id");
if (!node_id_from_hexstr(field, strlen(field), &local_id))
plugin_err(p, "getinfo didn't contain valid id: '%s'", field);
} }
static const struct plugin_hook hooks[] = { static const struct plugin_hook hooks[] = {

15
plugins/keysend.c

@ -95,18 +95,13 @@ REGISTER_PAYMENT_MODIFIER(keysend, struct keysend_data *, keysend_init,
static void init(struct plugin *p, const char *buf UNUSED, static void init(struct plugin *p, const char *buf UNUSED,
const jsmntok_t *config UNUSED) const jsmntok_t *config UNUSED)
{ {
const char *field; rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
"{id:%}", JSON_SCAN(json_to_node_id, &my_id));
field = rpc_delve(tmpctx, p, "getinfo", rpc_scan(p, "listconfigs",
take(json_out_obj(NULL, NULL, NULL)), ".id");
if (!node_id_from_hexstr(field, strlen(field), &my_id))
plugin_err(p, "getinfo didn't contain valid id: '%s'", field);
field =
rpc_delve(tmpctx, p, "listconfigs",
take(json_out_obj(NULL, "config", "max-locktime-blocks")), take(json_out_obj(NULL, "config", "max-locktime-blocks")),
".max-locktime-blocks"); "{max-locktime-blocks:%}",
maxdelay_default = atoi(field); JSON_SCAN(json_to_number, &maxdelay_default));
} }
struct payment_modifier *pay_mods[8] = { struct payment_modifier *pay_mods[8] = {

28
plugins/libplugin.c

@ -490,18 +490,19 @@ static struct json_out *start_json_request(const tal_t *ctx,
return jout; return jout;
} }
/* Synchronous routine to send command and extract single field from response */ /* Synchronous routine to send command and extract fields from response */
const char *rpc_delve(const tal_t *ctx, void rpc_scan(struct plugin *plugin,
struct plugin *plugin,
const char *method, const char *method,
const struct json_out *params TAKES, const struct json_out *params TAKES,
const char *guide) const char *guide,
...)
{ {
bool error; bool error;
const jsmntok_t *contents, *t; const jsmntok_t *contents;
int reqlen; int reqlen;
const char *ret; const char *p;
struct json_out *jout; struct json_out *jout;
va_list ap;
jout = start_json_request(tmpctx, 0, method, params); jout = start_json_request(tmpctx, 0, method, params);
finish_and_send_json(plugin->rpc_conn->fd, jout); finish_and_send_json(plugin->rpc_conn->fd, jout);
@ -511,14 +512,15 @@ const char *rpc_delve(const tal_t *ctx,
plugin_err(plugin, "Got error reply to %s: '%.*s'", plugin_err(plugin, "Got error reply to %s: '%.*s'",
method, reqlen, membuf_elems(&plugin->rpc_conn->mb)); method, reqlen, membuf_elems(&plugin->rpc_conn->mb));
t = json_delve(membuf_elems(&plugin->rpc_conn->mb), contents, guide); p = membuf_consume(&plugin->rpc_conn->mb, reqlen);
if (!t)
plugin_err(plugin, "Could not find %s in reply to %s: '%.*s'",
guide, method, reqlen, membuf_elems(&plugin->rpc_conn->mb));
ret = json_strdup(ctx, membuf_elems(&plugin->rpc_conn->mb), t); va_start(ap, guide);
membuf_consume(&plugin->rpc_conn->mb, reqlen); error = !json_scanv(p, contents, guide, ap);
return ret; va_end(ap);
if (error)
plugin_err(plugin, "Could not parse %s in reply to %s: '%.*s'",
guide, method, reqlen, membuf_elems(&plugin->rpc_conn->mb));
} }
static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks) static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks)

8
plugins/libplugin.h

@ -190,13 +190,13 @@ command_success_str(struct command *cmd, const char *str);
struct command_result *WARN_UNUSED_RESULT struct command_result *WARN_UNUSED_RESULT
command_hook_success(struct command *cmd); command_hook_success(struct command *cmd);
/* Synchronous helper to send command and extract single field from /* Synchronous helper to send command and extract fields from
* response; can only be used in init callback. */ * response; can only be used in init callback. */
const char *rpc_delve(const tal_t *ctx, void rpc_scan(struct plugin *plugin,
struct plugin *plugin,
const char *method, const char *method,
const struct json_out *params TAKES, const struct json_out *params TAKES,
const char *guide); const char *guide,
...);
/* Send an async rpc request to lightningd. */ /* Send an async rpc request to lightningd. */
struct command_result * struct command_result *

14
plugins/offers.c

@ -42,24 +42,18 @@ static void init(struct plugin *p,
const char *buf UNUSED, const char *buf UNUSED,
const jsmntok_t *config UNUSED) const jsmntok_t *config UNUSED)
{ {
const char *field;
struct pubkey k; struct pubkey k;
field = rpc_scan(p, "getinfo",
rpc_delve(tmpctx, p, "getinfo",
take(json_out_obj(NULL, NULL, NULL)), take(json_out_obj(NULL, NULL, NULL)),
".id"); "{id:%}", JSON_SCAN(json_to_pubkey, &k));
if (!pubkey_from_hexstr(field, strlen(field), &k))
abort();
if (secp256k1_xonly_pubkey_from_pubkey(secp256k1_ctx, &id.pubkey, if (secp256k1_xonly_pubkey_from_pubkey(secp256k1_ctx, &id.pubkey,
NULL, &k.pubkey) != 1) NULL, &k.pubkey) != 1)
abort(); abort();
field = rpc_scan(p, "listconfigs",
rpc_delve(tmpctx, p, "listconfigs",
take(json_out_obj(NULL, "config", "cltv-final")), take(json_out_obj(NULL, "config", "cltv-final")),
".cltv-final"); "{cltv-final:%}", JSON_SCAN(json_to_number, &cltv_final));
cltv_final = atoi(field);
} }
static const struct plugin_command commands[] = { static const struct plugin_command commands[] = {

19
plugins/pay.c

@ -1890,18 +1890,13 @@ static struct command_result *json_listpays(struct command *cmd,
static void init(struct plugin *p, static void init(struct plugin *p,
const char *buf UNUSED, const jsmntok_t *config UNUSED) const char *buf UNUSED, const jsmntok_t *config UNUSED)
{ {
const char *field; rpc_scan(p, "getinfo", take(json_out_obj(NULL, NULL, NULL)),
"{id:%}", JSON_SCAN(json_to_node_id, &my_id));
field = rpc_delve(tmpctx, p, "getinfo",
take(json_out_obj(NULL, NULL, NULL)), ".id"); rpc_scan(p, "listconfigs",
if (!node_id_from_hexstr(field, strlen(field), &my_id)) take(json_out_obj(NULL, "config", "max-locktime-blocks")),
plugin_err(p, "getinfo didn't contain valid id: '%s'", field); "{max-locktime-blocks:%}",
JSON_SCAN(json_to_number, &maxdelay_default));
field = rpc_delve(tmpctx, p, "listconfigs",
take(json_out_obj(NULL,
"config", "max-locktime-blocks")),
".max-locktime-blocks");
maxdelay_default = atoi(field);
} }
struct payment_modifier *paymod_mods[] = { struct payment_modifier *paymod_mods[] = {

Loading…
Cancel
Save