Skip to content

Commit e6d5df7

Browse files
cdeckerrustyrussell
authored andcommitted
jsonrpc: Simplify the state decision of listpays
It was really different from the way we decide the overall state of a `pay` command's output. Now we use a more similar state decision, based on collecting all states and checking them at the end to determine the outcome.
1 parent 6ad8a8a commit e6d5df7

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

plugins/libplugin-pay.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ struct createonion_request {
3030

3131
/* States returned by listsendpays, waitsendpay, etc. */
3232
enum payment_result_state {
33-
PAYMENT_PENDING,
34-
PAYMENT_COMPLETE,
35-
PAYMENT_FAILED,
33+
PAYMENT_PENDING = 1,
34+
PAYMENT_COMPLETE = 2,
35+
PAYMENT_FAILED = 4,
3636
};
3737

3838
/* A parsed version of the possible outcomes that a sendpay / payment may

plugins/pay.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,10 @@ struct pay_mpp {
16531653

16541654
/* This is the bolt11/bolt12 string */
16551655
const char *invstring;
1656-
/* Status of combined payment */
1657-
const char *status;
1656+
1657+
/* Accumulated states of all sendpay commands involved. */
1658+
enum payment_result_state state;
1659+
16581660
/* Optional label (of first one!) */
16591661
const jsmntok_t *label;
16601662
/* Optional preimage (iff status is successful) */
@@ -1763,7 +1765,14 @@ static void add_new_entry(struct json_stream *ret,
17631765
json_add_tok(ret, "destination", pm->destination, buf);
17641766

17651767
json_add_sha256(ret, "payment_hash", pm->payment_hash);
1766-
json_add_string(ret, "status", pm->status);
1768+
1769+
if (pm->state & PAYMENT_COMPLETE)
1770+
json_add_string(ret, "status", "complete");
1771+
else if (pm->state & PAYMENT_PENDING || attempt_ongoing(pm->payment_hash))
1772+
json_add_string(ret, "status", "pending");
1773+
else
1774+
json_add_string(ret, "status", "failed");
1775+
17671776
json_add_u32(ret, "created_at", pm->timestamp);
17681777

17691778
if (pm->label)
@@ -1828,6 +1837,7 @@ static struct command_result *listsendpays_done(struct command *cmd,
18281837
pm = pay_map_get(&pay_map, &payment_hash);
18291838
if (!pm) {
18301839
pm = tal(cmd, struct pay_mpp);
1840+
pm->state = 0;
18311841
pm->payment_hash = tal_dup(pm, struct sha256, &payment_hash);
18321842
pm->invstring = tal_steal(pm, invstr);
18331843
pm->destination = json_get_member(buf, t, "destination");
@@ -1836,7 +1846,6 @@ static struct command_result *listsendpays_done(struct command *cmd,
18361846
pm->amount_sent = AMOUNT_MSAT(0);
18371847
pm->amount = talz(pm, struct amount_msat);
18381848
pm->num_nonfailed_parts = 0;
1839-
pm->status = NULL;
18401849
pm->timestamp = created_at;
18411850
pay_map_add(&pay_map, pm);
18421851
}
@@ -1845,24 +1854,15 @@ static struct command_result *listsendpays_done(struct command *cmd,
18451854
if (json_tok_streq(buf, status, "complete")) {
18461855
add_amount_sent(cmd->plugin, pm->invstring, pm, buf, t);
18471856
pm->num_nonfailed_parts++;
1848-
pm->status = "complete";
18491857
pm->preimage
18501858
= json_get_member(buf, t, "payment_preimage");
1859+
pm->state |= PAYMENT_COMPLETE;
18511860
} else if (json_tok_streq(buf, status, "pending")) {
18521861
add_amount_sent(cmd->plugin, pm->invstring, pm, buf, t);
18531862
pm->num_nonfailed_parts++;
1854-
/* Failed -> pending; don't downgrade success. */
1855-
if (!pm->status || !streq(pm->status, "complete"))
1856-
pm->status = "pending";
1863+
pm->state |= PAYMENT_PENDING;
18571864
} else {
1858-
if (attempt_ongoing(pm->payment_hash)) {
1859-
/* Failed -> pending; don't downgrade success. */
1860-
if (!pm->status
1861-
|| !streq(pm->status, "complete"))
1862-
pm->status = "pending";
1863-
} else if (!pm->status)
1864-
/* Only failed if they all failed */
1865-
pm->status = "failed";
1865+
pm->state |= PAYMENT_FAILED;
18661866
}
18671867
}
18681868

0 commit comments

Comments
 (0)