Skip to content

Commit b298e3f

Browse files
Lagrang3rustyrussell
authored andcommitted
askrene: refine: add helper function remove_flows
remove_flows is a helper function to remove flows from a set so that we keep the number of flows limited. Signed-off-by: Lagrang3 <[email protected]>
1 parent 3dd0979 commit b298e3f

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

plugins/askrene/refine.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,3 +632,30 @@ double flows_probability(const tal_t *ctx, struct route_query *rq,
632632
tal_free(working_ctx);
633633
return probability;
634634
}
635+
636+
/* Compare flows by deliver amount */
637+
static int reverse_cmp_flows(struct flow *const *fa, struct flow *const *fb,
638+
void *unused UNUSED)
639+
{
640+
if (amount_msat_eq((*fa)->delivers, (*fb)->delivers))
641+
return 0;
642+
if (amount_msat_greater((*fa)->delivers, (*fb)->delivers))
643+
return -1;
644+
return 1;
645+
}
646+
647+
bool remove_flows(struct flow ***flows, u32 n)
648+
{
649+
if (n == 0)
650+
goto fail;
651+
if (n > tal_count(*flows))
652+
goto fail;
653+
asort(*flows, tal_count(*flows), reverse_cmp_flows, NULL);
654+
for (size_t count = tal_count(*flows); n > 0; n--, count--) {
655+
assert(count > 0);
656+
tal_arr_remove(flows, count - 1);
657+
}
658+
return true;
659+
fail:
660+
return false;
661+
}

plugins/askrene/refine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ void squash_flows(const tal_t *ctx, struct route_query *rq,
3333

3434
double flows_probability(const tal_t *ctx, struct route_query *rq,
3535
struct flow ***flows);
36+
37+
/* Helper function: removes n flows from the set. It will remove those flows
38+
* with the lowest amount values. */
39+
bool remove_flows(struct flow ***flows, u32 n);
3640
#endif /* LIGHTNING_PLUGINS_ASKRENE_REFINE_H */

0 commit comments

Comments
 (0)