Skip to content

Commit ce0a752

Browse files
committed
askrene: remove non child-friendly fields from struct route_query.
Notably no access to the struct command and struct plugin. Note: we actually *do* mess with askrene->reserves, but the previous code used cmd to get to it. Now we need to include a non-const pointer in struct route_query. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent cce3512 commit ce0a752

File tree

7 files changed

+67
-73
lines changed

7 files changed

+67
-73
lines changed

plugins/askrene/askrene.c

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,12 @@ struct amount_msat get_additional_per_htlc_cost(const struct route_query *rq,
301301
return AMOUNT_MSAT(0);
302302
}
303303

304-
const char *rq_log(const tal_t *ctx,
305-
const struct route_query *rq,
306-
enum log_level level,
307-
const char *fmt,
308-
...)
304+
PRINTF_FMT(4, 5)
305+
static const char *cmd_log(const tal_t *ctx,
306+
struct command *cmd,
307+
enum log_level level,
308+
const char *fmt,
309+
...)
309310
{
310311
va_list args;
311312
const char *msg;
@@ -314,14 +315,14 @@ const char *rq_log(const tal_t *ctx,
314315
msg = tal_vfmt(ctx, fmt, args);
315316
va_end(args);
316317

317-
plugin_notify_message(rq->cmd, level, "%s", msg);
318+
plugin_notify_message(cmd, level, "%s", msg);
318319

319320
/* Notifications already get logged at debug. Otherwise reduce
320321
* severity. */
321322
if (level != LOG_DBG)
322-
plugin_log(rq->plugin,
323+
plugin_log(cmd->plugin,
323324
level == LOG_BROKEN ? level : level - 1,
324-
"%s: %s", rq->cmd->id, msg);
325+
"%s: %s", cmd->id, msg);
325326
return msg;
326327
}
327328

@@ -363,7 +364,9 @@ struct getroutes_info {
363364
u32 maxparts;
364365
};
365366

366-
static void apply_layers(struct askrene *askrene, struct route_query *rq,
367+
static void apply_layers(struct askrene *askrene,
368+
struct command *cmd,
369+
struct route_query *rq,
367370
const struct node_id *source,
368371
struct amount_msat amount,
369372
struct gossmap_localmods *localmods,
@@ -375,14 +378,17 @@ static void apply_layers(struct askrene *askrene, struct route_query *rq,
375378
const struct layer *l = find_layer(askrene, layers[i]);
376379
if (!l) {
377380
if (streq(layers[i], "auto.localchans")) {
378-
plugin_log(rq->plugin, LOG_DBG, "Adding auto.localchans");
381+
cmd_log(tmpctx, cmd, LOG_DBG,
382+
"Adding auto.localchans");
379383
l = local_layer;
380384
} else if (streq(layers[i], "auto.no_mpp_support")) {
381-
plugin_log(rq->plugin, LOG_DBG, "Adding auto.no_mpp_support, sorry");
385+
cmd_log(tmpctx, cmd, LOG_DBG,
386+
"Adding auto.no_mpp_support, sorry");
382387
l = remove_small_channel_layer(layers, askrene, amount, localmods);
383388
} else {
384389
assert(streq(layers[i], "auto.sourcefree"));
385-
plugin_log(rq->plugin, LOG_DBG, "Adding auto.sourcefree");
390+
cmd_log(tmpctx, cmd, LOG_DBG,
391+
"Adding auto.sourcefree");
386392
l = source_free_layer(layers, askrene, source, localmods);
387393
}
388394
}
@@ -433,15 +439,16 @@ void get_constraints(const struct route_query *rq,
433439
reserve_sub(rq->reserved, &scidd, rq->layers, max);
434440
}
435441

436-
static void process_child_logs(struct route_query *rq, int log_fd)
442+
static void process_child_logs(struct command *cmd,
443+
int log_fd)
437444
{
438445
u8 *msg;
439446
while ((msg = wire_sync_read(tmpctx, log_fd)) != NULL) {
440447
enum log_level level;
441448
char *entry;
442449
struct node_id *peer;
443450
if (fromwire_status_log(tmpctx, msg, &level, &peer, &entry))
444-
rq_log(tmpctx, rq, level, "%s", entry);
451+
cmd_log(tmpctx, cmd, level, "%s", entry);
445452
}
446453
}
447454

@@ -454,6 +461,7 @@ static struct command_result *do_getroutes(struct command *cmd,
454461
const char *err, *json;
455462
struct timemono time_start, deadline;
456463
int child_fd, log_fd, child_pid, child_status;
464+
s8 *biases;
457465

458466
/* update the gossmap */
459467
if (gossmap_refresh(askrene->gossmap)) {
@@ -464,18 +472,16 @@ static struct command_result *do_getroutes(struct command *cmd,
464472
}
465473

466474
/* build this request structure */
467-
rq->cmd = cmd;
468-
rq->plugin = cmd->plugin;
475+
rq->cmd_id = cmd->id;
469476
rq->gossmap = askrene->gossmap;
470477
rq->reserved = askrene->reserved;
471478
rq->layers = tal_arr(rq, const struct layer *, 0);
472479
rq->capacities = tal_dup_talarr(rq, fp16_t, askrene->capacities);
473480
/* FIXME: we still need to do something useful with these */
474481
rq->additional_costs = info->additional_costs;
475-
rq->maxparts = info->maxparts;
476482

477483
/* apply selected layers to the localmods */
478-
apply_layers(askrene, rq, &info->source, info->amount, localmods,
484+
apply_layers(askrene, cmd, rq, &info->source, info->amount, localmods,
479485
info->layers, info->local_layer);
480486

481487
/* Clear scids with reservations, too, so we don't have to look up
@@ -495,18 +501,19 @@ static struct command_result *do_getroutes(struct command *cmd,
495501

496502
/* localmods can add channels, so we need to allocate biases array
497503
* *afterwards* */
498-
rq->biases =
504+
rq->biases = biases =
499505
tal_arrz(rq, s8, gossmap_max_chan_idx(askrene->gossmap) * 2);
500506

501507
/* Note any channel biases */
502508
for (size_t i = 0; i < tal_count(rq->layers); i++)
503-
layer_apply_biases(rq->layers[i], askrene->gossmap, rq->biases);
509+
layer_apply_biases(rq->layers[i], askrene->gossmap, biases);
504510

505511
/* checkout the source */
506512
const struct gossmap_node *srcnode =
507513
gossmap_find_node(askrene->gossmap, &info->source);
508514
if (!srcnode) {
509-
err = rq_log(tmpctx, rq, LOG_INFORM, "Unknown source node %s",
515+
err = cmd_log(tmpctx, cmd, LOG_INFORM,
516+
"Unknown source node %s",
510517
fmt_node_id(tmpctx, &info->source));
511518
goto fail;
512519
}
@@ -515,7 +522,7 @@ static struct command_result *do_getroutes(struct command *cmd,
515522
const struct gossmap_node *dstnode =
516523
gossmap_find_node(askrene->gossmap, &info->dest);
517524
if (!dstnode) {
518-
err = rq_log(tmpctx, rq, LOG_INFORM,
525+
err = cmd_log(tmpctx, cmd, LOG_INFORM,
519526
"Unknown destination node %s",
520527
fmt_node_id(tmpctx, &info->dest));
521528
goto fail;
@@ -525,14 +532,14 @@ static struct command_result *do_getroutes(struct command *cmd,
525532
if (have_layer(info->layers, "auto.no_mpp_support") &&
526533
info->dev_algo != ALGO_SINGLE_PATH) {
527534
info->dev_algo = ALGO_SINGLE_PATH;
528-
rq_log(tmpctx, rq, LOG_DBG,
535+
cmd_log(tmpctx, cmd, LOG_DBG,
529536
"Layer no_mpp_support is active we switch to a "
530537
"single path algorithm.");
531538
}
532-
if (rq->maxparts == 1 &&
539+
if (info->maxparts == 1 &&
533540
info->dev_algo != ALGO_SINGLE_PATH) {
534541
info->dev_algo = ALGO_SINGLE_PATH;
535-
rq_log(tmpctx, rq, LOG_DBG,
542+
cmd_log(tmpctx, cmd, LOG_DBG,
536543
"maxparts == 1: switching to a single path algorithm.");
537544
}
538545

@@ -542,14 +549,14 @@ static struct command_result *do_getroutes(struct command *cmd,
542549
child_fd = fork_router_child(rq, info->dev_algo == ALGO_SINGLE_PATH,
543550
deadline, srcnode, dstnode, info->amount,
544551
info->maxfee, info->finalcltv, info->maxdelay,
545-
cmd->id, cmd->filter, &log_fd, &child_pid);
552+
info->maxparts, cmd->id, cmd->filter, &log_fd, &child_pid);
546553
if (child_fd == -1) {
547554
err = tal_fmt(tmpctx, "failed to fork: %s", strerror(errno));
548555
goto fail_broken;
549556
}
550557

551558
/* FIXME: Go async! */
552-
process_child_logs(rq, log_fd);
559+
process_child_logs(cmd, log_fd);
553560
close(log_fd);
554561
json = grab_fd_str(cmd, child_fd);
555562
close(child_fd);
@@ -558,9 +565,9 @@ static struct command_result *do_getroutes(struct command *cmd,
558565
struct timerel time_delta = timemono_between(time_mono(), time_start);
559566

560567
/* log the time of computation */
561-
rq_log(tmpctx, rq, LOG_DBG, "get_routes %s %" PRIu64 " ms",
562-
WEXITSTATUS(child_status) != 0 ? "failed after" : "completed in",
563-
time_to_msec(time_delta));
568+
cmd_log(tmpctx, cmd, LOG_DBG, "get_routes %s %" PRIu64 " ms",
569+
WEXITSTATUS(child_status) != 0 ? "failed after" : "completed in",
570+
time_to_msec(time_delta));
564571

565572
if (WIFSIGNALED(child_status)) {
566573
err = tal_fmt(tmpctx, "child died with signal %u",

plugins/askrene/askrene.h

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,30 @@ struct askrene {
3232

3333
/* Information for a single route query. */
3434
struct route_query {
35-
/* Command pointer, mainly for command id. */
36-
struct command *cmd;
37-
38-
/* Plugin pointer, for logging mainly */
39-
struct plugin *plugin;
40-
4135
/* This is *not* updated during a query! Has all layers applied. */
4236
const struct gossmap *gossmap;
4337

44-
/* We need to take in-flight payments into account */
45-
const struct reserve_htable *reserved;
38+
/* command id to use for reservations we create. */
39+
const char *cmd_id;
4640

4741
/* Array of layers we're applying */
4842
const struct layer **layers;
4943

50-
/* Cache of channel capacities for non-reserved, unknown channels. */
51-
fp16_t *capacities;
52-
5344
/* Compact cache of biases */
54-
s8 *biases;
45+
const s8 *biases;
5546

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

50+
/* We need to take in-flight payments into account (this is
51+
* askrene->reserved, so make sure to undo changes! */
52+
struct reserve_htable *reserved;
53+
54+
/* Cache of channel capacities for non-reserved, unknown channels. */
55+
fp16_t *capacities;
56+
5957
/* channels we disable during computation to meet constraints */
6058
bitmap *disabled_chans;
61-
62-
/* limit the number of paths in the solution */
63-
u32 maxparts;
6459
};
6560

6661
/* Given a gossmap channel, get the current known min/max */
@@ -70,14 +65,6 @@ void get_constraints(const struct route_query *rq,
7065
struct amount_msat *min,
7166
struct amount_msat *max);
7267

73-
/* Say something about this route_query */
74-
const char *rq_log(const tal_t *ctx,
75-
const struct route_query *rq,
76-
enum log_level level,
77-
const char *fmt,
78-
...)
79-
PRINTF_FMT(4, 5);
80-
8168
/* Is there a known additional per-htlc cost for this channel? */
8269
struct amount_msat get_additional_per_htlc_cost(const struct route_query *rq,
8370
const struct short_channel_id_dir *scidd);

plugins/askrene/child/entry.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ int fork_router_child(struct route_query *rq,
134134
const struct gossmap_node *srcnode,
135135
const struct gossmap_node *dstnode,
136136
struct amount_msat amount, struct amount_msat maxfee,
137-
u32 finalcltv, u32 maxdelay,
137+
u32 finalcltv, u32 maxdelay, size_t maxparts,
138138
const char *cmd_id,
139139
struct json_filter *cmd_filter,
140140
int *log_fd,
@@ -181,7 +181,7 @@ int fork_router_child(struct route_query *rq,
181181
} else {
182182
err = default_routes(rq, rq, deadline, srcnode, dstnode,
183183
amount, maxfee, finalcltv, maxdelay,
184-
&flows, &probability);
184+
maxparts, &flows, &probability);
185185
}
186186
if (err) {
187187
write_all(replyfds[1], err, strlen(err));

plugins/askrene/child/entry.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int fork_router_child(struct route_query *rq,
1717
const struct gossmap_node *srcnode,
1818
const struct gossmap_node *dstnode,
1919
struct amount_msat amount, struct amount_msat maxfee,
20-
u32 finalcltv, u32 maxdelay,
20+
u32 finalcltv, u32 maxdelay, size_t maxparts,
2121
const char *cmd_id,
2222
struct json_filter *cmd_filter,
2323
int *log_fd,

plugins/askrene/child/mcf.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,7 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
13441344
const struct gossmap_node *srcnode,
13451345
const struct gossmap_node *dstnode, struct amount_msat amount,
13461346
struct amount_msat maxfee, u32 finalcltv, u32 maxdelay,
1347+
size_t maxparts,
13471348
struct flow ***flows, double *probability,
13481349
struct flow **(*solver)(const tal_t *, const struct route_query *,
13491350
const struct gossmap_node *,
@@ -1554,10 +1555,10 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
15541555

15551556
/* If we're over the number of parts, try to cram excess into the
15561557
* largest-capacity parts */
1557-
if (tal_count(*flows) > rq->maxparts) {
1558+
if (tal_count(*flows) > maxparts) {
15581559
struct amount_msat fee;
15591560

1560-
error_message = reduce_num_flows(rq, rq, flows, amount, rq->maxparts);
1561+
error_message = reduce_num_flows(rq, rq, flows, amount, maxparts);
15611562
if (error_message) {
15621563
*flows = tal_free(*flows);
15631564
return error_message;
@@ -1597,14 +1598,13 @@ linear_routes(const tal_t *ctx, struct route_query *rq,
15971598
return child_log(rq, LOG_BROKEN,
15981599
"%s: check_htlc_max_limits failed", __func__);
15991600
}
1600-
if (tal_count(*flows) > rq->maxparts) {
1601+
if (tal_count(*flows) > maxparts) {
16011602
size_t num_flows = tal_count(*flows);
16021603
*flows = tal_free(*flows);
16031604
return child_log(rq, LOG_BROKEN,
16041605
"%s: the number of flows (%zu) exceeds the limit set "
1605-
"on payment parts (%" PRIu32
1606-
"), please submit a bug report",
1607-
__func__, num_flows, rq->maxparts);
1606+
"on payment parts (%zu), please submit a bug report",
1607+
__func__, num_flows, maxparts);
16081608
}
16091609

16101610
return NULL;
@@ -1621,11 +1621,12 @@ const char *default_routes(const tal_t *ctx, struct route_query *rq,
16211621
const struct gossmap_node *srcnode,
16221622
const struct gossmap_node *dstnode,
16231623
struct amount_msat amount, struct amount_msat maxfee,
1624-
u32 finalcltv, u32 maxdelay, struct flow ***flows,
1624+
u32 finalcltv, u32 maxdelay, size_t maxparts,
1625+
struct flow ***flows,
16251626
double *probability)
16261627
{
16271628
return linear_routes(ctx, rq, deadline, srcnode, dstnode, amount, maxfee,
1628-
finalcltv, maxdelay, flows, probability, minflow);
1629+
finalcltv, maxdelay, maxparts, flows, probability, minflow);
16291630
}
16301631

16311632
const char *single_path_routes(const tal_t *ctx, struct route_query *rq,
@@ -1638,6 +1639,6 @@ const char *single_path_routes(const tal_t *ctx, struct route_query *rq,
16381639
double *probability)
16391640
{
16401641
return linear_routes(ctx, rq, deadline, srcnode, dstnode, amount, maxfee,
1641-
finalcltv, maxdelay, flows, probability,
1642+
finalcltv, maxdelay, 1, flows, probability,
16421643
single_path_flow);
16431644
}

plugins/askrene/child/mcf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const char *default_routes(const tal_t *ctx, struct route_query *rq,
1717
const struct gossmap_node *dstnode,
1818
struct amount_msat amount,
1919
struct amount_msat maxfee, u32 finalcltv,
20-
u32 maxdelay, struct flow ***flows,
20+
u32 maxdelay, size_t maxparts, struct flow ***flows,
2121
double *probability);
2222

2323
/* A wrapper to the single-path constrained solver. */

0 commit comments

Comments
 (0)