From deced56344e6866bdfde7a46170c5ee1d1405e7c Mon Sep 17 00:00:00 2001 From: ZmnSCPxj jxPCSnmZ Date: Thu, 13 Aug 2020 18:26:40 +0800 Subject: [PATCH] plugins/libplugin-pay.c: Add facility to have paymods request lowering of the estimated max HTLCs. --- plugins/libplugin-pay.c | 42 +++++++++++++++++++++++++++++++++++++++++ plugins/libplugin-pay.h | 10 ++++++++++ 2 files changed, 52 insertions(+) diff --git a/plugins/libplugin-pay.c b/plugins/libplugin-pay.c index 76c91c506..0723b686b 100644 --- a/plugins/libplugin-pay.c +++ b/plugins/libplugin-pay.c @@ -39,6 +39,7 @@ struct payment *payment_new(tal_t *ctx, struct command *cmd, p->failroute_retry = false; p->bolt11 = NULL; p->routetxt = NULL; + p->max_htlcs = UINT32_MAX; /* Copy over the relevant pieces of information. */ if (parent != NULL) { @@ -2908,6 +2909,7 @@ static struct presplit_mod_data *presplit_mod_data_init(struct payment *p) static u32 payment_max_htlcs(const struct payment *p) { + const struct payment *root; struct channel_hint *h; u32 res = 0; for (size_t i = 0; i < tal_count(p->channel_hints); i++) { @@ -2915,9 +2917,49 @@ static u32 payment_max_htlcs(const struct payment *p) if (h->local && h->enabled) res += h->htlc_budget; } + root = p; + while (root->parent) + root = root->parent; + if (res > root->max_htlcs) + res = root->max_htlcs; return res; } +/* Temporary comment out else GCC will complain that it is unused; + * will be used in a later commit. */ +#if 0 +/** payment_lower_max_htlcs + * + * @brief indicates that we have a good reason to believe that + * we should limit our number of max HTLCs. + * + * @desc Causes future payment_max_htlcs to have a maximum value + * they return. + * Can be called by multiple paymods: the lowest one any paymod + * has given will be used. + * If this is called with a limit higher than the existing limit, + * it just successfully returns without doing anything. + * + * @param p - a payment on the payment tree we should limit. + * @param limit - the number of max HTLCs. + * @param why - the reason we think the given max HTLCs is + * reasonable. + */ +static void payment_lower_max_htlcs(struct payment *p, u32 limit, + const char *why) +{ + struct payment *root = payment_root(p); + if (root->max_htlcs > limit) { + paymod_log(p, LOG_INFORM, + "%s limit on max HTLCs: %"PRIu32", %s", + root->max_htlcs == UINT32_MAX ? + "Initial" : "Lowering", + limit, why); + root->max_htlcs = limit; + } +} +#endif + static bool payment_supports_mpp(struct payment *p) { if (p->invoice == NULL || p->invoice->features == NULL) diff --git a/plugins/libplugin-pay.h b/plugins/libplugin-pay.h index 534b18808..623e59fd4 100644 --- a/plugins/libplugin-pay.h +++ b/plugins/libplugin-pay.h @@ -270,6 +270,16 @@ struct payment { /* A short description of the route of this payment. */ char *routetxt; + + /* The maximum number of parallel outgoing HTLCs we will allow. + * If unset, the maximum is based on the number of outgoing HTLCs. + * This only applies for the root payment, and is ignored on non-root + * payments. + * Clients of the paymod system MUST NOT modify it, and individual + * paymods MUST interact with it only via the payment_max_htlcs + * and payment_lower_max_htlcs functions. + */ + u32 max_htlcs; }; struct payment_modifier {