From 8fe4196d1d52335ad1a959cb4fac930ffb5ef61e Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 1 Mar 2024 13:54:26 +0100 Subject: [PATCH] pay: Prevent repeating the preapproveinvoice check So far we would call `preapproveinvoice` once for each payment split, i.e., at least once per HTLC, and potentially more often. There is no point in doing so repeatedly, and especially in remote signer setup this is actually counterproductive due to the additional roundtrips. Changelog-Changed pay: Improved performance by removing repeated `preapproveinvoice` calls --- plugins/libplugin-pay.c | 37 +++++++++++++++++++++++++++++++++++-- plugins/libplugin-pay.h | 6 +++++- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index 3bd5230b5fc9..ebf779c07e5b 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -3818,6 +3818,11 @@ check_preapproveinvoice_allow(struct command *cmd, struct payment *p) { /* On success, an empty object is returned. */ +// struct preapproveinvoice_data *d + + struct preapproveinvoice_data *d = payment_mod_check_preapproveinvoice_get_data(payment_root(p)); + d->approved = true; + paymod_log(p, LOG_DBG, "Result from preapproveinvoice: allow"); payment_continue(p); return command_still_pending(cmd); } @@ -3833,8 +3838,20 @@ static struct command_result *preapproveinvoice_rpc_failure(struct command *cmd, return command_still_pending(cmd); } -static void check_preapproveinvoice_start(void *d UNUSED, struct payment *p) +static void check_preapproveinvoice_start(struct preapproveinvoice_data *d UNUSED, struct payment *p) { + struct payment *root = payment_root(p); + + struct preapproveinvoice_data *data = + payment_mod_check_preapproveinvoice_get_data(root); + /* If the root payment was used to send the + * `preapproveinvoice` message to the signer, we don't need to + * do that again. */ + if (data->approved) { + return payment_continue(p); + } + + paymod_log(p, LOG_DBG, "Calling preapproveinvoice on signer for payment=%"PRIu64, root->id); /* Ask the HSM if the invoice is OK to pay */ struct out_req *req; req = jsonrpc_request_start(p->plugin, NULL, "preapproveinvoice", @@ -3845,7 +3862,23 @@ static void check_preapproveinvoice_start(void *d UNUSED, struct payment *p) (void) send_outreq(p->plugin, req); } -REGISTER_PAYMENT_MODIFIER(check_preapproveinvoice, void *, NULL, +static struct preapproveinvoice_data* preapproveinvoice_data_init(struct payment *p) +{ + struct preapproveinvoice_data *d; + /* Only keep state on the root. We will use the root's flag + * for all payments. */ + if (p == payment_root(p)) { + d = tal(p, struct preapproveinvoice_data); + d->approved = false; + return d; + } else { + return NULL; + } +} + +REGISTER_PAYMENT_MODIFIER(check_preapproveinvoice, + struct preapproveinvoice_data *, + preapproveinvoice_data_init, check_preapproveinvoice_start); static struct route_exclusions_data * diff --git a/plugins/libplugin-pay.h b/plugins/libplugin-pay.h index 77783c6274b8..cefdeae91803 100644 --- a/plugins/libplugin-pay.h +++ b/plugins/libplugin-pay.h @@ -432,6 +432,10 @@ struct route_exclusions_data { struct route_exclusion **exclusions; }; +struct preapproveinvoice_data { + bool approved; +}; + /* List of globally available payment modifiers. */ REGISTER_PAYMENT_MODIFIER_HEADER(retry, struct retry_mod_data); REGISTER_PAYMENT_MODIFIER_HEADER(routehints, struct routehints_data); @@ -453,7 +457,7 @@ REGISTER_PAYMENT_MODIFIER_HEADER(local_channel_hints, void); * each of those channels can bear. */ REGISTER_PAYMENT_MODIFIER_HEADER(payee_incoming_limit, void); REGISTER_PAYMENT_MODIFIER_HEADER(route_exclusions, struct route_exclusions_data); -REGISTER_PAYMENT_MODIFIER_HEADER(check_preapproveinvoice, void); +REGISTER_PAYMENT_MODIFIER_HEADER(check_preapproveinvoice, struct preapproveinvoice_data); struct payment *payment_new(tal_t *ctx, struct command *cmd,