libplugin: Add callbacks for successful and failed payments

We're about to suspend duplicate calls to `pay` and this will help us
notify them if the original payment completes.
This commit is contained in:
Christian Decker 2021-10-04 21:13:11 +02:00 committed by Rusty Russell
parent ce3d3d8e54
commit 99f6faaabb
3 changed files with 28 additions and 0 deletions

View file

@ -66,6 +66,8 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd,
p->routetxt = NULL;
p->max_htlcs = UINT32_MAX;
p->aborterror = NULL;
p->on_payment_success = NULL;
p->on_payment_failure = NULL;
/* Copy over the relevant pieces of information. */
if (parent != NULL) {
@ -1869,6 +1871,10 @@ static void payment_finished(struct payment *p)
assert(result.leafstates & PAYMENT_STEP_SUCCESS);
assert(result.preimage != NULL);
/* Call any callback we might have registered. */
if (p->on_payment_success != NULL)
p->on_payment_success(p);
ret = jsonrpc_stream_success(cmd);
json_add_node_id(ret, "destination", p->destination);
json_add_sha256(ret, "payment_hash", p->payment_hash);
@ -1911,6 +1917,9 @@ static void payment_finished(struct payment *p)
if (command_finished(cmd, ret)) {/* Ignore result. */}
return;
} else if (result.failure == NULL || result.failure->failcode < NODE) {
if (p->on_payment_failure != NULL)
p->on_payment_failure(p);
/* This is failing because we have no more routes to try */
msg = tal_fmt(cmd,
"Ran out of routes to try after "
@ -1929,6 +1938,8 @@ static void payment_finished(struct payment *p)
} else {
struct payment_result *failure = result.failure;
assert(failure!= NULL);
if (p->on_payment_failure != NULL)
p->on_payment_failure(p);
ret = jsonrpc_stream_fail(cmd, failure->code,
failure->message);

View file

@ -298,6 +298,11 @@ struct payment {
/* A human readable error message that is used as a top-level
* explanation if a payment is aborted. */
char *aborterror;
/* Callback to be called when the entire payment process
* completes successfully. */
void (*on_payment_success)(struct payment *p);
void (*on_payment_failure)(struct payment *p);
};
struct payment_modifier {

View file

@ -1936,6 +1936,16 @@ static const char *init(struct plugin *p,
return NULL;
}
static void on_payment_success(struct payment *payment)
{
return;
}
static void on_payment_failure(struct payment *payment)
{
return;
}
/* 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
* already have a pending payment running. We also collect the summary
@ -2043,6 +2053,8 @@ payment_listsendpays_previous(struct command *cmd, const char *buf,
last_group);
}
p->groupid = last_group + 1;
p->on_payment_success = on_payment_success;
p->on_payment_failure = on_payment_failure;
payment_start(p);
return command_still_pending(cmd);
}