forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossmap_manage.c
1375 lines (1179 loc) · 41.1 KB
/
gossmap_manage.c
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 <bitcoin/script.h>
#include <ccan/array_size/array_size.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/daemon_conn.h>
#include <common/gossip_store.h>
#include <common/gossmap.h>
#include <common/status.h>
#include <common/timeout.h>
#include <common/wire_error.h>
#include <errno.h>
#include <fcntl.h>
#include <gossipd/gossip_store.h>
#include <gossipd/gossip_store_wiregen.h>
#include <gossipd/gossipd.h>
#include <gossipd/gossipd_wiregen.h>
#include <gossipd/gossmap_manage.h>
#include <gossipd/seeker.h>
#include <gossipd/sigcheck.h>
#include <gossipd/txout_failures.h>
#include <string.h>
struct pending_cannounce {
const u8 *scriptpubkey;
const u8 *channel_announcement;
struct node_id node_id[2];
const struct node_id *source_peer;
};
struct pending_cupdate {
struct short_channel_id scid;
secp256k1_ecdsa_signature signature;
u8 message_flags;
u8 channel_flags;
u16 cltv_expiry_delta;
struct amount_msat htlc_minimum_msat, htlc_maximum_msat;
u32 fee_base_msat, fee_proportional_millionths;
u32 timestamp;
const u8 *update;
const struct node_id *source_peer;
};
struct pending_nannounce {
struct node_id node_id;
u32 timestamp;
const u8 *nannounce;
const struct node_id *source_peer;
};
struct cannounce_map {
UINTMAP(struct pending_cannounce *) map;
size_t count;
/* Name, for flood reporting */
const char *name;
bool flood_reported;
};
struct gossmap_manage {
struct daemon *daemon;
/* For us to write to gossip_store */
int fd;
/* gossip map itself (access via gossmap_manage_get_gossmap, so it's fresh!) */
struct gossmap *raw_gossmap;
/* Announcements we're checking, indexed by scid */
struct cannounce_map pending_ann_map;
/* Updates we've deferred for above */
struct pending_cupdate **pending_cupdates;
/* Announcements which are too early to check. */
struct cannounce_map early_ann_map;
struct pending_cupdate **early_cupdates;
/* Node announcements (waiting for a pending_cannounce maybe) */
struct pending_nannounce **pending_nannounces;
/* Lookups we've failed recently */
struct txout_failures *txf;
/* Blockheights of scids to remove */
struct chan_dying *dying_channels;
/* Occasional check for dead channels */
struct oneshot *prune_timer;
};
/* Timer recursion */
static void start_prune_timer(struct gossmap_manage *gm);
static void enqueue_cupdate(struct pending_cupdate ***queue,
struct short_channel_id scid,
const secp256k1_ecdsa_signature *signature,
u8 message_flags,
u8 channel_flags,
u16 cltv_expiry_delta,
struct amount_msat htlc_minimum_msat,
struct amount_msat htlc_maximum_msat,
u32 fee_base_msat,
u32 fee_proportional_millionths,
u32 timestamp,
const u8 *update TAKES,
const struct node_id *source_peer TAKES)
{
struct pending_cupdate *pcu = tal(*queue, struct pending_cupdate);
pcu->scid = scid;
pcu->signature = *signature;
pcu->message_flags = message_flags;
pcu->channel_flags = channel_flags;
pcu->cltv_expiry_delta = cltv_expiry_delta;
pcu->htlc_minimum_msat = htlc_minimum_msat;
pcu->htlc_maximum_msat = htlc_maximum_msat;
pcu->fee_base_msat = fee_base_msat;
pcu->fee_proportional_millionths = fee_proportional_millionths;
pcu->timestamp = timestamp;
pcu->update = tal_dup_talarr(pcu, u8, update);
pcu->source_peer = tal_dup_or_null(pcu, struct node_id, source_peer);
tal_arr_expand(queue, pcu);
}
static void enqueue_nannounce(struct pending_nannounce ***queue,
const struct node_id *node_id,
u32 timestamp,
const u8 *nannounce TAKES,
const struct node_id *source_peer TAKES)
{
struct pending_nannounce *pna = tal(*queue, struct pending_nannounce);
pna->node_id = *node_id;
pna->timestamp = timestamp;
pna->nannounce = tal_dup_talarr(pna, u8, nannounce);
pna->source_peer = tal_dup_or_null(pna, struct node_id, source_peer);
tal_arr_expand(queue, pna);
}
/* Helpers to keep counters in sync with maps! */
static void map_init(struct cannounce_map *map, const char *name)
{
uintmap_init(&map->map);
map->count = 0;
map->name = name;
map->flood_reported = false;
}
static bool map_add(struct cannounce_map *map,
struct short_channel_id scid,
struct pending_cannounce *pca)
{
/* More than 10000 pending things? Stop! */
if (map->count > 10000) {
if (!map->flood_reported) {
status_unusual("%s being flooded by %s: dropping some",
map->name,
pca->source_peer
? fmt_node_id(tmpctx, pca->source_peer)
: "unknown");
map->flood_reported = true;
}
return false;
}
if (uintmap_add(&map->map, scid.u64, pca)) {
map->count++;
return true;
}
return false;
}
static struct pending_cannounce *map_del(struct cannounce_map *map,
struct short_channel_id scid)
{
struct pending_cannounce *pca = uintmap_del(&map->map, scid.u64);
if (pca) {
assert(map->count);
map->count--;
if (map->flood_reported && uintmap_empty(&map->map)) {
status_unusual("%s flood has subsided", map->name);
map->flood_reported = false;
}
}
return pca;
}
static bool map_empty(const struct cannounce_map *map)
{
if (uintmap_empty(&map->map)) {
assert(map->count == 0);
return true;
}
assert(map->count != 0);
return false;
}
static struct pending_cannounce *map_get(struct cannounce_map *map,
struct short_channel_id scid)
{
return uintmap_get(&map->map, scid.u64);
}
/* Does any channel_announcement preceed this offset in the gossip_store? */
static bool any_cannounce_preceeds_offset(struct gossmap *gossmap,
const struct gossmap_node *node,
const struct gossmap_chan *exclude_chan,
u64 offset)
{
for (size_t i = 0; i < node->num_chans; i++) {
struct gossmap_chan *chan = gossmap_nth_chan(gossmap, node, i, NULL);
if (chan == exclude_chan)
continue;
if (chan->cann_off > offset)
continue;
/* Dying channels don't help! */
if (gossmap_chan_is_dying(gossmap, chan))
continue;
return true;
}
return false;
}
/* Are all channels associated with this node dying? (Perhaps ignoring one)*/
static bool all_node_channels_dying(struct gossmap *gossmap,
const struct gossmap_node *n,
const struct gossmap_chan *ignore)
{
for (size_t i = 0; i < n->num_chans; i++) {
const struct gossmap_chan *c = gossmap_nth_chan(gossmap, n, i, NULL);
if (c != ignore && !gossmap_chan_is_dying(gossmap, c))
return false;
}
return true;
}
/* To actually remove a channel:
* - Suppress future lookups in case we receive another channel_update.
* - Put deleted tombstone in gossip_store.
* - Mark records deleted in gossip_store.
* - See if node_announcement(s) need to be removed, marked dying, or moved.
*/
static void remove_channel(struct gossmap_manage *gm,
struct gossmap *gossmap,
struct gossmap_chan *chan,
struct short_channel_id scid)
{
/* Suppress any now-obsolete updates/announcements */
txout_failures_add(gm->txf, scid);
/* Cover race where we were looking up this UTXO as it was spent. */
tal_free(map_del(&gm->pending_ann_map, scid));
tal_free(map_del(&gm->early_ann_map, scid));
/* Put in tombstone marker. */
gossip_store_add(gm->daemon->gs,
towire_gossip_store_delete_chan(tmpctx, scid),
0);
/* Delete from store */
gossip_store_del(gm->daemon->gs, chan->cann_off, WIRE_CHANNEL_ANNOUNCEMENT);
for (int dir = 0; dir < 2; dir++) {
if (gossmap_chan_set(chan, dir))
gossip_store_del(gm->daemon->gs, chan->cupdate_off[dir], WIRE_CHANNEL_UPDATE);
}
/* Check for node_announcements which should no longer be there */
for (int dir = 0; dir < 2; dir++) {
struct gossmap_node *node;
u64 offset;
node = gossmap_nth_node(gossmap, chan, dir);
/* Don't get confused if a node has a channel with self! */
if (dir == 1 && node == gossmap_nth_node(gossmap, chan, 0))
continue;
/* If there was a node announcement, we might need to fix things up. */
if (!gossmap_node_announced(node))
continue;
/* Last channel? Delete node announce */
if (node->num_chans == 1) {
gossip_store_del(gm->daemon->gs, node->nann_off, WIRE_NODE_ANNOUNCEMENT);
continue;
}
/* Maybe this was the last channel_announcement which preceeded node_announcement? */
if (chan->cann_off < node->nann_off
&& any_cannounce_preceeds_offset(gossmap, node, chan, node->nann_off)) {
const u8 *nannounce;
u32 timestamp;
/* To maintain order, delete and re-add node_announcement */
nannounce = gossmap_node_get_announce(tmpctx, gossmap, node);
timestamp = gossip_store_get_timestamp(gm->daemon->gs, node->nann_off);
gossip_store_del(gm->daemon->gs, node->nann_off, WIRE_NODE_ANNOUNCEMENT);
offset = gossip_store_add(gm->daemon->gs, nannounce, timestamp);
} else {
/* Are all remaining channels dying but we weren't?
* Can happen if we removed this channel immediately
* for our own channels, without marking them
* dying. */
if (gossmap_chan_is_dying(gossmap, chan))
continue;
offset = node->nann_off;
}
/* Be sure to set DYING flag when we move (ignore current
* channel, we haven't reloaded gossmap yet!) */
if (all_node_channels_dying(gossmap, node, chan))
gossip_store_set_flag(gm->daemon->gs, offset,
GOSSIP_STORE_DYING_BIT,
WIRE_NODE_ANNOUNCEMENT);
}
}
static u32 get_timestamp(struct gossmap *gossmap,
const struct gossmap_chan *chan,
int dir)
{
u32 timestamp;
/* 0 is sufficient for our needs */
if (!gossmap_chan_set(chan, dir))
return 0;
gossmap_chan_get_update_details(gossmap, chan, dir,
×tamp,
NULL, NULL, NULL, NULL, NULL, NULL, NULL);
return timestamp;
}
/* Every half a week we look for dead channels (faster in dev) */
static void prune_network(struct gossmap_manage *gm)
{
u64 now = gossip_time_now(gm->daemon).ts.tv_sec;
/* Anything below this highwater mark ought to be pruned */
const s64 highwater = now - GOSSIP_PRUNE_INTERVAL(gm->daemon->dev_fast_gossip_prune);
const struct gossmap_node *me;
struct gossmap *gossmap;
/* We reload this every time we delete a channel: that way we can tell if it's
* time to remove a node! */
gossmap = gossmap_manage_get_gossmap(gm);
me = gossmap_find_node(gossmap, &gm->daemon->id);
/* Now iterate through all channels and see if it is still alive */
for (size_t i = 0; i < gossmap_max_chan_idx(gossmap); i++) {
struct gossmap_chan *chan = gossmap_chan_byidx(gossmap, i);
u32 timestamp[2];
struct short_channel_id scid;
if (!chan)
continue;
/* BOLT #7:
* - if the `timestamp` of the latest `channel_update` in
* either direction is older than two weeks (1209600 seconds):
* - MAY prune the channel.
*/
/* This is a fancy way of saying "both ends must refresh!" */
timestamp[0] = get_timestamp(gossmap, chan, 0);
timestamp[1] = get_timestamp(gossmap, chan, 1);
if (timestamp[0] >= highwater && timestamp[1] >= highwater)
continue;
scid = gossmap_chan_scid(gossmap, chan);
/* Is it one of mine? */
if (gossmap_nth_node(gossmap, chan, 0) == me
|| gossmap_nth_node(gossmap, chan, 1) == me) {
int local = (gossmap_nth_node(gossmap, chan, 1) == me);
status_unusual("Pruning local channel %s from gossip_store: local channel_update time %u, remote %u",
fmt_short_channel_id(tmpctx, scid),
timestamp[local], timestamp[!local]);
}
status_debug("Pruning channel %s from network view (ages %u and %u)",
fmt_short_channel_id(tmpctx, scid),
timestamp[0], timestamp[1]);
remove_channel(gm, gossmap, chan, scid);
gossmap = gossmap_manage_get_gossmap(gm);
me = gossmap_find_node(gossmap, &gm->daemon->id);
}
/* Note: some nodes may have been left with no channels! Gossmap will
* remove them on next refresh. */
start_prune_timer(gm);
}
static void start_prune_timer(struct gossmap_manage *gm)
{
/* Schedule next run now */
gm->prune_timer = new_reltimer(&gm->daemon->timers, gm,
time_from_sec(GOSSIP_PRUNE_INTERVAL(gm->daemon->dev_fast_gossip_prune)/4),
prune_network, gm);
}
static void reprocess_queued_msgs(struct gossmap_manage *gm);
static void report_bad_update(struct gossmap *map,
const struct short_channel_id_dir *scidd,
u16 cltv_expiry_delta,
u32 fee_base_msat,
u32 fee_proportional_millionths,
struct gossmap_manage *gm)
{
status_debug("Update for %s has silly values, disabling (cltv=%u, fee=%u+%u)",
fmt_short_channel_id_dir(tmpctx, scidd),
cltv_expiry_delta, fee_base_msat, fee_proportional_millionths);
}
struct gossmap_manage *gossmap_manage_new(const tal_t *ctx,
struct daemon *daemon,
struct chan_dying *dying_channels TAKES)
{
struct gossmap_manage *gm = tal(ctx, struct gossmap_manage);
gm->fd = gossip_store_get_fd(daemon->gs);
gm->raw_gossmap = gossmap_load_fd(gm, gm->fd, report_bad_update, NULL, gm);
assert(gm->raw_gossmap);
gm->daemon = daemon;
map_init(&gm->pending_ann_map, "pending announcements");
gm->pending_cupdates = tal_arr(gm, struct pending_cupdate *, 0);
map_init(&gm->early_ann_map, "too-early announcements");
gm->early_cupdates = tal_arr(gm, struct pending_cupdate *, 0);
gm->pending_nannounces = tal_arr(gm, struct pending_nannounce *, 0);
gm->txf = txout_failures_new(gm, daemon);
gm->dying_channels = tal_dup_talarr(gm, struct chan_dying, dying_channels);
start_prune_timer(gm);
return gm;
}
/* Catch CI giving out-of-order gossip: definitely happens IRL though */
static void bad_gossip(const struct node_id *source_peer, const char *str)
{
status_peer_trace(source_peer, "Bad gossip order: %s", str);
}
/* Send peer a warning message, if non-NULL. */
static void peer_warning(struct gossmap_manage *gm,
const struct node_id *source_peer,
const char *fmt, ...)
{
va_list ap;
char *formatted;
va_start(ap, fmt);
formatted = tal_vfmt(tmpctx, fmt, ap);
va_end(ap);
bad_gossip(source_peer, formatted);
if (!source_peer)
return;
queue_peer_msg(gm->daemon, source_peer,
take(towire_warningfmt(NULL, NULL, "%s", formatted)));
}
/* Subtle: if a new channel appears, it means those node announcements are no longer "dying" */
static void node_announcements_not_dying(struct gossmap_manage *gm,
const struct gossmap *gossmap,
const struct pending_cannounce *pca)
{
for (size_t i = 0; i < ARRAY_SIZE(pca->node_id); i++) {
struct gossmap_node *n = gossmap_find_node(gossmap, &pca->node_id[i]);
if (!n || !gossmap_node_announced(n))
continue;
if (gossip_store_get_flags(gm->daemon->gs, n->nann_off, WIRE_NODE_ANNOUNCEMENT)
& GOSSIP_STORE_DYING_BIT) {
gossip_store_clear_flag(gm->daemon->gs, n->nann_off,
GOSSIP_STORE_DYING_BIT,
WIRE_NODE_ANNOUNCEMENT);
}
}
}
const char *gossmap_manage_channel_announcement(const tal_t *ctx,
struct gossmap_manage *gm,
const u8 *announce TAKES,
const struct node_id *source_peer TAKES,
const struct amount_sat *known_amount)
{
secp256k1_ecdsa_signature node_signature_1, node_signature_2;
secp256k1_ecdsa_signature bitcoin_signature_1, bitcoin_signature_2;
u8 *features;
struct bitcoin_blkid chain_hash;
struct short_channel_id scid;
struct node_id node_id_1;
struct node_id node_id_2;
struct pubkey bitcoin_key_1;
struct pubkey bitcoin_key_2;
struct pending_cannounce *pca;
const char *warn;
u32 blockheight = gm->daemon->current_blockheight;
struct gossmap *gossmap = gossmap_manage_get_gossmap(gm);
/* Make sure we own msg, even if we don't save it. */
if (taken(announce))
tal_steal(tmpctx, announce);
if (!fromwire_channel_announcement(tmpctx, announce, &node_signature_1, &node_signature_2,
&bitcoin_signature_1, &bitcoin_signature_2, &features, &chain_hash,
&scid, &node_id_1, &node_id_2, &bitcoin_key_1, &bitcoin_key_2)) {
return tal_fmt(ctx, "Malformed channel_announcement %s",
tal_hex(tmpctx, announce));
}
/* If a prior txout lookup failed there is little point it trying
* again. Just drop the announcement and walk away whistling.
*
* Happens quite a lot in CI on just-closed channels.
*/
if (in_txout_failures(gm->txf, scid)) {
return NULL;
}
warn = sigcheck_channel_announcement(ctx, &node_id_1, &node_id_2,
&bitcoin_key_1, &bitcoin_key_2,
&node_signature_1, &node_signature_2,
&bitcoin_signature_1, &bitcoin_signature_2,
announce);
if (warn)
return warn;
/* Already known? */
if (gossmap_find_chan(gossmap, &scid))
return NULL;
pca = tal(gm, struct pending_cannounce);
pca->scriptpubkey = scriptpubkey_p2wsh(pca,
bitcoin_redeem_2of2(tmpctx,
&bitcoin_key_1,
&bitcoin_key_2));
pca->channel_announcement = tal_dup_talarr(pca, u8, announce);
pca->source_peer = tal_dup_or_null(pca, struct node_id, source_peer);
pca->node_id[0] = node_id_1;
pca->node_id[1] = node_id_2;
/* Are we supposed to add immediately without checking with lightningd?
* Unless we already got it from a peer and we're processing now!
*/
if (known_amount
&& !map_get(&gm->pending_ann_map, scid)
&& !map_get(&gm->early_ann_map, scid)) {
/* Set with timestamp 0 (we will update once we have a channel_update) */
gossip_store_add(gm->daemon->gs, announce, 0);
gossip_store_add(gm->daemon->gs,
towire_gossip_store_channel_amount(tmpctx, *known_amount), 0);
node_announcements_not_dying(gm, gossmap, pca);
tal_free(pca);
return NULL;
}
/* Don't know blockheight yet, or not yet deep enough? Don't even ask */
if (!is_scid_depth_announceable(scid, blockheight)) {
/* Don't expect to be more than 12 blocks behind! */
if (blockheight != 0
&& short_channel_id_blocknum(scid) > blockheight + 12) {
return tal_fmt(ctx,
"Bad gossip order: ignoring channel_announcement %s at blockheight %u",
fmt_short_channel_id(tmpctx, scid),
blockheight);
}
if (!map_add(&gm->early_ann_map, scid, pca)) {
/* Already pending? Ignore */
tal_free(pca);
return NULL;
}
/* We will retry in gossip_manage_new_block */
return NULL;
}
status_trace("channel_announcement: Adding %s to pending...",
fmt_short_channel_id(tmpctx, scid));
if (!map_add(&gm->pending_ann_map, scid, pca)) {
/* Already pending? Ignore */
tal_free(pca);
return NULL;
}
/* Ask lightningd about this scid: see
* gossmap_manage_handle_get_txout_reply */
daemon_conn_send(gm->daemon->master,
take(towire_gossipd_get_txout(NULL, scid)));
return NULL;
}
/*~ We queue incoming channel_announcement pending confirmation from lightningd
* that it really is an unspent output. Here's its reply. */
void gossmap_manage_handle_get_txout_reply(struct gossmap_manage *gm, const u8 *msg)
{
struct short_channel_id scid;
u8 *outscript;
struct amount_sat sat;
struct pending_cannounce *pca;
struct gossmap *gossmap;
if (!fromwire_gossipd_get_txout_reply(msg, msg, &scid, &sat, &outscript))
master_badmsg(WIRE_GOSSIPD_GET_TXOUT_REPLY, msg);
status_trace("channel_announcement: got reply for %s...",
fmt_short_channel_id(tmpctx, scid));
pca = map_del(&gm->pending_ann_map, scid);
if (!pca) {
/* If we were looking specifically for this, we no longer
* are (but don't penalize sender: we don't know if it was
* good or bad). */
remove_unknown_scid(gm->daemon->seeker, &scid, true);
/* Was it deleted because we saw channel close? */
if (!in_txout_failures(gm->txf, scid))
status_broken("get_txout_reply with unknown scid %s?",
fmt_short_channel_id(tmpctx, scid));
return;
}
/* BOLT #7:
*
* The receiving node:
*...
* - if the `short_channel_id`'s output... is spent:
* - MUST ignore the message.
*/
if (tal_count(outscript) == 0) {
peer_warning(gm, pca->source_peer,
"channel_announcement: no unspent txout %s",
fmt_short_channel_id(tmpctx, scid));
goto bad;
}
if (!tal_arr_eq(outscript, pca->scriptpubkey)) {
peer_warning(gm, pca->source_peer,
"channel_announcement: txout %s expected %s, got %s",
fmt_short_channel_id(tmpctx, scid),
tal_hex(tmpctx, pca->scriptpubkey),
tal_hex(tmpctx, outscript));
goto bad;
}
/* We have reports of doubled-up channel_announcements, hence this check! */
struct gossmap_chan *chan;
u64 before_length_processed, before_total_length;
before_length_processed = gossmap_lengths(gm->raw_gossmap, &before_total_length);
chan = gossmap_find_chan(gm->raw_gossmap, &scid);
if (chan) {
status_broken("Redundant channel_announce for scid %s at off %"PRIu64" (gossmap %"PRIu64"/%"PRIu64", store %"PRIu64")",
fmt_short_channel_id(tmpctx, scid), chan->cann_off,
before_length_processed, before_total_length,
gossip_store_len_written(gm->daemon->gs));
goto out;
} else {
u64 after_length_processed, after_total_length;
/* Good, now try refreshing in case it somehow slipped in! */
gossmap = gossmap_manage_get_gossmap(gm);
after_length_processed = gossmap_lengths(gm->raw_gossmap, &after_total_length);
chan = gossmap_find_chan(gm->raw_gossmap, &scid);
if (chan) {
status_broken("Redundant channel_announce *AFTER REFRESH* for scid %s at off %"PRIu64" (gossmap was %"PRIu64"/%"PRIu64", now %"PRIu64"/%"PRIu64", store %"PRIu64")",
fmt_short_channel_id(tmpctx, scid), chan->cann_off,
before_length_processed, before_total_length,
after_length_processed, after_total_length,
gossip_store_len_written(gm->daemon->gs));
goto out;
}
}
/* Set with timestamp 0 (we will update once we have a channel_update) */
gossip_store_add(gm->daemon->gs, pca->channel_announcement, 0);
gossip_store_add(gm->daemon->gs,
towire_gossip_store_channel_amount(tmpctx, sat), 0);
/* If we looking specifically for this, we no longer are. */
remove_unknown_scid(gm->daemon->seeker, &scid, true);
gossmap = gossmap_manage_get_gossmap(gm);
/* If node_announcements were dying, they no longer are. */
node_announcements_not_dying(gm, gossmap, pca);
tal_free(pca);
/* When all pending requests are done, we reconsider queued messages */
reprocess_queued_msgs(gm);
return;
bad:
txout_failures_add(gm->txf, scid);
out:
tal_free(pca);
/* If we were looking specifically for this, we no longer are. */
remove_unknown_scid(gm->daemon->seeker, &scid, false);
}
/* This is called both from when we receive the channel update, and if
* we had to defer. */
static const char *process_channel_update(const tal_t *ctx,
struct gossmap_manage *gm,
struct short_channel_id scid,
const secp256k1_ecdsa_signature *signature,
u8 message_flags,
u8 channel_flags,
u16 cltv_expiry_delta,
struct amount_msat htlc_minimum_msat,
struct amount_msat htlc_maximum_msat,
u32 fee_base_msat,
u32 fee_proportional_millionths,
u32 timestamp,
const u8 *update,
const struct node_id *source_peer)
{
struct gossmap_chan *chan;
struct node_id node_id, remote_id;
const char *err;
int dir = (channel_flags & ROUTING_FLAGS_DIRECTION);
struct gossmap *gossmap = gossmap_manage_get_gossmap(gm);
u64 offset;
chan = gossmap_find_chan(gossmap, &scid);
if (!chan) {
/* Did we explicitly reject announce? Ignore completely. */
if (in_txout_failures(gm->txf, scid))
return NULL;
/* Seeker may want to ask about this. */
query_unknown_channel(gm->daemon, source_peer, scid);
/* Don't send them warning, it can happen. */
bad_gossip(source_peer,
tal_fmt(tmpctx, "Unknown channel %s",
fmt_short_channel_id(tmpctx, scid)));
return NULL;
}
/* Now we know node, we can check signature. */
gossmap_node_get_id(gossmap,
gossmap_nth_node(gossmap, chan, dir),
&node_id);
err = sigcheck_channel_update(ctx, &node_id, signature, update);
if (err)
return err;
/* Don't allow private updates on public channels! */
if (message_flags & ROUTING_OPT_DONT_FORWARD) {
return tal_fmt(ctx, "Do not set DONT_FORWARD on public channel_updates (%s)",
fmt_short_channel_id(tmpctx, scid));
}
/* Do we have same or earlier update? */
if (gossmap_chan_set(chan, dir)) {
u32 prev_timestamp
= gossip_store_get_timestamp(gm->daemon->gs, chan->cupdate_off[dir]);
if (prev_timestamp >= timestamp) {
/* Too old, ignore */
return NULL;
}
} else {
/* Is this the first update in either direction? If so,
* rewrite channel_announcement so timestamp is correct. */
if (!gossmap_chan_set(chan, !dir))
gossip_store_set_timestamp(gm->daemon->gs, chan->cann_off, timestamp);
}
/* OK, apply the new one */
offset = gossip_store_add(gm->daemon->gs, update, timestamp);
/* If channel is dying, make sure update is also marked dying! */
if (gossmap_chan_is_dying(gossmap, chan)) {
gossip_store_set_flag(gm->daemon->gs,
offset,
GOSSIP_STORE_DYING_BIT,
WIRE_CHANNEL_UPDATE);
}
/* Now delete old */
if (gossmap_chan_set(chan, dir))
gossip_store_del(gm->daemon->gs, chan->cupdate_off[dir], WIRE_CHANNEL_UPDATE);
/* Is this an update for an incoming channel? If so, keep lightningd updated */
gossmap_node_get_id(gossmap,
gossmap_nth_node(gossmap, chan, !dir),
&remote_id);
if (node_id_eq(&remote_id, &gm->daemon->id)) {
tell_lightningd_peer_update(gm->daemon, source_peer,
scid, fee_base_msat,
fee_proportional_millionths,
cltv_expiry_delta, htlc_minimum_msat,
htlc_maximum_msat);
}
/* Used to evaluate gossip peers' performance */
peer_supplied_good_gossip(gm->daemon, source_peer, 1);
status_peer_trace(source_peer,
"Received channel_update for channel %s/%d now %s",
fmt_short_channel_id(tmpctx, scid),
dir,
channel_flags & ROUTING_FLAGS_DISABLED ? "DISABLED" : "ACTIVE");
return NULL;
}
const char *gossmap_manage_channel_update(const tal_t *ctx,
struct gossmap_manage *gm,
const u8 *update TAKES,
const struct node_id *source_peer TAKES)
{
secp256k1_ecdsa_signature signature;
struct short_channel_id scid;
u32 timestamp;
u8 message_flags, channel_flags;
u16 cltv_expiry_delta;
struct amount_msat htlc_minimum_msat, htlc_maximum_msat;
u32 fee_base_msat;
u32 fee_proportional_millionths;
struct bitcoin_blkid chain_hash;
struct gossmap *gossmap = gossmap_manage_get_gossmap(gm);
if (taken(update))
tal_steal(tmpctx, update);
if (taken(source_peer))
tal_steal(tmpctx, source_peer);
if (!fromwire_channel_update(update, &signature,
&chain_hash, &scid,
×tamp, &message_flags,
&channel_flags, &cltv_expiry_delta,
&htlc_minimum_msat, &fee_base_msat,
&fee_proportional_millionths,
&htlc_maximum_msat)) {
return tal_fmt(ctx, "channel_update: malformed %s",
tal_hex(tmpctx, update));
}
/* Don't accept ancient or far-future timestamps. */
if (!timestamp_reasonable(gm->daemon, timestamp))
return NULL;
/* Still waiting? */
if (map_get(&gm->pending_ann_map, scid)) {
enqueue_cupdate(&gm->pending_cupdates,
scid,
&signature,
message_flags,
channel_flags,
cltv_expiry_delta,
htlc_minimum_msat,
htlc_maximum_msat,
fee_base_msat,
fee_proportional_millionths,
timestamp,
take(update),
source_peer);
return NULL;
}
/* Too early? */
if (map_get(&gm->early_ann_map, scid)) {
enqueue_cupdate(&gm->early_cupdates,
scid,
&signature,
message_flags,
channel_flags,
cltv_expiry_delta,
htlc_minimum_msat,
htlc_maximum_msat,
fee_base_msat,
fee_proportional_millionths,
timestamp,
take(update),
source_peer);
return NULL;
}
/* Private channel_updates are not always marked as such. So check if it's an unknown
* channel, and signed by the peer itself. */
if (!gossmap_find_chan(gossmap, &scid)
&& source_peer
&& sigcheck_channel_update(tmpctx, source_peer, &signature, update) == NULL) {
tell_lightningd_peer_update(gm->daemon, source_peer,
scid, fee_base_msat,
fee_proportional_millionths,
cltv_expiry_delta, htlc_minimum_msat,
htlc_maximum_msat);
return NULL;
}
return process_channel_update(ctx, gm, scid, &signature,
message_flags, channel_flags,
cltv_expiry_delta,
htlc_minimum_msat,
htlc_maximum_msat,
fee_base_msat,
fee_proportional_millionths,
timestamp, update, source_peer);
}
static void process_node_announcement(struct gossmap_manage *gm,
struct gossmap *gossmap,
const struct gossmap_node *node,
u32 timestamp,
const struct node_id *node_id,
const u8 *nannounce,
const struct node_id *source_peer)
{
u64 offset;
/* Do we have a later one? If so, ignore */
if (gossmap_node_announced(node)) {
u32 prev_timestamp
= gossip_store_get_timestamp(gm->daemon->gs, node->nann_off);
if (prev_timestamp >= timestamp) {
/* Too old, ignore */
return;
}
}
/* OK, apply the new one */
offset = gossip_store_add(gm->daemon->gs, nannounce, timestamp);
/* If all channels are dying, make sure this is marked too. */
if (all_node_channels_dying(gossmap, node, NULL)) {
gossip_store_set_flag(gm->daemon->gs, offset,
GOSSIP_STORE_DYING_BIT,
WIRE_NODE_ANNOUNCEMENT);
}
/* Now delete old */
if (gossmap_node_announced(node))
gossip_store_del(gm->daemon->gs, node->nann_off, WIRE_NODE_ANNOUNCEMENT);
/* Used to evaluate gossip peers' performance */
peer_supplied_good_gossip(gm->daemon, source_peer, 1);
status_peer_trace(source_peer,
"Received node_announcement for node %s",
fmt_node_id(tmpctx, node_id));
}
const char *gossmap_manage_node_announcement(const tal_t *ctx,
struct gossmap_manage *gm,
const u8 *nannounce TAKES,
const struct node_id *source_peer TAKES)
{
secp256k1_ecdsa_signature signature;
u32 timestamp;
struct node_id node_id;
u8 rgb_color[3];
u8 alias[32];
u8 *features, *addresses;
struct wireaddr *wireaddrs;
struct tlv_node_ann_tlvs *na_tlv;
struct gossmap_node *node;
const char *err;
struct gossmap *gossmap = gossmap_manage_get_gossmap(gm);
if (taken(nannounce))
tal_steal(tmpctx, nannounce);
if (taken(source_peer))
tal_steal(tmpctx, source_peer);
if (!fromwire_node_announcement(tmpctx, nannounce,
&signature, &features, ×tamp,
&node_id, rgb_color, alias,
&addresses,
&na_tlv)) {
/* BOLT #7:
*
* - if `node_id` is NOT a valid compressed public key:
* - SHOULD send a `warning`.
* - MAY close the connection.
* - MUST NOT process the message further.
*/
return tal_fmt(ctx, "node_announcement: malformed %s",
tal_hex(tmpctx, nannounce));
}
wireaddrs = fromwire_wireaddr_array(tmpctx, addresses);
if (!wireaddrs) {
/* BOLT #7:
*
* - if `addrlen` is insufficient to hold the address
* descriptors of the known types: