paymod: Do not pass cmd to sub-payments, plugin suffices

We were passing the `cmd` instance around where `plugin` suffices (used to
start RPC calls and logs).
This commit is contained in:
Christian Decker
2020-05-18 20:44:21 +02:00
parent df94ec35ba
commit 958eb41cbb
2 changed files with 20 additions and 14 deletions

View File

@@ -38,14 +38,18 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd,
/* Copy over the relevant pieces of information. */ /* Copy over the relevant pieces of information. */
if (parent != NULL) { if (parent != NULL) {
assert(cmd == NULL);
tal_arr_expand(&parent->children, p); tal_arr_expand(&parent->children, p);
p->destination = p->getroute_destination = parent->destination; p->destination = p->getroute_destination = parent->destination;
p->amount = parent->amount; p->amount = parent->amount;
p->payment_hash = parent->payment_hash; p->payment_hash = parent->payment_hash;
p->partid = payment_root(p->parent)->next_partid++; p->partid = payment_root(p->parent)->next_partid++;
p->plugin = parent->plugin;
} else { } else {
assert(cmd != NULL);
p->partid = 0; p->partid = 0;
p->next_partid = 1; p->next_partid = 1;
p->plugin = cmd->plugin;
} }
/* Initialize all modifier data so we can point to the fields when /* Initialize all modifier data so we can point to the fields when
@@ -78,7 +82,7 @@ static struct command_result *payment_rpc_failure(struct command *cmd,
const jsmntok_t *toks, const jsmntok_t *toks,
struct payment *p) struct payment *p)
{ {
plugin_log(p->cmd->plugin, LOG_DBG, plugin_log(p->plugin, LOG_DBG,
"Failing a partial payment due to a failed RPC call: %.*s", "Failing a partial payment due to a failed RPC call: %.*s",
toks->end - toks->start, buffer + toks->start); toks->end - toks->start, buffer + toks->start);
@@ -114,7 +118,7 @@ static struct payment_tree_result payment_collect_result(struct payment *p)
* we sent in total. */ * we sent in total. */
if (!amount_msat_add(&res.sent, res.sent, cres.sent)) if (!amount_msat_add(&res.sent, res.sent, cres.sent))
plugin_err( plugin_err(
p->cmd->plugin, p->plugin,
"Number overflow summing partial payments: %s + %s", "Number overflow summing partial payments: %s + %s",
type_to_string(tmpctx, struct amount_msat, type_to_string(tmpctx, struct amount_msat,
&res.sent), &res.sent),
@@ -151,8 +155,8 @@ void payment_start(struct payment *p)
/* TODO If this is not the root, we can actually skip the getinfo call /* TODO If this is not the root, we can actually skip the getinfo call
* and just reuse the parent's value. */ * and just reuse the parent's value. */
send_outreq(p->cmd->plugin, send_outreq(p->plugin,
jsonrpc_request_start(p->cmd->plugin, NULL, "getinfo", jsonrpc_request_start(p->plugin, NULL, "getinfo",
payment_getinfo_success, payment_getinfo_success,
payment_rpc_failure, p)); payment_rpc_failure, p));
} }
@@ -231,13 +235,13 @@ static struct command_result *payment_getroute_error(struct command *cmd,
static void payment_getroute(struct payment *p) static void payment_getroute(struct payment *p)
{ {
struct out_req *req; struct out_req *req;
req = jsonrpc_request_start(p->cmd->plugin, NULL, "getroute", req = jsonrpc_request_start(p->plugin, NULL, "getroute",
payment_getroute_result, payment_getroute_result,
payment_getroute_error, p); payment_getroute_error, p);
json_add_node_id(req->js, "id", p->getroute_destination); json_add_node_id(req->js, "id", p->getroute_destination);
json_add_amount_msat_only(req->js, "msatoshi", p->amount); json_add_amount_msat_only(req->js, "msatoshi", p->amount);
json_add_num(req->js, "riskfactor", 1); json_add_num(req->js, "riskfactor", 1);
send_outreq(p->cmd->plugin, req); send_outreq(p->plugin, req);
} }
static u8 *tal_towire_legacy_payload(const tal_t *ctx, const struct legacy_payload *payload) static u8 *tal_towire_legacy_payload(const tal_t *ctx, const struct legacy_payload *payload)
@@ -376,12 +380,12 @@ static struct command_result *payment_sendonion_success(struct command *cmd,
struct payment *p) struct payment *p)
{ {
struct out_req *req; struct out_req *req;
req = jsonrpc_request_start(p->cmd->plugin, NULL, "waitsendpay", req = jsonrpc_request_start(p->plugin, NULL, "waitsendpay",
payment_waitsendpay_finished, payment_waitsendpay_finished,
payment_waitsendpay_finished, p); payment_waitsendpay_finished, p);
json_add_sha256(req->js, "payment_hash", p->payment_hash); json_add_sha256(req->js, "payment_hash", p->payment_hash);
json_add_num(req->js, "partid", p->partid); json_add_num(req->js, "partid", p->partid);
send_outreq(p->cmd->plugin, req); send_outreq(p->plugin, req);
return command_still_pending(cmd); return command_still_pending(cmd);
} }
@@ -396,7 +400,7 @@ static struct command_result *payment_createonion_success(struct command *cmd,
struct secret *secrets; struct secret *secrets;
p->createonion_response = tal_createonion_response_from_json(p, buffer, toks); p->createonion_response = tal_createonion_response_from_json(p, buffer, toks);
req = jsonrpc_request_start(p->cmd->plugin, NULL, "sendonion", req = jsonrpc_request_start(p->plugin, NULL, "sendonion",
payment_sendonion_success, payment_sendonion_success,
payment_rpc_failure, p); payment_rpc_failure, p);
json_add_hex_talarr(req->js, "onion", p->createonion_response->onion); json_add_hex_talarr(req->js, "onion", p->createonion_response->onion);
@@ -419,7 +423,7 @@ static struct command_result *payment_createonion_success(struct command *cmd,
json_add_num(req->js, "partid", p->partid); json_add_num(req->js, "partid", p->partid);
send_outreq(p->cmd->plugin, req); send_outreq(p->plugin, req);
return command_still_pending(cmd); return command_still_pending(cmd);
} }
@@ -488,7 +492,7 @@ static void payment_compute_onion_payloads(struct payment *p)
static void payment_sendonion(struct payment *p) static void payment_sendonion(struct payment *p)
{ {
struct out_req *req; struct out_req *req;
req = jsonrpc_request_start(p->cmd->plugin, NULL, "createonion", req = jsonrpc_request_start(p->plugin, NULL, "createonion",
payment_createonion_success, payment_createonion_success,
payment_rpc_failure, p); payment_rpc_failure, p);
@@ -511,7 +515,7 @@ static void payment_sendonion(struct payment *p)
json_add_secret(req->js, "sessionkey", json_add_secret(req->js, "sessionkey",
p->createonion_request->session_key); p->createonion_request->session_key);
send_outreq(p->cmd->plugin, req); send_outreq(p->plugin, req);
} }
/* Mutual recursion. */ /* Mutual recursion. */
@@ -731,7 +735,7 @@ static inline void retry_step_cb(struct retry_mod_data *rd,
struct payment *subpayment; struct payment *subpayment;
struct retry_mod_data *rdata = payment_mod_retry_get_data(p); struct retry_mod_data *rdata = payment_mod_retry_get_data(p);
if (p->step == PAYMENT_STEP_FAILED && rdata->retries > 0) { if (p->step == PAYMENT_STEP_FAILED && rdata->retries > 0) {
subpayment = payment_new(p, p->cmd, p, p->modifiers); subpayment = payment_new(p, NULL, p, p->modifiers);
payment_start(subpayment); payment_start(subpayment);
p->step = PAYMENT_STEP_RETRY; p->step = PAYMENT_STEP_RETRY;
} }

View File

@@ -101,8 +101,10 @@ enum payment_step {
}; };
struct payment { struct payment {
/* The command that triggered this payment. */ /* The command that triggered this payment. Only set for the root
* payment. */
struct command *cmd; struct command *cmd;
struct plugin *plugin;
struct list_node list; struct list_node list;
const char *json_buffer; const char *json_buffer;