mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-26 20:30:59 +01:00
pay: Make pay
idempotent
This commit is contained in:
parent
5f260840ab
commit
a4fb5bceb0
1 changed files with 48 additions and 7 deletions
|
@ -1938,7 +1938,9 @@ static const char *init(struct plugin *p,
|
||||||
|
|
||||||
/* We are interested in any prior attempts to pay this payment_hash /
|
/* We are interested in any prior attempts to pay this payment_hash /
|
||||||
* invoice so we can set the `groupid` correctly and ensure we don't
|
* invoice so we can set the `groupid` correctly and ensure we don't
|
||||||
* already have a pending payment running. */
|
* already have a pending payment running. We also collect the summary
|
||||||
|
* about an eventual previous complete payment so we can return that
|
||||||
|
* as a no-op. */
|
||||||
static struct command_result *
|
static struct command_result *
|
||||||
payment_listsendpays_previous(struct command *cmd, const char *buf,
|
payment_listsendpays_previous(struct command *cmd, const char *buf,
|
||||||
const jsmntok_t *result, struct payment *p)
|
const jsmntok_t *result, struct payment *p)
|
||||||
|
@ -1952,6 +1954,14 @@ payment_listsendpays_previous(struct command *cmd, const char *buf,
|
||||||
/* Did a prior attempt succeed? */
|
/* Did a prior attempt succeed? */
|
||||||
bool completed = false;
|
bool completed = false;
|
||||||
|
|
||||||
|
/* Metadata for a complete payment, if one exists. */
|
||||||
|
struct json_stream *ret;
|
||||||
|
u32 parts = 0;
|
||||||
|
struct preimage preimage;
|
||||||
|
struct amount_msat sent, msat;
|
||||||
|
struct node_id destination;
|
||||||
|
u32 created_at;
|
||||||
|
|
||||||
err = json_get_member(buf, result, "error");
|
err = json_get_member(buf, result, "error");
|
||||||
if (err)
|
if (err)
|
||||||
return command_fail(
|
return command_fail(
|
||||||
|
@ -1971,6 +1981,7 @@ payment_listsendpays_previous(struct command *cmd, const char *buf,
|
||||||
{
|
{
|
||||||
u64 groupid;
|
u64 groupid;
|
||||||
const jsmntok_t *status, *grouptok;
|
const jsmntok_t *status, *grouptok;
|
||||||
|
struct amount_msat diff_sent, diff_msat;
|
||||||
grouptok = json_get_member(buf, t, "groupid");
|
grouptok = json_get_member(buf, t, "groupid");
|
||||||
json_to_u64(buf, grouptok, &groupid);
|
json_to_u64(buf, grouptok, &groupid);
|
||||||
|
|
||||||
|
@ -1979,6 +1990,30 @@ payment_listsendpays_previous(struct command *cmd, const char *buf,
|
||||||
completed = false;
|
completed = false;
|
||||||
pending = false;
|
pending = false;
|
||||||
last_group = groupid;
|
last_group = groupid;
|
||||||
|
|
||||||
|
parts = 1;
|
||||||
|
json_scan(tmpctx, buf, t,
|
||||||
|
"{destination:%"
|
||||||
|
",created_at:%"
|
||||||
|
",amount_msat:%"
|
||||||
|
",amount_sent_msat:%"
|
||||||
|
",payment_preimage:%}",
|
||||||
|
JSON_SCAN(json_to_node_id, &destination),
|
||||||
|
JSON_SCAN(json_to_u32, &created_at),
|
||||||
|
JSON_SCAN(json_to_msat, &msat),
|
||||||
|
JSON_SCAN(json_to_msat, &sent),
|
||||||
|
JSON_SCAN(json_to_preimage, &preimage));
|
||||||
|
} else {
|
||||||
|
json_scan(tmpctx, buf, t,
|
||||||
|
"{amount_msat:%"
|
||||||
|
",amount_sent_msat:%}",
|
||||||
|
JSON_SCAN(json_to_msat, &diff_msat),
|
||||||
|
JSON_SCAN(json_to_msat, &diff_sent));
|
||||||
|
if (!amount_msat_add(&msat, msat, diff_msat) ||
|
||||||
|
!amount_msat_add(&sent, sent, diff_sent))
|
||||||
|
plugin_err(p->plugin,
|
||||||
|
"msat overflow adding up parts");
|
||||||
|
parts++;
|
||||||
}
|
}
|
||||||
|
|
||||||
status = json_get_member(buf, t, "status");
|
status = json_get_member(buf, t, "status");
|
||||||
|
@ -1987,12 +2022,18 @@ payment_listsendpays_previous(struct command *cmd, const char *buf,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (completed) {
|
if (completed) {
|
||||||
return command_fail(
|
ret = jsonrpc_stream_success(cmd);
|
||||||
cmd, PAY_RHASH_ALREADY_USED,
|
json_add_preimage(ret, "payment_preimage", &preimage);
|
||||||
"The payment with payment_hash=%s was successful "
|
json_add_string(ret, "status", "complete");
|
||||||
"(groupid=%" PRIu64 ")",
|
json_add_amount_msat_compat(ret, msat, "msatoshi",
|
||||||
type_to_string(tmpctx, struct sha256, p->payment_hash),
|
"amount_msat");
|
||||||
last_group);
|
json_add_amount_msat_compat(ret, sent, "msatoshi_sent",
|
||||||
|
"amount_sent_msat");
|
||||||
|
json_add_node_id(ret, "destination", p->destination);
|
||||||
|
json_add_sha256(ret, "payment_hash", p->payment_hash);
|
||||||
|
json_add_u32(ret, "created_at", created_at);
|
||||||
|
json_add_num(ret, "parts", parts);
|
||||||
|
return command_finished(cmd, ret);
|
||||||
} else if (pending) {
|
} else if (pending) {
|
||||||
return command_fail(
|
return command_fail(
|
||||||
cmd, PAY_IN_PROGRESS,
|
cmd, PAY_IN_PROGRESS,
|
||||||
|
|
Loading…
Add table
Reference in a new issue