Skip to content

Commit d85dcc0

Browse files
committed
askrene: persistent layer support.
Signed-off-by: Rusty Russell <[email protected]>
1 parent b2dcf72 commit d85dcc0

File tree

8 files changed

+651
-16
lines changed

8 files changed

+651
-16
lines changed

contrib/msggen/msggen/schema.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,13 @@
457457
"description": [
458458
"The name of the layer to create."
459459
]
460+
},
461+
"persistent": {
462+
"type": "boolean",
463+
"default": "False",
464+
"description": [
465+
"True if askrene should save and restore this layer. As a side-effect, create-layer also succeeds if the layer already exists and persistent is true."
466+
]
460467
}
461468
}
462469
},
@@ -473,6 +480,7 @@
473480
"additionalProperties": false,
474481
"required": [
475482
"layer",
483+
"persistent",
476484
"disabled_nodes",
477485
"created_channels",
478486
"channel_updates",
@@ -485,6 +493,12 @@
485493
"The name of the layer."
486494
]
487495
},
496+
"persistent": {
497+
"type": "boolean",
498+
"description": [
499+
"Whether the layer is saved/restored across restarts."
500+
]
501+
},
488502
"disabled_nodes": {
489503
"type": "array",
490504
"items": {
@@ -873,6 +887,7 @@
873887
"additionalProperties": false,
874888
"required": [
875889
"layer",
890+
"persistent",
876891
"disabled_nodes",
877892
"created_channels",
878893
"channel_updates",
@@ -885,6 +900,12 @@
885900
"The name of the layer."
886901
]
887902
},
903+
"persistent": {
904+
"type": "boolean",
905+
"description": [
906+
"Whether the layer is saved across restarts."
907+
]
908+
},
888909
"disabled_nodes": {
889910
"type": "array",
890911
"items": {

doc/schemas/lightning-askrene-create-layer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
"description": [
2020
"The name of the layer to create."
2121
]
22+
},
23+
"persistent": {
24+
"type": "boolean",
25+
"default": "False",
26+
"description": [
27+
"True if askrene should save and restore this layer. As a side-effect, create-layer also succeeds if the layer already exists and persistent is true."
28+
]
2229
}
2330
}
2431
},
@@ -35,6 +42,7 @@
3542
"additionalProperties": false,
3643
"required": [
3744
"layer",
45+
"persistent",
3846
"disabled_nodes",
3947
"created_channels",
4048
"channel_updates",
@@ -47,6 +55,12 @@
4755
"The name of the layer."
4856
]
4957
},
58+
"persistent": {
59+
"type": "boolean",
60+
"description": [
61+
"Whether the layer is saved/restored across restarts."
62+
]
63+
},
5064
"disabled_nodes": {
5165
"type": "array",
5266
"items": {

doc/schemas/lightning-askrene-listlayers.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"additionalProperties": false,
3434
"required": [
3535
"layer",
36+
"persistent",
3637
"disabled_nodes",
3738
"created_channels",
3839
"channel_updates",
@@ -45,6 +46,12 @@
4546
"The name of the layer."
4647
]
4748
},
49+
"persistent": {
50+
"type": "boolean",
51+
"description": [
52+
"Whether the layer is saved across restarts."
53+
]
54+
},
4855
"disabled_nodes": {
4956
"type": "array",
5057
"items": {

plugins/askrene/askrene.c

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,17 @@ static fp16_t *get_capacities(const tal_t *ctx,
175175
* but we won't. */
176176
/* FIXME: We could cache this until gossmap/layer changes... */
177177
static struct layer *source_free_layer(const tal_t *ctx,
178-
struct gossmap *gossmap,
178+
struct askrene *askrene,
179179
const struct node_id *source,
180180
struct gossmap_localmods *localmods)
181181
{
182182
/* We apply existing localmods so we see *all* channels */
183+
struct gossmap *gossmap = askrene->gossmap;
183184
const struct gossmap_node *srcnode;
184185
const struct amount_msat zero_base_fee = AMOUNT_MSAT(0);
185186
const u16 zero_delay = 0;
186187
const u32 zero_prop_fee = 0;
187-
struct layer *layer = new_temp_layer(ctx, "auto.sourcefree");
188+
struct layer *layer = new_temp_layer(ctx, askrene, "auto.sourcefree");
188189

189190
/* We apply this so we see any created channels */
190191
gossmap_apply_localmods(gossmap, localmods);
@@ -363,7 +364,7 @@ static const char *get_routes(const tal_t *ctx,
363364
/* Handled below, after other layers */
364365
assert(streq(layers[i], "auto.sourcefree"));
365366
plugin_log(rq->plugin, LOG_DBG, "Adding auto.sourcefree");
366-
l = source_free_layer(layers, askrene->gossmap, source, localmods);
367+
l = source_free_layer(layers, askrene, source, localmods);
367368
}
368369
}
369370

@@ -732,11 +733,12 @@ listpeerchannels_done(struct command *cmd,
732733
const jsmntok_t *toks,
733734
struct getroutes_info *info)
734735
{
736+
struct askrene *askrene = get_askrene(cmd->plugin);
735737
struct gossmap_localmods *localmods;
736738

737-
info->local_layer = new_temp_layer(info, "auto.localchans");
739+
info->local_layer = new_temp_layer(info, askrene, "auto.localchans");
738740
localmods = gossmods_from_listpeerchannels(cmd,
739-
&get_askrene(cmd->plugin)->my_id,
741+
&askrene->my_id,
740742
buffer, toks,
741743
false,
742744
add_localchan,
@@ -1066,24 +1068,33 @@ static struct command_result *json_askrene_create_layer(struct command *cmd,
10661068
struct layer *layer;
10671069
const char *layername;
10681070
struct json_stream *response;
1071+
bool *persistent;
10691072

10701073
if (!param_check(cmd, buffer, params,
10711074
p_req("layer", param_string, &layername),
1075+
p_opt_def("persistent", param_bool, &persistent, false),
10721076
NULL))
10731077
return command_param_failed();
10741078

1075-
if (find_layer(askrene, layername))
1076-
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1077-
"Layer already exists");
1078-
10791079
if (strstarts(layername, "auto."))
10801080
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
10811081
"Cannot create auto layer");
10821082

1083+
/* If it's persistent, creation is a noop if it already exists */
1084+
layer = find_layer(askrene, layername);
1085+
if (layer && !*persistent) {
1086+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
1087+
"Layer already exists");
1088+
}
1089+
10831090
if (command_check_only(cmd))
10841091
return command_check_done(cmd);
10851092

1086-
layer = new_layer(askrene, layername);
1093+
if (!layer) {
1094+
layer = new_layer(askrene, layername, *persistent);
1095+
if (*persistent)
1096+
save_new_layer(layer);
1097+
}
10871098

10881099
response = jsonrpc_stream_success(cmd);
10891100
json_add_layers(response, askrene, "layers", layer);
@@ -1102,7 +1113,7 @@ static struct command_result *json_askrene_remove_layer(struct command *cmd,
11021113
NULL))
11031114
return command_param_failed();
11041115

1105-
tal_free(layer);
1116+
remove_layer(layer);
11061117

11071118
response = jsonrpc_stream_success(cmd);
11081119
return command_finished(cmd, response);
@@ -1230,6 +1241,10 @@ static const char *init(struct command *init_cmd,
12301241

12311242
plugin_set_data(plugin, askrene);
12321243
plugin_set_memleak_handler(plugin, askrene_markmem);
1244+
1245+
/* Layer needs its own command to access datastore */
1246+
askrene->layer_cmd = aux_command(init_cmd);
1247+
load_layers(askrene);
12331248
return NULL;
12341249
}
12351250

plugins/askrene/askrene.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct askrene {
3131
fp16_t *capacities;
3232
/* My own id */
3333
struct node_id my_id;
34+
/* Aux command for layer */
35+
struct command *layer_cmd;
3436
};
3537

3638
/* Information for a single route query. */

0 commit comments

Comments
 (0)