Skip to content

Commit fafda82

Browse files
committed
askrene: fix up our handling of htlc_max.
It seems we didn't handle it correctly: we need to cap the first segment as well as the others, as far as I can tell. Also, it can be less than the maximum capacity. Signed-off-by: Rusty Russell <[email protected]>
1 parent 975326a commit fafda82

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

plugins/askrene/mcf.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ static struct arc node_adjacency_next(
436436
return linear_network->node_adjacency_next_arc[arc.idx];
437437
}
438438

439+
/* Set *capacity to value, up to *cap_on_capacity. Reduce cap_on_capacity */
440+
static void set_capacity(s64 *capacity, u64 value, u64 *cap_on_capacity)
441+
{
442+
*capacity = MIN(value, *cap_on_capacity);
443+
*cap_on_capacity -= *capacity;
444+
}
445+
439446
// TODO(eduardo): unit test this
440447
/* Split a directed channel into parts with linear cost function. */
441448
static void linearize_channel(const struct pay_parameters *params,
@@ -457,13 +464,11 @@ static void linearize_channel(const struct pay_parameters *params,
457464
* that it does not exceed htlcmax. */
458465
u64 cap_on_capacity = fp16_to_u64(c->half[dir].htlc_max) / 1000;
459466

460-
capacity[0]=a;
467+
set_capacity(&capacity[0], a, &cap_on_capacity);
461468
cost[0]=0;
462469
for(size_t i=1;i<CHANNEL_PARTS;++i)
463470
{
464-
capacity[i] = MIN(params->cap_fraction[i]*(b-a), cap_on_capacity);
465-
assert(cap_on_capacity >= capacity[i]);
466-
cap_on_capacity -= capacity[i];
471+
set_capacity(&capacity[i], params->cap_fraction[i]*(b-a), &cap_on_capacity);
467472

468473
cost[i] = params->cost_fraction[i]
469474
*params->amount.millisatoshis /* Raw: linearize_channel */

0 commit comments

Comments
 (0)