Browse Source

invoice: make invoice_payment hook a multi-user hook.

We register on it for offers, and without this nobody else can.

Changelog-Changed: plugins: more than one plugin can now register invoice_payment hook.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
fix-mocks
Rusty Russell 4 years ago
parent
commit
0ad269f5b6
  1. 58
      lightningd/invoice.c

58
lightningd/invoice.c

@ -233,29 +233,15 @@ static const u8 *hook_gives_failmsg(const tal_t *ctx,
}
static void
invoice_payment_hook_cb(struct invoice_payment_hook_payload *payload STEALS,
const char *buffer,
const jsmntok_t *toks)
invoice_payment_hooks_done(struct invoice_payment_hook_payload *payload STEALS)
{
struct lightningd *ld = payload->ld;
struct invoice invoice;
const u8 *failmsg;
/* We notify here to benefit from the payload and because the hook callback is
* called even if the hook is not registered. */
notify_invoice_payment(ld, payload->msat, payload->preimage, payload->label);
struct lightningd *ld = payload->ld;
tal_del_destructor2(payload->set, invoice_payload_remove_set, payload);
/* We want to free this, whatever happens. */
tal_steal(tmpctx, payload);
/* If peer dies or something, this can happen. */
if (!payload->set) {
log_debug(ld->log, "invoice '%s' paying htlc_in has gone!",
payload->label->s);
return;
}
/* If invoice gets paid meanwhile (plugin responds out-of-order?) then
* we can also fail */
if (!wallet_invoice_find_by_label(ld->wallet, &invoice, payload->label)) {
@ -264,14 +250,6 @@ invoice_payment_hook_cb(struct invoice_payment_hook_payload *payload STEALS,
return;
}
/* Did we have a hook result? */
failmsg = hook_gives_failmsg(NULL, ld,
payload->set->htlcs[0], buffer, toks);
if (failmsg) {
htlc_set_fail(payload->set, take(failmsg));
return;
}
log_info(ld->log, "Resolved invoice '%s' with amount %s in %zu htlcs",
payload->label->s,
type_to_string(tmpctx, struct amount_msat, &payload->msat),
@ -280,8 +258,34 @@ invoice_payment_hook_cb(struct invoice_payment_hook_payload *payload STEALS,
htlc_set_fulfill(payload->set, &payload->preimage);
}
REGISTER_SINGLE_PLUGIN_HOOK(invoice_payment,
invoice_payment_hook_cb,
static bool
invoice_payment_deserialize(struct invoice_payment_hook_payload *payload,
const char *buffer,
const jsmntok_t *toks)
{
struct lightningd *ld = payload->ld;
const u8 *failmsg;
/* If peer dies or something, this can happen. */
if (!payload->set) {
log_debug(ld->log, "invoice '%s' paying htlc_in has gone!",
payload->label->s);
return false;
}
/* Did we have a hook result? */
failmsg = hook_gives_failmsg(NULL, ld,
payload->set->htlcs[0], buffer, toks);
if (failmsg) {
htlc_set_fail(payload->set, take(failmsg));
return false;
}
return true;
}
REGISTER_PLUGIN_HOOK(invoice_payment,
invoice_payment_deserialize,
invoice_payment_hooks_done,
invoice_payment_serialize,
struct invoice_payment_hook_payload *);
@ -393,6 +397,8 @@ void invoice_try_pay(struct lightningd *ld,
payload->set = set;
tal_add_destructor2(set, invoice_payload_remove_set, payload);
notify_invoice_payment(ld, payload->msat, payload->preimage, payload->label);
plugin_hook_call_invoice_payment(ld, payload);
}

Loading…
Cancel
Save