Skip to content

Commit

Permalink
libplugin-pay: bias towards larger channels.
Browse files Browse the repository at this point in the history
We bias by channel by up to 2x the fee; this should drive us towards
larger channels.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Sep 8, 2021
1 parent 171d463 commit 6bfc039
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions plugins/libplugin-pay.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,53 @@ static bool payment_route_can_carry_even_disabled(const struct gossmap *map,
return payment_route_check(map, c, dir, amount, p);
}

/* Rene Pickhardt:
*
* Btw the linear term of the Taylor series of -log((c+1-x)/(c+1)) is 1/(c+1)
* meaning that another suitable Weight for Dijkstra would be amt/(c+1) +
* \mu*fee(amt) which is the linearized version which for small amounts and
* suitable value of \mu should be good enough)
*/
static u64 capacity_bias(const struct gossmap *map,
const struct gossmap_chan *c,
int dir,
struct amount_msat cost)
{
struct amount_msat fee;
struct amount_sat capacity;

/* Overflow is pretty-much impossible, so ignore. */
if (!amount_msat_fee(&fee, cost,
c->half[dir].base_fee,
c->half[dir].proportional_fee))
return 0;

/* Can fail in theory if gossmap changed underneath. */
if (!gossmap_chan_get_capacity(map, c, &capacity))
return 0;

/* Assume we want \mu = 1/2 (i.e. capacity matters twice as much
* as fees), we get: bias = 2 * fee * (amt / (c + 1)) */
return 2
* fee.millisatoshis /* Raw: complex math & laziness */
* cost.millisatoshis /* Raw: complex math & laziness */
/ (capacity.satoshis*1000 + 1); /* Raw: complex math & laziness */
}

/* Prioritize costs over distance, but bias to larger channels. */
static u64 route_score(u32 distance,
struct amount_msat cost,
struct amount_msat risk,
int dir,
const struct gossmap_chan *c)
{
u64 costs = cost.millisatoshis + risk.millisatoshis /* Raw: score */
+ capacity_bias(global_gossmap, c, dir, cost);
if (costs > 0xFFFFFFFF)
costs = 0xFFFFFFFF;
return costs;
}

static struct route_hop *route(const tal_t *ctx,
struct gossmap *gossmap,
const struct gossmap_node *src,
Expand All @@ -713,14 +760,14 @@ static struct route_hop *route(const tal_t *ctx,

can_carry = payment_route_can_carry;
dij = dijkstra(tmpctx, gossmap, dst, amount, riskfactor,
can_carry, route_score_cheaper, p);
can_carry, route_score, p);
r = route_from_dijkstra(ctx, gossmap, dij, src, amount, final_delay);
if (!r) {
/* Try using disabled channels too */
/* FIXME: is there somewhere we can annotate this for paystatus? */
can_carry = payment_route_can_carry_even_disabled;
dij = dijkstra(ctx, gossmap, dst, amount, riskfactor,
can_carry, route_score_cheaper, p);
can_carry, route_score, p);
r = route_from_dijkstra(ctx, gossmap, dij, src,
amount, final_delay);
if (!r) {
Expand Down

0 comments on commit 6bfc039

Please sign in to comment.