askrene: refine: add helper function remove_flows
What changed, and why it matters
This commit adds a small internal helper function called remove_flows to the askrene routing-refinement plugin. It simply trims the lowest-value payment routes from a list to keep the number of candidate routes manageable. There is no indication this change fixes a security bug or introduces a vulnerability.
No security action required. Treat as routine code refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces remove_flows(struct flow ***flows, u32 n) in plugins/askrene/refine.c/.h. It sorts an array of flow pointers by descending delivered amount and removes the n lowest-delivering entries using tal_arr_remove. The function returns false for invalid n (0 or greater than array length) and true otherwise. It is a pure code-organization/refactoring addition with no callers shown in the diff and no security-relevant logic changes.
Changed components
plugins/askrene/refine.cplugins/askrene/refine.hInspect captured patch +31 / −0
diff --git a/plugins/askrene/refine.c b/plugins/askrene/refine.c
index ee57628b..5335a9df 100644
--- a/plugins/askrene/refine.c
+++ b/plugins/askrene/refine.c
@@ -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);
+ }
+ return true;
+fail:
+ return false;
+}
diff --git a/plugins/askrene/refine.h b/plugins/askrene/refine.h
index 065cd1b2..c0d60109 100644
--- a/plugins/askrene/refine.h
+++ b/plugins/askrene/refine.h
@@ -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 */
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.