Skip to content

Commit

Permalink
renepay: flow routes limited by htlc_max and htlc_min
Browse files Browse the repository at this point in the history
The flow routes returned by minflow are bounded by the htlc_min and
htlc_max along the route whenever possible.

Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
  • Loading branch information
Lagrang3 committed Nov 29, 2023
1 parent 5887be0 commit 496301c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
12 changes: 12 additions & 0 deletions plugins/renepay/flow.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,4 +322,16 @@ void commit_flow_set(
struct chan_extra_map *chan_extra_map,
struct flow **flows);

static inline struct amount_msat channel_htlc_max(
const struct gossmap_chan *chan,
const int dir)
{
return amount_msat( fp16_to_u64(chan->half[dir].htlc_max) );
}
static inline struct amount_msat channel_htlc_min(
const struct gossmap_chan *chan,
const int dir)
{
return amount_msat( fp16_to_u64(chan->half[dir].htlc_min) );
}
#endif /* LIGHTNING_PLUGINS_RENEPAY_FLOW_H */
40 changes: 35 additions & 5 deletions plugins/renepay/mcf.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ccan/list/list.h>
#include <ccan/lqueue/lqueue.h>
#include <ccan/tal/tal.h>
#include <common/pseudorand.h>
#include <common/type_to_string.h>
#include <math.h>
#include <plugins/renepay/dijkstra.h>
Expand Down Expand Up @@ -190,6 +191,7 @@
static const double CHANNEL_PIVOTS[]={0,0.5,0.8,0.95};

static const s64 INFINITE = INT64_MAX;
static const u64 INFINITE_MSAT = UINT64_MAX;
static const u32 INVALID_INDEX=0xffffffff;
static const s64 MU_MAX = 128;

Expand Down Expand Up @@ -1105,6 +1107,12 @@ struct list_data
struct flow *flow_path;
};

static inline uint64_t pseudorand_interval(uint64_t a, uint64_t b)
{
assert(b>a);
return a + pseudorand(b-a);
}

/* Given a flow in the residual network, build a set of payment flows in the
* gossmap that corresponds to this flow. */
static struct flow **
Expand Down Expand Up @@ -1181,6 +1189,9 @@ static struct flow **
u32 final_idx = find_positive_balance(gossmap,chan_flow,node_idx,balance,
prev_chan,prev_dir,prev_idx);

struct amount_msat sup_htlc_min = AMOUNT_MSAT_INIT(0),
inf_htlc_max = AMOUNT_MSAT_INIT(INFINITE_MSAT);

s64 delta=-balance[node_idx];
int length = 0;
delta = MIN(delta,balance[final_idx]);
Expand All @@ -1200,13 +1211,32 @@ static struct flow **
delta=MIN(delta,chan_flow[c_idx].half[dir]);
length++;

// TODO(eduardo) does htlc_max has any relevance
// here?
// HINT: delta=MIN(delta,htlc_max);
// however this might not work because often we
// move delta+fees
/* obtain the supremum htlc_min along the route */
sup_htlc_min = amount_msat_max(sup_htlc_min, channel_htlc_min(c,dir));

/* obtain the infimum htlc_max along the route */
inf_htlc_max = amount_msat_min(inf_htlc_max, channel_htlc_max(c,dir));
}

s64 htlc_max = inf_htlc_max.millisatoshis/1000; /* Raw: need htlc_max in sats to do arithmetic operations. */
s64 htlc_min = (sup_htlc_min.millisatoshis+999)/1000 /* Raw: need htlc_min in stas to do arithmetic operations. */;

if(htlc_min>htlc_max)
{
/* htlc_min is too big or htlc_max is too small, we
* cannot send `delta` along this route.
*
* FIXME: We try anyways because failing channels will
* be blacklisted downstream. */
htlc_min = 0;
}

/* If we divide this route into different flows make it
* random to avoid routing nodes making correlations. */
if(delta>htlc_max)
{
delta = pseudorand_interval(MAX(htlc_min,(htlc_max*95)/100),htlc_max);
}

struct flow *fp = tal(this_ctx,struct flow);
fp->path = tal_arr(fp,const struct gossmap_chan *,length);
Expand Down

0 comments on commit 496301c

Please sign in to comment.