mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-20 23:54:22 +01:00
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:
committed by
Rusty Russell
parent
ce3d3d8e54
commit
99f6faaabb
@@ -66,6 +66,8 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd,
|
|||||||
p->routetxt = NULL;
|
p->routetxt = NULL;
|
||||||
p->max_htlcs = UINT32_MAX;
|
p->max_htlcs = UINT32_MAX;
|
||||||
p->aborterror = NULL;
|
p->aborterror = NULL;
|
||||||
|
p->on_payment_success = NULL;
|
||||||
|
p->on_payment_failure = NULL;
|
||||||
|
|
||||||
/* Copy over the relevant pieces of information. */
|
/* Copy over the relevant pieces of information. */
|
||||||
if (parent != NULL) {
|
if (parent != NULL) {
|
||||||
@@ -1869,6 +1871,10 @@ static void payment_finished(struct payment *p)
|
|||||||
assert(result.leafstates & PAYMENT_STEP_SUCCESS);
|
assert(result.leafstates & PAYMENT_STEP_SUCCESS);
|
||||||
assert(result.preimage != NULL);
|
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);
|
ret = jsonrpc_stream_success(cmd);
|
||||||
json_add_node_id(ret, "destination", p->destination);
|
json_add_node_id(ret, "destination", p->destination);
|
||||||
json_add_sha256(ret, "payment_hash", p->payment_hash);
|
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. */}
|
if (command_finished(cmd, ret)) {/* Ignore result. */}
|
||||||
return;
|
return;
|
||||||
} else if (result.failure == NULL || result.failure->failcode < NODE) {
|
} 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 */
|
/* This is failing because we have no more routes to try */
|
||||||
msg = tal_fmt(cmd,
|
msg = tal_fmt(cmd,
|
||||||
"Ran out of routes to try after "
|
"Ran out of routes to try after "
|
||||||
@@ -1929,6 +1938,8 @@ static void payment_finished(struct payment *p)
|
|||||||
} else {
|
} else {
|
||||||
struct payment_result *failure = result.failure;
|
struct payment_result *failure = result.failure;
|
||||||
assert(failure!= NULL);
|
assert(failure!= NULL);
|
||||||
|
if (p->on_payment_failure != NULL)
|
||||||
|
p->on_payment_failure(p);
|
||||||
ret = jsonrpc_stream_fail(cmd, failure->code,
|
ret = jsonrpc_stream_fail(cmd, failure->code,
|
||||||
failure->message);
|
failure->message);
|
||||||
|
|
||||||
|
|||||||
@@ -298,6 +298,11 @@ struct payment {
|
|||||||
/* A human readable error message that is used as a top-level
|
/* A human readable error message that is used as a top-level
|
||||||
* explanation if a payment is aborted. */
|
* explanation if a payment is aborted. */
|
||||||
char *aborterror;
|
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 {
|
struct payment_modifier {
|
||||||
|
|||||||
@@ -1936,6 +1936,16 @@ static const char *init(struct plugin *p,
|
|||||||
return NULL;
|
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 /
|
/* 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. We also collect the summary
|
* 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);
|
last_group);
|
||||||
}
|
}
|
||||||
p->groupid = last_group + 1;
|
p->groupid = last_group + 1;
|
||||||
|
p->on_payment_success = on_payment_success;
|
||||||
|
p->on_payment_failure = on_payment_failure;
|
||||||
payment_start(p);
|
payment_start(p);
|
||||||
return command_still_pending(cmd);
|
return command_still_pending(cmd);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user