From 99003a27dedcf6687e72ea173da2f290f4941737 Mon Sep 17 00:00:00 2001 From: decodekush Date: Thu, 19 Nov 2026 17:24:08 +0530 Subject: [PATCH] askrene: skip redundant small-channel layer for single-path routing When maxparts == 1, do_getroutes() previously built and applied the temporary layer auto.remove_small_channels, adding disabling updates for every channel whose capacity was below the payment amount. On a typical network graph with tens of thousands of channels, applying this layer triggered an O(N^2) scan in gossmap_local_updatechan() via find_localmod(), blocking askrene's main event loop for seconds to tens of seconds before the search even began. The single-path Dijkstra solver (init_linear_network_single_path) already excludes channels with insufficient capacity during graph initialization in O(1) time per channel. The small-channel layer was an artifact of the original MCF solver (#8059) before the single-path solver existed (#8299). Remove remove_small_channel_layer and avoid constructing this layer. Also update test_getroutes_single_path failure assertions now that askrene accurately reports capacity shortfall rather than an artificial layer disablement, and add a regression test verifying sub-second getroutes setup with maxparts=1 on real gossip data. Fixes: #9739 Changelog-Fixed: askrene: eliminate quadratic setup lag on single-path getroutes calls Signed-off-by: decodekush --- plugins/askrene/askrene.c | 48 ++----------------------- tests/test_askrene.py | 74 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 73 insertions(+), 49 deletions(-) diff --git a/plugins/askrene/askrene.c b/plugins/askrene/askrene.c index a6bf7e277ec3..c488142697c5 100644 --- a/plugins/askrene/askrene.c +++ b/plugins/askrene/askrene.c @@ -257,44 +257,6 @@ static struct layer *source_free_layer(const tal_t *ctx, return layer; } -/* We're going to abuse MCF, and take the largest flow it gives and ram everything - * through it. This is more effective if there's at least a *chance* that can handle - * the full amount. - * - * It's far from perfect, but I have very little sympathy: if you want - * to receive amounts reliably, enable MPP. - */ -static struct layer *remove_small_channel_layer(const tal_t *ctx, - struct askrene *askrene, - struct amount_msat min_amount, - struct gossmap_localmods *localmods) -{ - /* We use the prefix auto. to avoid clashing */ - struct layer *layer = new_temp_layer(ctx, askrene, "auto.remove_small_channels"); - struct gossmap *gossmap = askrene->gossmap; - struct gossmap_chan *c; - - /* We apply this so we see any created channels */ - gossmap_apply_localmods(gossmap, localmods); - - for (c = gossmap_first_chan(gossmap); c; c = gossmap_next_chan(gossmap, c)) { - struct short_channel_id_dir scidd; - if (amount_msat_greater_eq(gossmap_chan_get_capacity(gossmap, c), - min_amount)) - continue; - - scidd.scid = gossmap_chan_scid(gossmap, c); - /* Layer will disable this in both directions */ - for (scidd.dir = 0; scidd.dir < 2; scidd.dir++) { - const bool enabled = false; - layer_add_update_channel(layer, &scidd, &enabled, - NULL, NULL, NULL, NULL, NULL); - } - } - gossmap_remove_localmods(gossmap, localmods); - - return layer; -} PRINTF_FMT(4, 5) static const char *cmd_log(const tal_t *ctx, @@ -604,13 +566,9 @@ static struct command_result *do_getroutes(struct command *cmd, info->layers, info->local_layer, capacities); - /* no parallel payments means we can drop any smaller channels */ - if (info->maxparts == 1) { - add_layer(&layers, - remove_small_channel_layer(layers, askrene, info->amount, localmods), - askrene->gossmap, - localmods, capacities); - } + /* Note: For maxparts == 1, single-path routing (init_linear_network_single_path) + * natively skips channels with insufficient capacity in O(1) time without + * building an artificial disabling layer (see issue #9739). */ /* Clear scids with reservations, too, so we don't have to look up * all the time! */ diff --git a/tests/test_askrene.py b/tests/test_askrene.py index fc8515256e2c..3849fb18d0f3 100644 --- a/tests/test_askrene.py +++ b/tests/test_askrene.py @@ -9,6 +9,7 @@ import os import pytest import random +import re import subprocess import time import tempfile @@ -941,8 +942,7 @@ def test_getroutes_single_path(node_factory): # To be able to route this amount two parts are needed, therefore a single # pay search will fail. - # FIXME: the explanation for the failure is wrong - with pytest.raises(RpcError): + with pytest.raises(RpcError, match=r"isn't big enough to carry 10000001msat"): l1.rpc.getroutes( source=nodemap[1], destination=nodemap[2], @@ -974,8 +974,7 @@ def test_getroutes_single_path(node_factory): # To be able to route this amount two parts are needed, therefore a single # pay search will fail. - # FIXME: the explanation for the failure is wrong - with pytest.raises(RpcError): + with pytest.raises(RpcError, match=r"isn't big enough to carry 10000001msat"): l1.rpc.getroutes( source=nodemap[0], destination=nodemap[2], @@ -3490,3 +3489,70 @@ def test_flow_fee_adjustment_fallback(node_factory, bitcoind): ) msg = "Could not find routes with enough capacity, retrying without a flow_fee_adjustment" assert l1.daemon.is_in_log(msg, start=0) + + +def test_single_part_setup_time(node_factory, bitcoind): + """getroutes with maxparts=1 on real gossip: time of the whole call + against the search time askrene logs for it (issue #9739).""" + node_key = ( + '--node-map=2134=' + '033845802d25b4e074ccfd7cd8b339a41dc75bf9978a034800444b51d42b07799a' + ) + outfile = tempfile.NamedTemporaryFile(prefix='gossip-store-') + nodeids = subprocess.check_output( + [ + 'devtools/gossmap-compress', + 'decompress', + node_key, + 'tests/data/gossip-store-2026-02-03.compressed', + outfile.name, + ] + ).decode('utf-8').splitlines() + l1, l2 = node_factory.line_graph( + 2, + fundamount=100000000, + opts=[ + { + 'gossip_store_file': outfile.name, + 'allow_warning': True, + 'dev-throttle-gossip': None, + 'broken_log': '.*', + 'askrene-timeout': 600, + }, + {'allow_warning': True}, + ], + ) + for amount_sat in (10_000, 100_000, 500_000, 2_000_000): + for maxparts in (None, 1): + l1.daemon.logs_catchup() + start = len(l1.daemon.logs) + args = dict( + source=l1.info['id'], + destination=nodeids[0], + amount_msat=amount_sat * 1000, + layers=['auto.sourcefree', 'auto.localchans'], + maxfee_msat=amount_sat * 10, + final_cltv=18, + ) + if maxparts is not None: + args['maxparts'] = maxparts + t0 = time.time() + try: + l1.rpc.getroutes(**args) + except RpcError: + pass + call = time.time() - t0 + l1.daemon.logs_catchup() + pattern = ( + r'get_routes (?:failed after|completed in) (\d+) ms' + ) + searches = [ + int(m.group(1)) + for line in l1.daemon.logs[start:] + for m in [re.search(pattern, line)] + if m + ] + assert len(searches) > 0 + # Ensure maxparts=1 call doesn't stall with quadratic lag: + # whole call should complete well under 2 seconds. + assert call < 2.0