askrene: move route_query definition and functions into child/.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2026-02-16 17:35:33 +10:30
parent 85c9179f77
commit b002824217
10 changed files with 108 additions and 92 deletions

View File

@@ -15,6 +15,7 @@ PLUGIN_ASKRENE_CHILD_SRC := \
plugins/askrene/child/priorityqueue.c \
plugins/askrene/child/algorithm.c \
plugins/askrene/child/child_log.c \
plugins/askrene/child/route_query.c \
PLUGIN_ASKRENE_SRC := $(PLUGIN_ASKRENE_PARENT_SRC) $(PLUGIN_ASKRENE_CHILD_SRC)
PLUGIN_ASKRENE_HEADER := $(PLUGIN_ASKRENE_SRC:.c=.h) plugins/askrene/child/additional_costs.h

View File

@@ -23,8 +23,8 @@
#include <math.h>
#include <plugins/askrene/askrene.h>
#include <plugins/askrene/child/additional_costs.h>
#include <plugins/askrene/child/child_log.h>
#include <plugins/askrene/child/entry.h>
#include <plugins/askrene/child/route_query.h>
#include <plugins/askrene/layer.h>
#include <plugins/askrene/reserve.h>
#include <sys/wait.h>
@@ -267,17 +267,6 @@ static struct layer *remove_small_channel_layer(const tal_t *ctx,
return layer;
}
struct amount_msat get_additional_per_htlc_cost(const struct route_query *rq,
const struct short_channel_id_dir *scidd)
{
const struct per_htlc_cost *phc;
phc = additional_cost_htable_get(rq->additional_costs, scidd);
if (phc)
return phc->per_htlc_cost;
else
return AMOUNT_MSAT(0);
}
PRINTF_FMT(4, 5)
static const char *cmd_log(const tal_t *ctx,
struct command *cmd,
@@ -382,42 +371,6 @@ static void apply_layers(struct askrene *askrene,
}
}
void get_constraints(const struct route_query *rq,
const struct gossmap_chan *chan,
int dir,
struct amount_msat *min,
struct amount_msat *max)
{
struct short_channel_id_dir scidd;
size_t idx = gossmap_chan_idx(rq->gossmap, chan);
*min = AMOUNT_MSAT(0);
/* Fast path: no information known, no reserve. */
if (idx < tal_count(rq->capacities) && rq->capacities[idx] != 0) {
*max = amount_msat(fp16_to_u64(rq->capacities[idx]) * 1000);
return;
}
/* Naive implementation! */
scidd.scid = gossmap_chan_scid(rq->gossmap, chan);
scidd.dir = dir;
*max = AMOUNT_MSAT(-1ULL);
/* Look through layers for any constraints (might be dummy
* ones, for created channels!) */
for (size_t i = 0; i < tal_count(rq->layers); i++)
layer_apply_constraints(rq->layers[i], &scidd, min, max);
/* Might be here because it's reserved, but capacity is normal. */
if (amount_msat_eq(*max, AMOUNT_MSAT(-1ULL)))
*max = gossmap_chan_get_capacity(rq->gossmap, chan);
/* Finally, if any is in use, subtract that! */
reserve_sub(rq->reserved, &scidd, rq->layers, min);
reserve_sub(rq->reserved, &scidd, rq->layers, max);
}
static void process_child_logs(struct command *cmd,
int log_fd)
{

View File

@@ -30,45 +30,6 @@ struct askrene {
u32 route_seconds;
};
/* Information for a single route query. */
struct route_query {
/* This is *not* updated during a query! Has all layers applied. */
const struct gossmap *gossmap;
/* command id to use for reservations we create. */
const char *cmd_id;
/* Array of layers we're applying */
const struct layer **layers;
/* Compact cache of biases */
const s8 *biases;
/* Additional per-htlc cost for local channels */
const struct additional_cost_htable *additional_costs;
/* We need to take in-flight payments into account (this is
* askrene->reserved, so make sure to undo changes! */
struct reserve_htable *reserved;
/* Cache of channel capacities for non-reserved, unknown channels. */
fp16_t *capacities;
/* channels we disable during computation to meet constraints */
bitmap *disabled_chans;
};
/* Given a gossmap channel, get the current known min/max */
void get_constraints(const struct route_query *rq,
const struct gossmap_chan *chan,
int dir,
struct amount_msat *min,
struct amount_msat *max);
/* Is there a known additional per-htlc cost for this channel? */
struct amount_msat get_additional_per_htlc_cost(const struct route_query *rq,
const struct short_channel_id_dir *scidd);
/* Useful plugin->askrene mapping */
static inline struct askrene *get_askrene(struct plugin *plugin)
{

View File

@@ -7,11 +7,11 @@
#include <common/json_stream.h>
#include <common/route.h>
#include <common/utils.h>
#include <plugins/askrene/askrene.h>
#include <plugins/askrene/child/child_log.h>
#include <plugins/askrene/child/entry.h>
#include <plugins/askrene/child/flow.h>
#include <plugins/askrene/child/mcf.h>
#include <plugins/askrene/child/route_query.h>
#include <unistd.h>
/* A single route. */

View File

@@ -3,9 +3,10 @@
#include <common/dijkstra.h>
#include <common/gossmap.h>
#include <common/route.h>
#include <plugins/askrene/askrene.h>
#include <common/utils.h>
#include <plugins/askrene/child/child_log.h>
#include <plugins/askrene/child/explain_failure.h>
#include <plugins/askrene/child/route_query.h>
#include <plugins/askrene/layer.h>
#include <plugins/askrene/reserve.h>

View File

@@ -5,9 +5,9 @@
#include <common/fp16.h>
#include <common/overflows.h>
#include <math.h>
#include <plugins/askrene/askrene.h>
#include <plugins/askrene/child/child_log.h>
#include <plugins/askrene/child/flow.h>
#include <plugins/askrene/child/route_query.h>
#include <plugins/libplugin.h>
#include <stdio.h>

View File

@@ -9,7 +9,6 @@
#include <float.h>
#include <inttypes.h>
#include <math.h>
#include <plugins/askrene/askrene.h>
#include <plugins/askrene/child/algorithm.h>
#include <plugins/askrene/child/child_log.h>
#include <plugins/askrene/child/dijkstra.h>
@@ -18,6 +17,7 @@
#include <plugins/askrene/child/graph.h>
#include <plugins/askrene/child/mcf.h>
#include <plugins/askrene/child/refine.h>
#include <plugins/askrene/child/route_query.h>
#include <stdint.h>
/* # Optimal payments

View File

@@ -3,10 +3,11 @@
#include <ccan/cast/cast.h>
#include <ccan/tal/str/str.h>
#include <common/gossmap.h>
#include <plugins/askrene/askrene.h>
#include <common/utils.h>
#include <plugins/askrene/child/child_log.h>
#include <plugins/askrene/child/flow.h>
#include <plugins/askrene/child/refine.h>
#include <plugins/askrene/child/route_query.h>
#include <plugins/askrene/reserve.h>
#include <string.h>

View File

@@ -0,0 +1,53 @@
#include "config.h"
#include <common/gossmap.h>
#include <plugins/askrene/child/additional_costs.h>
#include <plugins/askrene/child/route_query.h>
#include <plugins/askrene/layer.h>
#include <plugins/askrene/reserve.h>
struct amount_msat get_additional_per_htlc_cost(const struct route_query *rq,
const struct short_channel_id_dir *scidd)
{
const struct per_htlc_cost *phc;
phc = additional_cost_htable_get(rq->additional_costs, scidd);
if (phc)
return phc->per_htlc_cost;
else
return AMOUNT_MSAT(0);
}
void get_constraints(const struct route_query *rq,
const struct gossmap_chan *chan,
int dir,
struct amount_msat *min,
struct amount_msat *max)
{
struct short_channel_id_dir scidd;
size_t idx = gossmap_chan_idx(rq->gossmap, chan);
*min = AMOUNT_MSAT(0);
/* Fast path: no information known, no reserve. */
if (idx < tal_count(rq->capacities) && rq->capacities[idx] != 0) {
*max = amount_msat(fp16_to_u64(rq->capacities[idx]) * 1000);
return;
}
/* Naive implementation! */
scidd.scid = gossmap_chan_scid(rq->gossmap, chan);
scidd.dir = dir;
*max = AMOUNT_MSAT(-1ULL);
/* Look through layers for any constraints (might be dummy
* ones, for created channels!) */
for (size_t i = 0; i < tal_count(rq->layers); i++)
layer_apply_constraints(rq->layers[i], &scidd, min, max);
/* Might be here because it's reserved, but capacity is normal. */
if (amount_msat_eq(*max, AMOUNT_MSAT(-1ULL)))
*max = gossmap_chan_get_capacity(rq->gossmap, chan);
/* Finally, if any is in use, subtract that! */
reserve_sub(rq->reserved, &scidd, rq->layers, min);
reserve_sub(rq->reserved, &scidd, rq->layers, max);
}

View File

@@ -0,0 +1,46 @@
#ifndef LIGHTNING_PLUGINS_ASKRENE_CHILD_ROUTE_QUERY_H
#define LIGHTNING_PLUGINS_ASKRENE_CHILD_ROUTE_QUERY_H
#include "config.h"
#include <ccan/bitmap/bitmap.h>
/* Child-safe access routines for the route query. */
/* Information for a single route query. */
struct route_query {
/* This is *not* updated during a query! Has all layers applied. */
const struct gossmap *gossmap;
/* command id to use for reservations we create. */
const char *cmd_id;
/* Array of layers we're applying */
const struct layer **layers;
/* Compact cache of biases */
const s8 *biases;
/* Additional per-htlc cost for local channels */
const struct additional_cost_htable *additional_costs;
/* We need to take in-flight payments into account (this is
* askrene->reserved, so make sure to undo changes! */
struct reserve_htable *reserved;
/* Cache of channel capacities for non-reserved, unknown channels. */
fp16_t *capacities;
/* channels we disable during computation to meet constraints */
bitmap *disabled_chans;
};
/* Given a gossmap channel, get the current known min/max */
void get_constraints(const struct route_query *rq,
const struct gossmap_chan *chan,
int dir,
struct amount_msat *min,
struct amount_msat *max);
/* Is there a known additional per-htlc cost for this channel? */
struct amount_msat get_additional_per_htlc_cost(const struct route_query *rq,
const struct short_channel_id_dir *scidd);
#endif /* LIGHTNING_PLUGINS_ASKRENE_CHILD_ROUTE_QUERY_H */