From aa0ff6b80403da49b3df97f1c61efed83f1fdcf1 Mon Sep 17 00:00:00 2001 From: darosior Date: Mon, 27 Jan 2020 13:06:02 +0100 Subject: [PATCH] libplugin: pass a pointer to plugin to send_outreq autoclean needs to send outreqs from a timer cb, hence with cmd == NULL. --- plugins/autoclean.c | 2 +- plugins/fundchannel.c | 18 +++++++++--------- plugins/libplugin.c | 15 ++++++++------- plugins/libplugin.h | 7 ++++--- plugins/pay.c | 16 ++++++++-------- 5 files changed, 30 insertions(+), 28 deletions(-) diff --git a/plugins/autoclean.c b/plugins/autoclean.c index 27a99c6b3917..9bfdef86cce0 100644 --- a/plugins/autoclean.c +++ b/plugins/autoclean.c @@ -32,7 +32,7 @@ static struct command_result *do_clean(struct plugin *p) json_out_finished(params); /* FIXME: delexpiredinvoice should be in our plugin too! */ - return send_outreq(NULL, "delexpiredinvoice", ignore, ignore, p, + return send_outreq(p, NULL, "delexpiredinvoice", ignore, ignore, p, take(params)); } diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index 1f63f9d4cb34..00b0d7831462 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -103,7 +103,7 @@ static struct command_result *tx_abort(struct command *cmd, /* We need to call txdiscard, and forward the actual cause for the * error after we've cleaned up. We swallow any errors returned by * this call, as we don't really care if it succeeds or not */ - return send_outreq(cmd, "txdiscard", + return send_outreq(cmd->plugin, cmd, "txdiscard", send_prior, send_prior, fr, take(ret)); } @@ -155,7 +155,7 @@ static struct command_result *send_tx(struct command *cmd, type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id)); json_out_end(ret, '}'); - return send_outreq(cmd, "txsend", + return send_outreq(cmd->plugin, cmd, "txsend", finish, tx_abort, fr, take(ret)); } @@ -214,7 +214,7 @@ static struct command_result *tx_prepare_done(struct command *cmd, json_out_add(ret, "txout", false, "%u", outnum); json_out_end(ret, '}'); - return send_outreq(cmd, "fundchannel_complete", + return send_outreq(cmd->plugin, cmd, "fundchannel_complete", send_tx, tx_abort, fr, take(ret)); } @@ -234,7 +234,7 @@ static struct command_result *cancel_start(struct command *cmd, json_out_addstr(ret, "id", node_id_to_hexstr(tmpctx, fr->id)); json_out_end(ret, '}'); - return send_outreq(cmd, "fundchannel_cancel", + return send_outreq(cmd->plugin, cmd, "fundchannel_cancel", send_prior, send_prior, fr, take(ret)); } @@ -274,7 +274,7 @@ static struct command_result *prepare_actual(struct command *cmd, ret = txprepare(cmd, fr, fr->funding_addr); - return send_outreq(cmd, "txprepare", + return send_outreq(cmd->plugin, cmd, "txprepare", tx_prepare_done, cancel_start, fr, take(ret)); } @@ -301,7 +301,7 @@ static struct command_result *fundchannel_start_done(struct command *cmd, type_to_string(tmpctx, struct bitcoin_txid, &fr->tx_id)); json_out_end(ret, '}'); - return send_outreq(cmd, "txdiscard", + return send_outreq(cmd->plugin, cmd, "txdiscard", prepare_actual, cancel_start, fr, take(ret)); } @@ -330,7 +330,7 @@ static struct command_result *fundchannel_start(struct command *cmd, json_out_end(ret, '}'); json_out_finished(ret); - return send_outreq(cmd, "fundchannel_start", + return send_outreq(cmd->plugin, cmd, "fundchannel_start", fundchannel_start_done, tx_abort, fr, take(ret)); } @@ -397,7 +397,7 @@ static struct command_result *exec_dryrun(struct command *cmd, * so we can get an accurate idea of the funding amount */ ret = txprepare(cmd, fr, placeholder_funding_addr); - return send_outreq(cmd, "txprepare", + return send_outreq(cmd->plugin, cmd, "txprepare", post_dryrun, forward_error, fr, take(ret)); @@ -413,7 +413,7 @@ static struct command_result *connect_to_peer(struct command *cmd, json_out_end(ret, '}'); json_out_finished(ret); - return send_outreq(cmd, "connect", + return send_outreq(cmd->plugin, cmd, "connect", exec_dryrun, forward_error, fr, take(ret)); } diff --git a/plugins/libplugin.c b/plugins/libplugin.c index 246ba01c8d1d..8a35cb7b4dc3 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -426,7 +426,8 @@ static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks) } struct command_result * -send_outreq_(struct command *cmd, +send_outreq_(struct plugin *plugin, + struct command *cmd, const char *method, struct command_result *(*cb)(struct command *command, const char *buf, @@ -442,23 +443,23 @@ send_outreq_(struct command *cmd, struct json_stream *js; struct out_req *out; - out = tal(cmd, struct out_req); - out->id = cmd->plugin->next_outreq_id++; + out = tal(plugin, struct out_req); + out->id = plugin->next_outreq_id++; out->cmd = cmd; out->cb = cb; out->errcb = errcb; out->arg = arg; - uintmap_add(&cmd->plugin->out_reqs, out->id, out); + uintmap_add(&plugin->out_reqs, out->id, out); - js = new_json_stream(NULL, cmd, NULL); + js = new_json_stream(NULL, NULL, NULL); json_object_start(js, NULL); json_add_string(js, "jsonrpc", "2.0"); json_add_u64(js, "id", out->id); json_add_string(js, "method", method); json_out_add_splice(js->jout, "params", params); json_object_compat_end(js); - json_stream_close(js, cmd); - ld_rpc_send(cmd->plugin, js); + json_stream_close(js, NULL); + ld_rpc_send(plugin, js); if (taken(params)) tal_free(params); diff --git a/plugins/libplugin.h b/plugins/libplugin.h index c8e74188f88d..f09b6c3cfd9f 100644 --- a/plugins/libplugin.h +++ b/plugins/libplugin.h @@ -170,7 +170,8 @@ const char *rpc_delve(const tal_t *ctx, * @params can be NULL, otherwise it's an array or object. */ struct command_result * -send_outreq_(struct command *cmd, +send_outreq_(struct plugin *plugin, + struct command *cmd, const char *method, struct command_result *(*cb)(struct command *command, const char *buf, @@ -183,8 +184,8 @@ send_outreq_(struct command *cmd, void *arg, const struct json_out *params TAKES); -#define send_outreq(cmd, method, cb, errcb, arg, params) \ - send_outreq_((cmd), (method), \ +#define send_outreq(plugin, cmd, method, cb, errcb, arg, params) \ + send_outreq_((plugin), (cmd), (method), \ typesafe_cb_preargs(struct command_result *, void *, \ (cb), (arg), \ struct command *command, \ diff --git a/plugins/pay.c b/plugins/pay.c index b9d3ded92be2..48632d4c9e39 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -376,7 +376,7 @@ execute_waitblockheight(struct command *cmd, json_out_end(params, '}'); json_out_finished(params); - return send_outreq(cmd, "waitblockheight", + return send_outreq(cmd->plugin, cmd, "waitblockheight", &waitblockheight_done, &waitblockheight_error, pc, @@ -581,7 +581,7 @@ static struct command_result *sendpay_done(struct command *cmd, const jsmntok_t *result, struct pay_command *pc) { - return send_outreq(cmd, "waitsendpay", + return send_outreq(cmd->plugin, cmd, "waitsendpay", waitsendpay_done, waitsendpay_error, pc, take(json_out_obj(NULL, "payment_hash", pc->payment_hash))); @@ -849,7 +849,7 @@ static struct command_result *getroute_done(struct command *cmd, pc->payment_secret); json_out_end(params, '}'); - return send_outreq(cmd, "sendpay", sendpay_done, sendpay_error, pc, + return send_outreq(cmd->plugin, cmd, "sendpay", sendpay_done, sendpay_error, pc, take(params)); } @@ -945,7 +945,7 @@ static struct command_result *execute_getroute(struct command *cmd, } json_out_end(params, '}'); - return send_outreq(cmd, "getroute", getroute_done, getroute_error, pc, + return send_outreq(cmd->plugin, cmd, "getroute", getroute_done, getroute_error, pc, take(params)); } @@ -989,7 +989,7 @@ static struct command_result * execute_getstartblockheight(struct command *cmd, struct pay_command *pc) { - return send_outreq(cmd, "getinfo", + return send_outreq(cmd->plugin, cmd, "getinfo", &getstartblockheight_done, &getstartblockheight_error, pc, @@ -1113,7 +1113,7 @@ static struct command_result *shadow_route(struct command *cmd, if (pseudorand(2) == 0) return start_pay_attempt(cmd, pc, "Initial attempt"); - return send_outreq(cmd, "listchannels", + return send_outreq(cmd->plugin, cmd, "listchannels", add_shadow_route, forward_error, pc, take(json_out_obj(NULL, "source", pc->shadow_dest))); } @@ -1357,7 +1357,7 @@ static struct command_result *json_pay(struct command *cmd, #endif /* Get capacities of local channels (no parameters) */ - return send_outreq(cmd, "listpeers", listpeers_done, forward_error, pc, + return send_outreq(cmd->plugin, cmd, "listpeers", listpeers_done, forward_error, pc, take(json_out_obj(NULL, NULL, NULL))); } @@ -1670,7 +1670,7 @@ static struct command_result *json_listpays(struct command *cmd, NULL)) return command_param_failed(); - return send_outreq(cmd, "listsendpays", + return send_outreq(cmd->plugin, cmd, "listsendpays", listsendpays_done, forward_error, cast_const(char *, b11str), /* Neatly returns empty object if b11str is NULL */