forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routing.c
1972 lines (1740 loc) · 56.6 KB
/
routing.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/chainparams.h>
#include <bitcoin/script.h>
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/memleak.h>
#include <common/pseudorand.h>
#include <common/status.h>
#include <common/timeout.h>
#include <common/type_to_string.h>
#include <common/wire_error.h>
#include <gossipd/gossip_generation.h>
#include <gossipd/gossip_store_wiregen.h>
#include <gossipd/gossipd.h>
#include <gossipd/gossipd_wiregen.h>
#include <gossipd/routing.h>
#ifndef SUPERVERBOSE
#define SUPERVERBOSE(...)
#endif
/* 365.25 * 24 * 60 / 10 */
#define BLOCKS_PER_YEAR 52596
struct pending_node_announce {
struct routing_state *rstate;
struct node_id nodeid;
size_t refcount;
u8 *node_announcement;
u32 timestamp;
u32 index;
/* Automagically turns to NULL if peer freed */
struct peer *peer_softref;
};
/* We consider a reasonable gossip rate to be 2 per day, with burst of
* 4 per day. So we use a granularity of one hour. */
#define TOKENS_PER_MSG 12
#define TOKEN_MAX (12 * 4)
static u8 update_tokens(const struct routing_state *rstate,
u8 tokens, u32 prev_timestamp, u32 new_timestamp)
{
u64 num_tokens = tokens;
assert(new_timestamp >= prev_timestamp);
num_tokens += ((new_timestamp - prev_timestamp)
/ GOSSIP_TOKEN_TIME(rstate->dev_fast_gossip));
if (num_tokens > TOKEN_MAX)
num_tokens = TOKEN_MAX;
return num_tokens;
}
static bool ratelimit(const struct routing_state *rstate,
u8 *tokens, u32 prev_timestamp, u32 new_timestamp)
{
*tokens = update_tokens(rstate, *tokens, prev_timestamp, new_timestamp);
/* Now, if we can afford it, pass this message. */
if (*tokens >= TOKENS_PER_MSG) {
*tokens -= TOKENS_PER_MSG;
return true;
}
return false;
}
static const struct node_id *
pending_node_announce_keyof(const struct pending_node_announce *a)
{
return &a->nodeid;
}
static bool pending_node_announce_eq(const struct pending_node_announce *pna,
const struct node_id *pc)
{
return node_id_eq(&pna->nodeid, pc);
}
HTABLE_DEFINE_TYPE(struct pending_node_announce, pending_node_announce_keyof,
node_map_hash_key, pending_node_announce_eq,
pending_node_map);
/* We keep around announcements for channels until we have an
* update for them (which gives us their timestamp) */
struct unupdated_channel {
/* The channel_announcement message */
const u8 *channel_announce;
/* The short_channel_id */
struct short_channel_id scid;
/* The ids of the nodes */
struct node_id id[2];
/* When we added, so we can discard old ones */
struct timeabs added;
/* If we loaded from the store, this is where. */
u32 index;
/* Channel capacity */
struct amount_sat sat;
/* Automagically turns to NULL of peer freed */
struct peer *peer_softref;
};
static struct unupdated_channel *
get_unupdated_channel(const struct routing_state *rstate,
const struct short_channel_id *scid)
{
return uintmap_get(&rstate->unupdated_chanmap, scid->u64);
}
static void destroy_unupdated_channel(struct unupdated_channel *uc,
struct routing_state *rstate)
{
uintmap_del(&rstate->unupdated_chanmap, uc->scid.u64);
}
static struct node_map *new_node_map(const tal_t *ctx)
{
struct node_map *map = tal(ctx, struct node_map);
node_map_init(map);
tal_add_destructor(map, node_map_clear);
return map;
}
/* We use a simple array (with NULL entries) until we have too many. */
static bool node_uses_chan_map(const struct node *node)
{
/* This is a layering violation: last entry in htable is the table ptr,
* which is never NULL */
return node->chans.arr[NUM_IMMEDIATE_CHANS] != NULL;
}
/* When simple array fills, use a htable. */
static void convert_node_to_chan_map(struct node *node)
{
struct chan *chans[NUM_IMMEDIATE_CHANS];
memcpy(chans, node->chans.arr, sizeof(chans));
chan_map_init_sized(&node->chans.map, NUM_IMMEDIATE_CHANS + 1);
assert(node_uses_chan_map(node));
for (size_t i = 0; i < ARRAY_SIZE(chans); i++)
chan_map_add(&node->chans.map, chans[i]);
}
static void add_chan(struct node *node, struct chan *chan)
{
if (!node_uses_chan_map(node)) {
for (size_t i = 0; i < NUM_IMMEDIATE_CHANS; i++) {
if (node->chans.arr[i] == NULL) {
node->chans.arr[i] = chan;
return;
}
}
convert_node_to_chan_map(node);
}
chan_map_add(&node->chans.map, chan);
}
static struct chan *next_chan_arr(const struct node *node,
struct chan_map_iter *i)
{
while (i->i.off < NUM_IMMEDIATE_CHANS) {
if (node->chans.arr[i->i.off])
return node->chans.arr[i->i.off];
i->i.off++;
}
return NULL;
}
struct chan *first_chan(const struct node *node, struct chan_map_iter *i)
{
if (!node_uses_chan_map(node)) {
i->i.off = 0;
return next_chan_arr(node, i);
}
return chan_map_first(&node->chans.map, i);
}
struct chan *next_chan(const struct node *node, struct chan_map_iter *i)
{
if (!node_uses_chan_map(node)) {
i->i.off++;
return next_chan_arr(node, i);
}
return chan_map_next(&node->chans.map, i);
}
static void destroy_routing_state(struct routing_state *rstate)
{
/* Since we omitted destructors on these, clean up manually */
u64 idx;
for (struct chan *chan = uintmap_first(&rstate->chanmap, &idx);
chan;
chan = uintmap_after(&rstate->chanmap, &idx))
free_chan(rstate, chan);
/* Free up our htables */
pending_cannouncement_map_clear(&rstate->pending_cannouncements);
}
/* We don't check this when loading from the gossip_store: that would break
* our canned tests, and usually old gossip is better than no gossip */
static bool timestamp_reasonable(struct routing_state *rstate, u32 timestamp)
{
u64 now = gossip_time_now(rstate).ts.tv_sec;
/* More than one day ahead? */
if (timestamp > now + 24*60*60)
return false;
/* More than 2 weeks behind? */
if (timestamp < now - GOSSIP_PRUNE_INTERVAL(rstate->dev_fast_gossip_prune))
return false;
return true;
}
#if DEVELOPER
static void memleak_help_routing_tables(struct htable *memtable,
struct routing_state *rstate)
{
struct node *n;
struct node_map_iter nit;
memleak_remove_htable(memtable, &rstate->nodes->raw);
memleak_remove_htable(memtable, &rstate->pending_node_map->raw);
memleak_remove_htable(memtable, &rstate->pending_cannouncements.raw);
memleak_remove_uintmap(memtable, &rstate->unupdated_chanmap);
for (n = node_map_first(rstate->nodes, &nit);
n;
n = node_map_next(rstate->nodes, &nit)) {
if (node_uses_chan_map(n))
memleak_remove_htable(memtable, &n->chans.map.raw);
}
}
#endif /* DEVELOPER */
/* Once an hour, or at 10000 entries, we expire old ones */
static void txout_failure_age(struct routing_state *rstate)
{
uintmap_clear(&rstate->txout_failures_old);
rstate->txout_failures_old = rstate->txout_failures;
uintmap_init(&rstate->txout_failures);
rstate->num_txout_failures = 0;
rstate->txout_failure_timer = new_reltimer(rstate->timers,
rstate, time_from_sec(3600),
txout_failure_age, rstate);
}
void add_to_txout_failures(struct routing_state *rstate,
const struct short_channel_id *scid)
{
if (uintmap_add(&rstate->txout_failures, scid->u64, true)
&& ++rstate->num_txout_failures == 10000) {
tal_free(rstate->txout_failure_timer);
txout_failure_age(rstate);
}
}
static bool in_txout_failures(struct routing_state *rstate,
const struct short_channel_id *scid)
{
if (uintmap_get(&rstate->txout_failures, scid->u64))
return true;
/* If we were going to expire it, we no longer are. */
if (uintmap_get(&rstate->txout_failures_old, scid->u64)) {
add_to_txout_failures(rstate, scid);
return true;
}
return false;
}
struct routing_state *new_routing_state(const tal_t *ctx,
const struct node_id *local_id,
struct list_head *peers,
struct timers *timers,
const u32 *dev_gossip_time TAKES,
bool dev_fast_gossip,
bool dev_fast_gossip_prune)
{
struct routing_state *rstate = tal(ctx, struct routing_state);
rstate->nodes = new_node_map(rstate);
rstate->timers = timers;
rstate->local_id = *local_id;
rstate->gs = gossip_store_new(rstate, peers);
rstate->local_channel_announced = false;
rstate->last_timestamp = 0;
pending_cannouncement_map_init(&rstate->pending_cannouncements);
uintmap_init(&rstate->chanmap);
uintmap_init(&rstate->unupdated_chanmap);
rstate->num_txout_failures = 0;
uintmap_init(&rstate->txout_failures);
uintmap_init(&rstate->txout_failures_old);
txout_failure_age(rstate);
rstate->pending_node_map = tal(ctx, struct pending_node_map);
pending_node_map_init(rstate->pending_node_map);
#if DEVELOPER
if (dev_gossip_time) {
rstate->gossip_time = tal(rstate, struct timeabs);
rstate->gossip_time->ts.tv_sec = *dev_gossip_time;
rstate->gossip_time->ts.tv_nsec = 0;
} else
rstate->gossip_time = NULL;
rstate->dev_fast_gossip = dev_fast_gossip;
rstate->dev_fast_gossip_prune = dev_fast_gossip_prune;
#endif
tal_add_destructor(rstate, destroy_routing_state);
memleak_add_helper(rstate, memleak_help_routing_tables);
if (taken(dev_gossip_time))
tal_free(dev_gossip_time);
return rstate;
}
const struct node_id *node_map_keyof_node(const struct node *n)
{
return &n->id;
}
size_t node_map_hash_key(const struct node_id *pc)
{
return siphash24(siphash_seed(), pc->k, sizeof(pc->k));
}
bool node_map_node_eq(const struct node *n, const struct node_id *pc)
{
return node_id_eq(&n->id, pc);
}
static void destroy_node(struct node *node, struct routing_state *rstate)
{
struct chan_map_iter i;
struct chan *c;
node_map_del(rstate->nodes, node);
/* These remove themselves from chans[]. */
while ((c = first_chan(node, &i)) != NULL)
free_chan(rstate, c);
/* Free htable if we need. */
if (node_uses_chan_map(node))
chan_map_clear(&node->chans.map);
}
struct node *get_node(struct routing_state *rstate,
const struct node_id *id)
{
return node_map_get(rstate->nodes, id);
}
static struct node *new_node(struct routing_state *rstate,
const struct node_id *id)
{
struct node *n;
assert(!get_node(rstate, id));
n = tal(rstate, struct node);
n->id = *id;
memset(n->chans.arr, 0, sizeof(n->chans.arr));
broadcastable_init(&n->bcast);
n->tokens = TOKEN_MAX;
node_map_add(rstate->nodes, n);
tal_add_destructor2(n, destroy_node, rstate);
return n;
}
/* We've received a channel_announce for a channel attached to this node:
* otherwise it's in the map only because it's a peer, or us. */
static bool node_has_public_channels(struct node *node)
{
struct chan_map_iter i;
struct chan *c;
for (c = first_chan(node, &i); c; c = next_chan(node, &i)) {
if (is_chan_public(c))
return true;
}
return false;
}
/* We can *send* a channel_announce for a channel attached to this node:
* we only send once we have a channel_update. */
static bool node_has_broadcastable_channels(struct node *node)
{
struct chan_map_iter i;
struct chan *c;
for (c = first_chan(node, &i); c; c = next_chan(node, &i)) {
if (!is_chan_public(c))
continue;
if (is_halfchan_defined(&c->half[0])
|| is_halfchan_defined(&c->half[1]))
return true;
}
return false;
}
static bool node_announce_predates_channels(const struct node *node)
{
struct chan_map_iter i;
struct chan *c;
for (c = first_chan(node, &i); c; c = next_chan(node, &i)) {
if (!is_chan_public(c))
continue;
if (c->bcast.index < node->bcast.index)
return false;
}
return true;
}
/* Move this node's announcement to the tail of the gossip_store, to
* make everyone send it again. */
static void force_node_announce_rexmit(struct routing_state *rstate,
struct node *node)
{
const u8 *announce;
bool is_local = node_id_eq(&node->id, &rstate->local_id);
announce = gossip_store_get(tmpctx, rstate->gs, node->bcast.index);
gossip_store_delete(rstate->gs,
&node->bcast,
WIRE_NODE_ANNOUNCEMENT);
node->bcast.index = gossip_store_add(rstate->gs,
announce,
node->bcast.timestamp,
is_local,
NULL);
}
static void remove_chan_from_node(struct routing_state *rstate,
struct node *node, const struct chan *chan)
{
size_t num_chans;
if (!node_uses_chan_map(node)) {
num_chans = 0;
for (size_t i = 0; i < NUM_IMMEDIATE_CHANS; i++) {
if (node->chans.arr[i] == chan)
node->chans.arr[i] = NULL;
else if (node->chans.arr[i] != NULL)
num_chans++;
}
} else {
if (!chan_map_del(&node->chans.map, chan))
abort();
num_chans = chan_map_count(&node->chans.map);
}
/* Last channel? Simply delete node (and associated announce) */
if (num_chans == 0) {
gossip_store_delete(rstate->gs,
&node->bcast,
WIRE_NODE_ANNOUNCEMENT);
tal_free(node);
return;
}
if (!node->bcast.index)
return;
/* Removed only public channel? Remove node announcement. */
if (!node_has_broadcastable_channels(node)) {
gossip_store_delete(rstate->gs,
&node->bcast,
WIRE_NODE_ANNOUNCEMENT);
} else if (node_announce_predates_channels(node)) {
/* node announcement predates all channel announcements?
* Move to end (we could, in theory, move to just past next
* channel_announce, but we don't care that much about spurious
* retransmissions in this corner case */
force_node_announce_rexmit(rstate, node);
}
}
#if DEVELOPER
/* We make sure that free_chan is called on this chan! */
static void destroy_chan_check(struct chan *chan)
{
assert(chan->sat.satoshis == (unsigned long)chan); /* Raw: dev-hack */
}
#endif
/* We used to make this a tal_add_destructor2, but that costs 40 bytes per
* chan, and we only ever explicitly free it anyway. */
void free_chan(struct routing_state *rstate, struct chan *chan)
{
remove_chan_from_node(rstate, chan->nodes[0], chan);
remove_chan_from_node(rstate, chan->nodes[1], chan);
uintmap_del(&rstate->chanmap, chan->scid.u64);
#if DEVELOPER
chan->sat.satoshis = (unsigned long)chan; /* Raw: dev-hack */
#endif
tal_free(chan);
}
static void init_half_chan(struct routing_state *rstate,
struct chan *chan,
int channel_idx)
{
struct half_chan *c = &chan->half[channel_idx];
broadcastable_init(&c->bcast);
c->tokens = TOKEN_MAX;
}
static void bad_gossip_order(const u8 *msg,
const struct peer *peer,
const char *details)
{
status_peer_debug(peer ? &peer->id : NULL,
"Bad gossip order: %s before announcement %s",
peer_wire_name(fromwire_peektype(msg)),
details);
}
struct chan *new_chan(struct routing_state *rstate,
const struct short_channel_id *scid,
const struct node_id *id1,
const struct node_id *id2,
struct amount_sat satoshis)
{
struct chan *chan = tal(rstate, struct chan);
int n1idx = node_id_idx(id1, id2);
struct node *n1, *n2;
#if DEVELOPER
tal_add_destructor(chan, destroy_chan_check);
#endif
/* We should never add a channel twice */
assert(!uintmap_get(&rstate->chanmap, scid->u64));
/* Create nodes on demand */
n1 = get_node(rstate, id1);
if (!n1)
n1 = new_node(rstate, id1);
n2 = get_node(rstate, id2);
if (!n2)
n2 = new_node(rstate, id2);
chan->scid = *scid;
chan->nodes[n1idx] = n1;
chan->nodes[!n1idx] = n2;
broadcastable_init(&chan->bcast);
/* This is how we indicate it's not public yet. */
chan->bcast.timestamp = 0;
chan->sat = satoshis;
add_chan(n2, chan);
add_chan(n1, chan);
/* Populate with (inactive) connections */
init_half_chan(rstate, chan, n1idx);
init_half_chan(rstate, chan, !n1idx);
uintmap_add(&rstate->chanmap, scid->u64, chan);
return chan;
}
/* Checks that key is valid, and signed this hash */
static bool check_signed_hash_nodeid(const struct sha256_double *hash,
const secp256k1_ecdsa_signature *signature,
const struct node_id *id)
{
struct pubkey key;
return pubkey_from_node_id(&key, id)
&& check_signed_hash(hash, signature, &key);
}
/* Verify the signature of a channel_update message */
static u8 *check_channel_update(const tal_t *ctx,
const struct node_id *node_id,
const secp256k1_ecdsa_signature *node_sig,
const u8 *update)
{
/* 2 byte msg type + 64 byte signatures */
int offset = 66;
struct sha256_double hash;
sha256_double(&hash, update + offset, tal_count(update) - offset);
if (!check_signed_hash_nodeid(&hash, node_sig, node_id))
return towire_warningfmt(ctx, NULL,
"Bad signature for %s hash %s"
" on channel_update %s",
type_to_string(tmpctx,
secp256k1_ecdsa_signature,
node_sig),
type_to_string(tmpctx,
struct sha256_double,
&hash),
tal_hex(tmpctx, update));
return NULL;
}
static u8 *check_channel_announcement(const tal_t *ctx,
const struct node_id *node1_id, const struct node_id *node2_id,
const struct pubkey *bitcoin1_key, const struct pubkey *bitcoin2_key,
const secp256k1_ecdsa_signature *node1_sig,
const secp256k1_ecdsa_signature *node2_sig,
const secp256k1_ecdsa_signature *bitcoin1_sig,
const secp256k1_ecdsa_signature *bitcoin2_sig, const u8 *announcement)
{
/* 2 byte msg type + 256 byte signatures */
int offset = 258;
struct sha256_double hash;
sha256_double(&hash, announcement + offset,
tal_count(announcement) - offset);
if (!check_signed_hash_nodeid(&hash, node1_sig, node1_id)) {
return towire_warningfmt(ctx, NULL,
"Bad node_signature_1 %s hash %s"
" on channel_announcement %s",
type_to_string(tmpctx,
secp256k1_ecdsa_signature,
node1_sig),
type_to_string(tmpctx,
struct sha256_double,
&hash),
tal_hex(tmpctx, announcement));
}
if (!check_signed_hash_nodeid(&hash, node2_sig, node2_id)) {
return towire_warningfmt(ctx, NULL,
"Bad node_signature_2 %s hash %s"
" on channel_announcement %s",
type_to_string(tmpctx,
secp256k1_ecdsa_signature,
node2_sig),
type_to_string(tmpctx,
struct sha256_double,
&hash),
tal_hex(tmpctx, announcement));
}
if (!check_signed_hash(&hash, bitcoin1_sig, bitcoin1_key)) {
return towire_warningfmt(ctx, NULL,
"Bad bitcoin_signature_1 %s hash %s"
" on channel_announcement %s",
type_to_string(tmpctx,
secp256k1_ecdsa_signature,
bitcoin1_sig),
type_to_string(tmpctx,
struct sha256_double,
&hash),
tal_hex(tmpctx, announcement));
}
if (!check_signed_hash(&hash, bitcoin2_sig, bitcoin2_key)) {
return towire_warningfmt(ctx, NULL,
"Bad bitcoin_signature_2 %s hash %s"
" on channel_announcement %s",
type_to_string(tmpctx,
secp256k1_ecdsa_signature,
bitcoin2_sig),
type_to_string(tmpctx,
struct sha256_double,
&hash),
tal_hex(tmpctx, announcement));
}
return NULL;
}
/* We allow node announcements for this node if it doesn't otherwise exist, so
* we can process them once it does exist (a channel_announce is being
* validated right now).
*
* If we attach one, remove it on destruction of @ctx.
*/
static void del_pending_node_announcement(const tal_t *ctx UNUSED,
struct pending_node_announce *pna)
{
if (--pna->refcount == 0) {
pending_node_map_del(pna->rstate->pending_node_map, pna);
tal_free(pna);
}
}
static void catch_node_announcement(const tal_t *ctx,
struct routing_state *rstate,
struct node_id *nodeid)
{
struct pending_node_announce *pna;
struct node *node;
/* No need if we already know about the node. We might, however, only
* know about it because it's a peer (maybe with private or
* not-yet-announced channels), so check for that too. */
node = get_node(rstate, nodeid);
if (node && node_has_public_channels(node))
return;
/* We can have multiple channels announced at same time for nodes;
* but we can only have one of these in the map. */
pna = pending_node_map_get(rstate->pending_node_map, nodeid);
if (!pna) {
pna = tal(rstate, struct pending_node_announce);
pna->rstate = rstate;
pna->nodeid = *nodeid;
pna->node_announcement = NULL;
pna->timestamp = 0;
pna->index = 0;
pna->refcount = 0;
pna->peer_softref = NULL;
pending_node_map_add(rstate->pending_node_map, pna);
}
pna->refcount++;
tal_add_destructor2(ctx, del_pending_node_announcement, pna);
}
static void process_pending_node_announcement(struct routing_state *rstate,
struct node_id *nodeid)
{
struct pending_node_announce *pna = pending_node_map_get(rstate->pending_node_map, nodeid);
if (!pna)
return;
if (pna->node_announcement) {
SUPERVERBOSE(
"Processing deferred node_announcement for node %s",
type_to_string(pna, struct node_id, nodeid));
/* Can fail it timestamp is now too old */
if (!routing_add_node_announcement(rstate,
pna->node_announcement,
pna->index,
pna->peer_softref, NULL))
status_unusual("pending node_announcement %s too old?",
tal_hex(tmpctx, pna->node_announcement));
/* Never send this again. */
pna->node_announcement = tal_free(pna->node_announcement);
}
/* We don't need to catch any more node_announcements, since we've
* accepted the public channel now. But other pending announcements
* may still hold a reference they use in
* del_pending_node_announcement, so simply delete it from the map. */
pending_node_map_del(rstate->pending_node_map, notleak(pna));
}
static struct pending_cannouncement *
find_pending_cannouncement(struct routing_state *rstate,
const struct short_channel_id *scid)
{
struct pending_cannouncement *pann;
pann = pending_cannouncement_map_get(&rstate->pending_cannouncements, scid);
return pann;
}
static void destroy_pending_cannouncement(struct pending_cannouncement *pending,
struct routing_state *rstate)
{
pending_cannouncement_map_del(&rstate->pending_cannouncements, pending);
}
static void add_channel_announce_to_broadcast(struct routing_state *rstate,
struct chan *chan,
const u8 *channel_announce,
u32 timestamp,
u32 index)
{
u8 *addendum = towire_gossip_store_channel_amount(tmpctx, chan->sat);
bool is_local = local_direction(rstate, chan, NULL);
chan->bcast.timestamp = timestamp;
/* 0, unless we're loading from store */
if (index)
chan->bcast.index = index;
else
chan->bcast.index = gossip_store_add(rstate->gs,
channel_announce,
chan->bcast.timestamp,
is_local,
addendum);
rstate->local_channel_announced |= is_local;
}
bool routing_add_channel_announcement(struct routing_state *rstate,
const u8 *msg TAKES,
struct amount_sat sat,
u32 index,
struct peer *peer)
{
struct chan *chan;
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 unupdated_channel *uc;
const u8 *private_updates[2] = { NULL, NULL };
/* Make sure we own msg, even if we don't save it. */
if (taken(msg))
tal_steal(tmpctx, msg);
if (!fromwire_channel_announcement(
tmpctx, msg, &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 false;
/* The channel may already exist if it was non-public from
* local_add_channel(); normally we don't accept new
* channel_announcements. See handle_channel_announcement. */
chan = get_channel(rstate, &scid);
/* private updates will exist in the store before the announce: we
* can't index those for broadcast since they would predate it, so we
* add fresh ones. */
if (chan) {
/* If this was in the gossip_store, gossip_store is bad! */
if (index) {
status_broken("gossip_store channel_announce"
" %u replaces %u!",
index, chan->bcast.index);
return false;
}
/* Reload any private updates */
if (chan->half[0].bcast.index)
private_updates[0]
= gossip_store_get_private_update(NULL,
rstate->gs,
chan->half[0].bcast.index);
if (chan->half[1].bcast.index)
private_updates[1]
= gossip_store_get_private_update(NULL,
rstate->gs,
chan->half[1].bcast.index);
remove_channel_from_store(rstate, chan);
free_chan(rstate, chan);
}
uc = tal(rstate, struct unupdated_channel);
uc->channel_announce = tal_dup_talarr(uc, u8, msg);
uc->added = gossip_time_now(rstate);
uc->index = index;
uc->sat = sat;
uc->scid = scid;
uc->id[0] = node_id_1;
uc->id[1] = node_id_2;
set_softref(uc, &uc->peer_softref, peer);
uintmap_add(&rstate->unupdated_chanmap, scid.u64, uc);
tal_add_destructor2(uc, destroy_unupdated_channel, rstate);
/* If a node_announcement comes along, save it for once we're updated */
catch_node_announcement(uc, rstate, &node_id_1);
catch_node_announcement(uc, rstate, &node_id_2);
/* If we had private updates, they'll immediately create the channel. */
if (private_updates[0])
routing_add_channel_update(rstate, take(private_updates[0]), 0,
peer, false);
if (private_updates[1])
routing_add_channel_update(rstate, take(private_updates[1]), 0,
peer, false);
return true;
}
u8 *handle_channel_announcement(struct routing_state *rstate,
const u8 *announce TAKES,
u32 current_blockheight,
const struct short_channel_id **scid,
struct peer *peer)
{
struct pending_cannouncement *pending;
struct bitcoin_blkid chain_hash;
u8 *features, *warn;
secp256k1_ecdsa_signature node_signature_1, node_signature_2;
secp256k1_ecdsa_signature bitcoin_signature_1, bitcoin_signature_2;
struct chan *chan;
pending = tal(rstate, struct pending_cannouncement);
set_softref(pending, &pending->peer_softref, peer);
pending->updates[0] = NULL;
pending->updates[1] = NULL;
pending->update_peer_softref[0] = pending->update_peer_softref[1] = NULL;
pending->announce = tal_dup_talarr(pending, u8, announce);
pending->update_timestamps[0] = pending->update_timestamps[1] = 0;
if (!fromwire_channel_announcement(pending, pending->announce,
&node_signature_1,
&node_signature_2,
&bitcoin_signature_1,
&bitcoin_signature_2,
&features,
&chain_hash,
&pending->short_channel_id,
&pending->node_id_1,
&pending->node_id_2,
&pending->bitcoin_key_1,
&pending->bitcoin_key_2)) {
warn = towire_warningfmt(rstate, NULL,
"Malformed channel_announcement %s",
tal_hex(pending, pending->announce));
goto malformed;
}
/* We don't use features */
tal_free(features);
/* If we know the blockheight, and it's in the future, reject
* out-of-hand. Remember, it should be 6 deep before they tell us
* anyway. */
if (current_blockheight != 0
&& short_channel_id_blocknum(&pending->short_channel_id) > current_blockheight) {
status_peer_debug(peer ? &peer->id : NULL,
"Ignoring future channel_announcment for %s"
" (current block %u)",
type_to_string(tmpctx, struct short_channel_id,
&pending->short_channel_id),
current_blockheight);
goto ignored;
}
/* If a prior txout lookup failed there is little point it trying
* again. Just drop the announcement and walk away whistling. */
if (in_txout_failures(rstate, &pending->short_channel_id)) {
SUPERVERBOSE(
"Ignoring channel_announcement of %s due to a prior txout "
"query failure. The channel was likely closed on-chain.",
type_to_string(tmpctx, struct short_channel_id,
&pending->short_channel_id));
goto ignored;
}
/* Check if we know the channel already (no matter in what
* state, we stop here if yes). */
chan = get_channel(rstate, &pending->short_channel_id);
if (chan != NULL && is_chan_public(chan)) {
SUPERVERBOSE("%s: %s already has public channel",
__func__,
type_to_string(tmpctx, struct short_channel_id,
&pending->short_channel_id));
goto ignored;
}
if (get_unupdated_channel(rstate, &pending->short_channel_id)) {
SUPERVERBOSE("%s: %s already has unupdated channel",
__func__,
type_to_string(tmpctx, struct short_channel_id,
&pending->short_channel_id));
goto ignored;
}
/* We don't replace previous ones, since we might validate that and
* think this one is OK! */
if (find_pending_cannouncement(rstate, &pending->short_channel_id)) {
SUPERVERBOSE("%s: %s already has pending cannouncement",
__func__,
type_to_string(tmpctx, struct short_channel_id,
&pending->short_channel_id));
goto ignored;
}
/* FIXME: Handle duplicates as per BOLT #7 */
/* BOLT #7:
* The receiving node:
*...
* - if the specified `chain_hash` is unknown to the receiver:
* - MUST ignore the message.
*/
if (!bitcoin_blkid_eq(&chain_hash, &chainparams->genesis_blockhash)) {
status_peer_debug(peer ? &peer->id : NULL,
"Received channel_announcement %s for unknown chain %s",
type_to_string(pending, struct short_channel_id,
&pending->short_channel_id),
type_to_string(pending, struct bitcoin_blkid, &chain_hash));
goto ignored;
}
/* Note that if node_id_1 or node_id_2 are malformed, it's caught here */
warn = check_channel_announcement(rstate,
&pending->node_id_1,
&pending->node_id_2,
&pending->bitcoin_key_1,
&pending->bitcoin_key_2,
&node_signature_1,
&node_signature_2,