Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions plugins/askrene/refine.c
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,30 @@ double flows_probability(const tal_t *ctx, struct route_query *rq,
tal_free(working_ctx);
return probability;
}

/* Compare flows by deliver amount */
static int reverse_cmp_flows(struct flow *const *fa, struct flow *const *fb,
void *unused UNUSED)
{
if (amount_msat_eq((*fa)->delivers, (*fb)->delivers))
return 0;
if (amount_msat_greater((*fa)->delivers, (*fb)->delivers))
return -1;
return 1;
}

bool remove_flows(struct flow ***flows, u32 n)
{
if (n == 0)
goto fail;
if (n > tal_count(*flows))
goto fail;
asort(*flows, tal_count(*flows), reverse_cmp_flows, NULL);
for (size_t count = tal_count(*flows); n > 0; n--, count--) {
assert(count > 0);
tal_arr_remove(flows, count - 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't free the flow when you removed it? I think you'll want this helper later, so:

static void remove_and_free_flow(struct flow ***flows, size_t i)
{
tal_free((*flows)[i]);
tal_arr_remove(flows, i);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But in this case it doesn't leak, so we can leave this neatning until later.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Removal was handled in the first version but I forgot after the changes.
Will do.

}
return true;
fail:
return false;
}
4 changes: 4 additions & 0 deletions plugins/askrene/refine.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ void squash_flows(const tal_t *ctx, struct route_query *rq,

double flows_probability(const tal_t *ctx, struct route_query *rq,
struct flow ***flows);

/* Helper function: removes n flows from the set. It will remove those flows
* with the lowest amount values. */
bool remove_flows(struct flow ***flows, u32 n);
#endif /* LIGHTNING_PLUGINS_ASKRENE_REFINE_H */