-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathlayer.c
More file actions
1558 lines (1371 loc) · 42.4 KB
/
Copy pathlayer.c
File metadata and controls
1558 lines (1371 loc) · 42.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "config.h"
#include <ccan/array_size/array_size.h>
#include <ccan/htable/htable_type.h>
#include <ccan/json_out/json_out.h>
#include <ccan/tal/str/str.h>
#include <common/clock_time.h>
#include <common/gossmap.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <plugins/askrene/askrene.h>
#include <plugins/askrene/datastore_wire.h>
#include <plugins/askrene/layer.h>
#include <wire/wire.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
/* A channels which doesn't (necessarily) exist in the gossmap. */
struct local_channel {
/* Canonical order, n1 < n2 */
struct node_id n1, n2;
struct short_channel_id scid;
struct amount_msat capacity;
};
struct local_update {
struct short_channel_id_dir scidd;
/* Non-null fields apply. */
const bool *enabled;
const u16 *delay;
const u32 *proportional_fee;
const struct amount_msat *base_fee;
const struct amount_msat *htlc_min, *htlc_max;
};
/* A bias, for special-effects (user-controlled) */
struct bias {
struct short_channel_id_dir scidd;
const char *description;
s8 bias;
u64 timestamp;
};
struct node_bias {
struct node_id node;
const char *description;
s8 in_bias, out_bias;
u64 timestamp;
};
static struct short_channel_id
channel_intel_scid(const struct channel_intel *intelarr)
{
if (intelarr[0].impression)
return intelarr[0].impression->scidd.scid;
return intelarr[0].constraint->scidd.scid;
}
static inline bool channel_intel_eq_scid(const struct channel_intel *intelarr,
struct short_channel_id scid)
{
return short_channel_id_eq(scid, channel_intel_scid(intelarr));
}
HTABLE_DEFINE_NODUPS_TYPE(struct channel_intel, channel_intel_scid, hash_scid,
channel_intel_eq_scid, channel_intel_hash);
static struct short_channel_id
local_channel_scid(const struct local_channel *lc)
{
return lc->scid;
}
static inline bool local_channel_eq_scid(const struct local_channel *lc,
const struct short_channel_id scid)
{
return short_channel_id_eq(scid, lc->scid);
}
HTABLE_DEFINE_NODUPS_TYPE(struct local_channel, local_channel_scid, hash_scid,
local_channel_eq_scid, local_channel_hash);
static const struct short_channel_id_dir *
local_update_scidd(const struct local_update *lu)
{
return &lu->scidd;
}
static inline bool local_update_eq_scidd(const struct local_update *lu,
const struct short_channel_id_dir *scidd)
{
return short_channel_id_dir_eq(scidd, &lu->scidd);
}
HTABLE_DEFINE_NODUPS_TYPE(struct local_update, local_update_scidd, hash_scidd,
local_update_eq_scidd, local_update_hash);
static const struct short_channel_id_dir *
bias_scidd(const struct bias *bias)
{
return &bias->scidd;
}
static bool bias_eq_scidd(const struct bias *bias,
const struct short_channel_id_dir *scidd)
{
return short_channel_id_dir_eq(scidd, &bias->scidd);
}
HTABLE_DEFINE_NODUPS_TYPE(struct bias, bias_scidd, hash_scidd,
bias_eq_scidd, bias_hash);
static const struct node_id *bias_nodeid(const struct node_bias *bias)
{
return &bias->node;
}
static size_t hash_nodeid(const struct node_id *node)
{
return *(size_t *)(node->k);
}
static bool bias_eq_nodeid(const struct node_bias *bias,
const struct node_id *node)
{
return node_id_eq(node, &bias->node);
}
HTABLE_DEFINE_NODUPS_TYPE(struct node_bias, bias_nodeid, hash_nodeid,
bias_eq_nodeid, node_bias_hash);
struct layer {
/* Convenience pointer to askrene */
struct askrene *askrene;
/* Unique identifiers */
const char *name;
/* Save to datastore */
bool persistent;
/* Completely made up local additions, indexed by scid */
struct local_channel_hash *local_channels;
/* Modifications to channels, indexed by scidd */
struct local_update_hash *local_updates;
/* Constraints and impressions, indexed by scid */
struct channel_intel_hash *channel_intels;
/* Bias, indexed by scid+dir */
struct bias_hash *biases;
/* Node bias, indexed by node_id */
struct node_bias_hash *node_biases;
/* Nodes to completely disable (tal_arr) */
struct node_id *disabled_nodes;
};
static size_t hash_str(const char *str)
{
return siphash24(siphash_seed(), str, strlen(str));
}
static bool layer_eq_name(const struct layer *l,
const char *name)
{
return streq(l->name, name);
}
HTABLE_DEFINE_NODUPS_TYPE(struct layer, layer_name, hash_str,
layer_eq_name, layer_name_hash);
struct layer_name_hash *new_layer_name_hash(const tal_t *ctx)
{
return new_htable(ctx, layer_name_hash);
}
struct layer *new_temp_layer(const tal_t *ctx, struct askrene *askrene, const char *name TAKES)
{
struct layer *l = tal(ctx, struct layer);
l->askrene = askrene;
l->name = tal_strdup(l, name);
l->persistent = false;
l->local_channels = new_htable(l, local_channel_hash);
l->local_updates = new_htable(l, local_update_hash);
l->channel_intels = new_htable(l, channel_intel_hash);
l->biases = new_htable(l, bias_hash);
l->node_biases = new_htable(l, node_bias_hash);
l->disabled_nodes = tal_arr(l, struct node_id, 0);
return l;
}
static void destroy_layer(struct layer *l, struct askrene *askrene)
{
if (!layer_name_hash_del(askrene->layers, l))
abort();
}
/* Low-level versions of routines which do *not* save (used for loading, too) */
static struct layer *add_layer(struct askrene *askrene, const char *name TAKES, bool persistent)
{
struct layer *l = new_temp_layer(askrene, askrene, name);
l->persistent = persistent;
assert(!find_layer(askrene, l->name));
layer_name_hash_add(askrene->layers, l);
tal_add_destructor2(l, destroy_layer, askrene);
return l;
}
static struct local_channel *add_local_channel(struct layer *layer,
const struct node_id *n1,
const struct node_id *n2,
struct short_channel_id scid,
struct amount_msat capacity)
{
struct local_channel *lc = tal(layer, struct local_channel);
/* Swap if necessary to make into BOLT-7 order. */
if (node_id_cmp(n1, n2) < 0) {
lc->n1 = *n1;
lc->n2 = *n2;
} else {
assert(!node_id_eq(n1, n2));
lc->n1 = *n2;
lc->n2 = *n1;
}
lc->scid = scid;
lc->capacity = capacity;
local_channel_hash_add(layer->local_channels, lc);
return lc;
}
static struct local_update *add_update_channel(struct layer *layer,
const struct short_channel_id_dir *scidd,
const bool *enabled,
const struct amount_msat *htlc_min,
const struct amount_msat *htlc_max,
const struct amount_msat *base_fee,
const u32 *proportional_fee,
const u16 *delay)
{
struct local_update *lu;
lu = local_update_hash_get(layer->local_updates, scidd);
if (!lu) {
lu = tal(layer, struct local_update);
lu->scidd = *scidd;
lu->enabled = NULL;
lu->delay = NULL;
lu->proportional_fee = NULL;
lu->base_fee = lu->htlc_min = lu->htlc_max = NULL;
local_update_hash_add(layer->local_updates, lu);
}
if (enabled) {
tal_free(lu->enabled);
lu->enabled = tal_dup(lu, bool, enabled);
}
if (htlc_min) {
tal_free(lu->htlc_min);
lu->htlc_min = tal_dup(lu, struct amount_msat, htlc_min);
}
if (htlc_max) {
tal_free(lu->htlc_max);
lu->htlc_max = tal_dup(lu, struct amount_msat, htlc_max);
}
if (base_fee) {
tal_free(lu->base_fee);
lu->base_fee = tal_dup(lu, struct amount_msat, base_fee);
}
if (proportional_fee) {
tal_free(lu->proportional_fee);
lu->proportional_fee = tal_dup(lu, u32, proportional_fee);
}
if (delay) {
tal_free(lu->delay);
lu->delay = tal_dup(lu, u16, delay);
}
return lu;
}
/* Insert this constraint/impression in htable, maintaining timestamp order */
static void add_channel_intel(struct layer *layer,
const struct constraint *constraint STEALS,
const struct impression *impression STEALS)
{
struct channel_intel intel, *intelarr;
intel.impression = impression;
intel.constraint = constraint;
/* Exactly one is set */
if (constraint)
assert(!impression);
else
assert(impression);
intelarr = channel_intel_hash_get(layer->channel_intels, channel_intel_scid(&intel));
if (!intelarr) {
intelarr = tal_dup(layer->channel_intels, struct channel_intel, &intel);
goto done;
}
/* Insert in timestamp order, then insertion order. Realloc
* mean we have to delete, readd */
channel_intel_hash_del(layer->channel_intels, intelarr);
for (size_t i = 0; i < tal_count(intelarr); i++) {
if (channel_intel_timestamp(&intel) < channel_intel_timestamp(&intelarr[i])) {
tal_arr_insert(&intelarr, i, intel);
goto done;
}
}
tal_arr_expand(&intelarr, intel);
done:
channel_intel_hash_add(layer->channel_intels, intelarr);
/* Make sure array owns the impression/constraint, to avoid memleak */
tal_steal(intelarr, intel.impression);
tal_steal(intelarr, intel.constraint);
}
static const struct constraint *add_constraint(struct layer *layer,
const struct short_channel_id_dir *scidd,
u64 timestamp,
const struct amount_msat *min,
const struct amount_msat *max)
{
struct constraint *c = tal(NULL, struct constraint);
c->scidd = *scidd;
if (min)
c->min = *min;
else
c->min = AMOUNT_MSAT(0);
if (max)
c->max = *max;
else
c->max = AMOUNT_MSAT(UINT64_MAX);
c->timestamp = timestamp;
add_channel_intel(layer, c, NULL);
return c;
}
static const struct impression *add_impression(struct layer *layer,
const struct short_channel_id_dir *scidd,
u64 timestamp,
struct amount_msat amount)
{
struct impression *imp = tal(NULL, struct impression);
imp->scidd = *scidd;
imp->amount = amount;
imp->timestamp = timestamp;
add_channel_intel(layer, NULL, imp);
return imp;
}
static const struct bias *set_bias(struct layer *layer,
const struct short_channel_id_dir *scidd,
const char *description TAKES,
s8 bias_factor,
bool relative,
u64 timestamp)
{
struct bias *bias;
bias = bias_hash_get(layer->biases, scidd);
if (!bias) {
bias = tal(layer, struct bias);
bias->scidd = *scidd;
bias_hash_add(layer->biases, bias);
bias->bias = 0;
} else {
tal_free(bias->description);
}
int bias_new = relative ? bias->bias + bias_factor : bias_factor;
bias_new = MIN(100, bias_new);
bias_new = MAX(-100, bias_new);
bias->bias = bias_new;
bias->description = tal_strdup_or_null(bias, description);
bias->timestamp = timestamp;
/* Don't bother keeping around zero biases */
if (bias->bias == 0) {
bias_hash_del(layer->biases, bias);
bias = tal_free(bias);
}
return bias;
}
static const struct node_bias *set_node_bias(struct layer *layer,
const struct node_id *node,
const char *description TAKES,
s8 bias_factor,
bool relative,
bool dir_out,
u64 timestamp)
{
struct node_bias *bias;
bias = node_bias_hash_get(layer->node_biases, node);
if (!bias) {
bias = tal(layer, struct node_bias);
bias->node = *node;
node_bias_hash_add(layer->node_biases, bias);
bias->in_bias = 0;
bias->out_bias = 0;
} else {
tal_free(bias->description);
}
bias->description = tal_strdup_or_null(bias, description);
bias->timestamp = timestamp;
if (dir_out) {
int bias_new =
relative ? bias->out_bias + bias_factor : bias_factor;
bias_new = MIN(100, bias_new);
bias_new = MAX(-100, bias_new);
bias->out_bias = bias_new;
} else {
int bias_new =
relative ? bias->in_bias + bias_factor : bias_factor;
bias_new = MIN(100, bias_new);
bias_new = MAX(-100, bias_new);
bias->in_bias = bias_new;
}
/* Don't bother keeping around zero biases */
if (bias->in_bias == 0 && bias->out_bias == 0) {
node_bias_hash_del(layer->node_biases, bias);
bias = tal_free(bias);
}
return bias;
}
static void add_disabled_node(struct layer *layer, const struct node_id *node)
{
tal_arr_expand(&layer->disabled_nodes, *node);
}
static struct command_result *ignore_result(struct command *aux_cmd,
const char *method,
const char *buf,
const jsmntok_t *result,
void *arg)
{
return command_still_pending(aux_cmd);
}
static void json_add_layer_key(struct json_stream *js,
const char *fieldname,
const struct layer *layer)
{
json_array_start(js, fieldname);
json_add_string(js, NULL, "askrene");
json_add_string(js, NULL, "layers");
json_add_string(js, NULL, layer->name);
json_array_end(js);
}
static void save_remove(struct layer *layer)
{
struct out_req *req;
if (!layer->persistent)
return;
req = jsonrpc_request_start(layer->askrene->layer_cmd,
"deldatastore",
ignore_result,
plugin_broken_cb,
NULL);
json_add_layer_key(req->js, "key", layer);
send_outreq(req);
}
static void append_layer_datastore(struct layer *layer, const u8 *data)
{
struct out_req *req = jsonrpc_request_start(layer->askrene->layer_cmd,
"datastore",
ignore_result,
plugin_broken_cb,
NULL);
json_add_layer_key(req->js, "key", layer);
json_add_hex_talarr(req->js, "hex", data);
json_add_string(req->js, "mode", "create-or-append");
send_outreq(req);
}
static void towire_save_channel(u8 **data, const struct local_channel *lc)
{
towire_dstore_channel(data, &lc->n1, &lc->n2, lc->scid, lc->capacity);
}
static void save_channel(struct layer *layer, const struct local_channel *lc)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_save_channel(&data, lc);
append_layer_datastore(layer, data);
}
static void load_channel(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct node_id n1, n2;
struct short_channel_id scid;
struct amount_msat capacity;
if (fromwire_dstore_channel(cursor, len, &n1, &n2, &scid, &capacity))
add_local_channel(layer, &n1, &n2, scid, capacity);
}
static void towire_save_channel_update(u8 **data, const struct local_update *lu)
{
towire_dstore_channel_update(data,
&lu->scidd,
lu->enabled,
lu->htlc_min,
lu->htlc_max,
lu->base_fee,
lu->proportional_fee,
lu->delay);
}
static void save_channel_update(struct layer *layer, const struct local_update *lu)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_save_channel_update(&data, lu);
append_layer_datastore(layer, data);
}
static void load_channel_update(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct short_channel_id_dir scidd;
bool *enabled;
struct amount_msat *htlc_min, *htlc_max, *base_fee;
u32 *proportional_fee;
u16 *delay;
if (fromwire_dstore_channel_update(tmpctx, cursor, len,
&scidd,
&enabled,
&htlc_min,
&htlc_max,
&base_fee,
&proportional_fee,
&delay))
add_update_channel(layer, &scidd,
enabled,
htlc_min,
htlc_max,
base_fee,
proportional_fee,
delay);
}
static void towire_save_channel_constraint(u8 **data, const struct constraint *c)
{
towire_dstore_channel_constraint(data, &c->scidd, c->timestamp,
amount_msat_is_zero(c->min) ? NULL : &c->min,
amount_msat_eq(c->max, AMOUNT_MSAT(UINT64_MAX)) ? NULL: &c->max);
}
static void save_channel_constraint(struct layer *layer, const struct constraint *c)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_save_channel_constraint(&data, c);
append_layer_datastore(layer, data);
}
static void load_channel_constraint(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct short_channel_id_dir scidd;
struct amount_msat *min;
struct amount_msat *max;
u64 timestamp;
if (fromwire_dstore_channel_constraint(tmpctx, cursor, len,
&scidd, ×tamp,
&min, &max))
add_constraint(layer, &scidd, timestamp, min, max);
}
static void towire_save_channel_impression(u8 **data, const struct impression *imp)
{
towire_dstore_channel_impression(data, &imp->scidd, imp->timestamp, imp->amount);
}
static void save_channel_impression(struct layer *layer, const struct impression *imp)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_save_channel_impression(&data, imp);
append_layer_datastore(layer, data);
}
static void load_channel_impression(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct short_channel_id_dir scidd;
struct amount_msat amount;
u64 timestamp;
if (fromwire_dstore_channel_impression(tmpctx, cursor, len,
&scidd, ×tamp,
&amount))
add_impression(layer, &scidd, timestamp, amount);
}
static void towire_save_channel_bias(u8 **data, const struct bias *bias)
{
towire_dstore_channel_bias_v2(data,
&bias->scidd,
bias->bias,
bias->description,
bias->timestamp);
}
static void save_channel_bias(struct layer *layer, const struct bias *bias)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_save_channel_bias(&data, bias);
append_layer_datastore(layer, data);
}
static void towire_save_node_bias(u8 **data, const struct node_bias *bias)
{
towire_dstore_node_bias(data,
&bias->node,
bias->description,
bias->in_bias,
bias->out_bias,
bias->timestamp);
}
static void save_node_bias(struct layer *layer, const struct node_bias *bias)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_save_node_bias(&data, bias);
append_layer_datastore(layer, data);
}
static void load_channel_bias(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct short_channel_id_dir scidd;
const char *description;
s8 bias_factor;
/* If we read an old version without timestamp, just put the current
* time. */
u64 timestamp = clock_time().ts.tv_sec;
if (fromwire_dstore_channel_bias(tmpctx, cursor, len,
&scidd,
&bias_factor,
&description))
set_bias(layer, &scidd, take(description), bias_factor, false,
timestamp);
}
static void load_channel_bias_v2(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct short_channel_id_dir scidd;
const char *description;
s8 bias_factor;
u64 timestamp;
if (fromwire_dstore_channel_bias_v2(tmpctx, cursor, len,
&scidd,
&bias_factor,
&description,
×tamp))
set_bias(layer, &scidd, take(description), bias_factor, false,
timestamp);
}
static void load_node_bias(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct node_id node;
const char *description;
s8 in_bias, out_bias;
u64 timestamp;
if (fromwire_dstore_node_bias(tmpctx, cursor, len,
&node,
&description,
&in_bias,
&out_bias,
×tamp)) {
set_node_bias(layer, &node, take(description), in_bias,
/* relative = */ false,
/* out dir = */ false, timestamp);
set_node_bias(layer, &node, take(description), out_bias,
/* relative = */ false,
/* out dir = */ true, timestamp);
}
}
static void save_disabled_node(struct layer *layer, const struct node_id *node)
{
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
towire_dstore_disabled_node(&data, node);
append_layer_datastore(layer, data);
}
static void load_disabled_node(struct plugin *plugin,
struct layer *layer,
const u8 **cursor,
size_t *len)
{
struct node_id node;
if (fromwire_dstore_disabled_node(cursor, len, &node))
add_disabled_node(layer, &node);
}
static void save_complete_layer(struct layer *layer)
{
struct local_channel_hash_iter lcit;
const struct local_channel *lc;
const struct local_update *lu;
struct local_update_hash_iter luit;
const struct channel_intel *intelarr;
struct channel_intel_hash_iter intelit;
struct bias_hash_iter biasit;
const struct bias *b;
struct node_bias_hash_iter nbiasit;
const struct node_bias *nb;
struct out_req *req;
u8 *data;
if (!layer->persistent)
return;
data = tal_arr(tmpctx, u8, 0);
for (size_t i = 0; i < tal_count(layer->disabled_nodes); i++)
towire_dstore_disabled_node(&data, &layer->disabled_nodes[i]);
for (lc = local_channel_hash_first(layer->local_channels, &lcit);
lc;
lc = local_channel_hash_next(layer->local_channels, &lcit)) {
towire_save_channel(&data, lc);
}
for (lu = local_update_hash_first(layer->local_updates, &luit);
lu;
lu = local_update_hash_next(layer->local_updates, &luit)) {
towire_save_channel_update(&data, lu);
}
for (intelarr = channel_intel_hash_first(layer->channel_intels, &intelit);
intelarr;
intelarr = channel_intel_hash_next(layer->channel_intels, &intelit)) {
for (size_t i = 0; i < tal_count(intelarr); i++) {
if (intelarr[i].constraint) {
/* Don't save ones we generated internally */
if (intelarr[i].constraint->timestamp == UINT64_MAX)
continue;
towire_save_channel_constraint(&data, intelarr[i].constraint);
} else {
towire_save_channel_impression(&data, intelarr[i].impression);
}
}
}
for (b = bias_hash_first(layer->biases, &biasit);
b;
b = bias_hash_next(layer->biases, &biasit)) {
towire_save_channel_bias(&data, b);
}
for (nb = node_bias_hash_first(layer->node_biases, &nbiasit); nb;
nb = node_bias_hash_next(layer->node_biases, &nbiasit)) {
towire_save_node_bias(&data, nb);
}
/* Wholesale replacement */
req = jsonrpc_request_start(layer->askrene->layer_cmd,
"datastore",
ignore_result,
plugin_broken_cb,
NULL);
json_add_layer_key(req->js, "key", layer);
json_add_hex_talarr(req->js, "hex", data);
json_add_string(req->js, "mode", "create-or-replace");
send_outreq(req);
}
static void populate_layer(struct askrene *askrene,
const char *layername TAKES,
const u8 *data)
{
struct layer *layer;
size_t len = tal_bytelen(data);
layer = add_layer(askrene, layername, true);
plugin_log(askrene->plugin, LOG_DBG,
"Loaded level %s (%zu bytes)",
layer->name, len);
while (len != 0) {
enum dstore_layer_type type;
type = fromwire_peektypen(data, len);
switch (type) {
case DSTORE_CHANNEL:
load_channel(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_CHANNEL_UPDATE:
load_channel_update(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_CHANNEL_CONSTRAINT:
load_channel_constraint(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_CHANNEL_BIAS:
load_channel_bias(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_CHANNEL_BIAS_V2:
load_channel_bias_v2(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_DISABLED_NODE:
load_disabled_node(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_NODE_BIAS:
load_node_bias(askrene->plugin, layer, &data, &len);
continue;
case DSTORE_CHANNEL_IMPRESSION:
load_channel_impression(askrene->plugin, layer, &data, &len);
continue;
}
plugin_err(askrene->plugin, "Invalid type %i in datastore: layer %s %s",
type, layer->name, tal_hexstr(tmpctx, data, len));
}
if (!data)
plugin_log(askrene->plugin, LOG_BROKEN,
"%s: invalid data in datastore",
layer->name);
}
void load_layers(struct askrene *askrene, struct command *init_cmd)
{
struct json_out *params = json_out_new(init_cmd);
const jsmntok_t *result;
const char *buf;
const jsmntok_t *datastore, *t, *key, *data;
size_t i;
json_out_start(params, NULL, '{');
json_out_start(params, "key", '[');
json_out_addstr(params, NULL, "askrene");
json_out_addstr(params, NULL, "layers");
json_out_end(params, ']');
json_out_end(params, '}');
result = jsonrpc_request_sync(tmpctx, init_cmd,
"listdatastore",
params, &buf);
datastore = json_get_member(buf, result, "datastore");
json_for_each_arr(i, t, datastore) {
const char *layername;
/* Key is an array, first two elements are askrene, layers */
key = json_get_member(buf, t, "key") + 3;
data = json_get_member(buf, t, "hex");
/* In case someone creates a subdir? */
if (!data)
continue;
layername = json_strdup(NULL, buf, key);
populate_layer(askrene,
take(layername),
json_tok_bin_from_hex(tmpctx, buf, data));
}
}
void remove_layer(struct layer *l)
{
save_remove(l);
tal_free(l);
}
struct layer *new_layer(struct askrene *askrene,
const char *name TAKES,
bool persistent)
{
struct layer *layer = add_layer(askrene, name, persistent);
if (persistent)
save_complete_layer(layer);
return layer;
}
struct layer *find_layer(struct askrene *askrene, const char *name)
{
return layer_name_hash_get(askrene->layers, name);
}
const char *layer_name(const struct layer *layer)
{
return layer->name;
}
void layer_add_local_channel(struct layer *layer,
const struct node_id *src,
const struct node_id *dst,
struct short_channel_id scid,
struct amount_msat capacity)
{
struct local_channel *lc;
lc = add_local_channel(layer, src, dst, scid, capacity);
save_channel(layer, lc);
}
void layer_add_update_channel(struct layer *layer,
const struct short_channel_id_dir *scidd,
const bool *enabled,
const struct amount_msat *htlc_min,
const struct amount_msat *htlc_max,
const struct amount_msat *base_fee,
const u32 *proportional_fee,
const u16 *delay)
{
struct local_update *lu;
lu = add_update_channel(layer, scidd, enabled,
htlc_min, htlc_max,
base_fee, proportional_fee,
delay);
save_channel_update(layer, lu);
}
void layer_remove_channel_update(struct layer *layer,
const struct short_channel_id_dir *scidd)
{
struct local_update *lu;
lu = local_update_hash_get(layer->local_updates, scidd);
if (lu) {
local_update_hash_del(layer->local_updates, lu);
tal_free(lu);
}
}
const struct bias *layer_set_bias(struct layer *layer,
const struct short_channel_id_dir *scidd,
const char *description TAKES,
s8 bias_factor,
bool relative,
u64 timestamp)