Skip to content

Commit 00ccf25

Browse files
committed
xpay: add developer option dev_maxparts
dev_maxparts limits the number of pending routes allowed at any given time. Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent 9d260d4 commit 00ccf25

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

plugins/xpay/xpay.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ struct payment {
7878
struct amount_msat maxfee;
7979
/* Maximum delay on the route we're ok with */
8080
u32 *maxdelay;
81+
/* Maximum number of payment routes that can be pending. */
82+
u32 *maxparts;
8183
/* Do we have to do it all in a single part? */
8284
bool disable_mpp;
8385
/* BOLT11 payment secret (NULL for BOLT12, it uses blinded paths) */
@@ -113,6 +115,9 @@ struct payment {
113115
* call outstanding). */
114116
struct amount_msat amount_being_routed;
115117

118+
/* Number of routes attempted and pending */
119+
u32 pending_attempts;
120+
116121
/* Useful information from prior attempts if any. */
117122
char *prior_results;
118123

@@ -334,6 +339,15 @@ static u32 initial_cltv_delta(const struct attempt *attempt)
334339
return attempt->hops[0].cltv_value_in;
335340
}
336341

342+
/* Find the total number of pending attempts */
343+
static size_t count_current_attempts(const struct payment *payment)
344+
{
345+
const struct attempt *i;
346+
size_t result = 0;
347+
list_for_each(&payment->current_attempts, i, list) { result++; }
348+
return result;
349+
}
350+
337351
/* We total up all attempts which succeeded in the past (if we're not
338352
* in slow mode, that's only the one which just succeeded), and then we
339353
* assume any others currently-in-flight will also succeed. */
@@ -1296,6 +1310,7 @@ static struct command_result *getroutes_for(struct command *aux_cmd,
12961310
struct out_req *req;
12971311
const struct pubkey *dst;
12981312
struct amount_msat maxfee;
1313+
size_t count_pending;
12991314

13001315
/* I would normally assert here, but we have reports of this happening... */
13011316
if (amount_msat_is_zero(deliver)) {
@@ -1356,6 +1371,9 @@ static struct command_result *getroutes_for(struct command *aux_cmd,
13561371
json_add_amount_msat(req->js, "maxfee_msat", maxfee);
13571372
json_add_u32(req->js, "final_cltv", payment->final_cltv);
13581373
json_add_u32(req->js, "maxdelay", *payment->maxdelay);
1374+
count_pending = count_current_attempts(payment);
1375+
assert(*payment->maxparts > count_pending);
1376+
json_add_u32(req->js, "maxparts", *payment->maxparts - count_pending);
13591377

13601378
return send_payment_req(aux_cmd, payment, req);
13611379
}
@@ -1646,14 +1664,19 @@ static struct command_result *json_xpay_core(struct command *cmd,
16461664
p_opt_def("retry_for", param_number, &retryfor, 60),
16471665
p_opt("partial_msat", param_msat, &partial),
16481666
p_opt_def("maxdelay", param_u32, &payment->maxdelay, 2016),
1667+
p_opt_dev("dev_maxparts", param_u32, &payment->maxparts, 100),
16491668
NULL))
16501669
return command_param_failed();
1670+
if (*payment->maxparts == 0)
1671+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1672+
"maxparts cannot be zero");
16511673

16521674
list_head_init(&payment->current_attempts);
16531675
list_head_init(&payment->past_attempts);
16541676
payment->plugin = cmd->plugin;
16551677
payment->cmd = cmd;
16561678
payment->amount_being_routed = AMOUNT_MSAT(0);
1679+
payment->pending_attempts = 0;
16571680
payment->group_id = pseudorand(INT64_MAX);
16581681
payment->total_num_attempts = payment->num_failures = 0;
16591682
payment->requests = tal_arr(payment, struct out_req *, 0);

0 commit comments

Comments
 (0)