@@ -1377,34 +1377,14 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
13771377 struct reserve_hop * reservations = new_reservations (working_ctx , rq );
13781378
13791379 while (!amount_msat_is_zero (amount_to_deliver )) {
1380- size_t num_parts , parts_slots , excess_parts ;
1381- u32 bottleneck_idx ;
1382-
13831380 if (timemono_after (time_mono (), deadline )) {
13841381 error_message = rq_log (ctx , rq , LOG_BROKEN ,
13851382 "%s: timed out after deadline" ,
13861383 __func__ );
13871384 goto fail ;
13881385 }
13891386
1390- /* FIXME: This algorithm to limit the number of parts is dumb
1391- * for two reasons:
1392- * 1. it does not take into account that several loop
1393- * iterations here may produce two flows along the same
1394- * path that after "squash_flows" become a single flow.
1395- * 2. limiting the number of "slots" to 1 makes us fail to
1396- * see some solutions that use more than one of those
1397- * existing paths.
1398- *
1399- * A better approach could be to run MCF, remove the excess
1400- * paths and then recompute a MCF on a network where each arc is
1401- * one of the previously remaining paths, ie. redistributing the
1402- * payment amount among the selected paths in a cost-efficient
1403- * way. */
14041387 new_flows = tal_free (new_flows );
1405- num_parts = tal_count (* flows );
1406- assert (num_parts < rq -> maxparts );
1407- parts_slots = rq -> maxparts - num_parts ;
14081388
14091389 /* If the amount_to_deliver is very small we better use a single
14101390 * path computation because:
@@ -1415,8 +1395,7 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
14151395 * do not deliver the entire payment amount by just a small
14161396 * amount. */
14171397 if (amount_msat_less_eq (amount_to_deliver ,
1418- SINGLE_PATH_THRESHOLD ) ||
1419- parts_slots == 1 ) {
1398+ SINGLE_PATH_THRESHOLD )) {
14201399 new_flows = single_path_flow (working_ctx , rq , srcnode ,
14211400 dstnode , amount_to_deliver ,
14221401 mu , delay_feefactor );
@@ -1433,7 +1412,7 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
14331412 }
14341413
14351414 error_message =
1436- refine_flows (ctx , rq , amount_to_deliver , & new_flows , & bottleneck_idx );
1415+ refine_flows (ctx , rq , amount_to_deliver , & new_flows , NULL );
14371416 if (error_message )
14381417 goto fail ;
14391418
@@ -1457,32 +1436,6 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
14571436 assert (!amount_msat_is_zero (new_flows [i ]-> delivers ));
14581437 }
14591438
1460- if (tal_count (new_flows ) > parts_slots ) {
1461- /* Remove the excees of parts and leave one slot for the
1462- * next round of computations. */
1463- excess_parts = 1 + tal_count (new_flows ) - parts_slots ;
1464- } else if (tal_count (new_flows ) == parts_slots &&
1465- amount_msat_less (all_deliver , amount_to_deliver )) {
1466- /* Leave exactly 1 slot for the next round of
1467- * computations. */
1468- excess_parts = 1 ;
1469- } else
1470- excess_parts = 0 ;
1471- if (excess_parts > 0 ) {
1472- /* If we removed all the flows we found, avoid selecting them again,
1473- * by disabling one. */
1474- if (excess_parts == tal_count (new_flows ))
1475- bitmap_set_bit (rq -> disabled_chans , bottleneck_idx );
1476- if (!remove_flows (& new_flows , excess_parts )) {
1477- error_message = rq_log (ctx , rq , LOG_BROKEN ,
1478- "%s: failed to remove %zu"
1479- " flows from a set of %zu" ,
1480- __func__ , excess_parts ,
1481- tal_count (new_flows ));
1482- goto fail ;
1483- }
1484- }
1485-
14861439 /* Is this set of flows too expensive?
14871440 * We can check if the new flows are within the fee budget,
14881441 * however in some cases we have discarded some flows at this
@@ -1606,6 +1559,34 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
16061559 /* all set! Now squash flows that use the same path */
16071560 squash_flows (ctx , rq , flows );
16081561
1562+ /* If we're over the number of parts, try to cram excess into the
1563+ * largest-capacity parts */
1564+ if (tal_count (* flows ) > rq -> maxparts ) {
1565+ struct amount_msat fee ;
1566+
1567+ error_message = reduce_num_flows (rq , rq , flows , amount , rq -> maxparts );
1568+ if (error_message ) {
1569+ * flows = tal_free (* flows );
1570+ return error_message ;
1571+ }
1572+
1573+ /* Check fee budget! */
1574+ fee = flowset_fee (rq -> plugin , * flows );
1575+ if (amount_msat_greater (fee , maxfee )) {
1576+ error_message = rq_log (rq , rq , LOG_INFORM ,
1577+ "After reducing the flows to %zu (i.e. maxparts),"
1578+ " we had a fee of %s, greater than "
1579+ "max of %s." ,
1580+ tal_count (* flows ),
1581+ fmt_amount_msat (tmpctx , fee ),
1582+ fmt_amount_msat (tmpctx , maxfee ));
1583+ if (error_message ) {
1584+ * flows = tal_free (* flows );
1585+ return error_message ;
1586+ }
1587+ }
1588+ }
1589+
16091590 /* flows_probability re-does a temporary reservation so we need to call
16101591 * it after we have cleaned the reservations we used to build the flows
16111592 * hence after we freed working_ctx. */
0 commit comments