@@ -263,6 +263,56 @@ static const char *fmt_route(const tal_t *ctx,
263263 return str ;
264264}
265265
266+ static const char * fmt_flow_full (const tal_t * ctx ,
267+ const struct route_query * rq ,
268+ const struct flow * flow ,
269+ struct amount_msat total_delivered ,
270+ double delay_feefactor )
271+ {
272+ struct amount_msat amt = flow -> delivers ;
273+ char * str = tal_fmt (ctx , "%s (linear cost %s)" ,
274+ fmt_amount_msat (tmpctx , amt ),
275+ fmt_amount_msat (tmpctx , linear_flow_cost (flow ,
276+ total_delivered ,
277+ delay_feefactor )));
278+
279+ for (int i = tal_count (flow -> path ) - 1 ; i >= 0 ; i -- ) {
280+ struct short_channel_id_dir scidd ;
281+ struct amount_msat min , max ;
282+ scidd .scid = gossmap_chan_scid (rq -> gossmap , flow -> path [i ]);
283+ scidd .dir = flow -> dirs [i ];
284+ if (!amount_msat_add_fee (& amt ,
285+ flow -> path [i ]-> half [scidd .dir ].base_fee ,
286+ flow -> path [i ]-> half [scidd .dir ].proportional_fee ))
287+ abort ();
288+ get_constraints (rq , flow -> path [i ], scidd .dir , & min , & max );
289+ tal_append_fmt (& str , " <- %s %s (cap=%s,fee=%u+%u,delay=%u)" ,
290+ fmt_amount_msat (tmpctx , amt ),
291+ fmt_short_channel_id_dir (tmpctx , & scidd ),
292+ fmt_amount_msat (tmpctx , max ),
293+ flow -> path [i ]-> half [scidd .dir ].base_fee ,
294+ flow -> path [i ]-> half [scidd .dir ].proportional_fee ,
295+ flow -> path [i ]-> half [scidd .dir ].delay );
296+ }
297+ return str ;
298+ }
299+
300+ static struct amount_msat linear_flows_cost (struct flow * * flows ,
301+ struct amount_msat total_amount ,
302+ double delay_feefactor )
303+ {
304+ struct amount_msat total = AMOUNT_MSAT (0 );
305+
306+ for (size_t i = 0 ; i < tal_count (flows ); i ++ ) {
307+ if (!amount_msat_accumulate (& total ,
308+ linear_flow_cost (flows [i ],
309+ total_amount ,
310+ delay_feefactor )))
311+ abort ();
312+ }
313+ return total ;
314+ }
315+
266316/* Returns an error message, or sets *routes */
267317static const char * get_routes (const tal_t * ctx ,
268318 struct command * cmd ,
@@ -383,19 +433,46 @@ static const char *get_routes(const tal_t *ctx,
383433 /* Too expensive? */
384434too_expensive :
385435 while (amount_msat_greater (flowset_fee (rq -> plugin , flows ), maxfee )) {
436+ struct flow * * new_flows ;
437+
386438 mu += 10 ;
387439 rq_log (tmpctx , rq , LOG_UNUSUAL ,
388440 "The flows had a fee of %s, greater than max of %s, retrying with mu of %u%%..." ,
389441 fmt_amount_msat (tmpctx , flowset_fee (rq -> plugin , flows )),
390442 fmt_amount_msat (tmpctx , maxfee ),
391443 mu );
392- flows = minflow (rq , rq , srcnode , dstnode , amount ,
393- mu > 100 ? 100 : mu , delay_feefactor );
444+ new_flows = minflow (rq , rq , srcnode , dstnode , amount ,
445+ mu > 100 ? 100 : mu , delay_feefactor );
394446 if (!flows || mu >= 100 ) {
395447 ret = rq_log (ctx , rq , LOG_UNUSUAL ,
396448 "Could not find route without excessive cost" );
397449 goto fail ;
398450 }
451+
452+ /* This is possible, because MCF's linear fees are not the same. */
453+ if (amount_msat_greater (flowset_fee (rq -> plugin , new_flows ),
454+ flowset_fee (rq -> plugin , flows ))) {
455+ struct amount_msat old_cost = linear_flows_cost (flows , amount , delay_feefactor );
456+ struct amount_msat new_cost = linear_flows_cost (new_flows , amount , delay_feefactor );
457+ if (amount_msat_greater_eq (new_cost , old_cost )) {
458+ rq_log (tmpctx , rq , LOG_BROKEN , "Old flows cost %s:" ,
459+ fmt_amount_msat (tmpctx , old_cost ));
460+ for (size_t i = 0 ; i < tal_count (flows ); i ++ ) {
461+ rq_log (tmpctx , rq , LOG_BROKEN ,
462+ "Flow %zu/%zu: %s" , i , tal_count (flows ),
463+ fmt_flow_full (tmpctx , rq , flows [i ], amount , delay_feefactor ));
464+ }
465+ rq_log (tmpctx , rq , LOG_BROKEN , "Old flows cost %s:" ,
466+ fmt_amount_msat (tmpctx , new_cost ));
467+ for (size_t i = 0 ; i < tal_count (new_flows ); i ++ ) {
468+ rq_log (tmpctx , rq , LOG_BROKEN ,
469+ "Flow %zu/%zu: %s" , i , tal_count (new_flows ),
470+ fmt_flow_full (tmpctx , rq , new_flows [i ], amount , delay_feefactor ));
471+ }
472+ }
473+ }
474+ tal_free (flows );
475+ flows = new_flows ;
399476 }
400477
401478 if (finalcltv + flows_worst_delay (flows ) > 2016 ) {
0 commit comments