Skip to content

Commit ec46c12

Browse files
committed
[WIP] askrene: add activation costs per channel
Changelog-None Signed-off-by: Lagrang3 <[email protected]>
1 parent 9f75c99 commit ec46c12

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

plugins/askrene/mcf.c

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <plugins/libplugin.h>
2020
#include <stdint.h>
2121

22+
23+
// FIXME: review this description still holds.
2224
/* # Optimal payments
2325
*
2426
* In this module we reduce the routing optimization problem to a linear
@@ -47,10 +49,12 @@
4749
*
4850
* fee_msat = base_msat + floor(millionths*x_msat / 10^6)
4951
*
50-
* We approximate this fee into a linear function by computing a slope `c_fee` such
51-
* that:
52+
* We approximate this fee into a linear function plus a constant term:
53+
*
54+
* cost(x) = (k*base_msat) + (k*A_msat*millionths/10^6) * x
5255
*
53-
* fee_microsat = c_fee * x_sat
56+
* where A_msat is the accuracy of the flows, the smallest unit of flow, so that
57+
* x_msat = x*A_msat, and k is an arbitrary constant.
5458
*
5559
* Function `linear_fee_cost` computes `c_fee` based on the base and
5660
* proportional fees of a channel.
@@ -159,12 +163,17 @@
159163
*
160164
* */
161165

162-
#define PARTS_BITS 2
166+
/* Bits required to store all channel parts. */
167+
#define PARTS_BITS 3
168+
/* How many parts have a proportional cost. */
169+
#define N_LINEAR_PARTS 5
170+
/* The number of the channel part that holds the base fee. */
171+
#define CHANNEL_BASE_PART 7
163172
#define CHANNEL_PARTS (1 << PARTS_BITS)
164173

165174
// These are the probability intervals we use to decompose a channel into linear
166175
// cost function arcs.
167-
static const double CHANNEL_PIVOTS[]={0,0.5,0.8,0.95};
176+
static const double CHANNEL_PIVOTS[]={0,0.5,0.8,0.95,0.99};
168177

169178
static const s64 INFINITE = INT64_MAX;
170179
static const s64 MU_MAX = 100;
@@ -237,7 +246,7 @@ static const struct amount_msat SINGLE_PATH_THRESHOLD = AMOUNT_MSAT(1000000);
237246
* we can't us a union, since bit order is implementation-defined and
238247
* we want chanidx on the highest bits:
239248
*
240-
* [ 0 1 2 3 4 5 6 ... 31 ]
249+
* [ 0 1 2 3 4 5 6 ... 31 ]
241250
* dual part chandir chanidx
242251
*/
243252
#define ARC_DUAL_BITOFF (0)
@@ -280,6 +289,13 @@ static inline struct arc arc_from_parts(u32 chanidx, int chandir, u32 part, bool
280289
return arc;
281290
}
282291

292+
/* A fake node ID that we use to separate channel base costs from proportional
293+
* costs. */
294+
static u32 auxiliary_node(u32 chanidx, int chandir, u32 auxiliary_node_offset)
295+
{
296+
return auxiliary_node_offset + ((chanidx + 1) | chandir);
297+
}
298+
283299
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
284300
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
285301

@@ -295,11 +311,18 @@ struct pay_parameters {
295311
struct amount_msat accuracy;
296312

297313
// channel linearization parameters
298-
double cap_fraction[CHANNEL_PARTS],
299-
cost_fraction[CHANNEL_PARTS];
314+
double cap_fraction[N_LINEAR_PARTS],
315+
cost_fraction[N_LINEAR_PARTS];
300316

301317
double delay_feefactor;
302318
double base_fee_penalty;
319+
320+
/* FIXME: set this */
321+
u32 auxiliary_node_offset;
322+
323+
/* FIXME: our estimate of the constant probability of failure of any
324+
* randomly chosen channel. */
325+
double constant_fail_probability;
303326
};
304327

305328
/* Helper function.
@@ -553,11 +576,14 @@ static void init_linear_network(const tal_t *ctx,
553576
s64 **arc_fee_cost, s64 **arc_capacity)
554577
{
555578
const struct gossmap *gossmap = params->rq->gossmap;
556-
const size_t max_num_chans = gossmap_max_chan_idx(gossmap);
557-
const size_t max_num_arcs = max_num_chans * ARCS_PER_CHANNEL;
579+
/* topology of the network */
580+
const size_t max_num_chans = gossmap_max_chan_idx(gossmap);
558581
const size_t max_num_nodes = gossmap_max_node_idx(gossmap);
582+
/* topology of the MCF problem */
583+
const size_t max_num_arcs = max_num_chans * ARCS_PER_CHANNEL;
584+
const size_t max_num_edges = max_num_nodes + max_num_chans * 2;
559585

560-
*graph = graph_new(ctx, max_num_nodes, max_num_arcs, ARC_DUAL_BITOFF);
586+
*graph = graph_new(ctx, max_num_edges, max_num_arcs, ARC_DUAL_BITOFF);
561587
*arc_prob_cost = tal_arr(ctx, double, max_num_arcs);
562588
for (size_t i = 0; i < max_num_arcs; ++i)
563589
(*arc_prob_cost)[i] = DBL_MAX;

0 commit comments

Comments
 (0)