Skip to content

Askrene: fix constraints #8358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
7 changes: 7 additions & 0 deletions plugins/askrene/askrene.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,13 @@ static struct command_result *do_getroutes(struct command *cmd,
/* we temporarily apply localmods */
gossmap_apply_localmods(askrene->gossmap, localmods);

/* I want to be able to disable channels while working on this query.
* Layers are for user interaction and cannot be used for this purpose.
*/
rq->disabled_chans =
tal_arrz(rq, bitmap,
2 * BITMAP_NWORDS(gossmap_max_chan_idx(askrene->gossmap)));

/* localmods can add channels, so we need to allocate biases array
* *afterwards* */
rq->biases =
Expand Down
4 changes: 4 additions & 0 deletions plugins/askrene/askrene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIGHTNING_PLUGINS_ASKRENE_ASKRENE_H
#include "config.h"
#include <bitcoin/short_channel_id.h>
#include <ccan/bitmap/bitmap.h>
#include <ccan/htable/htable_type.h>
#include <ccan/list/list.h>
#include <common/amount.h>
Expand Down Expand Up @@ -60,6 +61,9 @@ struct route_query {

/* Additional per-htlc cost for local channels */
const struct additional_cost_htable *additional_costs;

/* channels we disable during computation to meet constraints */
bitmap *disabled_chans;
};

/* Given a gossmap channel, get the current known min/max */
Expand Down
30 changes: 19 additions & 11 deletions plugins/askrene/flow.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,24 @@ struct amount_msat flowset_delivers(struct plugin *plugin,
return final;
}

static double edge_probability(struct amount_msat sent,
struct amount_msat mincap,
struct amount_msat maxcap,
struct amount_msat used)
/* Stolen whole-cloth from @Lagrang3 in renepay's flow.c. Wrong
* because of htlc overhead in reservations! */
static double edge_probability(const struct route_query *rq,
const struct short_channel_id_dir *scidd,
struct amount_msat sent)
{
struct amount_msat numerator, denominator;
struct amount_msat mincap, maxcap, additional;
const struct gossmap_chan *c = gossmap_find_chan(rq->gossmap, &scidd->scid);

if (!amount_msat_sub(&mincap, mincap, used))
mincap = AMOUNT_MSAT(0);
if (!amount_msat_sub(&maxcap, maxcap, used))
maxcap = AMOUNT_MSAT(0);
get_constraints(rq, c, scidd->dir, &mincap, &maxcap);

/* We add an extra per-htlc reservation for the *next* HTLC, so we "over-reserve"
* on local channels. Undo that! */
additional = get_additional_per_htlc_cost(rq, scidd);
if (!amount_msat_accumulate(&mincap, additional)
|| !amount_msat_accumulate(&maxcap, additional))
abort();

if (amount_msat_less_eq(sent, mincap))
return 1.0;
Expand Down Expand Up @@ -129,10 +136,11 @@ double flow_probability(const struct flow *flow,

for (int i = (int)pathlen - 1; i >= 0; i--) {
const struct half_chan *h = flow_edge(flow, i);
struct amount_msat mincap, maxcap;
struct short_channel_id_dir scidd;
scidd.scid = gossmap_chan_scid(rq->gossmap, flow->path[i]);
scidd.dir = flow->dirs[i];

get_constraints(rq, flow->path[i], flow->dirs[i], &mincap, &maxcap);
prob *= edge_probability(spend, mincap, maxcap, AMOUNT_MSAT(0));
prob *= edge_probability(rq, &scidd, spend);

if (!amount_msat_add_fee(&spend, h->base_fee,
h->proportional_fee)) {
Expand Down
Loading
Loading