@@ -1375,7 +1375,27 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
13751375 struct reserve_hop * reservations = new_reservations (working_ctx , rq );
13761376
13771377 while (!amount_msat_is_zero (amount_to_deliver )) {
1378- new_flows = tal_free (new_flows );
1378+ u32 num_parts , parts_slots , excess_parts ;
1379+
1380+ /* FIXME: This algorithm to limit the number of parts is dumb
1381+ * for two reasons:
1382+ * 1. it does not take into account that several loop
1383+ * iterations here may produce two flows along the same
1384+ * path that after "squash_flows" become a single flow.
1385+ * 2. limiting the number of "slots" to 1 makes us fail to
1386+ * see some solutions that use more than one of those
1387+ * existing paths.
1388+ *
1389+ * A better approach could be to run MCF, remove the excess
1390+ * paths and then recompute a MCF on a network where each arc is
1391+ * one of the previously remaining paths, ie. redistributing the
1392+ * payment amount among the selected paths in a cost-efficient
1393+ * way. */
1394+ new_flows = tal_free (new_flows );
1395+ num_parts = tal_count (* flows );
1396+ assert (num_parts < rq -> maxparts );
1397+ parts_slots = rq -> maxparts - num_parts ;
1398+ excess_parts = 0 ;
13791399
13801400 /* If the amount_to_deliver is very small we better use a single
13811401 * path computation because:
@@ -1385,9 +1405,12 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
13851405 * refine_with_fees_and_limits we might have a set of flows that
13861406 * do not deliver the entire payment amount by just a small
13871407 * amount. */
1388- if (amount_msat_less_eq (amount_to_deliver , SINGLE_PATH_THRESHOLD )){
1389- new_flows = single_path_flow (working_ctx , rq , srcnode , dstnode ,
1390- amount_to_deliver , mu , delay_feefactor );
1408+ if (amount_msat_less_eq (amount_to_deliver ,
1409+ SINGLE_PATH_THRESHOLD ) ||
1410+ parts_slots == 1 ) {
1411+ new_flows = single_path_flow (working_ctx , rq , srcnode ,
1412+ dstnode , amount_to_deliver ,
1413+ mu , delay_feefactor );
13911414 } else {
13921415 new_flows =
13931416 solver (working_ctx , rq , srcnode , dstnode ,
@@ -1425,6 +1448,26 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
14251448 assert (!amount_msat_is_zero (new_flows [i ]-> delivers ));
14261449 }
14271450
1451+ if (tal_count (new_flows ) > parts_slots ) {
1452+ /* Remove the excees of parts and leave one slot for the
1453+ * next round of computations. */
1454+ excess_parts = 1 + tal_count (new_flows ) - parts_slots ;
1455+ } else if (tal_count (new_flows ) == parts_slots &&
1456+ amount_msat_less (all_deliver , amount_to_deliver )) {
1457+ /* Leave exactly 1 slot for the next round of
1458+ * computations. */
1459+ excess_parts = 1 ;
1460+ }
1461+ if (excess_parts > 0 &&
1462+ !remove_flows (ctx , & new_flows , excess_parts )) {
1463+ error_message = rq_log (ctx , rq , LOG_BROKEN ,
1464+ "%s: failed to remove %" PRIu32
1465+ " flows from a set of %zu" ,
1466+ __func__ , excess_parts ,
1467+ tal_count (new_flows ));
1468+ goto fail ;
1469+ }
1470+
14281471 /* Is this set of flows too expensive?
14291472 * We can check if the new flows are within the fee budget,
14301473 * however in some cases we have discarded some flows at this
@@ -1570,6 +1613,16 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
15701613 * flows = tal_free (* flows );
15711614 goto fail ;
15721615 }
1616+ if (tal_count (* flows ) > rq -> maxparts ) {
1617+ error_message = rq_log (
1618+ rq , rq , LOG_BROKEN ,
1619+ "%s: the number of flows (%zu) exceeds the limit set "
1620+ "on payment parts (%" PRIu32
1621+ "), please submit a bug report" ,
1622+ __func__ , tal_count (* flows ), rq -> maxparts );
1623+ * flows = tal_free (* flows );
1624+ goto fail ;
1625+ }
15731626
15741627 return NULL ;
15751628fail :
0 commit comments