From aca9c7d49ca787ad478dbbe19846b7206fa73a6d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 3 Aug 2022 16:45:44 +0200 Subject: [PATCH] pay: Annotate suspended payments with the groupid they mirror This should prevent us from accidentally completing a payment twice, when replaying the result of an actual attempt against pay call that was suspended due to it still being pending. --- plugins/pay.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/plugins/pay.c b/plugins/pay.c index fc39b8bb3..5bf33031a 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -787,6 +787,10 @@ payment_listsendpays_previous(struct command *cmd, const char *buf, u64 last_group = 0; /* Do we have pending sendpays for the previous attempt? */ bool pending = false; + + /* Group ID of the first pending payment, this will be the one + * who's result gets replayed if we end up suspending. */ + u64 pending_group_id = 0; /* Did a prior attempt succeed? */ bool completed = false; @@ -855,6 +859,11 @@ payment_listsendpays_previous(struct command *cmd, const char *buf, status = json_get_member(buf, t, "status"); completed |= json_tok_streq(buf, status, "complete"); pending |= json_tok_streq(buf, status, "pending"); + + /* Remember the group id of the first pending group so + * we can replay its result later. */ + if (!pending_group_id && pending) + pending_group_id = groupid; } if (completed) { @@ -874,7 +883,9 @@ payment_listsendpays_previous(struct command *cmd, const char *buf, /* We suspend this call and wait for the * `on_payment_success` or `on_payment_failure` * handler of the currently running payment to notify - * us about its completion. */ + * us about its completion. We latch on to the result + * from the call we extracted above. */ + p->groupid = pending_group_id; return command_still_pending(cmd); } p->groupid = last_group + 1;