Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 3 additions & 45 deletions plugins/askrene/askrene.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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! */
Expand Down
4 changes: 3 additions & 1 deletion plugins/askrene/child/mcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,9 @@ static void init_linear_network_single_path(
if (amount_msat_less(params->amount,
gossmap_chan_htlc_min(c, half)) ||
amount_msat_greater(params->amount,
gossmap_chan_htlc_max(c, half)))
gossmap_chan_htlc_max(c, half)) ||
amount_msat_greater(params->amount,
gossmap_chan_get_capacity(gossmap, c)))
continue;

get_constraints(params->rq, c, half, &mincap, &maxcap);
Expand Down
4 changes: 4 additions & 0 deletions plugins/askrene/child/route_query.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ void get_constraints(const struct route_query *rq,
for (size_t i = 0; i < tal_count(rq->layers); i++)
layer_apply_constraints(rq->layers[i], &scidd, min, max);

/* Impressions in the opposite direction can increase max, but liquidity
* can never exceed the channel's actual capacity. */
*max = amount_msat_min(*max, gossmap_chan_get_capacity(rq->gossmap, chan));

/* Finally, if any is in use, subtract that! */
reserve_sub(rq->reserved, &scidd, rq->layers, min);
reserve_sub(rq->reserved, &scidd, rq->layers, max);
Expand Down
89 changes: 85 additions & 4 deletions tests/test_askrene.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import pytest
import random
import re
import subprocess
import time
import tempfile
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down Expand Up @@ -1011,6 +1010,22 @@ def test_getroutes_single_path(node_factory):
maxparts=1,
)

# Even if an impression in the opposite direction attempts to inflate the
# liquidity bound, the channel cannot exceed its raw on-chain capacity.
l1.rpc.askrene_create_layer("imp_layer")
# Add an impression in the reverse direction 2->1 on channel 3x2x2
l1.rpc.askrene_inform_channel("imp_layer", "3x2x2/0", 10000000, "succeeded")
with pytest.raises(RpcError, match=r"isn't big enough to carry 10000001msat"):
l1.rpc.getroutes(
source=nodemap[1],
destination=nodemap[2],
amount_msat=10000001,
layers=["imp_layer"],
maxparts=1,
maxfee_msat=1000,
final_cltv=99,
)


def test_getroutes_fee_fallback(node_factory):
"""Test getroutes call takes into account fees, if excessive"""
Expand Down Expand Up @@ -3490,3 +3505,69 @@ 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)


@pytest.mark.slow_test
def test_single_part_setup_time(node_factory):
"""getroutes with maxparts=1 on real gossip: time of the whole call
against the search time askrene logs for it (issue #9739)."""
src_node = (
'033845802d25b4e074ccfd7cd8b339a41dc75bf9978a034800444b51d42b07799a'
)
node_key = f'--node-map=2134={src_node}'
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 = node_factory.get_node(
allow_warning=True,
broken_log="Throttling (incoming|outgoing) peer",
gossip_store_file=outfile.name,
options={
'dev-throttle-gossip': None,
'askrene-timeout': 600,
},
)
for amount_sat in (10_000, 100_000, 500_000, 2_000_000):
times = {}
for maxparts in (None, 1):
l1.daemon.logs_catchup()
start = len(l1.daemon.logs)
args = dict(
source=src_node,
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
times[maxparts] = 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
# Call time minus search time should be small (< 1s setup overhead)
assert times[maxparts] - (searches[-1] / 1000.0) < 1.0

# maxparts=1 setup time should not blow up quadratically compared to default
assert times[1] < times[None] * 5 + 1