Skip to content

Commit 7506e2d

Browse files
committed
askrene: have child make struct route_query internally.
Signed-off-by: Rusty Russell <[email protected]>
1 parent d262cf6 commit 7506e2d

File tree

3 files changed

+98
-50
lines changed

3 files changed

+98
-50
lines changed

plugins/askrene/askrene.c

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <common/gossmods_listpeerchannels.h>
1717
#include <common/json_param.h>
1818
#include <common/json_stream.h>
19+
#include <common/memleak.h>
1920
#include <common/route.h>
2021
#include <common/status_wiregen.h>
2122
#include <errno.h>
@@ -24,7 +25,6 @@
2425
#include <plugins/askrene/askrene.h>
2526
#include <plugins/askrene/child/additional_costs.h>
2627
#include <plugins/askrene/child/entry.h>
27-
#include <plugins/askrene/child/route_query.h>
2828
#include <plugins/askrene/layer.h>
2929
#include <plugins/askrene/reserve.h>
3030
#include <sys/wait.h>
@@ -329,43 +329,47 @@ struct getroutes_info {
329329
u32 maxparts;
330330
};
331331

332-
static void apply_layers(struct askrene *askrene,
333-
struct command *cmd,
334-
struct route_query *rq,
335-
const struct node_id *source,
336-
struct amount_msat amount,
337-
struct gossmap_localmods *localmods,
338-
const char **layers,
339-
const struct layer *local_layer)
332+
/* Gather layers, clear capacities where layers contains info */
333+
static const struct layer **apply_layers(const tal_t *ctx,
334+
struct askrene *askrene,
335+
struct command *cmd,
336+
const struct node_id *source,
337+
struct amount_msat amount,
338+
struct gossmap_localmods *localmods,
339+
const char **layernames,
340+
const struct layer *local_layer,
341+
fp16_t *capacities)
340342
{
343+
const struct layer **layers = tal_arr(ctx, const struct layer *, 0);
341344
/* Layers must exist, but might be special ones! */
342-
for (size_t i = 0; i < tal_count(layers); i++) {
343-
const struct layer *l = find_layer(askrene, layers[i]);
345+
for (size_t i = 0; i < tal_count(layernames); i++) {
346+
const struct layer *l = find_layer(askrene, layernames[i]);
344347
if (!l) {
345-
if (streq(layers[i], "auto.localchans")) {
348+
if (streq(layernames[i], "auto.localchans")) {
346349
cmd_log(tmpctx, cmd, LOG_DBG,
347350
"Adding auto.localchans");
348351
l = local_layer;
349-
} else if (streq(layers[i], "auto.no_mpp_support")) {
352+
} else if (streq(layernames[i], "auto.no_mpp_support")) {
350353
cmd_log(tmpctx, cmd, LOG_DBG,
351354
"Adding auto.no_mpp_support, sorry");
352-
l = remove_small_channel_layer(layers, askrene, amount, localmods);
355+
l = remove_small_channel_layer(layernames, askrene, amount, localmods);
353356
} else {
354-
assert(streq(layers[i], "auto.sourcefree"));
357+
assert(streq(layernames[i], "auto.sourcefree"));
355358
cmd_log(tmpctx, cmd, LOG_DBG,
356359
"Adding auto.sourcefree");
357-
l = source_free_layer(layers, askrene, source, localmods);
360+
l = source_free_layer(layernames, askrene, source, localmods);
358361
}
359362
}
360363

361-
tal_arr_expand(&rq->layers, l);
364+
tal_arr_expand(&layers, l);
362365
/* FIXME: Implement localmods_merge, and cache this in layer? */
363-
layer_add_localmods(l, rq->gossmap, localmods);
366+
layer_add_localmods(l, askrene->gossmap, localmods);
364367

365368
/* Clear any entries in capacities array if we
366369
* override them (incl local channels) */
367-
layer_clear_overridden_capacities(l, askrene->gossmap, rq->capacities);
370+
layer_clear_overridden_capacities(l, askrene->gossmap, capacities);
368371
}
372+
return layers;
369373
}
370374

371375
static void process_child_logs(struct command *cmd,
@@ -386,11 +390,12 @@ static struct command_result *do_getroutes(struct command *cmd,
386390
struct getroutes_info *info)
387391
{
388392
struct askrene *askrene = get_askrene(cmd->plugin);
389-
struct route_query *rq = tal(cmd, struct route_query);
390393
const char *err, *json;
391394
struct timemono time_start, deadline;
392395
int child_fd, log_fd, child_pid, child_status;
396+
const struct layer **layers;
393397
s8 *biases;
398+
fp16_t *capacities;
394399

395400
/* update the gossmap */
396401
if (gossmap_refresh(askrene->gossmap)) {
@@ -400,42 +405,28 @@ static struct command_result *do_getroutes(struct command *cmd,
400405
get_capacities(askrene, askrene->plugin, askrene->gossmap);
401406
}
402407

403-
/* build this request structure */
404-
rq->cmd_id = cmd->id;
405-
rq->gossmap = askrene->gossmap;
406-
rq->reserved = askrene->reserved;
407-
rq->layers = tal_arr(rq, const struct layer *, 0);
408-
rq->capacities = tal_dup_talarr(rq, fp16_t, askrene->capacities);
409-
/* FIXME: we still need to do something useful with these */
410-
rq->additional_costs = info->additional_costs;
408+
capacities = tal_dup_talarr(cmd, fp16_t, askrene->capacities);
411409

412410
/* apply selected layers to the localmods */
413-
apply_layers(askrene, cmd, rq, &info->source, info->amount, localmods,
414-
info->layers, info->local_layer);
411+
layers = apply_layers(cmd, askrene, cmd,
412+
&info->source, info->amount, localmods,
413+
info->layers, info->local_layer, capacities);
415414

416415
/* Clear scids with reservations, too, so we don't have to look up
417416
* all the time! */
418417
reserves_clear_capacities(askrene->reserved, askrene->gossmap,
419-
rq->capacities);
418+
capacities);
420419

421420
/* we temporarily apply localmods */
422421
gossmap_apply_localmods(askrene->gossmap, localmods);
423422

424-
/* I want to be able to disable channels while working on this query.
425-
* Layers are for user interaction and cannot be used for this purpose.
426-
*/
427-
rq->disabled_chans =
428-
tal_arrz(rq, bitmap,
429-
2 * BITMAP_NWORDS(gossmap_max_chan_idx(askrene->gossmap)));
430-
431423
/* localmods can add channels, so we need to allocate biases array
432424
* *afterwards* */
433-
rq->biases = biases =
434-
tal_arrz(rq, s8, gossmap_max_chan_idx(askrene->gossmap) * 2);
425+
biases = tal_arrz(cmd, s8, gossmap_max_chan_idx(askrene->gossmap) * 2);
435426

436427
/* Note any channel biases */
437-
for (size_t i = 0; i < tal_count(rq->layers); i++)
438-
layer_apply_biases(rq->layers[i], askrene->gossmap, biases);
428+
for (size_t i = 0; i < tal_count(layers); i++)
429+
layer_apply_biases(layers[i], askrene->gossmap, biases);
439430

440431
/* checkout the source */
441432
const struct gossmap_node *srcnode =
@@ -475,7 +466,13 @@ static struct command_result *do_getroutes(struct command *cmd,
475466
time_start = time_mono();
476467
deadline = timemono_add(time_start,
477468
time_from_sec(askrene->route_seconds));
478-
child_fd = fork_router_child(rq, info->dev_algo == ALGO_SINGLE_PATH,
469+
child_fd = fork_router_child(askrene->gossmap,
470+
layers,
471+
biases,
472+
info->additional_costs,
473+
askrene->reserved,
474+
take(capacities),
475+
info->dev_algo == ALGO_SINGLE_PATH,
479476
deadline, srcnode, dstnode, info->amount,
480477
info->maxfee, info->finalcltv, info->maxdelay,
481478
info->maxparts, cmd->id, cmd->filter, &log_fd, &child_pid);
@@ -690,8 +687,7 @@ static struct command_result *json_getroutes(struct command *cmd,
690687
info->finalcltv = *finalcltv;
691688
info->maxdelay = *maxdelay;
692689
info->dev_algo = *dev_algo;
693-
info->additional_costs = tal(info, struct additional_cost_htable);
694-
additional_cost_htable_init(info->additional_costs);
690+
info->additional_costs = new_htable(info, additional_cost_htable);
695691
info->maxparts = *maxparts;
696692

697693
if (have_layer(info->layers, "auto.localchans")) {

plugins/askrene/child/entry.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,38 @@ static void json_add_getroutes(struct json_stream *js,
127127
json_array_end(js);
128128
}
129129

130+
static struct route_query *new_route_query(const tal_t *ctx,
131+
const struct gossmap *gossmap,
132+
const char *cmd_id,
133+
const struct layer **layers,
134+
const s8 *biases,
135+
const struct additional_cost_htable *additional_costs,
136+
struct reserve_htable *reserved,
137+
fp16_t *capacities TAKES)
138+
{
139+
struct route_query *rq = tal(ctx, struct route_query);
140+
141+
rq->gossmap = gossmap;
142+
rq->cmd_id = tal_strdup(rq, cmd_id);
143+
rq->layers = layers;
144+
rq->biases = biases;
145+
rq->additional_costs = additional_costs;
146+
rq->reserved = reserved;
147+
rq->capacities = tal_dup_talarr(rq, fp16_t, capacities);
148+
rq->disabled_chans =
149+
tal_arrz(rq, bitmap,
150+
2 * BITMAP_NWORDS(gossmap_max_chan_idx(gossmap)));
151+
152+
return rq;
153+
}
154+
130155
/* Returns fd to child */
131-
int fork_router_child(struct route_query *rq,
156+
int fork_router_child(const struct gossmap *gossmap,
157+
const struct layer **layers,
158+
const s8 *biases,
159+
const struct additional_cost_htable *additional_costs,
160+
struct reserve_htable *reserved,
161+
fp16_t *capacities TAKES,
132162
bool single_path,
133163
struct timemono deadline,
134164
const struct gossmap_node *srcnode,
@@ -147,33 +177,41 @@ int fork_router_child(struct route_query *rq,
147177
struct amount_msat *amounts;
148178
const char *err, *p;
149179
size_t len;
180+
struct route_query *rq;
150181

151182
if (pipe(replyfds) != 0)
152-
return -1;
183+
goto parent_fail;
153184
if (pipe(logfds) != 0) {
154185
close_noerr(replyfds[0]);
155186
close_noerr(replyfds[1]);
156-
return -1;
187+
goto parent_fail;
157188
}
158189
*child_pid = fork();
159190
if (*child_pid < 0) {
160191
close_noerr(replyfds[0]);
161192
close_noerr(replyfds[1]);
162193
close_noerr(logfds[0]);
163194
close_noerr(logfds[1]);
164-
return -1;
195+
goto parent_fail;
165196
}
166197
if (*child_pid != 0) {
167198
close(logfds[1]);
168199
close(replyfds[1]);
169200
*log_fd = logfds[0];
201+
if (taken(capacities))
202+
tal_free(capacities);
170203
return replyfds[0];
171204
}
172205

173206
/* We are the child. Run the algo */
174207
close(logfds[0]);
175208
close(replyfds[0]);
176209
set_child_log_fd(logfds[1]);
210+
211+
/* We exit below, so we don't bother freeing this */
212+
rq = new_route_query(NULL, gossmap, cmd_id, layers,
213+
biases, additional_costs,
214+
reserved, capacities);
177215
if (single_path) {
178216
err = single_path_routes(rq, rq, deadline, srcnode, dstnode,
179217
amount, maxfee, finalcltv,
@@ -226,5 +264,10 @@ int fork_router_child(struct route_query *rq,
226264
if (!write_all(replyfds[1], p, len))
227265
abort();
228266
exit(0);
267+
268+
parent_fail:
269+
if (taken(capacities))
270+
tal_free(capacities);
271+
return -1;
229272
}
230273

plugins/askrene/child/entry.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,23 @@
44
#include <ccan/short_types/short_types.h>
55
#include <ccan/time/time.h>
66
#include <common/amount.h>
7+
#include <common/fp16.h>
78
#include <stdbool.h>
89

910
struct route_query;
1011
struct gossmap_node;
1112
struct json_filter;
13+
struct layer;
14+
struct reserve_htable;
15+
struct additional_cost_htable;
1216

1317
/* Entry point to the child process. */
14-
int fork_router_child(struct route_query *rq,
18+
int fork_router_child(const struct gossmap *gossmap,
19+
const struct layer **layers,
20+
const s8 *biases,
21+
const struct additional_cost_htable *additional_costs,
22+
struct reserve_htable *reserved,
23+
fp16_t *capacities TAKES,
1524
bool single_path,
1625
struct timemono deadline,
1726
const struct gossmap_node *srcnode,

0 commit comments

Comments
 (0)